github.com/openshift/installer@v1.4.17/pkg/asset/kubeconfig/kubeconfig_test.go (about) 1 package kubeconfig 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 9 "github.com/openshift/installer/pkg/asset/tls" 10 "github.com/openshift/installer/pkg/types" 11 ) 12 13 type testCertKey struct { 14 key string 15 cert string 16 } 17 18 func (t *testCertKey) Key() []byte { 19 return []byte(t.key) 20 } 21 22 func (t *testCertKey) Cert() []byte { 23 return []byte(t.cert) 24 } 25 26 func TestKubeconfigGenerate(t *testing.T) { 27 rootCA := &testCertKey{ 28 key: "THIS IS ROOT CA KEY DATA", 29 cert: "THIS IS ROOT CA CERT DATA", 30 } 31 32 adminCert := &testCertKey{ 33 key: "THIS IS ADMIN KEY DATA", 34 cert: "THIS IS ADMIN CERT DATA", 35 } 36 37 kubeletCert := &testCertKey{ 38 key: "THIS IS KUBELET KEY DATA", 39 cert: "THIS IS KUBELET CERT DATA", 40 } 41 42 installConfig := &types.InstallConfig{ 43 ObjectMeta: metav1.ObjectMeta{ 44 Name: "test-cluster-name", 45 }, 46 BaseDomain: "test.example.com", 47 } 48 49 tests := []struct { 50 name string 51 userName string 52 filename string 53 clientCert tls.CertKeyInterface 54 apiURL string 55 expectedData []byte 56 }{ 57 { 58 name: "admin kubeconfig", 59 userName: "admin", 60 filename: "auth/kubeconfig", 61 clientCert: adminCert, 62 apiURL: "https://api-int.test-cluster-name.test.example.com:6443", 63 expectedData: []byte(`clusters: 64 - cluster: 65 certificate-authority-data: VEhJUyBJUyBST09UIENBIENFUlQgREFUQQ== 66 server: https://api-int.test-cluster-name.test.example.com:6443 67 name: test-cluster-name 68 contexts: 69 - context: 70 cluster: test-cluster-name 71 user: admin 72 name: admin 73 current-context: admin 74 preferences: {} 75 users: 76 - name: admin 77 user: 78 client-certificate-data: VEhJUyBJUyBBRE1JTiBDRVJUIERBVEE= 79 client-key-data: VEhJUyBJUyBBRE1JTiBLRVkgREFUQQ== 80 `), 81 }, 82 { 83 name: "kubelet kubeconfig", 84 userName: "kubelet", 85 filename: "auth/kubeconfig-kubelet", 86 clientCert: kubeletCert, 87 apiURL: "https://api-int.test-cluster-name.test.example.com:6443", 88 expectedData: []byte(`clusters: 89 - cluster: 90 certificate-authority-data: VEhJUyBJUyBST09UIENBIENFUlQgREFUQQ== 91 server: https://api-int.test-cluster-name.test.example.com:6443 92 name: test-cluster-name 93 contexts: 94 - context: 95 cluster: test-cluster-name 96 user: kubelet 97 name: kubelet 98 current-context: kubelet 99 preferences: {} 100 users: 101 - name: kubelet 102 user: 103 client-certificate-data: VEhJUyBJUyBLVUJFTEVUIENFUlQgREFUQQ== 104 client-key-data: VEhJUyBJUyBLVUJFTEVUIEtFWSBEQVRB 105 `), 106 }, 107 } 108 109 for _, tt := range tests { 110 t.Run(tt.name, func(t *testing.T) { 111 kc := &kubeconfig{} 112 err := kc.generate(rootCA, tt.clientCert, tt.apiURL, installConfig.GetName(), tt.userName, tt.filename) 113 assert.NoError(t, err, "unexpected error generating config") 114 actualFiles := kc.Files() 115 assert.Equal(t, 1, len(actualFiles), "unexpected number of files generated") 116 assert.Equal(t, tt.filename, actualFiles[0].Filename, "unexpected file name generated") 117 assert.Equal(t, tt.expectedData, actualFiles[0].Data, "unexpected config") 118 }) 119 } 120 121 }