Skip to content

Commit bc4ae15

Browse files
committed
fix wrong assertion on tests
Signed-off-by: xin.li <[email protected]>
1 parent 0a08529 commit bc4ae15

File tree

17 files changed

+177
-61
lines changed

17 files changed

+177
-61
lines changed

pkg/client/tests/remotecommand_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import (
2929
"testing"
3030
"time"
3131

32-
"github.com/stretchr/testify/require"
33-
3432
"k8s.io/apimachinery/pkg/runtime"
3533
"k8s.io/apimachinery/pkg/runtime/schema"
3634
"k8s.io/apimachinery/pkg/types"
@@ -124,7 +122,9 @@ func fakeServer(t *testing.T, requestReceived chan struct{}, testName string, ex
124122
}
125123

126124
opts, err := remotecommand.NewOptions(req)
127-
require.NoError(t, err)
125+
if err != nil {
126+
t.Errorf("unexpected error %v", err)
127+
}
128128
if exec {
129129
cmd := req.URL.Query()[api.ExecCommandParam]
130130
remotecommand.ServeExec(w, req, executor, "pod", "uid", "container", cmd, opts, 0, 10*time.Second, serverProtocols)

pkg/kubelet/logs/container_log_manager_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ func TestRotateLogs(t *testing.T) {
172172
err = wait.PollUntilContextCancel(pollTimeoutCtx, 5*time.Millisecond, false, func(ctx context.Context) (done bool, err error) {
173173
return c.queue.Len() == 0, nil
174174
})
175-
require.NoError(t, err)
175+
if err != nil {
176+
t.Errorf("unexpected error %v", err)
177+
}
176178
c.queue.ShutDown()
177179
}()
178180
// This is a blocking call. But the above routine takes care of ensuring that this is terminated once the queue is shutdown

staging/src/k8s.io/apiserver/pkg/admission/audit_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,13 @@ func TestWithAuditConcurrency(t *testing.T) {
200200
go func() {
201201
defer wg.Done()
202202
mutator, ok := handler.(MutationInterface)
203-
require.True(t, ok)
203+
if !ok {
204+
t.Error("handler is not an interface of type MutationInterface")
205+
}
204206
auditMutator, ok := auditHandler.(MutationInterface)
205-
require.True(t, ok)
207+
if !ok {
208+
t.Error("handler is not an interface of type MutationInterface")
209+
}
206210
assert.Equal(t, mutator.Admit(ctx, a, nil), auditMutator.Admit(ctx, a, nil), "WithAudit decorator should not effect the return value")
207211
}()
208212
}

staging/src/k8s.io/apiserver/pkg/admission/plugin/policy/internal/generic/controller_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ func TestReconcile(t *testing.T) {
199199
go func() {
200200
defer wg.Done()
201201
stopReason := myController.Run(testContext)
202-
require.ErrorIs(t, stopReason, context.Canceled)
202+
if !errors.Is(stopReason, context.Canceled) {
203+
t.Errorf("expected error to be context.Canceled, but got: %v", stopReason)
204+
}
203205
}()
204206

205207
// The controller is blocked because the reconcile function sends on an
@@ -255,7 +257,9 @@ func TestShutdown(t *testing.T) {
255257
go func() {
256258
defer wg.Done()
257259
stopReason := myController.Run(testContext)
258-
require.ErrorIs(t, stopReason, context.Canceled)
260+
if !errors.Is(stopReason, context.Canceled) {
261+
t.Errorf("expected error to be context.Canceled, but got: %v", stopReason)
262+
}
259263
}()
260264

261265
// Wait for controller and informer to start up
@@ -287,7 +291,9 @@ func TestInformerNeverStarts(t *testing.T) {
287291
go func() {
288292
defer wg.Done()
289293
stopReason := myController.Run(testContext)
290-
require.ErrorIs(t, stopReason, context.DeadlineExceeded)
294+
if !errors.Is(stopReason, context.Canceled) {
295+
t.Errorf("expected error to be context.Canceled, but got: %v", stopReason)
296+
}
291297
}()
292298

293299
// Wait for deadline to pass without syncing the cache
@@ -335,7 +341,9 @@ func TestIgnoredUpdate(t *testing.T) {
335341
go func() {
336342
defer wg.Done()
337343
stopReason := myController.Run(testContext)
338-
require.ErrorIs(t, stopReason, context.Canceled)
344+
if !errors.Is(stopReason, context.Canceled) {
345+
t.Errorf("expected error to be context.Canceled, but got: %v", stopReason)
346+
}
339347
}()
340348

341349
// The controller is blocked because the reconcile function sends on an
@@ -392,7 +400,9 @@ func TestReconcileRetry(t *testing.T) {
392400
go func() {
393401
defer wg.Done()
394402
stopReason := myController.Run(testContext)
395-
require.ErrorIs(t, stopReason, context.Canceled)
403+
if !errors.Is(stopReason, context.Canceled) {
404+
t.Errorf("expected error to be context.Canceled, but got: %v", stopReason)
405+
}
396406
}()
397407

398408
// Add object to informer

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,9 @@ func TestWaitUntilWatchCacheFreshAndForceAllEvents(t *testing.T) {
21972197

21982198
go func(t *testing.T) {
21992199
err := cacher.watchCache.Add(makeTestPodDetails("pod1", 105, "node1", map[string]string{"label": "value1"}))
2200-
require.NoError(t, err, "failed adding a pod to the watchCache")
2200+
if err != nil {
2201+
t.Errorf("failed adding a pod to the watchCache %v", err)
2202+
}
22012203
}(t)
22022204
w, err = cacher.Watch(context.Background(), "pods/ns", scenario.opts)
22032205
require.NoError(t, err, "failed to create watch: %v")

staging/src/k8s.io/apiserver/pkg/util/proxy/streamtunnel_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ func TestTunnelingHandler_UpgradeStreamingAndTunneling(t *testing.T) {
5757
defer close(stopServerChan)
5858
spdyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
5959
_, err := httpstream.Handshake(req, w, []string{constants.PortForwardV1Name})
60-
require.NoError(t, err)
60+
if err != nil {
61+
t.Errorf("unexpected error %v", err)
62+
}
6163
upgrader := spdy.NewResponseUpgrader()
6264
conn := upgrader.UpgradeResponse(w, req, justQueueStream(streamChan))
63-
require.NotNil(t, conn)
65+
if conn == nil {
66+
t.Error("connect is unexpected nil")
67+
}
6468
defer conn.Close() //nolint:errcheck
6569
<-stopServerChan
6670
}))
@@ -97,9 +101,13 @@ func TestTunnelingHandler_UpgradeStreamingAndTunneling(t *testing.T) {
97101
var actual []byte
98102
go func() {
99103
clientStream, err := spdyClient.CreateStream(http.Header{})
100-
require.NoError(t, err)
104+
if err != nil {
105+
t.Errorf("unexpected error %v", err)
106+
}
101107
_, err = io.Copy(clientStream, bytes.NewReader(randomData))
102-
require.NoError(t, err)
108+
if err != nil {
109+
t.Errorf("unexpected error %v", err)
110+
}
103111
clientStream.Close() //nolint:errcheck
104112
}()
105113
select {
@@ -169,7 +177,9 @@ func TestTunnelingHandler_BadHandshakeError(t *testing.T) {
169177
spdyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
170178
// Handshake fails.
171179
_, err := httpstream.Handshake(req, w, []string{constants.PortForwardV1Name})
172-
require.Error(t, err, "handshake should have returned an error")
180+
if err == nil {
181+
t.Errorf("handshake should have returned an error %v", err)
182+
}
173183
assert.ErrorContains(t, err, "unable to negotiate protocol")
174184
w.WriteHeader(http.StatusForbidden)
175185
}))
@@ -223,7 +233,9 @@ func TestTunnelingHandler_UpstreamSPDYServerErrorPropagated(t *testing.T) {
223233
// Create fake upstream SPDY server, which returns a 500-level error.
224234
spdyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
225235
_, err := httpstream.Handshake(req, w, []string{constants.PortForwardV1Name})
226-
require.NoError(t, err, "handshake should have succeeded")
236+
if err != nil {
237+
t.Errorf("handshake should have succeeded %v", err)
238+
}
227239
// Returned status code should be incremented in metrics.
228240
w.WriteHeader(statusCode)
229241
}))

staging/src/k8s.io/client-go/discovery/cached/disk/cached_discovery_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ func TestCachedDiscoveryClientUnaggregatedServerGroups(t *testing.T) {
350350
return
351351
}
352352
output, err := json.Marshal(body)
353-
require.NoError(t, err)
353+
if err != nil {
354+
t.Errorf("unexpected error %v", err)
355+
}
354356
// Content-type is "unaggregated" discovery format -- no resources returned.
355357
w.Header().Set("Content-Type", discovery.AcceptV1)
356358
w.WriteHeader(http.StatusOK)

staging/src/k8s.io/client-go/discovery/cached/memory/memcache_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,9 @@ func TestMemCacheGroupsAndMaybeResources(t *testing.T) {
587587
return
588588
}
589589
output, err := json.Marshal(body)
590-
require.NoError(t, err)
590+
if err != nil {
591+
t.Errorf("unexpected error %v", err)
592+
}
591593
// Content-type is "unaggregated" discovery format -- no resources returned.
592594
w.Header().Set("Content-Type", discovery.AcceptV1)
593595
w.WriteHeader(http.StatusOK)
@@ -1116,7 +1118,9 @@ func TestAggregatedMemCacheGroupsAndMaybeResources(t *testing.T) {
11161118
return
11171119
}
11181120
output, err := json.Marshal(agg)
1119-
require.NoError(t, err)
1121+
if err != nil {
1122+
t.Errorf("unexpected error %v", err)
1123+
}
11201124
// Content-type is "aggregated" discovery format.
11211125
w.Header().Set("Content-Type", discovery.AcceptV2)
11221126
w.WriteHeader(http.StatusOK)
@@ -1414,7 +1418,9 @@ func TestMemCacheAggregatedServerGroups(t *testing.T) {
14141418
return
14151419
}
14161420
output, err := json.Marshal(agg)
1417-
require.NoError(t, err)
1421+
if err != nil {
1422+
t.Errorf("unexpected error %v", err)
1423+
}
14181424
// Content-type is "aggregated" discovery format.
14191425
w.Header().Set("Content-Type", discovery.AcceptV2Beta1)
14201426
w.WriteHeader(http.StatusOK)

staging/src/k8s.io/client-go/discovery/discovery_client_test.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func TestGetServerVersion(t *testing.T) {
6060
w.Header().Set("Content-Type", "application/json")
6161
w.WriteHeader(http.StatusOK)
6262
_, err = w.Write(output)
63-
require.NoError(t, err)
63+
if err != nil {
64+
t.Errorf("unexpected error %v", err)
65+
}
6466
}))
6567
defer server.Close()
6668
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
@@ -107,7 +109,9 @@ func TestGetServerGroupsWithV1Server(t *testing.T) {
107109
w.Header().Set("Content-Type", "application/json")
108110
w.WriteHeader(http.StatusOK)
109111
_, err = w.Write(output)
110-
require.NoError(t, err)
112+
if err != nil {
113+
t.Errorf("unexpected error %v", err)
114+
}
111115
}))
112116
defer server.Close()
113117
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
@@ -148,7 +152,9 @@ func TestDiscoveryToleratesMissingCoreGroup(t *testing.T) {
148152
w.Header().Set("Content-Type", "application/json")
149153
w.WriteHeader(http.StatusOK)
150154
_, err = w.Write(output)
151-
require.NoError(t, err)
155+
if err != nil {
156+
t.Errorf("unexpected error %v", err)
157+
}
152158
}))
153159
defer server.Close()
154160
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
@@ -185,7 +191,9 @@ func TestDiscoveryFailsWhenNonCoreGroupsMissing(t *testing.T) {
185191
w.Header().Set("Content-Type", "application/json")
186192
w.WriteHeader(http.StatusOK)
187193
_, err = w.Write(output)
188-
require.NoError(t, err)
194+
if err != nil {
195+
t.Errorf("unexpected error %v", err)
196+
}
189197
}))
190198
defer server.Close()
191199
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
@@ -386,7 +394,9 @@ func TestGetServerResourcesForGroupVersion(t *testing.T) {
386394
w.Header().Set("Content-Type", "application/json")
387395
w.WriteHeader(http.StatusOK)
388396
_, err = w.Write(output)
389-
require.NoError(t, err)
397+
if err != nil {
398+
t.Errorf("unexpected error %v", err)
399+
}
390400
}))
391401
defer server.Close()
392402
for _, test := range tests {
@@ -1313,13 +1323,17 @@ func TestAggregatedServerGroups(t *testing.T) {
13131323
return
13141324
}
13151325
output, err = json.Marshal(agg)
1316-
require.NoError(t, err)
1326+
if err != nil {
1327+
t.Errorf("unexpected error %v", err)
1328+
}
13171329
// Content-Type is "aggregated" discovery format. Add extra parameter
13181330
// to ensure we are resilient to these extra parameters.
13191331
w.Header().Set("Content-Type", AcceptV2+"; charset=utf-8")
13201332
w.WriteHeader(http.StatusOK)
13211333
_, err = w.Write(output)
1322-
require.NoError(t, err)
1334+
if err != nil {
1335+
t.Errorf("unexpected error %v", err)
1336+
}
13231337
}))
13241338
defer server.Close()
13251339
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
@@ -2383,7 +2397,9 @@ func TestAggregatedServerGroupsAndResources(t *testing.T) {
23832397
return
23842398
}
23852399
output, err = json.Marshal(agg)
2386-
require.NoError(t, err)
2400+
if err != nil {
2401+
t.Errorf("unexpected error %v", err)
2402+
}
23872403
} else {
23882404
var agg *apidiscoveryv2beta1.APIGroupDiscoveryList
23892405
switch req.URL.Path {
@@ -2396,14 +2412,18 @@ func TestAggregatedServerGroupsAndResources(t *testing.T) {
23962412
return
23972413
}
23982414
output, err = json.Marshal(&agg)
2399-
require.NoError(t, err)
2415+
if err != nil {
2416+
t.Errorf("unexpected error %v", err)
2417+
}
24002418
}
24012419
// Content-Type is "aggregated" discovery format. Add extra parameter
24022420
// to ensure we are resilient to these extra parameters.
24032421
w.Header().Set("Content-Type", accept+"; charset=utf-8")
24042422
w.WriteHeader(http.StatusOK)
24052423
_, err = w.Write(output)
2406-
require.NoError(t, err)
2424+
if err != nil {
2425+
t.Errorf("unexpected error %v", err)
2426+
}
24072427

24082428
}))
24092429
defer server.Close()
@@ -2543,13 +2563,17 @@ func TestAggregatedServerGroupsAndResourcesWithErrors(t *testing.T) {
25432563
return
25442564
}
25452565
output, err = json.Marshal(agg)
2546-
require.NoError(t, err)
2566+
if err != nil {
2567+
t.Errorf("unexpected error %v", err)
2568+
}
25472569
// Content-Type is "aggregated" discovery format. Add extra parameter
25482570
// to ensure we are resilient to these extra parameters.
25492571
w.Header().Set("Content-Type", AcceptV2+"; charset=utf-8")
25502572
w.WriteHeader(status)
25512573
_, err = w.Write(output)
2552-
require.NoError(t, err)
2574+
if err != nil {
2575+
t.Errorf("unexpected error %v", err)
2576+
}
25532577
}))
25542578
defer server.Close()
25552579

@@ -3156,13 +3180,17 @@ func TestAggregatedServerPreferredResources(t *testing.T) {
31563180
return
31573181
}
31583182
output, err = json.Marshal(agg)
3159-
require.NoError(t, err)
3183+
if err != nil {
3184+
t.Errorf("unexpected error %v", err)
3185+
}
31603186
// Content-Type is "aggregated" discovery format. Add extra parameter
31613187
// to ensure we are resilient to these extra parameters.
31623188
w.Header().Set("Content-Type", AcceptV2+"; charset=utf-8")
31633189
w.WriteHeader(http.StatusOK)
31643190
_, err = w.Write(output)
3165-
require.NoError(t, err)
3191+
if err != nil {
3192+
t.Errorf("unexpected error %v", err)
3193+
}
31663194
}))
31673195
defer server.Close()
31683196
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})

0 commit comments

Comments
 (0)