diff --git a/test/e2e/framework/pod/BUILD b/test/e2e/framework/pod/BUILD index 22c8d836dec..2847c017ed3 100644 --- a/test/e2e/framework/pod/BUILD +++ b/test/e2e/framework/pod/BUILD @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", @@ -47,3 +47,15 @@ filegroup( tags = ["automanaged"], visibility = ["//visibility:public"], ) + +go_test( + name = "go_default_test", + srcs = ["resource_test.go"], + embed = [":go_default_library"], + deps = [ + "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library", + ], +) diff --git a/test/e2e/framework/pod/resource.go b/test/e2e/framework/pod/resource.go index 7e16022d426..34bc6517257 100644 --- a/test/e2e/framework/pod/resource.go +++ b/test/e2e/framework/pod/resource.go @@ -533,8 +533,9 @@ func GetPodsInNamespace(c clientset.Interface, ns string, ignoreLabels map[strin return []*v1.Pod{}, err } ignoreSelector := labels.SelectorFromSet(ignoreLabels) - filtered := []*v1.Pod{} - for _, p := range pods.Items { + var filtered []*v1.Pod + for i := range pods.Items { + p := pods.Items[i] if len(ignoreLabels) != 0 && ignoreSelector.Matches(labels.Set(p.Labels)) { continue } diff --git a/test/e2e/framework/pod/resource_test.go b/test/e2e/framework/pod/resource_test.go new file mode 100644 index 00000000000..639b7c96d4e --- /dev/null +++ b/test/e2e/framework/pod/resource_test.go @@ -0,0 +1,64 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pod + +import ( + "reflect" + "testing" + + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + fakeclient "k8s.io/client-go/kubernetes/fake" +) + +func TestGetPodsInNamespace(t *testing.T) { + tests := []struct { + name string + pods []runtime.Object + wantPods []*v1.Pod + expectErr bool + }{ + { + name: "nil check", + }, + { + name: "2 pods", + pods: []runtime.Object{ + &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}}, + &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}}, + }, + wantPods: []*v1.Pod{ + {ObjectMeta: metav1.ObjectMeta{Name: "pod1"}}, + {ObjectMeta: metav1.ObjectMeta{Name: "pod2"}}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cs := fakeclient.NewSimpleClientset(tt.pods...) + got, err := GetPodsInNamespace(cs, "", map[string]string{}) + if (err != nil) != tt.expectErr { + t.Errorf("expectErr = %v, but got err = %v", tt.expectErr, err) + } + if !reflect.DeepEqual(got, tt.wantPods) { + t.Errorf("expect %v, got %v", tt.wantPods, got) + } + }) + } +}