Merge pull request #110360 from 21kyu/fix_defer_in_for_loop_listwatch_test

fix defer in for loop, maybe resource leak
This commit is contained in:
Kubernetes Prow Robot 2022-06-02 22:38:19 -07:00 committed by GitHub
commit ccfac6d320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -57,20 +57,21 @@ func buildLocation(resourcePath string, query url.Values) string {
func TestListWatchesCanList(t *testing.T) { func TestListWatchesCanList(t *testing.T) {
fieldSelectorQueryParamName := metav1.FieldSelectorQueryParam("v1") fieldSelectorQueryParamName := metav1.FieldSelectorQueryParam("v1")
table := []struct { table := []struct {
desc string
location string location string
resource string resource string
namespace string namespace string
fieldSelector fields.Selector fieldSelector fields.Selector
}{ }{
// Node
{ {
desc: "node",
location: "/api/v1/nodes", location: "/api/v1/nodes",
resource: "nodes", resource: "nodes",
namespace: metav1.NamespaceAll, namespace: metav1.NamespaceAll,
fieldSelector: parseSelectorOrDie(""), fieldSelector: parseSelectorOrDie(""),
}, },
// pod with "assigned" field selector.
{ {
desc: "pod with 'assigned' field selector",
location: buildLocation( location: buildLocation(
"/api/v1/pods", "/api/v1/pods",
buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})), buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})),
@ -78,8 +79,8 @@ func TestListWatchesCanList(t *testing.T) {
namespace: metav1.NamespaceAll, namespace: metav1.NamespaceAll,
fieldSelector: fields.Set{"spec.host": ""}.AsSelector(), fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
}, },
// pod in namespace "foo"
{ {
desc: "pod in namespace 'foo'",
location: buildLocation( location: buildLocation(
"/api/v1/namespaces/foo/pods", "/api/v1/namespaces/foo/pods",
buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})), buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})),
@ -89,6 +90,7 @@ func TestListWatchesCanList(t *testing.T) {
}, },
} }
for _, item := range table { for _, item := range table {
t.Run(item.desc, func(t *testing.T) {
handler := utiltesting.FakeHandler{ handler := utiltesting.FakeHandler{
StatusCode: 500, StatusCode: 500,
ResponseBody: "", ResponseBody: "",
@ -100,22 +102,24 @@ func TestListWatchesCanList(t *testing.T) {
lw := NewListWatchFromClient(client.CoreV1().RESTClient(), item.resource, item.namespace, item.fieldSelector) lw := NewListWatchFromClient(client.CoreV1().RESTClient(), item.resource, item.namespace, item.fieldSelector)
lw.DisableChunking = true lw.DisableChunking = true
// This test merely tests that the correct request is made. // This test merely tests that the correct request is made.
lw.List(metav1.ListOptions{}) _, _ = lw.List(metav1.ListOptions{})
handler.ValidateRequest(t, item.location, "GET", nil) handler.ValidateRequest(t, item.location, "GET", nil)
})
} }
} }
func TestListWatchesCanWatch(t *testing.T) { func TestListWatchesCanWatch(t *testing.T) {
fieldSelectorQueryParamName := metav1.FieldSelectorQueryParam("v1") fieldSelectorQueryParamName := metav1.FieldSelectorQueryParam("v1")
table := []struct { table := []struct {
desc string
rv string rv string
location string location string
resource string resource string
namespace string namespace string
fieldSelector fields.Selector fieldSelector fields.Selector
}{ }{
// Node
{ {
desc: "node without rv",
location: buildLocation( location: buildLocation(
"/api/v1/nodes", "/api/v1/nodes",
buildQueryValues(url.Values{"watch": []string{"true"}})), buildQueryValues(url.Values{"watch": []string{"true"}})),
@ -125,6 +129,7 @@ func TestListWatchesCanWatch(t *testing.T) {
fieldSelector: parseSelectorOrDie(""), fieldSelector: parseSelectorOrDie(""),
}, },
{ {
desc: "node with rv",
location: buildLocation( location: buildLocation(
"/api/v1/nodes", "/api/v1/nodes",
buildQueryValues(url.Values{"resourceVersion": []string{"42"}, "watch": []string{"true"}})), buildQueryValues(url.Values{"resourceVersion": []string{"42"}, "watch": []string{"true"}})),
@ -133,8 +138,8 @@ func TestListWatchesCanWatch(t *testing.T) {
namespace: metav1.NamespaceAll, namespace: metav1.NamespaceAll,
fieldSelector: parseSelectorOrDie(""), fieldSelector: parseSelectorOrDie(""),
}, },
// pod with "assigned" field selector.
{ {
desc: "pod with 'assigned' field selector",
location: buildLocation( location: buildLocation(
"/api/v1/pods", "/api/v1/pods",
buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}, "resourceVersion": []string{"0"}, "watch": []string{"true"}})), buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}, "resourceVersion": []string{"0"}, "watch": []string{"true"}})),
@ -143,8 +148,8 @@ func TestListWatchesCanWatch(t *testing.T) {
namespace: metav1.NamespaceAll, namespace: metav1.NamespaceAll,
fieldSelector: fields.Set{"spec.host": ""}.AsSelector(), fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
}, },
// pod with namespace foo and assigned field selector
{ {
desc: "pod with namespace foo and assigned field selector",
location: buildLocation( location: buildLocation(
"/api/v1/namespaces/foo/pods", "/api/v1/namespaces/foo/pods",
buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}, "resourceVersion": []string{"0"}, "watch": []string{"true"}})), buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}, "resourceVersion": []string{"0"}, "watch": []string{"true"}})),
@ -156,6 +161,7 @@ func TestListWatchesCanWatch(t *testing.T) {
} }
for _, item := range table { for _, item := range table {
t.Run(item.desc, func(t *testing.T) {
handler := utiltesting.FakeHandler{ handler := utiltesting.FakeHandler{
StatusCode: 500, StatusCode: 500,
ResponseBody: "", ResponseBody: "",
@ -166,7 +172,8 @@ func TestListWatchesCanWatch(t *testing.T) {
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}}) client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
lw := NewListWatchFromClient(client.CoreV1().RESTClient(), item.resource, item.namespace, item.fieldSelector) lw := NewListWatchFromClient(client.CoreV1().RESTClient(), item.resource, item.namespace, item.fieldSelector)
// This test merely tests that the correct request is made. // This test merely tests that the correct request is made.
lw.Watch(metav1.ListOptions{ResourceVersion: item.rv}) _, _ = lw.Watch(metav1.ListOptions{ResourceVersion: item.rv})
handler.ValidateRequest(t, item.location, "GET", nil) handler.ValidateRequest(t, item.location, "GET", nil)
})
} }
} }