forked from kubernetes/kubernetes
/
kubeconfig_test.go
175 lines (150 loc) · 4.88 KB
/
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package alpha
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/yaml"
kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
kubeconfigtestutil "k8s.io/kubernetes/cmd/kubeadm/test/kubeconfig"
)
func generateTestKubeadmConfig(dir, id, certDir, clusterName string) (string, error) {
cfgPath := filepath.Join(dir, id)
initCfg := kubeadmapiv1beta2.InitConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubeadm.k8s.io/v1beta2",
Kind: "InitConfiguration",
},
LocalAPIEndpoint: kubeadmapiv1beta2.APIEndpoint{
AdvertiseAddress: "1.2.3.4",
BindPort: 1234,
},
}
clusterCfg := kubeadmapiv1beta2.ClusterConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubeadm.k8s.io/v1beta2",
Kind: "ClusterConfiguration",
},
CertificatesDir: certDir,
ClusterName: clusterName,
KubernetesVersion: kubeadmconstants.MinimumControlPlaneVersion.String(),
}
var buf bytes.Buffer
data, err := yaml.Marshal(&initCfg)
if err != nil {
return "", err
}
buf.Write(data)
buf.WriteString("---\n")
data, err = yaml.Marshal(&clusterCfg)
if err != nil {
return "", err
}
buf.Write(data)
err = ioutil.WriteFile(cfgPath, buf.Bytes(), 0644)
return cfgPath, err
}
func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
// Temporary folders for the test case
tmpdir := testutil.SetupTempDir(t)
defer os.RemoveAll(tmpdir)
// Adds a pki folder with a ca cert to the temp folder
pkidir := testutil.SetupPkiDirWithCertificateAuthority(t, tmpdir)
// Retrieves ca cert for assertions
caCert, _, err := pkiutil.TryLoadCertAndKeyFromDisk(pkidir, kubeadmconstants.CACertAndKeyBaseName)
if err != nil {
t.Fatalf("couldn't retrieve ca cert: %v", err)
}
var tests = []struct {
name string
command string
clusterName string
withClientCert bool
withToken bool
additionalFlags []string
}{
{
name: "user subCommand withClientCert",
command: "user",
withClientCert: true,
},
{
name: "user subCommand withClientCert",
command: "user",
withClientCert: true,
clusterName: "my-cluster",
},
{
name: "user subCommand withToken",
withToken: true,
command: "user",
additionalFlags: []string{"--token=123456"},
},
{
name: "user subCommand withToken",
withToken: true,
command: "user",
clusterName: "my-cluster-with-token",
additionalFlags: []string{"--token=123456"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := new(bytes.Buffer)
// Get subcommands working in the temporary directory
cmd := newCmdUserKubeConfig(buf)
cfgPath, err := generateTestKubeadmConfig(tmpdir, test.name, pkidir, test.clusterName)
if err != nil {
t.Fatalf("Failed to generate kubeadm config: %v", err)
}
commonFlags := []string{
"--client-name=myUser",
fmt.Sprintf("--config=%s", cfgPath),
}
// Execute the subcommand
allFlags := append(commonFlags, test.additionalFlags...)
cmd.SetArgs(allFlags)
if err := cmd.Execute(); err != nil {
t.Fatalf("Could not execute subcommand: %v", err)
}
// reads kubeconfig written to stdout
config, err := clientcmd.Load(buf.Bytes())
if err != nil {
t.Fatalf("couldn't read kubeconfig file from buffer: %v", err)
}
// checks that CLI flags are properly propagated
kubeconfigtestutil.AssertKubeConfigCurrentCluster(t, config, "https://1.2.3.4:1234", caCert)
if test.withClientCert {
// checks that kubeconfig files have expected client cert
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithClientCert(t, config, caCert, "myUser")
}
if test.withToken {
// checks that kubeconfig files have expected token
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456")
}
if len(test.clusterName) > 0 {
// checks that kubeconfig files have expected cluster name
kubeconfigtestutil.AssertKubeConfigCurrentContextWithClusterName(t, config, test.clusterName)
}
})
}
}