@@ -17,12 +17,25 @@ limitations under the License.
17
17
package resource
18
18
19
19
import (
20
+ "math/rand"
20
21
"testing"
21
22
22
23
. "github.com/onsi/gomega"
23
24
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24
25
)
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
+
26
39
func TestSortForCreate (t * testing.T ) {
27
40
g := NewWithT (t )
28
41
@@ -35,9 +48,49 @@ func TestSortForCreate(t *testing.T) {
35
48
ep := unstructured.Unstructured {}
36
49
ep .SetKind ("Endpoint" )
37
50
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 }
39
58
sorted := SortForCreate (resources )
40
- g .Expect (sorted ).To (HaveLen (3 ))
59
+ g .Expect (sorted ).To (HaveLen (5 ))
41
60
g .Expect (sorted [0 ].GetKind ()).To (BeIdenticalTo ("Namespace" ))
42
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
+ }
43
96
}
0 commit comments