Path: blob/main/install/installer/pkg/components/ws-manager-mk2/configmap_test.go
2501 views
// Copyright (c) 2021 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 wsmanagermk256import (7"encoding/json"8"testing"910"github.com/google/go-cmp/cmp"11"github.com/stretchr/testify/require"12corev1 "k8s.io/api/core/v1"13"k8s.io/utils/pointer"1415"github.com/gitpod-io/gitpod/installer/pkg/common"16config "github.com/gitpod-io/gitpod/installer/pkg/config/v1"17"github.com/gitpod-io/gitpod/installer/pkg/config/versions"18wsmancfg "github.com/gitpod-io/gitpod/ws-manager/api/config"19)2021func TestBuildWorkspaceTemplates(t *testing.T) {22type Expectation struct {23TplConfig wsmancfg.WorkspacePodTemplateConfiguration24Data map[string]bool25}26tests := []struct {27Name string28ClassName string29Config *config.WorkspaceTemplates30ContainerRegistry *config.ContainerRegistry31Expectation Expectation32}{33{34Name: "no templates",35ClassName: "",36Expectation: Expectation{},37},38{39Name: "empty templates",40ClassName: "",41Config: &config.WorkspaceTemplates{},42Expectation: Expectation{},43},44{45Name: "default tpl",46ClassName: "",47Config: &config.WorkspaceTemplates{48Default: &corev1.Pod{},49},50Expectation: Expectation{51TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{DefaultPath: "/workspace-templates/default.yaml"},52Data: map[string]bool{"default.yaml": true},53},54},55{56Name: "regular tpl",57ClassName: "",58Config: &config.WorkspaceTemplates{59Regular: &corev1.Pod{},60},61Expectation: Expectation{62TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{63RegularPath: "/workspace-templates/regular.yaml",64},65Data: map[string]bool{66"regular.yaml": true,67},68},69},70{71Name: "prebuild tpl",72ClassName: "",73Config: &config.WorkspaceTemplates{74Prebuild: &corev1.Pod{},75},76Expectation: Expectation{77TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{78PrebuildPath: "/workspace-templates/prebuild.yaml",79},80Data: map[string]bool{81"prebuild.yaml": true,82},83},84},85{86Name: "imgbuild tpl",87ClassName: "",88Config: &config.WorkspaceTemplates{89ImageBuild: &corev1.Pod{},90},91Expectation: Expectation{92TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{93ImagebuildPath: "/workspace-templates/imagebuild.yaml",94},95Data: map[string]bool{96"imagebuild.yaml": true,97},98},99},100{101Name: "regular class tpl",102ClassName: "awesome-class",103Config: &config.WorkspaceTemplates{104Regular: &corev1.Pod{},105},106Expectation: Expectation{107TplConfig: wsmancfg.WorkspacePodTemplateConfiguration{108RegularPath: "/workspace-templates/awesome-class-regular.yaml",109},110Data: map[string]bool{111"awesome-class-regular.yaml": true,112},113},114},115}116117for _, test := range tests {118t.Run(test.Name, func(t *testing.T) {119var (120act Expectation121tpls map[string]string122err error123)124125if test.ContainerRegistry == nil {126test.ContainerRegistry = &config.ContainerRegistry{InCluster: pointer.Bool(true)}127}128129act.TplConfig, tpls, err = buildWorkspaceTemplates(&common.RenderContext{Config: config.Config{130ContainerRegistry: *test.ContainerRegistry,131}}, test.Config, test.ClassName)132if err != nil {133t.Error(err)134}135136if len(tpls) > 0 {137dt := make(map[string]bool)138for k := range tpls {139dt[k] = true140}141act.Data = dt142}143144if diff := cmp.Diff(test.Expectation, act); diff != "" {145t.Errorf("Expectation mismatch (-want +got):\n%s", diff)146}147})148}149}150151func TestWorkspaceURLTemplates(t *testing.T) {152tests := []struct {153Name string154Domain string155InstallationShortname string156ExpectedWorkspaceUrlTemplate string157ExpectedWorkspacePortURLTemplate string158}{159{160Name: "With an installation shortname",161Domain: "example.com",162InstallationShortname: "eu02",163ExpectedWorkspaceUrlTemplate: "https://{{ .Prefix }}.ws-eu02.example.com",164ExpectedWorkspacePortURLTemplate: "https://{{ .WorkspacePort }}-{{ .Prefix }}.ws-eu02.example.com",165},166{167Name: "Without an installation shortname",168Domain: "example.com",169InstallationShortname: "",170ExpectedWorkspaceUrlTemplate: "https://{{ .Prefix }}.ws.example.com",171ExpectedWorkspacePortURLTemplate: "https://{{ .WorkspacePort }}-{{ .Prefix }}.ws.example.com",172},173{174Name: "With old default installation shortname for existing self-hosted installations",175Domain: "example.com",176InstallationShortname: config.InstallationShortNameOldDefault,177ExpectedWorkspaceUrlTemplate: "https://{{ .Prefix }}.ws.example.com",178ExpectedWorkspacePortURLTemplate: "https://{{ .WorkspacePort }}-{{ .Prefix }}.ws.example.com",179},180}181182for _, test := range tests {183t.Run(test.Name, func(t *testing.T) {184ctx, err := common.NewRenderContext(config.Config{185Domain: test.Domain,186Metadata: config.Metadata{187InstallationShortname: test.InstallationShortname,188},189ObjectStorage: config.ObjectStorage{190InCluster: pointer.Bool(true),191},192}, versions.Manifest{}, "test_namespace")193require.NoError(t, err)194195objs, err := configmap(ctx)196require.NoError(t, err)197198cfgmap, ok := objs[0].(*corev1.ConfigMap)199require.Truef(t, ok, "configmap function did not return a configmap")200201configJson, ok := cfgmap.Data["config.json"]202require.Truef(t, ok, "configmap data did not contain %q key", "config.json")203204serviceConfig := wsmancfg.ServiceConfiguration{}205json.Unmarshal([]byte(configJson), &serviceConfig)206207require.Equal(t, test.ExpectedWorkspaceUrlTemplate, serviceConfig.Manager.WorkspaceURLTemplate)208require.Equal(t, test.ExpectedWorkspacePortURLTemplate, serviceConfig.Manager.WorkspacePortURLTemplate)209})210}211}212213214