k8s.io/kubernetes@v1.29.3/test/e2e/framework/pod/resource_test.go (about)

     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  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	fakeclient "k8s.io/client-go/kubernetes/fake"
    28  )
    29  
    30  func TestGetPodsInNamespace(t *testing.T) {
    31  	tests := []struct {
    32  		name      string
    33  		pods      []runtime.Object
    34  		wantPods  []*v1.Pod
    35  		expectErr bool
    36  	}{
    37  		{
    38  			name: "nil check",
    39  		},
    40  		{
    41  			name: "2 pods",
    42  			pods: []runtime.Object{
    43  				&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
    44  				&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
    45  			},
    46  			wantPods: []*v1.Pod{
    47  				{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
    48  				{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
    49  			},
    50  		},
    51  	}
    52  
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			cs := fakeclient.NewSimpleClientset(tt.pods...)
    56  			got, err := GetPodsInNamespace(context.Background(), cs, "", map[string]string{})
    57  			if (err != nil) != tt.expectErr {
    58  				t.Errorf("expectErr = %v, but got err = %v", tt.expectErr, err)
    59  			}
    60  			if !reflect.DeepEqual(got, tt.wantPods) {
    61  				t.Errorf("expect %v, got %v", tt.wantPods, got)
    62  			}
    63  		})
    64  	}
    65  }