Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues with teams, improved e2e-tests and add missing tests for teams and sm, clean up unused code #354

Merged
merged 35 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bd81402
convert team/user and job service to new service system
saffronjam Dec 20, 2023
36cf845
refactor zone service for new service system
saffronjam Dec 20, 2023
8a51716
add user id to notification read
saffronjam Dec 21, 2023
f9d199f
fix bug with pfr not being correctly created if already exists in cs
saffronjam Dec 21, 2023
63c87a1
improve error handling if resources are already deleted
saffronjam Dec 21, 2023
15019c7
refactor notifiction service to new service structure
saffronjam Dec 21, 2023
f4b5c9d
fix jobs binding to wrong list of status
saffronjam Dec 22, 2023
2a85b0d
fix broken create user snapshot
saffronjam Dec 22, 2023
d77d0ae
fix safer access to service cache
saffronjam Dec 22, 2023
267fe8d
refactor test structure and add many tests
saffronjam Dec 22, 2023
2d34515
remove unused file
saffronjam Dec 27, 2023
ceefab5
fix bugs in tests for deployments, vms, jobs and teams
saffronjam Dec 27, 2023
c821d0a
fix nil map error
saffronjam Dec 27, 2023
5648131
improve test clean up init worker
saffronjam Dec 27, 2023
2a7167d
change team description to not be a ptr
saffronjam Dec 27, 2023
c5bd13b
fix team list binding validator to also work on team create
saffronjam Dec 27, 2023
38e8a57
rename *Model to *Models
saffronjam Dec 27, 2023
d1398e5
add test workflow
saffronjam Dec 27, 2023
1bfe17d
Merge branch 'dev' of https://github.com/kthcloud/go-deploy into dev
saffronjam Dec 27, 2023
6fecc81
Merge branch 'main' into dev
saffronjam Dec 27, 2023
d747ae4
update action test.yml
saffronjam Dec 27, 2023
71779db
Merge branch 'dev' of https://github.com/kthcloud/go-deploy into dev
saffronjam Dec 27, 2023
be02864
remove binary
saffronjam Dec 27, 2023
fbbf06f
update action test.yml
saffronjam Dec 27, 2023
e2429f5
update go version in action test
saffronjam Dec 27, 2023
d27d94c
fix bugs in test action file
saffronjam Dec 27, 2023
27ca800
fix bad env name for config file in test action
saffronjam Dec 27, 2023
162d3b7
use .env instead of .secrets
saffronjam Dec 27, 2023
d3a9637
Update test.yml
saffronjam Dec 27, 2023
5b45130
Update test.yml
saffronjam Dec 27, 2023
7f1d59f
update e2e test structure for vms, dry code
saffronjam Dec 28, 2023
6559eb5
add all option when listing storage managers
saffronjam Dec 29, 2023
30c030c
refactor and cleanup e2e tests, and parallelize
saffronjam Dec 29, 2023
0ba57cd
Merge branch 'dev' of https://github.com/kthcloud/go-deploy into dev
saffronjam Dec 29, 2023
e7bb941
Update test.yml
saffronjam Dec 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ci
name: Build Image

on:
workflow_dispatch:
Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Test

on:
workflow_dispatch:
push:
branches:
- main
paths:
- '**.go'
- '.github/workflows/test.yml'
pull_request:
branches:
- main
paths:
- '**.go'
- '.github/workflows/test.yml'

jobs:
e2e:
runs-on: ubuntu-latest

# This assumes that the configuration file connects to Redis at localhost:6379
services:
redis:
image: redis
ports:
- 6379:6379

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '>=1.20.0'

- name: Create config directory
run: sudo mkdir -p /etc/deploy && sudo chown $USER /etc/deploy

- name: Create config file from secret
run: |
echo "${{ vars.CONFIG_FILE }}" | sudo tee /etc/deploy/config.yaml > /dev/null

- name: Export configuration file path
run: echo "DEPLOY_CONFIG_FILE=/etc/deploy/config.yaml" >> $GITHUB_ENV

