-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_internal_test.go
108 lines (89 loc) · 3.39 KB
/
main_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"github.com/cloudogu/k8s-ces-setup/app/context"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd/api"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_createSetupRouter(t *testing.T) {
_ = os.Unsetenv(context.EnvironmentVariableTargetNamespace)
t.Run("Startup without error", func(t *testing.T) {
// given
oldGetConfig := ctrl.GetConfig
defer func() { ctrl.GetConfig = oldGetConfig }()
ctrl.GetConfig = func() (*rest.Config, error) {
return &rest.Config{}, nil
}
t.Setenv(context.EnvironmentVariableTargetNamespace, "myTestNamespace")
t.Setenv(context.EnvironmentVariableStage, "development")
contextBuilder := context.NewSetupContextBuilder("dev")
contextBuilder.DevSetupConfigPath = "testdata/k8s-ces-setup-testdata.yaml"
contextBuilder.DevStartupConfigPath = "testdata/testSetup.json.yaml"
contextBuilder.DevDoguRegistrySecretPath = "testdata/testRegistrySecret.yaml"
contextBuilder.DevHelmRepositoryDataPath = "testdata/test-helm-repository.yaml"
// when
router, err := createSetupRouter(contextBuilder)
// then
require.NoError(t, err)
assert.NotNil(t, router)
})
t.Run("Startup with error while creating client set", func(t *testing.T) {
// given
oldGetConfig := ctrl.GetConfig
defer func() { ctrl.GetConfig = oldGetConfig }()
ctrl.GetConfig = func() (*rest.Config, error) {
return &rest.Config{
AuthProvider: &api.AuthProviderConfig{},
ExecProvider: &api.ExecConfig{},
}, nil
}
t.Setenv(context.EnvironmentVariableTargetNamespace, "myTestNamespace")
t.Setenv(context.EnvironmentVariableStage, "development")
contextBuilder := &context.SetupContextBuilder{}
contextBuilder.DevSetupConfigPath = "testdata/k8s-ces-setup-testdata.yaml"
contextBuilder.DevStartupConfigPath = "testdata/testSetup.json.yaml"
contextBuilder.DevDoguRegistrySecretPath = "testdata/testRegistrySecret.yaml"
// when
_, err := createSetupRouter(contextBuilder)
// then
require.Error(t, err)
assert.ErrorContains(t, err, "cannot create kubernetes client")
})
t.Run("Startup with error while creating setup context", func(t *testing.T) {
// given
oldGetConfig := ctrl.GetConfig
defer func() { ctrl.GetConfig = oldGetConfig }()
ctrl.GetConfig = func() (*rest.Config, error) {
return &rest.Config{}, nil
}
_ = os.Unsetenv(context.EnvironmentVariableTargetNamespace)
t.Setenv(context.EnvironmentVariableStage, "development")
contextBuilder := &context.SetupContextBuilder{}
contextBuilder.DevSetupConfigPath = "testdata/k8s-ces-setup-testdata.yaml"
contextBuilder.DevStartupConfigPath = "testdata/testSetup.json.yaml"
contextBuilder.DevDoguRegistrySecretPath = "testdata/testRegistrySecret.yaml"
// when
_, err := createSetupRouter(contextBuilder)
// then
require.Error(t, err)
assert.ErrorContains(t, err, "could not read current namespace: POD_NAMESPACE must be set")
})
t.Run("Startup with config error", func(t *testing.T) {
// given
oldGetConfig := ctrl.GetConfig
defer func() { ctrl.GetConfig = oldGetConfig }()
ctrl.GetConfig = func() (*rest.Config, error) {
return nil, assert.AnError
}
contextBuilder := &context.SetupContextBuilder{}
// when
_, err := createSetupRouter(contextBuilder)
// then
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to load cluster configuration")
})
}