github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/pod_test.go (about)

     1  /*
     2  Copyright 2021 The Skaffold 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 validator
    18  
    19  import (
    20  	"context"
    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  	fakekubeclientset "k8s.io/client-go/kubernetes/fake"
    27  
    28  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    29  )
    30  
    31  func TestStandalonePodsSelector(t *testing.T) {
    32  	tests := []struct {
    33  		description  string
    34  		allPods      []v1.Pod
    35  		expectedPods []v1.Pod
    36  	}{
    37  		{
    38  			description: "pod don't exist in test namespace",
    39  			allPods: []v1.Pod{{
    40  				ObjectMeta: metav1.ObjectMeta{
    41  					Name:      "foo",
    42  					Namespace: "foo-ns",
    43  				},
    44  				TypeMeta: metav1.TypeMeta{Kind: "Pod"}},
    45  			},
    46  			expectedPods: nil,
    47  		},
    48  		{
    49  			description: "only deployment pods",
    50  			allPods: []v1.Pod{{
    51  				ObjectMeta: metav1.ObjectMeta{
    52  					Name:            "foo",
    53  					Namespace:       "test",
    54  					OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}},
    55  				},
    56  				TypeMeta: metav1.TypeMeta{Kind: "Pod"},
    57  			}},
    58  			expectedPods: nil,
    59  		},
    60  		{
    61  			description: "only standalone pods",
    62  			allPods: []v1.Pod{{
    63  				ObjectMeta: metav1.ObjectMeta{
    64  					Name:      "foo",
    65  					Namespace: "test",
    66  				},
    67  				TypeMeta: metav1.TypeMeta{Kind: "Pod"},
    68  			}},
    69  			expectedPods: []v1.Pod{{
    70  				ObjectMeta: metav1.ObjectMeta{
    71  					Name:      "foo",
    72  					Namespace: "test",
    73  				},
    74  				TypeMeta: metav1.TypeMeta{Kind: "Pod"},
    75  			}},
    76  		},
    77  		{
    78  			description: "standalone pods and deployment pods",
    79  			allPods: []v1.Pod{{
    80  				ObjectMeta: metav1.ObjectMeta{
    81  					Name:      "foo1",
    82  					Namespace: "test",
    83  				},
    84  				TypeMeta: metav1.TypeMeta{Kind: "Pod"},
    85  			}, {
    86  				ObjectMeta: metav1.ObjectMeta{
    87  					Name:            "foo2",
    88  					Namespace:       "test",
    89  					OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}},
    90  				},
    91  				TypeMeta: metav1.TypeMeta{Kind: "Pod"},
    92  			}},
    93  			expectedPods: []v1.Pod{{
    94  				ObjectMeta: metav1.ObjectMeta{
    95  					Name:      "foo1",
    96  					Namespace: "test",
    97  				},
    98  				TypeMeta: metav1.TypeMeta{Kind: "Pod"},
    99  			}},
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		testutil.Run(t, test.description, func(t *testutil.T) {
   105  			var rs []runtime.Object
   106  			for i := range test.allPods {
   107  				p := test.allPods[i]
   108  				rs = append(rs, &p)
   109  			}
   110  			f := fakekubeclientset.NewSimpleClientset(rs...)
   111  			s := NewStandalonePodsSelector(f)
   112  			actualPods, err := s.Select(context.Background(), "test", metav1.ListOptions{})
   113  			t.CheckNoError(err)
   114  			t.CheckDeepEqual(test.expectedPods, actualPods)
   115  		})
   116  	}
   117  }