- name: Start main program
run: |
go build -o go-deploy . # Build go-deploy
./go-deploy --test-mode & # Starts in the background
sleep 15 # Wait a bit to ensure it has started

- name: Run e2e tests
run: go test ./test/e2e/...
8 changes: 4 additions & 4 deletions models/config/helpers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package config

import (
roleModel "go-deploy/models/sys/role"
roleModels "go-deploy/models/sys/role"
)

func (e *ConfigType) GetRole(roleName string) *roleModel.Role {
func (e *ConfigType) GetRole(roleName string) *roleModels.Role {
for _, role := range e.Roles {
if role.Name == roleName {
return &role
Expand All @@ -14,8 +14,8 @@ func (e *ConfigType) GetRole(roleName string) *roleModel.Role {
return nil
}

func (e *ConfigType) GetRolesByIamGroups(iamGroups []string) []roleModel.Role {
var roles []roleModel.Role
func (e *ConfigType) GetRolesByIamGroups(iamGroups []string) []roleModels.Role {
var roles []roleModels.Role

for _, role := range e.Roles {
for _, iamGroup := range iamGroups {
Expand Down
2 changes: 1 addition & 1 deletion models/dto/body/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ type JobRead struct {
}

type JobUpdate struct {
Status *string `json:"status" binding:"omitempty,oneof=pending running failed terminated finished"`
Status *string `json:"status" binding:"omitempty,oneof=pending running failed terminated finished completed"`
}
1 change: 1 addition & 0 deletions models/dto/body/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "time"

type NotificationRead struct {
ID string `json:"id"`
UserID string `json:"userId"`
Type string `json:"type"`
Content map[string]interface{} `json:"content"`
CreatedAt time.Time `json:"createdAt"`
Expand Down
2 changes: 1 addition & 1 deletion models/dto/body/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type TeamRead struct {
ID string `json:"id"`
Name string `json:"name"`
OwnerID string `json:"ownerId"`
Description *string `json:"description,omitempty"`
Description string `json:"description,omitempty"`
Resources []TeamResource `json:"resources"`
Members []TeamMember `json:"members"`
CreatedAt time.Time `json:"createdAt"`
Expand Down
2 changes: 1 addition & 1 deletion models/dto/query/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type JobList struct {
Pagination

All bool `form:"all" binding:"omitempty,boolean"`
Status *string `form:"status" binding:"omitempty,oneof=pending running failed terminated finished"`
Status *string `form:"status" binding:"omitempty,oneof=pending running failed terminated finished completed"`
Type *string `form:"type" binding:"omitempty,ascii"`
UserID *string `form:"userId" binding:"omitempty,uuid4"`
}
20 changes: 20 additions & 0 deletions models/sys/deployment/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go-deploy/models/sys/base/resource"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"time"
)

type Client struct {
Expand Down Expand Up @@ -102,6 +103,25 @@ func (client *Client) WithNoActivities() *Client {

func (client *Client) WithGitHubWebhookID(id int64) *Client {
filter := bson.D{{"subsystems.github.webhook.id", id}}

client.ResourceClient.AddExtraFilter(filter)
client.ActivityResourceClient.AddExtraFilter(filter)

return client
}

func (client *Client) WithNameRegex(name string) *Client {
filter := bson.D{{"name", bson.D{{"$regex", name}}}}

client.ResourceClient.AddExtraFilter(filter)
client.ActivityResourceClient.AddExtraFilter(filter)

return client
}

func (client *Client) OlderThan(timestamp time.Time) *Client {
filter := bson.D{{"createdAt", bson.D{{"$lt", timestamp}}}}

client.ResourceClient.AddExtraFilter(filter)
client.ActivityResourceClient.AddExtraFilter(filter)

Expand Down
12 changes: 12 additions & 0 deletions models/sys/job/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,24 @@ func (client *Client) IncludeTypes(types ...string) *Client {
return client
}

func (client *Client) IncludeStatus(status ...string) *Client {
client.AddExtraFilter(bson.D{{"status", bson.D{{"$in", status}}}})

return client
}

func (client *Client) ExcludeStatus(status ...string) *Client {
client.AddExtraFilter(bson.D{{"status", bson.D{{"$nin", status}}}})

return client
}

func (client *Client) ExcludeTypes(types ...string) *Client {
client.AddExtraFilter(bson.D{{"type", bson.D{{"$nin", types}}}})

return client
}

func (client *Client) FilterArgs(argName string, filter interface{}) *Client {
client.AddExtraFilter(bson.D{{"args." + argName, filter}})

Expand Down
1 change: 1 addition & 0 deletions models/sys/notification/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "go-deploy/models/dto/body"
func (notification *Notification) ToDTO() body.NotificationRead {
return body.NotificationRead{
ID: notification.ID,
UserID: notification.UserID,
Type: notification.Type,
Content: notification.Content,
CreatedAt: notification.CreatedAt,
Expand Down
15 changes: 15 additions & 0 deletions models/sys/team/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go-deploy/models/sys/base"
"go-deploy/models/sys/base/resource"
"go.mongodb.org/mongo-driver/bson"
"time"
)

type Client struct {
Expand Down Expand Up @@ -49,3 +50,17 @@ func (client *Client) WithResourceID(resourceID string) *Client {

return client
}

func (client *Client) WithNameRegex(name string) *Client {
filter := bson.D{{"name", bson.D{{"$regex", name}}}}
client.ResourceClient.AddExtraFilter(filter)

return client
}

func (client *Client) OlderThan(timestamp time.Time) *Client {
filter := bson.D{{"createdAt", bson.D{{"$lt", timestamp}}}}
client.ResourceClient.AddExtraFilter(filter)

return client
}
14 changes: 7 additions & 7 deletions models/sys/team/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (client *Client) Create(id, ownerID string, params *CreateParams) (*Team, e
team := &Team{
ID: id,
Name: params.Name,
Description: &params.Description,
Description: params.Description,
OwnerID: ownerID,
CreatedAt: time.Now(),
ResourceMap: params.ResourceMap,
Expand Down Expand Up @@ -59,7 +59,9 @@ func (client *Client) ListByUserID(userID string) ([]Team, error) {
}

func (client *Client) UpdateWithParams(id string, params *UpdateParams) error {
updateData := bson.D{}
updateData := bson.D{
{"updatedAt", time.Now()},
}

models.AddIfNotNil(&updateData, "name", params.Name)
models.AddIfNotNil(&updateData, "description", params.Description)
Expand All @@ -74,7 +76,9 @@ func (client *Client) UpdateWithParams(id string, params *UpdateParams) error {
}

func (client *Client) UpdateMember(id string, memberID string, member *Member) error {
updateData := bson.D{}
updateData := bson.D{
{"updatedAt", time.Now()},
}

models.AddIfNotNil(&updateData, "memberMap."+memberID, member)

Expand All @@ -84,7 +88,3 @@ func (client *Client) UpdateMember(id string, memberID string, member *Member) e

return client.SetWithBsonByID(id, updateData)
}

func (client *Client) MarkUpdated(id string) error {
return client.SetWithBsonByID(id, bson.D{{"updatedAt", time.Now()}})
}
1 change: 1 addition & 0 deletions models/sys/team/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (params *CreateParams) FromDTO(teamCreateDTO *body.TeamCreate, ownerID stri
params.Name = teamCreateDTO.Name
params.MemberMap = make(map[string]Member)
params.Description = teamCreateDTO.Description
params.ResourceMap = make(map[string]Resource)

for _, resourceDTO := range teamCreateDTO.Resources {
if resource := getResourceFunc(resourceDTO); resource != nil {
Expand Down
2 changes: 1 addition & 1 deletion models/sys/team/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Resource struct {
type Team struct {
ID string `bson:"id"`
Name string `bson:"name"`
Description *string `bson:"description"`
Description string `bson:"description"`
OwnerID string `bson:"ownerId"`
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
Expand Down
6 changes: 3 additions & 3 deletions models/sys/user/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package user

import (
"go-deploy/models/dto/body"
roleModel "go-deploy/models/sys/role"
roleModels "go-deploy/models/sys/role"
"log"
)

func (user *User) ToDTO(effectiveRole *roleModel.Role, usage *Usage, storageURL *string) body.UserRead {
func (user *User) ToDTO(effectiveRole *roleModels.Role, usage *Usage, storageURL *string) body.UserRead {
publicKeys := make([]body.PublicKey, len(user.PublicKeys))
for i, key := range user.PublicKeys {
publicKeys[i] = body.PublicKey{
Expand All @@ -21,7 +21,7 @@ func (user *User) ToDTO(effectiveRole *roleModel.Role, usage *Usage, storageURL

if effectiveRole == nil {
log.Println("effective role is nil when creating user read for user", user.Username)
effectiveRole = &roleModel.Role{
effectiveRole = &roleModels.Role{
Name: "unknown",
Description: "unknown",
}
Expand Down
19 changes: 19 additions & 0 deletions models/sys/vm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go-deploy/models/sys/base/resource"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"time"
)

type Client struct {
Expand Down Expand Up @@ -108,3 +109,21 @@ func (client *Client) WithCustomFilter(filter bson.D) *Client {

return client
}

func (client *Client) WithNameRegex(name string) *Client {
filter := bson.D{{"name", bson.D{{"$regex", name}}}}

client.ResourceClient.AddExtraFilter(filter)
client.ActivityResourceClient.AddExtraFilter(filter)

return client
}

func (client *Client) OlderThan(timestamp time.Time) *Client {
filter := bson.D{{"createdAt", bson.D{{"$lt", timestamp}}}}

client.ResourceClient.AddExtraFilter(filter)
client.ActivityResourceClient.AddExtraFilter(filter)

return client
}
10 changes: 5 additions & 5 deletions models/sys/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package vm

import (
"go-deploy/models/sys/activity"
gpuModel "go-deploy/models/sys/gpu"
gpuModels "go-deploy/models/sys/gpu"
"time"
)

Expand Down Expand Up @@ -56,7 +56,7 @@ func (vm *VM) DoingActivity(activity string) bool {
}

func (vm *VM) HasGPU() bool {
exists, err := gpuModel.New().WithVM(vm.ID).ExistsAny()
exists, err := gpuModels.New().WithVM(vm.ID).ExistsAny()
if err != nil {
return false
}
Expand All @@ -65,16 +65,16 @@ func (vm *VM) HasGPU() bool {
}

func (vm *VM) GetGpuID() *string {
idStruct, err := gpuModel.New().WithVM(vm.ID).GetID()
idStruct, err := gpuModels.New().WithVM(vm.ID).GetID()
if err != nil || idStruct == nil {
return nil
}

return &idStruct.ID
}

func (vm *VM) GetGpu() *gpuModel.GPU {
gpu, err := gpuModel.New().WithVM(vm.ID).Get()
func (vm *VM) GetGpu() *gpuModels.GPU {
gpu, err := gpuModels.New().WithVM(vm.ID).Get()
if err != nil || gpu == nil {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func shutdown() {
db.Shutdown()
}

func Create(options *Options) *App {
func Create(opts *Options) *App {
config.SetupEnvironment()
metrics.Setup()

if options.TestMode {
if opts.TestMode {
config.Config.TestMode = true
config.Config.MongoDB.Name = config.Config.MongoDB.Name + "-test"
}
Expand All @@ -56,7 +56,7 @@ func Create(options *Options) *App {

ctx, cancel := context.WithCancel(context.Background())

for _, flag := range options.Flags {
for _, flag := range opts.Flags {
// handle api worker separately
if flag.Name == "api" {
continue
Expand All @@ -69,7 +69,7 @@ func Create(options *Options) *App {

var httpServer *http.Server

if options.Flags.GetPassedValue("api").(bool) {
if opts.Flags.GetPassedValue("api").(bool) {
ginMode, exists := os.LookupEnv("GIN_MODE")
if exists {
gin.SetMode(ginMode)
Expand Down
Loading