Skip to content

Commit 4c08717

Browse files
authored
Merge pull request kubernetes#88143 from Huang-Wei/GetPodsInNamespace-bug
Fix a bug in e2epod function
2 parents 12eeb34 + c83a860 commit 4c08717

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

test/e2e/framework/pod/BUILD

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
@@ -47,3 +47,15 @@ filegroup(
4747
tags = ["automanaged"],
4848
visibility = ["//visibility:public"],
4949
)
50+
51+
go_test(
52+
name = "go_default_test",
53+
srcs = ["resource_test.go"],
54+
embed = [":go_default_library"],
55+
deps = [
56+
"//staging/src/k8s.io/api/core/v1:go_default_library",
57+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
58+
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
59+
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
60+
],
61+
)

test/e2e/framework/pod/resource.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,9 @@ func GetPodsInNamespace(c clientset.Interface, ns string, ignoreLabels map[strin
533533
return []*v1.Pod{}, err
534534
}
535535
ignoreSelector := labels.SelectorFromSet(ignoreLabels)
536-
filtered := []*v1.Pod{}
537-
for _, p := range pods.Items {
536+
var filtered []*v1.Pod
537+
for i := range pods.Items {
538+
p := pods.Items[i]
538539
if len(ignoreLabels) != 0 && ignoreSelector.Matches(labels.Set(p.Labels)) {
539540
continue
540541
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pod
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
v1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
fakeclient "k8s.io/client-go/kubernetes/fake"
27+
)
28+
29+
func TestGetPodsInNamespace(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
pods []runtime.Object
33+
wantPods []*v1.Pod
34+
expectErr bool
35+
}{
36+
{
37+
name: "nil check",
38+
},
39+
{
40+
name: "2 pods",
41+
pods: []runtime.Object{
42+
&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
43+
&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
44+
},
45+
wantPods: []*v1.Pod{
46+
{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
47+
{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
48+
},
49+
},
50+
}
51+
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
cs := fakeclient.NewSimpleClientset(tt.pods...)
55+
got, err := GetPodsInNamespace(cs, "", map[string]string{})
56+
if (err != nil) != tt.expectErr {
57+
t.Errorf("expectErr = %v, but got err = %v", tt.expectErr, err)
58+
}
59+
if !reflect.DeepEqual(got, tt.wantPods) {
60+
t.Errorf("expect %v, got %v", tt.wantPods, got)
61+
}
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)