Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-manager-mk2/controllers/create_test.go
2498 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package controllers
6
7
import (
8
"testing"
9
10
"github.com/gitpod-io/gitpod/ws-manager/api/config"
11
v1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
12
"github.com/google/go-cmp/cmp"
13
corev1 "k8s.io/api/core/v1"
14
)
15
16
func TestCreateWorkspaceEnvironment(t *testing.T) {
17
type Expectation struct {
18
Error string
19
Vars []corev1.EnvVar
20
}
21
tests := []struct {
22
Name string
23
Expectation Expectation
24
Context *startWorkspaceContext
25
}{
26
{
27
Name: "with Git config",
28
Context: &startWorkspaceContext{
29
Config: &config.Configuration{
30
WorkspaceClasses: map[string]*config.WorkspaceClass{
31
"default": {Name: "default"},
32
},
33
},
34
Workspace: &v1.Workspace{
35
Spec: v1.WorkspaceSpec{
36
Class: "default",
37
Git: &v1.GitSpec{
38
Username: "foobar",
39
Email: "[email protected]",
40
},
41
},
42
},
43
},
44
Expectation: Expectation{
45
Vars: []corev1.EnvVar{
46
{Name: "GITPOD_REPO_ROOT", Value: "/workspace"},
47
{Name: "GITPOD_REPO_ROOTS", Value: "/workspace"},
48
{Name: "GITPOD_THEIA_PORT", Value: "0"},
49
{Name: "THEIA_WORKSPACE_ROOT", Value: "/workspace"},
50
{Name: "GITPOD_WORKSPACE_CLASS", Value: "default"},
51
{Name: "THEIA_SUPERVISOR_ENDPOINT", Value: ":0"},
52
{Name: "THEIA_WEBVIEW_EXTERNAL_ENDPOINT", Value: "webview-{{hostname}}"},
53
{Name: "THEIA_MINI_BROWSER_HOST_PATTERN", Value: "browser-{{hostname}}"},
54
{Name: "GITPOD_GIT_USER_NAME", Value: "foobar"},
55
{Name: "GITPOD_GIT_USER_EMAIL", Value: "[email protected]"},
56
{Name: "GITPOD_INTERVAL", Value: "0"}, {Name: "GITPOD_MEMORY", Value: "0"}, {Name: "GITPOD_CPU_COUNT", Value: "0"},
57
},
58
},
59
},
60
{
61
Name: "without Git config",
62
Context: &startWorkspaceContext{
63
Config: &config.Configuration{
64
WorkspaceClasses: map[string]*config.WorkspaceClass{
65
"default": {Name: "default"},
66
},
67
},
68
Workspace: &v1.Workspace{
69
Spec: v1.WorkspaceSpec{
70
Class: "default",
71
},
72
},
73
},
74
Expectation: Expectation{
75
Vars: []corev1.EnvVar{
76
{Name: "GITPOD_REPO_ROOT", Value: "/workspace"},
77
{Name: "GITPOD_REPO_ROOTS", Value: "/workspace"},
78
{Name: "GITPOD_THEIA_PORT", Value: "0"},
79
{Name: "THEIA_WORKSPACE_ROOT", Value: "/workspace"},
80
{Name: "GITPOD_WORKSPACE_CLASS", Value: "default"},
81
{Name: "THEIA_SUPERVISOR_ENDPOINT", Value: ":0"},
82
{Name: "THEIA_WEBVIEW_EXTERNAL_ENDPOINT", Value: "webview-{{hostname}}"},
83
{Name: "THEIA_MINI_BROWSER_HOST_PATTERN", Value: "browser-{{hostname}}"},
84
{Name: "GITPOD_INTERVAL", Value: "0"}, {Name: "GITPOD_MEMORY", Value: "0"}, {Name: "GITPOD_CPU_COUNT", Value: "0"},
85
},
86
},
87
},
88
}
89
90
for _, test := range tests {
91
t.Run(test.Name, func(t *testing.T) {
92
var act Expectation
93
94
res, err := createWorkspaceEnvironment(test.Context)
95
if err != nil {
96
act.Error = err.Error()
97
}
98
act.Vars = res
99
100
if diff := cmp.Diff(test.Expectation, act); diff != "" {
101
t.Errorf("createWorkspaceEnvironment() mismatch (-want +got):\n%s", diff)
102
}
103
})
104
}
105
}
106
107