sigs.k8s.io/cluster-api@v1.7.1/util/resource/resource_test.go (about) 1 /* 2 Copyright 2020 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 resource 18 19 import ( 20 "math/rand" 21 "testing" 22 23 . "github.com/onsi/gomega" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 ) 26 27 var ( 28 kindsWithPriority = []string{ 29 "Namespace", "CustomResourceDefinition", "StorageClass", "PersistentVolume", 30 "PersistentVolumeClaim", "Secret", "ConfigMap", "ServiceAccount", "LimitRange", 31 "Pod", "ReplicaSet", "Endpoint", 32 } 33 kindsOther = []string{ 34 "Deployment", "Service", "DaemonSet", "StatefulSet", "Job", "CronJob", "Ingress", 35 "NetworkPolicy", "Role", "RoleBinding", 36 } 37 ) 38 39 func TestSortForCreate(t *testing.T) { 40 g := NewWithT(t) 41 42 cm := unstructured.Unstructured{} 43 cm.SetKind("ConfigMap") 44 45 ns := unstructured.Unstructured{} 46 ns.SetKind("Namespace") 47 48 ep := unstructured.Unstructured{} 49 ep.SetKind("Endpoint") 50 51 dp := unstructured.Unstructured{} 52 dp.SetKind("Deployment") 53 54 ds := unstructured.Unstructured{} 55 ds.SetKind("DaemonSet") 56 57 resources := []unstructured.Unstructured{ds, dp, ep, cm, ns} 58 sorted := SortForCreate(resources) 59 g.Expect(sorted).To(HaveLen(5)) 60 g.Expect(sorted[0].GetKind()).To(BeIdenticalTo("Namespace")) 61 g.Expect(sorted[1].GetKind()).To(BeIdenticalTo("ConfigMap")) 62 g.Expect(sorted[2].GetKind()).To(BeIdenticalTo("Endpoint")) 63 // we have no way to determine the order of the last two elements 64 g.Expect(sorted[3].GetKind()).To(BeElementOf([]string{"DaemonSet", "Deployment"})) 65 g.Expect(sorted[4].GetKind()).To(BeElementOf([]string{"DaemonSet", "Deployment"})) 66 } 67 68 func TestSortForCreateAllShuffle(t *testing.T) { 69 g := NewWithT(t) 70 71 resources := make([]unstructured.Unstructured, 0, len(kindsWithPriority)+len(kindsOther)) 72 for _, kind := range append(kindsWithPriority, kindsOther...) { 73 resource := unstructured.Unstructured{} 74 resource.SetKind(kind) 75 resources = append(resources, resource) 76 } 77 for j := 0; j < 100; j++ { 78 // determinically shuffle resources 79 rnd := rand.New(rand.NewSource(int64(j))) //nolint:gosec 80 rnd.Shuffle(len(resources), func(i, j int) { 81 resources[i], resources[j] = resources[j], resources[i] 82 }) 83 84 sorted := SortForCreate(resources) 85 g.Expect(sorted).To(HaveLen(len(kindsWithPriority) + len(kindsOther))) 86 // first check that the first len(kindsWithPriority) elements are from 87 // the list of kinds with priority (and in order) 88 for i, res := range sorted[:len(kindsWithPriority)] { 89 g.Expect(res.GetKind()).To(BeIdenticalTo(kindsWithPriority[i])) 90 } 91 // while the rest of resources can be any of the other kinds 92 for _, res := range sorted[len(kindsWithPriority):] { 93 g.Expect(kindsOther).To(ContainElement(res.GetKind())) 94 } 95 } 96 }