Skip to content

Commit 857f98c

Browse files
Andrew Farriesroboquat
Andrew Farries
authored andcommitted
Add tests for server configmap logic
Extract named structs for fields that were nested under `ServerConfig` so that they can be instantiated in tests.
1 parent faff7db commit 857f98c

File tree

2 files changed

+146
-23
lines changed

2 files changed

+146
-23
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the MIT License. See License-MIT.txt in the project root for license information.
3+
4+
package server
5+
6+
import (
7+
"encoding/json"
8+
"testing"
9+
10+
"github.com/gitpod-io/gitpod/installer/pkg/common"
11+
"github.com/gitpod-io/gitpod/installer/pkg/config/v1"
12+
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
13+
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
16+
corev1 "k8s.io/api/core/v1"
17+
)
18+
19+
func TestConfigMap(t *testing.T) {
20+
type Expectation struct {
21+
EnableLocalApp bool
22+
DisableDynamicAuthProviderLogin bool
23+
DefaultBaseImageRegistryWhiteList []string
24+
WorkspaceImage string
25+
JWTSecret string
26+
SessionSecret string
27+
GitHubApp experimental.GithubApp
28+
}
29+
30+
expectation := Expectation{
31+
EnableLocalApp: true,
32+
DisableDynamicAuthProviderLogin: true,
33+
DefaultBaseImageRegistryWhiteList: []string{"some-registry"},
34+
WorkspaceImage: "some-workspace-image",
35+
JWTSecret: "some-jwt-secret",
36+
SessionSecret: "some-session-secret",
37+
GitHubApp: experimental.GithubApp{
38+
AppId: 123,
39+
AuthProviderId: "some-auth-provider-id",
40+
BaseUrl: "some-base-url",
41+
CertPath: "some-cert-path",
42+
Enabled: true,
43+
LogLevel: "some-log-level",
44+
MarketplaceName: "some-marketplace-name",
45+
WebhookSecret: "some-webhook-secret",
46+
CertSecretName: "some-cert-secret-name",
47+
},
48+
}
49+
50+
ctx, err := common.NewRenderContext(config.Config{
51+
Experimental: &experimental.Config{
52+
WebApp: &experimental.WebAppConfig{
53+
Server: &experimental.ServerConfig{
54+
DisableDynamicAuthProviderLogin: expectation.DisableDynamicAuthProviderLogin,
55+
EnableLocalApp: expectation.EnableLocalApp,
56+
DefaultBaseImageRegistryWhiteList: expectation.DefaultBaseImageRegistryWhiteList,
57+
WorkspaceDefaults: experimental.WorkspaceDefaults{
58+
WorkspaceImage: expectation.WorkspaceImage,
59+
},
60+
OAuthServer: experimental.OAuthServer{
61+
JWTSecret: expectation.JWTSecret,
62+
},
63+
Session: experimental.Session{
64+
Secret: expectation.SessionSecret,
65+
},
66+
GithubApp: &expectation.GitHubApp,
67+
},
68+
},
69+
},
70+
}, versions.Manifest{}, "test_namespace")
71+
72+
require.NoError(t, err)
73+
objs, err := configmap(ctx)
74+
if err != nil {
75+
t.Errorf("failed to generate configmap: %s\n", err)
76+
}
77+
78+
configmap, ok := objs[0].(*corev1.ConfigMap)
79+
if !ok {
80+
t.Fatalf("rendering configmap did not return a configMap")
81+
return
82+
}
83+
84+
configJson, ok := configmap.Data["config.json"]
85+
if ok == false {
86+
t.Errorf("no %q key found in configmap data", "config.json")
87+
}
88+
89+
var config ConfigSerialized
90+
if err := json.Unmarshal([]byte(configJson), &config); err != nil {
91+
t.Errorf("failed to unmarshal config json: %s", err)
92+
}
93+
94+
actual := Expectation{
95+
DisableDynamicAuthProviderLogin: config.DisableDynamicAuthProviderLogin,
96+
EnableLocalApp: config.EnableLocalApp,
97+
DefaultBaseImageRegistryWhiteList: config.DefaultBaseImageRegistryWhitelist,
98+
WorkspaceImage: config.WorkspaceDefaults.WorkspaceImage,
99+
JWTSecret: config.OAuthServer.JWTSecret,
100+
SessionSecret: config.Session.Secret,
101+
GitHubApp: experimental.GithubApp{
102+
AppId: config.GitHubApp.AppId,
103+
AuthProviderId: config.GitHubApp.AuthProviderId,
104+
BaseUrl: config.GitHubApp.BaseUrl,
105+
CertPath: config.GitHubApp.CertPath,
106+
Enabled: config.GitHubApp.Enabled,
107+
LogLevel: config.GitHubApp.LogLevel,
108+
MarketplaceName: config.GitHubApp.MarketplaceName,
109+
WebhookSecret: config.GitHubApp.WebhookSecret,
110+
CertSecretName: config.GitHubApp.CertSecretName,
111+
},
112+
}
113+
114+
assert.Equal(t, expectation, actual)
115+
}

