github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/statefulset_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 appsv1 "k8s.io/api/apps/v1" 24 v1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apimachinery/pkg/types" 28 fakekubeclientset "k8s.io/client-go/kubernetes/fake" 29 appsclient "k8s.io/client-go/kubernetes/typed/apps/v1" 30 31 "github.com/GoogleContainerTools/skaffold/v2/testutil" 32 ) 33 34 func TestStatefulSetPodsSelector(t *testing.T) { 35 tests := []struct { 36 description string 37 allPods []v1.Pod 38 expectedPods []v1.Pod 39 }{ 40 { 41 description: "pod don't exist in test namespace", 42 allPods: []v1.Pod{{ 43 ObjectMeta: metav1.ObjectMeta{ 44 Name: "foo", 45 Namespace: "foo-ns", 46 OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, 47 }, 48 TypeMeta: metav1.TypeMeta{Kind: "Pod"}}, 49 }, 50 expectedPods: nil, 51 }, 52 { 53 description: "only statefulset pods", 54 allPods: []v1.Pod{{ 55 ObjectMeta: metav1.ObjectMeta{ 56 Name: "foo", 57 Namespace: "test", 58 OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, 59 }, 60 TypeMeta: metav1.TypeMeta{Kind: "Pod"}, 61 }}, 62 expectedPods: []v1.Pod{{ 63 ObjectMeta: metav1.ObjectMeta{ 64 Name: "foo", 65 Namespace: "test", 66 OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, 67 }, 68 TypeMeta: metav1.TypeMeta{Kind: "Pod"}, 69 }}, 70 }, 71 { 72 description: "only standalone pods", 73 allPods: []v1.Pod{{ 74 ObjectMeta: metav1.ObjectMeta{ 75 Name: "foo", 76 Namespace: "test", 77 }, 78 TypeMeta: metav1.TypeMeta{Kind: "Pod"}, 79 }}, 80 expectedPods: nil, 81 }, 82 { 83 description: "standalone pods and statefulset pods", 84 allPods: []v1.Pod{{ 85 ObjectMeta: metav1.ObjectMeta{ 86 Name: "foo1", 87 Namespace: "test", 88 }, 89 TypeMeta: metav1.TypeMeta{Kind: "Pod"}, 90 }, { 91 ObjectMeta: metav1.ObjectMeta{ 92 Name: "foo2", 93 Namespace: "test", 94 OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, 95 }, 96 TypeMeta: metav1.TypeMeta{Kind: "Pod"}, 97 }}, 98 expectedPods: []v1.Pod{{ 99 ObjectMeta: metav1.ObjectMeta{ 100 Name: "foo2", 101 Namespace: "test", 102 OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, 103 }, 104 TypeMeta: metav1.TypeMeta{Kind: "Pod"}, 105 }}, 106 }, 107 } 108 109 for _, test := range tests { 110 testutil.Run(t, test.description, func(t *testutil.T) { 111 t.Override(&getReplicaSet, func(_ *appsv1.Deployment, _ appsclient.AppsV1Interface) ([]*appsv1.ReplicaSet, []*appsv1.ReplicaSet, *appsv1.ReplicaSet, error) { 112 return nil, nil, &appsv1.ReplicaSet{ 113 ObjectMeta: metav1.ObjectMeta{ 114 UID: types.UID(""), 115 }, 116 }, nil 117 }) 118 var rs []runtime.Object 119 for i := range test.allPods { 120 p := test.allPods[i] 121 rs = append(rs, &p) 122 } 123 f := fakekubeclientset.NewSimpleClientset(rs...) 124 s := NewStatefulSetPodsSelector(f, appsv1.StatefulSet{}) 125 actualPods, err := s.Select(context.Background(), "test", metav1.ListOptions{}) 126 t.CheckNoError(err) 127 t.CheckDeepEqual(test.expectedPods, actualPods) 128 }) 129 } 130 }