Skip to content

Commit 9fe6aad

Browse files
authored
test(nodeaffinitywithcache): migrate Ginkgo/Gomega tests to standard testing with testify (#5756)
* test(nodeaffinitywithcache): migrate Ginkgo/Gomega tests to standard testing with testify Signed-off-by: Harsh <harshmastic@gmail.com> * refactor(nodeaffinitywithcache): promote test scheme init to package-level var Replace per-test newTestScheme(t) function with a package-level testScheme var initialized once via an immediately-invoked function. This avoids redundant scheme registration on every test invocation. Addresses Gemini review feedback on PR #5756. Signed-off-by: Harsh <harshmastic@gmail.com> * fix(nodeaffinitywithcache): address SonarCloud issues - extract duplicate literals and fix function naming Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent 1c1d870 commit 9fe6aad

11 files changed

Lines changed: 4252 additions & 364 deletions

File tree

pkg/webhook/plugins/nodeaffinitywithcache/node_affinity_with_cache_test.go

Lines changed: 316 additions & 306 deletions
Large diffs are not rendered by default.

pkg/webhook/plugins/nodeaffinitywithcache/nodeaffinitywithcache_suite_test.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

pkg/webhook/plugins/nodeaffinitywithcache/tiered_locaity_test.go

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,41 @@ limitations under the License.
1717
package nodeaffinitywithcache
1818

1919
import (
20-
"github.com/onsi/ginkgo/v2"
21-
"github.com/onsi/gomega"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
2223
corev1 "k8s.io/api/core/v1"
2324
)
2425

25-
var _ = ginkgo.Describe("TieredLocality.hasRepeatedLocality", func() {
26-
var tieredLocality *TieredLocality
27-
28-
ginkgo.BeforeEach(func() {
29-
tieredLocality = &TieredLocality{
30-
Preferred: []Preferred{
31-
{
32-
Name: "label.a",
33-
Weight: 1,
34-
},
35-
{
36-
Name: "label.b",
37-
Weight: 2,
38-
},
39-
},
40-
Required: []string{"label.a"},
41-
}
42-
})
26+
const (
27+
testLabelA = "label.a"
28+
testLabelB = "label.b"
29+
)
4330

44-
ginkgo.DescribeTable("hasRepeatedLocality cases",
45-
func(pod *corev1.Pod, want bool) {
46-
got := tieredLocality.hasRepeatedLocality(pod)
47-
gomega.Expect(got).To(gomega.Equal(want))
31+
func TestHasRepeatedLocality(t *testing.T) {
32+
tieredLocality := &TieredLocality{
33+
Preferred: []Preferred{
34+
{Name: testLabelA, Weight: 1},
35+
{Name: testLabelB, Weight: 2},
4836
},
49-
ginkgo.Entry("empty affinity and selector",
50-
&corev1.Pod{
37+
Required: []string{testLabelA},
38+
}
39+
40+
tests := []struct {
41+
name string
42+
pod *corev1.Pod
43+
want bool
44+
}{
45+
{
46+
name: "empty affinity and selector",
47+
pod: &corev1.Pod{
5148
Spec: corev1.PodSpec{},
5249
},
53-
false,
54-
),
55-
ginkgo.Entry("affinity and empty selector, has same label",
56-
&corev1.Pod{
50+
want: false,
51+
},
52+
{
53+
name: "affinity and empty selector, has same label",
54+
pod: &corev1.Pod{
5755
Spec: corev1.PodSpec{
5856
Affinity: &corev1.Affinity{
5957
NodeAffinity: &corev1.NodeAffinity{
@@ -62,7 +60,7 @@ var _ = ginkgo.Describe("TieredLocality.hasRepeatedLocality", func() {
6260
{
6361
MatchExpressions: []corev1.NodeSelectorRequirement{
6462
{
65-
Key: "label.b",
63+
Key: testLabelB,
6664
Operator: corev1.NodeSelectorOpIn,
6765
Values: []string{"b.value"},
6866
},
@@ -76,7 +74,7 @@ var _ = ginkgo.Describe("TieredLocality.hasRepeatedLocality", func() {
7674
Preference: corev1.NodeSelectorTerm{
7775
MatchExpressions: []corev1.NodeSelectorRequirement{
7876
{
79-
Key: "label.b",
77+
Key: testLabelB,
8078
Operator: corev1.NodeSelectorOpIn,
8179
Values: []string{"b.value"},
8280
},
@@ -88,27 +86,36 @@ var _ = ginkgo.Describe("TieredLocality.hasRepeatedLocality", func() {
8886
},
8987
},
9088
},
91-
true,
92-
),
93-
ginkgo.Entry("node selector with same label",
94-
&corev1.Pod{
89+
want: true,
90+
},
91+
{
92+
name: "node selector with same label",
93+
pod: &corev1.Pod{
9594
Spec: corev1.PodSpec{
9695
NodeSelector: map[string]string{
97-
"label.a": "a-value",
96+
testLabelA: "a-value",
9897
},
9998
},
10099
},
101-
true,
102-
),
103-
ginkgo.Entry("node selector without same label",
104-
&corev1.Pod{
100+
want: true,
101+
},
102+
{
103+
name: "node selector without same label",
104+
pod: &corev1.Pod{
105105
Spec: corev1.PodSpec{
106106
NodeSelector: map[string]string{
107107
"label.c": "a-value",
108108
},
109109
},
110110
},
111-
false,
112-
),
113-
)
114-
})
111+
want: false,
112+
},
113+
}
114+
115+
for _, tc := range tests {
116+
t.Run(tc.name, func(t *testing.T) {
117+
got := tieredLocality.hasRepeatedLocality(tc.pod)
118+
assert.Equal(t, tc.want, got)
119+
})
120+
}
121+
}

vendor/github.com/stretchr/testify/require/doc.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/stretchr/testify/require/forward_requirements.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)