|
5 | 5 | package dbtest
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "fmt" |
8 | 9 | "github.com/gitpod-io/gitpod/usage/pkg/db"
|
9 | 10 | "github.com/google/uuid"
|
| 11 | + "math/rand" |
10 | 12 | "testing"
|
11 | 13 | )
|
12 | 14 |
|
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 { |
14 | 24 | t.Helper()
|
15 | 25 |
|
| 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 | + |
16 | 56 | return db.Workspace{
|
17 | 57 | 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))] |
23 | 76 | }
|
| 77 | + return string(b) |
24 | 78 | }
|
0 commit comments