|
| 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