install/installer/pkg/config/v1/experimental/experimental.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,38 @@ type WebAppConfig struct {
8686
UsePodAntiAffinity bool `json:"usePodAntiAffinity"`
8787
}
8888

89+
type WorkspaceDefaults struct {
90+
WorkspaceImage string `json:"workspaceImage"`
91+
}
92+
93+
type OAuthServer struct {
94+
JWTSecret string `json:"jwtSecret"`
95+
}
96+
97+
type Session struct {
98+
Secret string `json:"secret"`
99+
}
100+
101+
type GithubApp struct {
102+
AppId int32 `json:"appId"`
103+
AuthProviderId string `json:"authProviderId"`
104+
BaseUrl string `json:"baseUrl"`
105+
CertPath string `json:"certPath"`
106+
Enabled bool `json:"enabled"`
107+
LogLevel string `json:"logLevel"`
108+
MarketplaceName string `json:"marketplaceName"`
109+
WebhookSecret string `json:"webhookSecret"`
110+
CertSecretName string `json:"certSecretName"`
111+
}
112+
89113
type ServerConfig struct {
90-
WorkspaceDefaults struct {
91-
WorkspaceImage string `json:"workspaceImage"`
92-
} `json:"workspaceDefaults"`
93-
OAuthServer struct {
94-
JWTSecret string `json:"jwtSecret"`
95-
} `json:"oauthServer"`
96-
Session struct {
97-
Secret string `json:"secret"`
98-
} `json:"session"`
99-
GithubApp *struct {
100-
AppId int32 `json:"appId"`
101-
AuthProviderId string `json:"authProviderId"`
102-
BaseUrl string `json:"baseUrl"`
103-
CertPath string `json:"certPath"`
104-
Enabled bool `json:"enabled"`
105-
LogLevel string `json:"logLevel"`
106-
MarketplaceName string `json:"marketplaceName"`
107-
WebhookSecret string `json:"webhookSecret"`
108-
CertSecretName string `json:"certSecretName"`
109-
} `json:"githubApp"`
110-
DisableDynamicAuthProviderLogin bool `json:"disableDynamicAuthProviderLogin"`
111-
EnableLocalApp bool `json:"enableLocalApp"`
112-
DefaultBaseImageRegistryWhiteList []string `json:"defaultBaseImageRegistryWhitelist"`
114+
WorkspaceDefaults WorkspaceDefaults `json:"workspaceDefaults"`
115+
OAuthServer OAuthServer `json:"oauthServer"`
116+
Session Session `json:"session"`
117+
GithubApp *GithubApp `json:"githubApp"`
118+
DisableDynamicAuthProviderLogin bool `json:"disableDynamicAuthProviderLogin"`
119+
EnableLocalApp bool `json:"enableLocalApp"`
120+
DefaultBaseImageRegistryWhiteList []string `json:"defaultBaseImageRegistryWhitelist"`
113121
}
114122

115123
type PublicAPIConfig struct {

0 commit comments

Comments
 (0)