Merge pull request #130693 from novahe/fix/test-cases

Fix test cases that may potentially cause a panic.

Kubernetes-commit: 336a32a270997731873b6d8b616f4ae91d903fcf
This commit is contained in:
Kubernetes Publisher
2025-03-13 05:33:49 -07:00
7 changed files with 27 additions and 6 deletions

View File

@@ -352,6 +352,7 @@ func TestCachedDiscoveryClientUnaggregatedServerGroups(t *testing.T) {
output, err := json.Marshal(body)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
// Content-type is "unaggregated" discovery format -- no resources returned.
w.Header().Set("Content-Type", discovery.AcceptV1)

View File

@@ -589,6 +589,7 @@ func TestMemCacheGroupsAndMaybeResources(t *testing.T) {
output, err := json.Marshal(body)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
// Content-type is "unaggregated" discovery format -- no resources returned.
w.Header().Set("Content-Type", discovery.AcceptV1)
@@ -1120,6 +1121,7 @@ func TestAggregatedMemCacheGroupsAndMaybeResources(t *testing.T) {
output, err := json.Marshal(agg)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
// Content-type is "aggregated" discovery format.
w.Header().Set("Content-Type", discovery.AcceptV2)
@@ -1420,6 +1422,7 @@ func TestMemCacheAggregatedServerGroups(t *testing.T) {
output, err := json.Marshal(agg)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
// Content-type is "aggregated" discovery format.
w.Header().Set("Content-Type", discovery.AcceptV2Beta1)

View File

@@ -1325,6 +1325,7 @@ func TestAggregatedServerGroups(t *testing.T) {
output, err = json.Marshal(agg)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
// Content-Type is "aggregated" discovery format. Add extra parameter
// to ensure we are resilient to these extra parameters.
@@ -1333,6 +1334,7 @@ func TestAggregatedServerGroups(t *testing.T) {
_, err = w.Write(output)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
}))
defer server.Close()
@@ -2399,6 +2401,7 @@ func TestAggregatedServerGroupsAndResources(t *testing.T) {
output, err = json.Marshal(agg)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
} else {
var agg *apidiscoveryv2beta1.APIGroupDiscoveryList
@@ -2414,6 +2417,7 @@ func TestAggregatedServerGroupsAndResources(t *testing.T) {
output, err = json.Marshal(&agg)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
}
// Content-Type is "aggregated" discovery format. Add extra parameter
@@ -2565,6 +2569,7 @@ func TestAggregatedServerGroupsAndResourcesWithErrors(t *testing.T) {
output, err = json.Marshal(agg)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
// Content-Type is "aggregated" discovery format. Add extra parameter
// to ensure we are resilient to these extra parameters.
@@ -3182,6 +3187,7 @@ func TestAggregatedServerPreferredResources(t *testing.T) {
output, err = json.Marshal(agg)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
// Content-Type is "aggregated" discovery format. Add extra parameter
// to ensure we are resilient to these extra parameters.

4
go.mod
View File

@@ -25,8 +25,8 @@ require (
golang.org/x/time v0.9.0
google.golang.org/protobuf v1.36.5
gopkg.in/evanphx/json-patch.v4 v4.12.0
k8s.io/api v0.0.0-20250312173022-9807c671aa78
k8s.io/apimachinery v0.0.0-20250312043753-2687636770f8
k8s.io/api v0.0.0-20250313053026-3552524c7c52
k8s.io/apimachinery v0.0.0-20250313012745-a04ff375cef8
k8s.io/klog/v2 v2.130.1
k8s.io/kube-openapi v0.0.0-20250304201544-e5f78fe3ede9
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738

8
go.sum
View File

@@ -146,10 +146,10 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.0.0-20250312173022-9807c671aa78 h1:cRabLSfBAorbZPNM3UIroZGQWopPIcS8iDWWIItZpGw=
k8s.io/api v0.0.0-20250312173022-9807c671aa78/go.mod h1:jdaCxHUNcIitYJayJjmk9QUVxGaPHpBmxonBTtuRnmA=
k8s.io/apimachinery v0.0.0-20250312043753-2687636770f8 h1:5Mh2OhmA4UBjm84f2wHYwwaAhA7Unw+PpIyiAJuqeUg=
k8s.io/apimachinery v0.0.0-20250312043753-2687636770f8/go.mod h1:S2OIkExGqJOXYSYcAJwQ9zWcc6BkBUdTJUu4M7z0cvo=
k8s.io/api v0.0.0-20250313053026-3552524c7c52 h1:Zc4+vYT1fBJYPzq4zgP360RcNux8J6IiOr4YOq/R6wI=
k8s.io/api v0.0.0-20250313053026-3552524c7c52/go.mod h1:nyT7xKOld0k8IraPSyWNF96YIMppoRBYxtAUIehRQW0=
k8s.io/apimachinery v0.0.0-20250313012745-a04ff375cef8 h1:aZ3lyC/cbDqhg3/RCSobSABNBeUzBWh6LrHeSnIVzVY=
k8s.io/apimachinery v0.0.0-20250313012745-a04ff375cef8/go.mod h1:S2OIkExGqJOXYSYcAJwQ9zWcc6BkBUdTJUu4M7z0cvo=
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20250304201544-e5f78fe3ede9 h1:t0huyHnz6HsokckRxAF1bY0cqPFwzINKCL7yltEjZQc=

View File

@@ -53,15 +53,18 @@ func TestTunnelingConnection_ReadWriteClose(t *testing.T) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
defer conn.Close() //nolint:errcheck
if conn.Subprotocol() != constants.WebsocketsSPDYTunnelingPortForwardV1 {
t.Errorf("Not acceptable agreement Subprotocol: %v", conn.Subprotocol())
return
}
tunnelingConn := NewTunnelingConnection("server", conn)
spdyConn, err := spdy.NewServerConnection(tunnelingConn, justQueueStream(streamChan))
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
defer spdyConn.Close() //nolint:errcheck
<-stopServerChan
@@ -85,10 +88,12 @@ func TestTunnelingConnection_ReadWriteClose(t *testing.T) {
clientStream, err := spdyClient.CreateStream(http.Header{})
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
_, err = io.Copy(clientStream, strings.NewReader(expected))
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
clientStream.Close() //nolint:errcheck
}()
@@ -114,6 +119,7 @@ func TestTunnelingConnection_LocalRemoteAddress(t *testing.T) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
defer conn.Close() //nolint:errcheck
if conn.Subprotocol() != constants.WebsocketsSPDYTunnelingPortForwardV1 {
@@ -150,10 +156,12 @@ func TestTunnelingConnection_ReadWriteDeadlines(t *testing.T) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
defer conn.Close() //nolint:errcheck
if conn.Subprotocol() != constants.WebsocketsSPDYTunnelingPortForwardV1 {
t.Errorf("Not acceptable agreement Subprotocol: %v", conn.Subprotocol())
return
}
<-stopServerChan
}))

View File

@@ -115,15 +115,18 @@ func TestWebSocketRoundTripper_RoundTripperFails(t *testing.T) {
statusBytes, err := runtime.Encode(encoder, testCase.status)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
_, err = w.Write(statusBytes)
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
} else if len(testCase.body) > 0 {
_, err := w.Write([]byte(testCase.body))
if err != nil {
t.Errorf("unexpected error %v", err)
return
}
}
}))