@@ -17,12 +17,25 @@ limitations under the License.
1717package resource
1818
1919import (
20+ "math/rand"
2021 "testing"
2122
2223 . "github.com/onsi/gomega"
2324 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2425)
2526
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+
2639func TestSortForCreate (t * testing.T ) {
2740 g := NewWithT (t )
2841
@@ -35,9 +48,49 @@ func TestSortForCreate(t *testing.T) {
3548 ep := unstructured.Unstructured {}
3649 ep .SetKind ("Endpoint" )
3750
38- resources := []unstructured.Unstructured {ep , cm , ns }
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 }
3958 sorted := SortForCreate (resources )
40- g .Expect (sorted ).To (HaveLen (3 ))
59+ g .Expect (sorted ).To (HaveLen (5 ))
4160 g .Expect (sorted [0 ].GetKind ()).To (BeIdenticalTo ("Namespace" ))
4261 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+ }
4396}
0 commit comments