Path: blob/main/components/ws-manager-api/go/config/config_test.go
2500 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package config56import (7"testing"8"time"910"github.com/gitpod-io/gitpod/common-go/util"11)1213func BenchmarkRenderWorkspacePortURL(b *testing.B) {14b.ReportAllocs()1516for n := 0; n < b.N; n++ {17RenderWorkspaceURL("{{.Port}}-{{.Prefix}}.{{.Host}}", "foo", "bar", "gitpod.io")18}19}2021func TestValidate(t *testing.T) {22fromValidConfig := func(mod func(*Configuration)) *Configuration {23res := &Configuration{24Timeouts: WorkspaceTimeoutConfiguration{25TotalStartup: util.Duration(10 * time.Second),26Initialization: util.Duration(10 * time.Second),27RegularWorkspace: util.Duration(10 * time.Second),28MaxLifetime: util.Duration(10 * time.Second),29HeadlessWorkspace: util.Duration(10 * time.Second),30AfterClose: util.Duration(10 * time.Second),31ContentFinalization: util.Duration(10 * time.Second),32Stopping: util.Duration(10 * time.Second),33Interrupted: util.Duration(10 * time.Second),34},35WorkspaceClasses: map[string]*WorkspaceClass{36DefaultWorkspaceClass: {},37},38HeartbeatInterval: util.Duration(10 * time.Second),39GitpodHostURL: "https://gitpod.io",40ReconnectionInterval: util.Duration(10 * time.Second),41WorkspaceURLTemplate: "https://gitpod.io/foobar",42WorkspaceHostPath: "/mnt/data",43}44mod(res)45return res46}4748tests := []struct {49Name string50Expectation string51Cfg *Configuration52}{53{54Name: "missing default class",55Cfg: fromValidConfig(func(c *Configuration) {56delete(c.WorkspaceClasses, DefaultWorkspaceClass)57}),58Expectation: `missing default workspace class ("g1-standard")`,59},60{61Name: "invalid workspace class name",62Cfg: fromValidConfig(func(c *Configuration) {63c.WorkspaceClasses["not/a/valid/name"] = &WorkspaceClass{}64}),65Expectation: `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])?')]`,66},67}68for _, test := range tests {69t.Run(test.Name, func(t *testing.T) {70err := test.Cfg.Validate()7172var errMsg string73if err != nil {74errMsg = err.Error()75}7677if errMsg != test.Expectation {78t.Errorf("unexpected validation result: expect \"%s\", got \"%s\"", test.Expectation, errMsg)79}80})81}82}838485