k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/phases/bootstraptoken/node/token_test.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package node 18 19 import ( 20 "context" 21 "testing" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 clientsetfake "k8s.io/client-go/kubernetes/fake" 26 bootstraptokenv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1" 27 ) 28 29 func TestUpdateOrCreateTokens(t *testing.T) { 30 tests := []struct { 31 name string 32 failIfExists bool 33 tokens []bootstraptokenv1.BootstrapToken 34 wantErr bool 35 }{ 36 { 37 name: "token is nil", 38 failIfExists: true, 39 tokens: []bootstraptokenv1.BootstrapToken{}, 40 wantErr: false, 41 }, 42 { 43 name: "create secret which does not exist", 44 failIfExists: true, 45 tokens: []bootstraptokenv1.BootstrapToken{ 46 { 47 Token: &bootstraptokenv1.BootstrapTokenString{ 48 ID: "token1", 49 Secret: "token1data", 50 }, 51 }, 52 }, 53 wantErr: false, 54 }, 55 { 56 name: "create multiple secrets which do not exist", 57 failIfExists: true, 58 tokens: []bootstraptokenv1.BootstrapToken{ 59 { 60 Token: &bootstraptokenv1.BootstrapTokenString{ 61 ID: "token1", 62 Secret: "token1data", 63 }, 64 }, 65 { 66 Token: &bootstraptokenv1.BootstrapTokenString{ 67 ID: "token2", 68 Secret: "token2data", 69 }, 70 }, 71 { 72 Token: &bootstraptokenv1.BootstrapTokenString{ 73 ID: "token3", 74 Secret: "token3data", 75 }, 76 }, 77 }, 78 wantErr: false, 79 }, 80 { 81 name: "create secret which exists, failIfExists is false", 82 failIfExists: false, 83 tokens: []bootstraptokenv1.BootstrapToken{ 84 { 85 Token: &bootstraptokenv1.BootstrapTokenString{ 86 ID: "foo", 87 Secret: "bar", 88 }, 89 }, 90 }, 91 wantErr: false, 92 }, 93 { 94 name: "create secret which exists, failIfExists is true", 95 failIfExists: true, 96 tokens: []bootstraptokenv1.BootstrapToken{ 97 { 98 Token: &bootstraptokenv1.BootstrapTokenString{ 99 ID: "foo", 100 Secret: "bar", 101 }, 102 }, 103 }, 104 wantErr: true, 105 }, 106 } 107 for _, tc := range tests { 108 t.Run(tc.name, func(t *testing.T) { 109 client := newMockClientForTest(t) 110 if err := UpdateOrCreateTokens(client, tc.failIfExists, tc.tokens); (err != nil) != tc.wantErr { 111 t.Fatalf("UpdateOrCreateTokens() error = %v, wantErr %v", err, tc.wantErr) 112 } 113 }) 114 } 115 } 116 117 func newMockClientForTest(t *testing.T) *clientsetfake.Clientset { 118 client := clientsetfake.NewSimpleClientset() 119 _, err := client.CoreV1().Secrets(metav1.NamespaceSystem).Create(context.TODO(), &v1.Secret{ 120 TypeMeta: metav1.TypeMeta{ 121 Kind: "Secret", 122 APIVersion: "v1", 123 }, 124 ObjectMeta: metav1.ObjectMeta{ 125 Name: "bootstrap-token-foo", 126 Labels: map[string]string{"app": "foo"}, 127 Namespace: metav1.NamespaceSystem, 128 }, 129 Data: map[string][]byte{"foo": {'f', 'o', 'o'}}, 130 }, metav1.CreateOptions{}) 131 if err != nil { 132 t.Fatalf("error creating sercet: %v", err) 133 } 134 return client 135 }