@@ -16,13 +16,16 @@ package v1beta1_test
16
16
17
17
import (
18
18
"context"
19
+ "math/rand"
19
20
"testing"
20
21
21
22
. "github.com/onsi/ginkgo/v2"
22
23
. "github.com/onsi/gomega"
24
+ "github.com/samber/lo"
23
25
. "knative.dev/pkg/logging/testing"
24
26
25
27
"sigs.k8s.io/karpenter/pkg/apis"
28
+ "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
26
29
"sigs.k8s.io/karpenter/pkg/operator/scheme"
27
30
"sigs.k8s.io/karpenter/pkg/test"
28
31
. "sigs.k8s.io/karpenter/pkg/test/expectations"
@@ -48,3 +51,50 @@ var _ = AfterEach(func() {
48
51
var _ = AfterSuite (func () {
49
52
Expect (env .Stop ()).To (Succeed (), "Failed to stop environment" )
50
53
})
54
+
55
+ var _ = Describe ("OrderByWeight" , func () {
56
+ It ("should order the NodePools by weight" , func () {
57
+ // Generate 10 NodePools that have random weights, some might have the same weights
58
+ var nodePools []v1beta1.NodePool
59
+ for i := 0 ; i < 10 ; i ++ {
60
+ np := test .NodePool (v1beta1.NodePool {
61
+ Spec : v1beta1.NodePoolSpec {
62
+ Weight : lo.ToPtr [int32 ](int32 (rand .Intn (100 ) + 1 )), //nolint:gosec
63
+ },
64
+ })
65
+ nodePools = append (nodePools , * np )
66
+ }
67
+
68
+ nodePools = lo .Shuffle (nodePools )
69
+ nodePoolList := v1beta1.NodePoolList {Items : nodePools }
70
+ nodePoolList .OrderByWeight ()
71
+
72
+ lastWeight := 101 // This is above the allowed weight values
73
+ for _ , np := range nodePoolList .Items {
74
+ Expect (lo .FromPtr (np .Spec .Weight )).To (BeNumerically ("<=" , lastWeight ))
75
+ lastWeight = int (lo .FromPtr (np .Spec .Weight ))
76
+ }
77
+ })
78
+ It ("should order the NodePools by name when the weights are the same" , func () {
79
+ // Generate 10 NodePools with the same weight
80
+ var nodePools []v1beta1.NodePool
81
+ for i := 0 ; i < 10 ; i ++ {
82
+ np := test .NodePool (v1beta1.NodePool {
83
+ Spec : v1beta1.NodePoolSpec {
84
+ Weight : lo.ToPtr [int32 ](10 ),
85
+ },
86
+ })
87
+ nodePools = append (nodePools , * np )
88
+ }
89
+
90
+ nodePools = lo .Shuffle (nodePools )
91
+ nodePoolList := v1beta1.NodePoolList {Items : nodePools }
92
+ nodePoolList .OrderByWeight ()
93
+
94
+ lastName := "zzzzzzzzzzzzzzzzzzzzzzzz" // large string value
95
+ for _ , np := range nodePoolList .Items {
96
+ Expect (np .Name < lastName ).To (BeTrue ())
97
+ lastName = np .Name
98
+ }
99
+ })
100
+ })
0 commit comments