-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_kubeconfig_test.go
152 lines (136 loc) · 4.78 KB
/
parse_kubeconfig_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package arcaflow_lib_kubernetes
import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)
const (
CACERTPATH = "testdata/ca.crt"
CERTPATH = "testdata/client.crt"
KEYPATH = "testdata/client.key"
)
type testFixtures struct {
caCert string
clientCrt string
clientKey string
kubeconfig string
kubeconfigNoData string
kubeconfigNoHost string
kubeconfigNoContext string
kubeconfigSkipTLS string
tokenFile string
kubeconfigExtensions string
}
func NewFixtures(t *testing.T) testFixtures {
caCrt, err := os.ReadFile(CACERTPATH)
assert.Nil(t, err)
clientCrt, err := os.ReadFile(CERTPATH)
assert.Nil(t, err)
clientKey, err := os.ReadFile(KEYPATH)
assert.Nil(t, err)
kubeNodata, err := os.ReadFile("testdata/kubeconfig-nodata.yaml")
assert.Nil(t, err)
kube, err := os.ReadFile("testdata/kubeconfig-data.yaml")
assert.Nil(t, err)
kubeNoHost, err := os.ReadFile("testdata/kubeconfig-nohost.yaml")
assert.Nil(t, err)
kubeNoCtx, err := os.ReadFile("testdata/kubeconfig-nocontext.yaml")
assert.Nil(t, err)
kubeTLSSkip, err := os.ReadFile("testdata/kubeconfig-tlsskip.yaml")
assert.Nil(t, err)
tokenFile, err := os.ReadFile("testdata/tokenfile")
assert.Nil(t, err)
kubeExtensions, err := os.ReadFile("testdata/kubeconfig-extensions.yaml")
assert.Nil(t, err)
return testFixtures{
caCert: string(caCrt),
clientCrt: string(clientCrt),
clientKey: string(clientKey),
kubeconfig: string(kube),
kubeconfigNoData: string(kubeNodata),
kubeconfigNoHost: string(kubeNoHost),
kubeconfigNoContext: string(kubeNoCtx),
kubeconfigSkipTLS: string(kubeTLSSkip),
tokenFile: string(tokenFile),
kubeconfigExtensions: string(kubeExtensions),
}
}
func TestParseKubeConfig(t *testing.T) {
fixtures := NewFixtures(t)
kubeconf, err := ParseKubeConfig(fixtures.kubeconfig)
assert.Nil(t, err)
assert.NotNil(t, kubeconf)
kubeconfExtensions, err := ParseKubeConfig(fixtures.kubeconfigExtensions)
assert.Nil(t, err)
assert.NotNil(t, kubeconfExtensions)
}
func TestKubeConfigToConnection(t *testing.T) {
// test with cert inlining
fixtures := NewFixtures(t)
kubeconf, err := ParseKubeConfig(fixtures.kubeconfigNoData)
assert.Nil(t, err)
connection, err := KubeConfigToConnection(kubeconf, true)
assert.Nil(t, err)
assert.NotNil(t, connection)
assert.Equal(t, connection.KeyData, fixtures.clientKey)
assert.Equal(t, connection.CAData, fixtures.caCert)
assert.Equal(t, connection.CertData, fixtures.clientCrt)
// test that by default insecure-skip-tls-verify is false
assert.False(t, connection.Insecure)
// test without inlining
kubeconf, err = ParseKubeConfig(fixtures.kubeconfigNoData)
assert.Nil(t, err)
connection, err = KubeConfigToConnection(kubeconf, false)
assert.Nil(t, err)
assert.Equal(t, connection.KeyFile, KEYPATH)
assert.Equal(t, connection.CertFile, CERTPATH)
assert.Equal(t, connection.CAFile, CACERTPATH)
// test failure on empty host
kubeconf, err = ParseKubeConfig(fixtures.kubeconfigNoHost)
assert.NoError(t, err)
assert.NotNil(t, kubeconf)
connection, err = KubeConfigToConnection(kubeconf, true)
assert.NotNil(t, err)
// test failure on empty default context
kubeconf, err = ParseKubeConfig(fixtures.kubeconfigNoContext)
assert.NoError(t, err)
assert.NotNil(t, kubeconf)
connection, err = KubeConfigToConnection(kubeconf, true)
assert.NotNil(t, err)
// test success on insecure-skip-tls-verify: true
kubeconf, err = ParseKubeConfig(fixtures.kubeconfigSkipTLS)
assert.NoError(t, err)
assert.NotNil(t, kubeconf)
connection, err = KubeConfigToConnection(kubeconf, true)
assert.True(t, connection.Insecure)
assert.Nil(t, err)
}
func TestConnectionToKubeConfig(t *testing.T) {
// test parsing without file inlining
fixtures := NewFixtures(t)
kubeconf, err := ParseKubeConfig(fixtures.kubeconfigNoData)
assert.Nil(t, err)
connection, err := KubeConfigToConnection(kubeconf, false)
assert.NoError(t, err)
kubeconfBack, err := ConnectionToKubeConfig(connection)
assert.Nil(t, err)
assert.Equal(t, kubeconf, kubeconfBack)
// test parsing with file inlining
kubeconf, err = ParseKubeConfig(fixtures.kubeconfig)
assert.Nil(t, err)
connection, err = KubeConfigToConnection(kubeconf, false)
assert.NoError(t, err)
kubeconfBack, err = ConnectionToKubeConfig(connection)
assert.Nil(t, err)
assert.Equal(t, kubeconf, kubeconfBack)
// test parsing without inlining with token file in connection
kubeconf, err = ParseKubeConfig(fixtures.kubeconfigNoData)
assert.Nil(t, err)
connection, err = KubeConfigToConnection(kubeconf, false)
assert.NoError(t, err)
connection.BearerToken = ""
connection.BearerTokenFile = "testdata/tokenfile"
kubeconfBack, err = ConnectionToKubeConfig(connection)
assert.Nil(t, err)
assert.Equal(t, kubeconf, kubeconfBack)
}