|
| 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 | +} |
0 commit comments