Skip to content

Commit b1a3665

Browse files
easyCZroboquat
authored andcommitted
[usage] Simplify Workspace record creation for tests
1 parent 6525911 commit b1a3665

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

components/usage/pkg/controller/reconciler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestUsageReconciler_Reconcile(t *testing.T) {
1919
instanceStatus := []byte(`{"phase": "stopped", "conditions": {"deployed": false, "pullingImages": false, "serviceExists": false}}`)
2020
startOfMay := time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)
2121
startOfJune := time.Date(2022, 06, 1, 0, 00, 00, 00, time.UTC)
22-
workspace := dbtest.NewWorkspace(t, "gitpodio-gitpod-gyjr82jkfnd")
22+
workspace := dbtest.NewWorkspace(t, db.Workspace{ID: "gitpodio-gitpod-gyjr82jkfnd"})
2323
instances := []db.WorkspaceInstance{
2424
// Ran throughout the reconcile period
2525
{

components/usage/pkg/db/dbtest/workspace.go

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,74 @@
55
package dbtest
66

77
import (
8+
"fmt"
89
"github.com/gitpod-io/gitpod/usage/pkg/db"
910
"github.com/google/uuid"
11+
"math/rand"
1012
"testing"
1113
)
1214

13-
func NewWorkspace(t *testing.T, id string) db.Workspace {
15+
const (
16+
WorkspaceContext = `{"title":"[usage] List workspaces for each workspace instance in usage period","repository":{"cloneUrl":"https://github.com/gitpod-io/gitpod.git","host":"github.com","name":"gitpod","owner":"gitpod-io","private":false},"ref":"mp/usage-list-workspaces","refType":"branch","revision":"586f22ecaeeb3b4796fd92f9ae1ca3512ca1e330","nr":10495,"base":{"repository":{"cloneUrl":"https://github.com/gitpod-io/gitpod.git","host":"github.com","name":"gitpod","owner":"gitpod-io","private":false},"ref":"mp/usage-validate-instances","refType":"branch"},"normalizedContextURL":"https://github.com/gitpod-io/gitpod/pull/10495","checkoutLocation":"gitpod"}`
17+
WorkspaceConfig = `{"image":"eu.gcr.io/gitpod-core-dev/dev/dev-environment:me-me-image.1","workspaceLocation":"gitpod/gitpod-ws.code-workspace","checkoutLocation":"gitpod","ports":[{"port":1337,"onOpen":"open-preview"},{"port":3000,"onOpen":"ignore"},{"port":3001,"onOpen":"ignore"},{"port":3306,"onOpen":"ignore"},{"port":4000,"onOpen":"ignore"},{"port":5900,"onOpen":"ignore"},{"port":6080,"onOpen":"ignore"},{"port":7777,"onOpen":"ignore"},{"port":9229,"onOpen":"ignore"},{"port":9999,"onOpen":"ignore"},{"port":13001,"onOpen":"ignore"},{"port":13444}],"tasks":[{"name":"Install Preview Environment kube-context","command":"(cd dev/preview/previewctl && go install .)\npreviewctl install-context\nexit\n"},{"name":"Add Harvester kubeconfig","command":"./dev/preview/util/download-and-merge-harvester-kubeconfig.sh\nexit 0\n"},{"name":"Java","command":"if [ -z \"$RUN_GRADLE_TASK\" ]; then\n read -r -p \"Press enter to continue Java gradle task\"\nfi\nleeway exec --package components/supervisor-api/java:lib --package components/gitpod-protocol/java:lib -- ./gradlew --build-cache build\nleeway exec --package components/ide/jetbrains/backend-plugin:plugin --package components/ide/jetbrains/gateway-plugin:publish --parallel -- ./gradlew --build-cache buildPlugin\n"},{"name":"TypeScript","before":"scripts/branch-namespace.sh","init":"yarn --network-timeout 100000 && yarn build"},{"name":"Go","before":"pre-commit install --install-hooks","init":"leeway exec --filter-type go -v -- go mod verify","openMode":"split-right"}],"vscode":{"extensions":["bradlc.vscode-tailwindcss","EditorConfig.EditorConfig","golang.go","hashicorp.terraform","ms-azuretools.vscode-docker","ms-kubernetes-tools.vscode-kubernetes-tools","stkb.rewrap","zxh404.vscode-proto3","matthewpi.caddyfile-support","heptio.jsonnet","timonwong.shellcheck","vscjava.vscode-java-pack","fwcd.kotlin","dbaeumer.vscode-eslint","esbenp.prettier-vscode"]},"jetbrains":{"goland":{"prebuilds":{"version":"stable"}}},"_origin":"repo","_featureFlags":[]}`
18+
)
19+
20+
// NewWorkspace creates a new stub workspace with default values, unless these are set on the workspace argument
21+
// Records are not stored, use `db.Create(dbtest.NewWorkspace(t, db.Workspace{})) to store it.
22+
// Only used for tests. Additional default properties may be added in the future.
23+
func NewWorkspace(t *testing.T, workspace db.Workspace) db.Workspace {
1424
t.Helper()
1525

26+
id := generateWorkspaceID()
27+
if workspace.ID != "" {
28+
id = workspace.ID
29+
}
30+
31+
ownerID := uuid.New()
32+
if workspace.OwnerID.ID() != 0 { // empty value
33+
ownerID = workspace.OwnerID
34+
}
35+
36+
workspaceType := db.WorkspaceType_Regular
37+
if workspace.Type != "" {
38+
workspaceType = workspace.Type
39+
}
40+
41+
contextURL := "https://github.com/gitpod-io/gitpod"
42+
if workspace.ContextURL != "" {
43+
contextURL = workspace.ContextURL
44+
}
45+
46+
context := []byte(WorkspaceContext)
47+
if workspace.Context.String() != "" {
48+
context = workspace.Context
49+
}
50+
51+
config := []byte(WorkspaceConfig)
52+
if workspace.Config.String() != "" {
53+
config = workspace.Config
54+
}
55+
1656
return db.Workspace{
1757
ID: id,
18-
OwnerID: uuid.New(),
19-
Type: "prebuild",
20-
ContextURL: "https://github.com/gitpod-io/gitpod",
21-
Context: []byte(`{"title":"[usage] List workspaces for each workspace instance in usage period","repository":{"cloneUrl":"https://github.com/gitpod-io/gitpod.git","host":"github.com","name":"gitpod","owner":"gitpod-io","private":false},"ref":"mp/usage-list-workspaces","refType":"branch","revision":"586f22ecaeeb3b4796fd92f9ae1ca3512ca1e330","nr":10495,"base":{"repository":{"cloneUrl":"https://github.com/gitpod-io/gitpod.git","host":"github.com","name":"gitpod","owner":"gitpod-io","private":false},"ref":"mp/usage-validate-instances","refType":"branch"},"normalizedContextURL":"https://github.com/gitpod-io/gitpod/pull/10495","checkoutLocation":"gitpod"}`),
22-
Config: []byte(`{"image":"eu.gcr.io/gitpod-core-dev/dev/dev-environment:me-me-image.1","workspaceLocation":"gitpod/gitpod-ws.code-workspace","checkoutLocation":"gitpod","ports":[{"port":1337,"onOpen":"open-preview"},{"port":3000,"onOpen":"ignore"},{"port":3001,"onOpen":"ignore"},{"port":3306,"onOpen":"ignore"},{"port":4000,"onOpen":"ignore"},{"port":5900,"onOpen":"ignore"},{"port":6080,"onOpen":"ignore"},{"port":7777,"onOpen":"ignore"},{"port":9229,"onOpen":"ignore"},{"port":9999,"onOpen":"ignore"},{"port":13001,"onOpen":"ignore"},{"port":13444}],"tasks":[{"name":"Install Preview Environment kube-context","command":"(cd dev/preview/previewctl && go install .)\npreviewctl install-context\nexit\n"},{"name":"Add Harvester kubeconfig","command":"./dev/preview/util/download-and-merge-harvester-kubeconfig.sh\nexit 0\n"},{"name":"Java","command":"if [ -z \"$RUN_GRADLE_TASK\" ]; then\n read -r -p \"Press enter to continue Java gradle task\"\nfi\nleeway exec --package components/supervisor-api/java:lib --package components/gitpod-protocol/java:lib -- ./gradlew --build-cache build\nleeway exec --package components/ide/jetbrains/backend-plugin:plugin --package components/ide/jetbrains/gateway-plugin:publish --parallel -- ./gradlew --build-cache buildPlugin\n"},{"name":"TypeScript","before":"scripts/branch-namespace.sh","init":"yarn --network-timeout 100000 && yarn build"},{"name":"Go","before":"pre-commit install --install-hooks","init":"leeway exec --filter-type go -v -- go mod verify","openMode":"split-right"}],"vscode":{"extensions":["bradlc.vscode-tailwindcss","EditorConfig.EditorConfig","golang.go","hashicorp.terraform","ms-azuretools.vscode-docker","ms-kubernetes-tools.vscode-kubernetes-tools","stkb.rewrap","zxh404.vscode-proto3","matthewpi.caddyfile-support","heptio.jsonnet","timonwong.shellcheck","vscjava.vscode-java-pack","fwcd.kotlin","dbaeumer.vscode-eslint","esbenp.prettier-vscode"]},"jetbrains":{"goland":{"prebuilds":{"version":"stable"}}},"_origin":"repo","_featureFlags":[]}`),
58+
OwnerID: ownerID,
59+
Type: workspaceType,
60+
ContextURL: contextURL,
61+
Context: context,
62+
Config: config,
63+
}
64+
}
65+
66+
func generateWorkspaceID() string {
67+
return fmt.Sprintf("gitpodio-gitpod-%s", randSeq(11))
68+
}
69+
70+
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
71+
72+
func randSeq(n int) string {
73+
b := make([]rune, n)
74+
for i := range b {
75+
b[i] = letters[rand.Intn(len(letters))]
2376
}
77+
return string(b)
2478
}

components/usage/pkg/db/workspace.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Workspace struct {
2020
OwnerID uuid.UUID `gorm:"column:ownerId;type:char;size:36;" json:"ownerId"`
2121
ProjectID sql.NullString `gorm:"column:projectId;type:char;size:36;" json:"projectId"`
2222
Description string `gorm:"column:description;type:varchar;size:255;" json:"description"`
23-
Type string `gorm:"column:type;type:char;size:16;default:regular;" json:"type"`
23+
Type WorkspaceType `gorm:"column:type;type:char;size:16;default:regular;" json:"type"`
2424
CloneURL string `gorm:"column:cloneURL;type:varchar;size:255;" json:"cloneURL"`
2525

2626
ContextURL string `gorm:"column:contextURL;type:text;size:65535;" json:"contextURL"`
@@ -51,6 +51,14 @@ func (d *Workspace) TableName() string {
5151
return "d_b_workspace"
5252
}
5353

54+
type WorkspaceType string
55+
56+
const (
57+
WorkspaceType_Prebuild WorkspaceType = "prebuild"
58+
WorkspaceType_Probe WorkspaceType = "probe"
59+
WorkspaceType_Regular WorkspaceType = "regular"
60+
)
61+
5462
func ListWorkspacesByID(ctx context.Context, conn *gorm.DB, ids []string) ([]Workspace, error) {
5563
if len(ids) == 0 {
5664
return nil, nil

components/usage/pkg/db/workspace_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func TestListWorkspacesByID(t *testing.T) {
9090
conn := db.ConnectForTests(t)
9191

9292
workspaces := []db.Workspace{
93-
dbtest.NewWorkspace(t, "gitpodio-gitpod-aaaaaaaaaaa"),
94-
dbtest.NewWorkspace(t, "gitpodio-gitpod-bbbbbbbbbbb"),
93+
dbtest.NewWorkspace(t, db.Workspace{ID: "gitpodio-gitpod-aaaaaaaaaaa"}),
94+
dbtest.NewWorkspace(t, db.Workspace{ID: "gitpodio-gitpod-bbbbbbbbbbb"}),
9595
}
9696
tx := conn.Create(workspaces)
9797
require.NoError(t, tx.Error)

0 commit comments

Comments
 (0)