...
1
16
17 package kubeconfig
18
19 import (
20 "crypto/x509"
21 "encoding/pem"
22 "testing"
23
24 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
25
26 certstestutil "k8s.io/kubernetes/cmd/kubeadm/app/util/certs"
27 )
28
29
30
31 func AssertKubeConfigCurrentCluster(t *testing.T, config *clientcmdapi.Config, expectedAPIServerAddress string, expectedAPIServerCaCert *x509.Certificate) {
32 currentContext := config.Contexts[config.CurrentContext]
33 currentCluster := config.Clusters[currentContext.Cluster]
34
35
36 if currentCluster.Server != expectedAPIServerAddress {
37 t.Errorf("kubeconfig.currentCluster.Server is [%s], expected [%s]", currentCluster.Server, expectedAPIServerAddress)
38 }
39
40
41 if len(currentCluster.CertificateAuthorityData) == 0 {
42 t.Error("kubeconfig.currentCluster.CertificateAuthorityData is empty, expected not empty")
43 return
44 }
45
46 block, _ := pem.Decode(currentCluster.CertificateAuthorityData)
47 currentAPIServerCaCert, err := x509.ParseCertificate(block.Bytes)
48 if err != nil {
49 t.Errorf("kubeconfig.currentCluster.CertificateAuthorityData is not a valid CA: %v", err)
50 return
51 }
52
53 if !currentAPIServerCaCert.Equal(expectedAPIServerCaCert) {
54 t.Errorf("kubeconfig.currentCluster.CertificateAuthorityData not correspond to the expected CA cert")
55 }
56 }
57
58
59
60 func AssertKubeConfigCurrentAuthInfoWithClientCert(t *testing.T, config *clientcmdapi.Config, signinCa *x509.Certificate, expectedClientName string, expectedOrganizations ...string) {
61 currentContext := config.Contexts[config.CurrentContext]
62 currentAuthInfo := config.AuthInfos[currentContext.AuthInfo]
63
64
65 if len(currentAuthInfo.ClientCertificateData) == 0 {
66 t.Error("kubeconfig.currentAuthInfo.ClientCertificateData is empty, expected not empty")
67 return
68 }
69
70 block, _ := pem.Decode(config.AuthInfos[currentContext.AuthInfo].ClientCertificateData)
71 currentClientCert, err := x509.ParseCertificate(block.Bytes)
72 if err != nil {
73 t.Errorf("kubeconfig.currentAuthInfo.ClientCertificateData is not a valid CA: %v", err)
74 return
75 }
76
77
78 certstestutil.AssertCertificateIsSignedByCa(t, currentClientCert, signinCa)
79
80
81 certstestutil.AssertCertificateHasClientAuthUsage(t, currentClientCert)
82
83
84 certstestutil.AssertCertificateHasCommonName(t, currentClientCert, expectedClientName)
85
86
87 certstestutil.AssertCertificateHasOrganizations(t, currentClientCert, expectedOrganizations...)
88 }
89
90
91
92 func AssertKubeConfigCurrentAuthInfoWithToken(t *testing.T, config *clientcmdapi.Config, expectedClientName, expectedToken string) {
93 currentContext := config.Contexts[config.CurrentContext]
94 currentAuthInfo := config.AuthInfos[currentContext.AuthInfo]
95
96
97 if currentAuthInfo.Token != expectedToken {
98 t.Errorf("kubeconfig.currentAuthInfo.Token [%s], expected [%s]", currentAuthInfo.Token, expectedToken)
99 return
100 }
101 }
102
103
104
105 func AssertKubeConfigCurrentContextWithClusterName(t *testing.T, config *clientcmdapi.Config, expectedClusterName string) {
106 currentContext := config.Contexts[config.CurrentContext]
107
108
109 if currentContext.Cluster != expectedClusterName {
110 t.Errorf("kubeconfig.currentContext.clusterName [%s], expected [%s]", currentContext.Cluster, expectedClusterName)
111 return
112 }
113 }
114
View as plain text