Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-manager-api/go/config/config_test.go
2500 views
1
// Copyright (c) 2020 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 config
6
7
import (
8
"testing"
9
"time"
10
11
"github.com/gitpod-io/gitpod/common-go/util"
12
)
13
14
func BenchmarkRenderWorkspacePortURL(b *testing.B) {
15
b.ReportAllocs()
16
17
for n := 0; n < b.N; n++ {
18
RenderWorkspaceURL("{{.Port}}-{{.Prefix}}.{{.Host}}", "foo", "bar", "gitpod.io")
19
}
20
}
21
22
func TestValidate(t *testing.T) {
23
fromValidConfig := func(mod func(*Configuration)) *Configuration {
24
res := &Configuration{
25
Timeouts: WorkspaceTimeoutConfiguration{
26
TotalStartup: util.Duration(10 * time.Second),
27
Initialization: util.Duration(10 * time.Second),
28
RegularWorkspace: util.Duration(10 * time.Second),
29
MaxLifetime: util.Duration(10 * time.Second),
30
HeadlessWorkspace: util.Duration(10 * time.Second),
31
AfterClose: util.Duration(10 * time.Second),
32
ContentFinalization: util.Duration(10 * time.Second),
33
Stopping: util.Duration(10 * time.Second),
34
Interrupted: util.Duration(10 * time.Second),
35
},
36
WorkspaceClasses: map[string]*WorkspaceClass{
37
DefaultWorkspaceClass: {},
38
},
39
HeartbeatInterval: util.Duration(10 * time.Second),
40
GitpodHostURL: "https://gitpod.io",
41
ReconnectionInterval: util.Duration(10 * time.Second),
42
WorkspaceURLTemplate: "https://gitpod.io/foobar",
43
WorkspaceHostPath: "/mnt/data",
44
}
45
mod(res)
46
return res
47
}
48
49
tests := []struct {
50
Name string
51
Expectation string
52
Cfg *Configuration
53
}{
54
{
55
Name: "missing default class",
56
Cfg: fromValidConfig(func(c *Configuration) {
57
delete(c.WorkspaceClasses, DefaultWorkspaceClass)
58
}),
59
Expectation: `missing default workspace class ("g1-standard")`,
60
},
61
{
62
Name: "invalid workspace class name",
63
Cfg: fromValidConfig(func(c *Configuration) {
64
c.WorkspaceClasses["not/a/valid/name"] = &WorkspaceClass{}
65
}),
66
Expectation: `workspace class name "not/a/valid/name" is invalid: [a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')]`,
67
},
68
}
69
for _, test := range tests {
70
t.Run(test.Name, func(t *testing.T) {
71
err := test.Cfg.Validate()
72
73
var errMsg string
74
if err != nil {
75
errMsg = err.Error()
76
}
77
78
if errMsg != test.Expectation {
79
t.Errorf("unexpected validation result: expect \"%s\", got \"%s\"", test.Expectation, errMsg)
80
}
81
})
82
}
83
}
84
85