Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/components/ws-manager-mk2/configmap_test.go
2501 views
1
// Copyright (c) 2021 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 wsmanagermk2
6
7
import (
8
"encoding/json"
9
"testing"
10
11
"github.com/google/go-cmp/cmp"
12
"github.com/stretchr/testify/require"
13
corev1 "k8s.io/api/core/v1"
14
"k8s.io/utils/pointer"
15
16
"github.com/gitpod-io/gitpod/installer/pkg/common"
17
config "github.com/gitpod-io/gitpod/installer/pkg/config/v1"
18
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
19
wsmancfg "github.com/gitpod-io/gitpod/ws-manager/api/config"
20
)
21
22
func TestBuildWorkspaceTemplates(t *testing.T) {
23
type Expectation struct {
24
TplConfig wsmancfg.WorkspacePodTemplateConfiguration
25
Data map[string]bool
26
}
27
tests := []struct {
28
Name string
29
ClassName string
30
Config *config.WorkspaceTemplates
31
ContainerRegistry *config.ContainerRegistry
32
Expectation Expectation
33
}{
34
{
35
Name: "no templates",
36
ClassName: "",
37
Expectation: Expectation{},
38
},
39
{
40
Name: "empty templates",
41
ClassName: "",
42
Config: &config.WorkspaceTemplates{},
43
Expectation: Expectation{},
44
},
45
{
46
Name: "default tpl",
47
ClassName: "",
48
Config: &config.WorkspaceTemplates{
49
Default: &corev1.Pod{},
50
},
51
Expectation: Expectation{
52
TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{DefaultPath: "/workspace-templates/default.yaml"},
53
Data: map[string]bool{"default.yaml": true},
54
},
55
},
56
{
57
Name: "regular tpl",
58
ClassName: "",
59
Config: &config.WorkspaceTemplates{
60
Regular: &corev1.Pod{},
61
},
62
Expectation: Expectation{
63
TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{
64
RegularPath: "/workspace-templates/regular.yaml",
65
},
66
Data: map[string]bool{
67
"regular.yaml": true,
68
},
69
},
70
},
71
{
72
Name: "prebuild tpl",
73
ClassName: "",
74
Config: &config.WorkspaceTemplates{
75
Prebuild: &corev1.Pod{},
76
},
77
Expectation: Expectation{
78
TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{
79
PrebuildPath: "/workspace-templates/prebuild.yaml",
80
},
81
Data: map[string]bool{
82
"prebuild.yaml": true,
83
},
84
},
85
},
86
{
87
Name: "imgbuild tpl",
88
ClassName: "",
89
Config: &config.WorkspaceTemplates{
90
ImageBuild: &corev1.Pod{},
91
},
92
Expectation: Expectation{
93
TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{
94
ImagebuildPath: "/workspace-templates/imagebuild.yaml",
95
},
96
Data: map[string]bool{
97
"imagebuild.yaml": true,
98
},
99
},
100
},
101
{
102
Name: "regular class tpl",
103
ClassName: "awesome-class",
104
Config: &config.WorkspaceTemplates{
105
Regular: &corev1.Pod{},
106
},
107
Expectation: Expectation{
108
TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{
109
RegularPath: "/workspace-templates/awesome-class-regular.yaml",
110
},
111
Data: map[string]bool{
112
"awesome-class-regular.yaml": true,
113
},
114
},
115
},
116
}
117
118
for _, test := range tests {
119
t.Run(test.Name, func(t *testing.T) {
120
var (
121
act Expectation
122
tpls map[string]string
123
err error
124
)
125
126
if test.ContainerRegistry == nil {
127
test.ContainerRegistry = &config.ContainerRegistry{InCluster: pointer.Bool(true)}
128
}
129
130
act.TplConfig, tpls, err = buildWorkspaceTemplates(&common.RenderContext{Config: config.Config{
131
ContainerRegistry: *test.ContainerRegistry,
132
}}, test.Config, test.ClassName)
133
if err != nil {
134
t.Error(err)
135
}
136
137
if len(tpls) > 0 {
138
dt := make(map[string]bool)
139
for k := range tpls {
140
dt[k] = true
141
}
142
act.Data = dt
143
}
144
145
if diff := cmp.Diff(test.Expectation, act); diff != "" {
146
t.Errorf("Expectation mismatch (-want +got):\n%s", diff)
147
}
148
})
149
}
150
}
151
152
func TestWorkspaceURLTemplates(t *testing.T) {
153
tests := []struct {
154
Name string
155
Domain string
156
InstallationShortname string
157
ExpectedWorkspaceUrlTemplate string
158
ExpectedWorkspacePortURLTemplate string
159
}{
160
{
161
Name: "With an installation shortname",
162
Domain: "example.com",
163
InstallationShortname: "eu02",
164
ExpectedWorkspaceUrlTemplate: "https://{{ .Prefix }}.ws-eu02.example.com",
165
ExpectedWorkspacePortURLTemplate: "https://{{ .WorkspacePort }}-{{ .Prefix }}.ws-eu02.example.com",
166
},
167
{
168
Name: "Without an installation shortname",
169
Domain: "example.com",
170
InstallationShortname: "",
171
ExpectedWorkspaceUrlTemplate: "https://{{ .Prefix }}.ws.example.com",
172
ExpectedWorkspacePortURLTemplate: "https://{{ .WorkspacePort }}-{{ .Prefix }}.ws.example.com",
173
},
174
{
175
Name: "With old default installation shortname for existing self-hosted installations",
176
Domain: "example.com",
177
InstallationShortname: config.InstallationShortNameOldDefault,
178
ExpectedWorkspaceUrlTemplate: "https://{{ .Prefix }}.ws.example.com",
179
ExpectedWorkspacePortURLTemplate: "https://{{ .WorkspacePort }}-{{ .Prefix }}.ws.example.com",
180
},
181
}
182
183
for _, test := range tests {
184
t.Run(test.Name, func(t *testing.T) {
185
ctx, err := common.NewRenderContext(config.Config{
186
Domain: test.Domain,
187
Metadata: config.Metadata{
188
InstallationShortname: test.InstallationShortname,
189
},
190
ObjectStorage: config.ObjectStorage{
191
InCluster: pointer.Bool(true),
192
},
193
}, versions.Manifest{}, "test_namespace")
194
require.NoError(t, err)
195
196
objs, err := configmap(ctx)
197
require.NoError(t, err)
198
199
cfgmap, ok := objs[0].(*corev1.ConfigMap)
200
require.Truef(t, ok, "configmap function did not return a configmap")
201
202
configJson, ok := cfgmap.Data["config.json"]
203
require.Truef(t, ok, "configmap data did not contain %q key", "config.json")
204
205
serviceConfig := wsmancfg.ServiceConfiguration{}
206
json.Unmarshal([]byte(configJson), &serviceConfig)
207
208
require.Equal(t, test.ExpectedWorkspaceUrlTemplate, serviceConfig.Manager.WorkspaceURLTemplate)
209
require.Equal(t, test.ExpectedWorkspacePortURLTemplate, serviceConfig.Manager.WorkspacePortURLTemplate)
210
})
211
}
212
}
213
214