Skip to content

Commit dc48d57

Browse files
committed
test(zrpc/resolver): update EventHandler unit tests to use discovery/v1 EndpointSlice
* Replace legacy core/v1 Endpoints fixtures with discovery/v1 EndpointSlice * Adapt test data: switch Subsets/Addresses to Endpoints[].Addresses[] * Preserve existing test scenarios for add, delete, and update event flows Signed-off-by: soasurs <soasurs@gmail.com>
1 parent d1affa7 commit dc48d57

File tree

1 file changed

+108
-154
lines changed

1 file changed

+108
-154
lines changed

zrpc/resolver/internal/kube/eventhandler_test.go

Lines changed: 108 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7-
v1 "k8s.io/api/core/v1"
7+
discoveryv1 "k8s.io/api/discovery/v1"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
)
1010

@@ -14,21 +14,19 @@ func TestAdd(t *testing.T) {
1414
endpoints = change
1515
})
1616
h.OnAdd("bad", false)
17-
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
18-
{
19-
Addresses: []v1.EndpointAddress{
20-
{
21-
IP: "0.0.0.1",
22-
},
23-
{
24-
IP: "0.0.0.2",
25-
},
26-
{
27-
IP: "0.0.0.3",
28-
},
17+
h.OnAdd(&discoveryv1.EndpointSlice{
18+
Endpoints: []discoveryv1.Endpoint{
19+
{
20+
Addresses: []string{"0.0.0.1"},
21+
},
22+
{
23+
Addresses: []string{"0.0.0.2"},
24+
},
25+
{
26+
Addresses: []string{"0.0.0.3"},
2927
},
3028
},
31-
}}, false)
29+
}, false)
3230
assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2", "0.0.0.3"}, endpoints)
3331
}
3432

