Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/preview/previewctl/pkg/k8s/context/k3s/k3s_test.go
2506 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package k3s
6
7
import (
8
"context"
9
"testing"
10
11
"github.com/cockroachdb/errors"
12
"github.com/stretchr/testify/assert"
13
"k8s.io/apimachinery/pkg/runtime"
14
"k8s.io/client-go/tools/clientcmd/api"
15
16
pssh "github.com/gitpod-io/gitpod/previewctl/pkg/ssh"
17
)
18
19
func Test_LoadK3SConfig(t *testing.T) {
20
type k3sExpStruct struct {
21
config *api.Config
22
err error
23
}
24
type testCase struct {
25
name string
26
cmd pssh.MockCmd
27
expected *k3sExpStruct
28
}
29
30
testCases := []testCase{
31
{
32
name: "k3s config not found",
33
cmd: pssh.MockCmd{
34
CMD: catK3sConfigCmd,
35
STDOUT: []byte(""),
36
STDERR: []byte("cat: /etc/rancher/k3s/k3s.yaml: No such file or directory"),
37
Err: errors.New("some error that will be irrelevant"),
38
},
39
expected: &k3sExpStruct{
40
config: nil,
41
err: ErrK3SConfigNotFound,
42
},
43
},
44
{
45
name: "returned config",
46
cmd: pssh.MockCmd{
47
CMD: catK3sConfigCmd,
48
STDOUT: []byte(`
49
apiVersion: v1
50
clusters:
51
- cluster:
52
certificate-authority-data: dGVzdF9kYXRh
53
server: https://default.kube.gitpod-dev.com:6443
54
name: default
55
contexts:
56
- context:
57
cluster: default
58
user: default
59
name: default
60
current-context: default
61
kind: Config
62
preferences: {}
63
users:
64
- name: default
65
user:
66
client-certificate-data: dGVzdF9kYXRh
67
client-key-data: dGVzdF9kYXRh
68
`),
69
STDERR: nil,
70
Err: nil,
71
},
72
expected: &k3sExpStruct{
73
config: &api.Config{
74
Preferences: api.Preferences{
75
Extensions: map[string]runtime.Object{},
76
},
77
Contexts: map[string]*api.Context{
78
"k3s": {
79
Cluster: "k3s",
80
AuthInfo: "k3s",
81
Extensions: map[string]runtime.Object{},
82
},
83
},
84
Clusters: map[string]*api.Cluster{
85
"k3s": {
86
Server: "https://k3s.preview.gitpod-dev.com:6443",
87
CertificateAuthorityData: []byte("test_data"),
88
Extensions: map[string]runtime.Object{},
89
},
90
},
91
CurrentContext: "k3s",
92
AuthInfos: map[string]*api.AuthInfo{
93
"k3s": {
94
ClientCertificateData: []byte("test_data"),
95
ClientKeyData: []byte("test_data"),
96
Extensions: map[string]runtime.Object{},
97
},
98
},
99
Extensions: map[string]runtime.Object{},
100
},
101
err: nil,
102
},
103
},
104
}
105
106
for _, test := range testCases {
107
t.Run(test.name, func(t *testing.T) {
108
c := &pssh.MockClient{Command: test.cmd}
109
k := &ConfigLoader{client: c, opts: ConfigLoaderOpts{
110
PreviewName: "k3s",
111
}}
112
113
config, err := k.Load(context.TODO())
114
115
assert.ErrorIs(t, test.expected.err, err)
116
assert.Equal(t, test.expected.config, config)
117
})
118
}
119
}
120
121