From 7a59d20c680f6ba5ce350efeb1a2eb3cad37987b Mon Sep 17 00:00:00 2001 From: Marko Mikulicic Date: Tue, 29 Oct 2019 16:34:58 +0100 Subject: [PATCH] Fix GenerateName in test --- cmd/controller/main_test.go | 23 ++++ .../k8s.io/apimachinery/pkg/util/rand/rand.go | 127 ++++++++++++++++++ vendor/modules.txt | 1 + 3 files changed, 151 insertions(+) create mode 100644 vendor/k8s.io/apimachinery/pkg/util/rand/rand.go diff --git a/cmd/controller/main_test.go b/cmd/controller/main_test.go index 7b3ffe7ea..087bb80b8 100644 --- a/cmd/controller/main_test.go +++ b/cmd/controller/main_test.go @@ -4,11 +4,14 @@ import ( "crypto/rsa" "crypto/x509" "encoding/pem" + "fmt" "testing" "time" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + krand "k8s.io/apimachinery/pkg/util/rand" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" ktesting "k8s.io/client-go/testing" @@ -29,9 +32,20 @@ func hasAction(fake *fake.Clientset, verb, resource string) bool { return findAction(fake, verb, resource) != nil } +// generateNameReactor implements the logic required for the GenerateName field to work when using +// the fake client. Add it with client.PrependReactor to your fake client. +func generateNameReactor(action ktesting.Action) (handled bool, ret runtime.Object, err error) { + s := action.(ktesting.CreateAction).GetObject().(*v1.Secret) + if s.Name == "" && s.GenerateName != "" { + s.Name = fmt.Sprintf("%s-%s", s.GenerateName, krand.String(16)) + } + return false, nil, nil +} + func TestInitKeyRegistry(t *testing.T) { rand := testRand() client := fake.NewSimpleClientset() + client.PrependReactor("create", "secrets", generateNameReactor) registry, err := initKeyRegistry(client, rand, "namespace", "prefix", "label", 1024) if err != nil { @@ -59,6 +73,8 @@ func TestInitKeyRegistry(t *testing.T) { func TestInitKeyRotation(t *testing.T) { rand := testRand() client := fake.NewSimpleClientset() + client.PrependReactor("create", "secrets", generateNameReactor) + registry, err := initKeyRegistry(client, rand, "namespace", "prefix", "label", 1024) if err != nil { t.Fatalf("initKeyRegistry() returned err: %v", err) @@ -95,6 +111,8 @@ func TestInitKeyRotation(t *testing.T) { func TestInitKeyRotationTick(t *testing.T) { rand := testRand() client := fake.NewSimpleClientset() + client.PrependReactor("create", "secrets", generateNameReactor) + registry, err := initKeyRegistry(client, rand, "namespace", "prefix", "label", 1024) if err != nil { t.Fatalf("initKeyRegistry() returned err: %v", err) @@ -138,6 +156,8 @@ func TestReuseKey(t *testing.T) { } client := fake.NewSimpleClientset() + client.PrependReactor("create", "secrets", generateNameReactor) + _, err = writeKey(client, key, []*x509.Certificate{cert}, "namespace", SealedSecretsKeyLabel, "prefix") if err != nil { t.Errorf("writeKey() failed with: %v", err) @@ -180,6 +200,8 @@ func TestRenewStaleKey(t *testing.T) { oldAge = period - staleness ) client := fake.NewSimpleClientset() + client.PrependReactor("create", "secrets", generateNameReactor) + _, err = writeKey(client, key, []*x509.Certificate{cert}, "namespace", SealedSecretsKeyLabel, "prefix", writeKeyWithCreationTime(metav1.NewTime(time.Now().Add(-oldAge)))) if err != nil { @@ -250,6 +272,7 @@ func TestLegacySecret(t *testing.T) { } client := fake.NewSimpleClientset() + client.PrependReactor("create", "secrets", generateNameReactor) _, err = writeLegacyKey(client, key, []*x509.Certificate{cert}, "namespace", "prefix") if err != nil { diff --git a/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go b/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go new file mode 100644 index 000000000..82a473bb1 --- /dev/null +++ b/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go @@ -0,0 +1,127 @@ +/* +Copyright 2015 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 rand provides utilities related to randomization. +package rand + +import ( + "math/rand" + "sync" + "time" +) + +var rng = struct { + sync.Mutex + rand *rand.Rand +}{ + rand: rand.New(rand.NewSource(time.Now().UnixNano())), +} + +// Int returns a non-negative pseudo-random int. +func Int() int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Int() +} + +// Intn generates an integer in range [0,max). +// By design this should panic if input is invalid, <= 0. +func Intn(max int) int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Intn(max) +} + +// IntnRange generates an integer in range [min,max). +// By design this should panic if input is invalid, <= 0. +func IntnRange(min, max int) int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Intn(max-min) + min +} + +// IntnRange generates an int64 integer in range [min,max). +// By design this should panic if input is invalid, <= 0. +func Int63nRange(min, max int64) int64 { + rng.Lock() + defer rng.Unlock() + return rng.rand.Int63n(max-min) + min +} + +// Seed seeds the rng with the provided seed. +func Seed(seed int64) { + rng.Lock() + defer rng.Unlock() + + rng.rand = rand.New(rand.NewSource(seed)) +} + +// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n) +// from the default Source. +func Perm(n int) []int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Perm(n) +} + +const ( + // We omit vowels from the set of available characters to reduce the chances + // of "bad words" being formed. + alphanums = "bcdfghjklmnpqrstvwxz2456789" + // No. of bits required to index into alphanums string. + alphanumsIdxBits = 5 + // Mask used to extract last alphanumsIdxBits of an int. + alphanumsIdxMask = 1<>= alphanumsIdxBits + remaining-- + } + return string(b) +} + +// SafeEncodeString encodes s using the same characters as rand.String. This reduces the chances of bad words and +// ensures that strings generated from hash functions appear consistent throughout the API. +func SafeEncodeString(s string) string { + r := make([]byte, len(s)) + for i, b := range []rune(s) { + r[i] = alphanums[(int(b) % len(alphanums))] + } + return string(r) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 22ef1a091..7a2c6b414 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -288,6 +288,7 @@ k8s.io/apimachinery/pkg/util/json k8s.io/apimachinery/pkg/util/mergepatch k8s.io/apimachinery/pkg/util/naming k8s.io/apimachinery/pkg/util/net +k8s.io/apimachinery/pkg/util/rand k8s.io/apimachinery/pkg/util/runtime k8s.io/apimachinery/pkg/util/sets k8s.io/apimachinery/pkg/util/strategicpatch