@@ -37,34 +35,30 @@ func TestDelete(t *testing.T) {
3735
h := NewEventHandler(func(change []string) {
3836
endpoints = change
3937
})
40-
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
41-
{
42-
Addresses: []v1.EndpointAddress{
43-
{
44-
IP: "0.0.0.1",
45-
},
46-
{
47-
IP: "0.0.0.2",
48-
},
49-
{
50-
IP: "0.0.0.3",
51-
},
38+
h.OnAdd(&discoveryv1.EndpointSlice{
39+
Endpoints: []discoveryv1.Endpoint{
40+
{
41+
Addresses: []string{"0.0.0.1"},
42+
},
43+
{
44+
Addresses: []string{"0.0.0.2"},
45+
},
46+
{
47+
Addresses: []string{"0.0.0.3"},
5248
},
5349
},
54-
}}, false)
50+
}, false)
5551
h.OnDelete("bad")
56-
h.OnDelete(&v1.Endpoints{Subsets: []v1.EndpointSubset{
57-
{
58-
Addresses: []v1.EndpointAddress{
59-
{
60-
IP: "0.0.0.1",
61-
},
62-
{
63-
IP: "0.0.0.2",
64-
},
52+
h.OnDelete(&discoveryv1.EndpointSlice{
53+
Endpoints: []discoveryv1.Endpoint{
54+
{
55+
Addresses: []string{"0.0.0.1"},
56+
},
57+
{
58+
Addresses: []string{"0.0.0.2"},
6559
},
6660
},
67-
}})
61+
})
6862
assert.ElementsMatch(t, []string{"0.0.0.3"}, endpoints)
6963
}
7064

@@ -73,36 +67,28 @@ func TestUpdate(t *testing.T) {
7367
h := NewEventHandler(func(change []string) {
7468
endpoints = change
7569
})
76-
h.OnUpdate(&v1.Endpoints{
77-
Subsets: []v1.EndpointSubset{
70+
h.OnUpdate(&discoveryv1.EndpointSlice{
71+
Endpoints: []discoveryv1.Endpoint{
72+
{
73+
Addresses: []string{"0.0.0.1"},
74+
},
7875
{
79-
Addresses: []v1.EndpointAddress{
80-
{
81-
IP: "0.0.0.1",
82-
},
83-
{
84-
IP: "0.0.0.2",
85-
},
86-
},
76+
Addresses: []string{"0.0.0.2"},
8777
},
8878
},
8979
ObjectMeta: metav1.ObjectMeta{
9080
ResourceVersion: "1",
9181
},
92-
}, &v1.Endpoints{
93-
Subsets: []v1.EndpointSubset{
82+
}, &discoveryv1.EndpointSlice{
83+
Endpoints: []discoveryv1.Endpoint{
9484
{
95-
Addresses: []v1.EndpointAddress{
96-
{
97-
IP: "0.0.0.1",
98-
},
99-
{
100-
IP: "0.0.0.2",
101-
},
102-
{
103-
IP: "0.0.0.3",
104-
},
105-
},
85+
Addresses: []string{"0.0.0.1"},
86+
},
87+
{
88+
Addresses: []string{"0.0.0.2"},
89+
},
90+
{
91+
Addresses: []string{"0.0.0.3"},
10692
},
10793
},
10894
ObjectMeta: metav1.ObjectMeta{
@@ -116,33 +102,25 @@ func TestUpdateNoChange(t *testing.T) {
116102
h := NewEventHandler(func(change []string) {
117103
assert.Fail(t, "should not called")
118104
})
119-
h.OnUpdate(&v1.Endpoints{
120-
Subsets: []v1.EndpointSubset{
105+
h.OnUpdate(&discoveryv1.EndpointSlice{
106+
Endpoints: []discoveryv1.Endpoint{
121107
{
122-
Addresses: []v1.EndpointAddress{
123-
{
124-
IP: "0.0.0.1",
125-
},
126-
{
127-
IP: "0.0.0.2",
128-
},
129-
},
108+
Addresses: []string{"0.0.0.1"},
109+
},
110+
{
111+
Addresses: []string{"0.0.0.2"},
130112
},
131113
},
132114
ObjectMeta: metav1.ObjectMeta{
133115
ResourceVersion: "1",
134116
},
135-
}, &v1.Endpoints{
136-
Subsets: []v1.EndpointSubset{
117+
}, &discoveryv1.EndpointSlice{
118+
Endpoints: []discoveryv1.Endpoint{
119+
{
120+
Addresses: []string{"0.0.0.1"},
121+
},
137122
{
138-
Addresses: []v1.EndpointAddress{
139-
{
140-
IP: "0.0.0.1",
141-
},
142-
{
143-
IP: "0.0.0.2",
144-
},
145-
},
123+
Addresses: []string{"0.0.0.2"},
146124
},
147125
},
148126
ObjectMeta: metav1.ObjectMeta{
@@ -156,45 +134,35 @@ func TestUpdateChangeWithDifferentVersion(t *testing.T) {
156134
h := NewEventHandler(func(change []string) {
157135
endpoints = change
158136
})
159-
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
160-
{
161-
Addresses: []v1.EndpointAddress{
162-
{
163-
IP: "0.0.0.1",
164-
},
165-
{
166-
IP: "0.0.0.3",
167-
},
137+
h.OnAdd(&discoveryv1.EndpointSlice{
138+
Endpoints: []discoveryv1.Endpoint{
139+
{
140+
Addresses: []string{"0.0.0.1"},
141+
},
142+
{
143+
Addresses: []string{"0.0.0.3"},
168144
},
169145
},
170-
}}, false)
171-
h.OnUpdate(&v1.Endpoints{
172-
Subsets: []v1.EndpointSubset{
146+
}, false)
147+
h.OnUpdate(&discoveryv1.EndpointSlice{
148+
Endpoints: []discoveryv1.Endpoint{
149+
{
150+
Addresses: []string{"0.0.0.1"},
151+
},
173152
{
174-
Addresses: []v1.EndpointAddress{
175-
{
176-
IP: "0.0.0.1",
177-
},
178-
{
179-
IP: "0.0.0.3",
180-
},
181-
},
153+
Addresses: []string{"0.0.0.3"},
182154
},
183155
},
184156
ObjectMeta: metav1.ObjectMeta{
185157
ResourceVersion: "1",
186158
},
187-
}, &v1.Endpoints{
188-
Subsets: []v1.EndpointSubset{
159+
}, &discoveryv1.EndpointSlice{
160+
Endpoints: []discoveryv1.Endpoint{
161+
{
162+
Addresses: []string{"0.0.0.1"},
163+
},
189164
{
190-
Addresses: []v1.EndpointAddress{
191-
{
192-
IP: "0.0.0.1",
193-
},
194-
{
195-
IP: "0.0.0.2",
196-
},
197-
},
165+
Addresses: []string{"0.0.0.2"},
198166
},
199167
},
200168
ObjectMeta: metav1.ObjectMeta{
@@ -209,63 +177,49 @@ func TestUpdateNoChangeWithDifferentVersion(t *testing.T) {
209177
h := NewEventHandler(func(change []string) {
210178
endpoints = change
211179
})
212-
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
213-
{
214-
Addresses: []v1.EndpointAddress{
215-
{
216-
IP: "0.0.0.1",
217-
},
218-
{
219-
IP: "0.0.0.2",
220-
},
180+
h.OnAdd(&discoveryv1.EndpointSlice{
181+
Endpoints: []discoveryv1.Endpoint{
182+
{
183+
Addresses: []string{"0.0.0.1"},
184+
},
185+
{
186+
Addresses: []string{"0.0.0.2"},
221187
},
222188
},
223-
}}, false)
224-
h.OnUpdate("bad", &v1.Endpoints{Subsets: []v1.EndpointSubset{
225-
{
226-
Addresses: []v1.EndpointAddress{
227-
{
228-
IP: "0.0.0.1",
229-
},
189+
}, false)
190+
h.OnUpdate("bad", &discoveryv1.EndpointSlice{
191+
Endpoints: []discoveryv1.Endpoint{
192+
{
193+
Addresses: []string{"0.0.0.1"},
230194
},
231195
},
232-
}})
233-
h.OnUpdate(&v1.Endpoints{Subsets: []v1.EndpointSubset{
234-
{
235-
Addresses: []v1.EndpointAddress{
236-
{
237-
IP: "0.0.0.1",
238-
},
196+
})
197+
h.OnUpdate(&discoveryv1.EndpointSlice{
198+
Endpoints: []discoveryv1.Endpoint{
199+
{
200+
Addresses: []string{"0.0.0.1"},
239201
},
240202
},
241-
}}, "bad")
242-
h.OnUpdate(&v1.Endpoints{
243-
Subsets: []v1.EndpointSubset{
203+
}, "bad")
204+
h.OnUpdate(&discoveryv1.EndpointSlice{
205+
Endpoints: []discoveryv1.Endpoint{
206+
{
207+
Addresses: []string{"0.0.0.1"},
208+
},
244209
{
245-
Addresses: []v1.EndpointAddress{
246-
{
247-
IP: "0.0.0.1",
248-
},
249-
{
250-
IP: "0.0.0.2",
251-
},
252-
},
210+
Addresses: []string{"0.0.0.2"},
253211
},
254212
},
255213
ObjectMeta: metav1.ObjectMeta{
256214
ResourceVersion: "1",
257215
},
258-
}, &v1.Endpoints{
259-
Subsets: []v1.EndpointSubset{
216+
}, &discoveryv1.EndpointSlice{
217+
Endpoints: []discoveryv1.Endpoint{
218+
{
219+
Addresses: []string{"0.0.0.1"},
220+
},
260221
{
261-
Addresses: []v1.EndpointAddress{
262-
{
263-
IP: "0.0.0.1",
264-
},
265-
{
266-
IP: "0.0.0.2",
267-
},
268-
},
222+
Addresses: []string{"0.0.0.2"},
269223
},
270224
},
271225
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)