k8s.io/kubernetes@v1.29.3/pkg/controller/bootstrap/common_test.go (about) 1 /* 2 Copyright 2016 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 bootstrap 18 19 import ( 20 "testing" 21 22 v1 "k8s.io/api/core/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/types" 25 "k8s.io/apimachinery/pkg/util/dump" 26 core "k8s.io/client-go/testing" 27 bootstrapapi "k8s.io/cluster-bootstrap/token/api" 28 "k8s.io/kubernetes/pkg/apis/core/helper" 29 ) 30 31 func newTokenSecret(tokenID, tokenSecret string) *v1.Secret { 32 return &v1.Secret{ 33 ObjectMeta: metav1.ObjectMeta{ 34 Namespace: metav1.NamespaceSystem, 35 Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID, 36 ResourceVersion: "1", 37 UID: types.UID("uid" + tokenID), 38 }, 39 Type: bootstrapapi.SecretTypeBootstrapToken, 40 Data: map[string][]byte{ 41 bootstrapapi.BootstrapTokenIDKey: []byte(tokenID), 42 bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret), 43 }, 44 } 45 } 46 47 func addSecretExpiration(s *v1.Secret, expiration string) { 48 s.Data[bootstrapapi.BootstrapTokenExpirationKey] = []byte(expiration) 49 } 50 51 func addSecretSigningUsage(s *v1.Secret, value string) { 52 s.Data[bootstrapapi.BootstrapTokenUsageSigningKey] = []byte(value) 53 } 54 55 func verifyActions(t *testing.T, expected, actual []core.Action) { 56 for i, a := range actual { 57 if len(expected) < i+1 { 58 t.Errorf("%d unexpected actions: %s", len(actual)-len(expected), dump.Pretty(actual[i:])) 59 break 60 } 61 62 e := expected[i] 63 if !helper.Semantic.DeepEqual(e, a) { 64 t.Errorf("Expected\n\t%s\ngot\n\t%s", dump.Pretty(e), dump.Pretty(a)) 65 continue 66 } 67 } 68 69 if len(expected) > len(actual) { 70 t.Errorf("%d additional expected actions", len(expected)-len(actual)) 71 for _, a := range expected[len(actual):] { 72 t.Logf(" %s", dump.Pretty(a)) 73 } 74 } 75 }