github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/deploy/label/labels_test.go (about) 1 /* 2 Copyright 2019 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 label 18 19 import ( 20 "context" 21 "testing" 22 23 v1 "k8s.io/api/apps/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime/schema" 26 "k8s.io/client-go/dynamic" 27 fakedynclient "k8s.io/client-go/dynamic/fake" 28 "k8s.io/client-go/kubernetes" 29 fakeclient "k8s.io/client-go/kubernetes/fake" 30 "k8s.io/client-go/kubernetes/scheme" 31 32 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/types" 33 kubernetesclient "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/client" 34 "github.com/GoogleContainerTools/skaffold/testutil" 35 ) 36 37 func mockClient(m kubernetes.Interface) func(string) (kubernetes.Interface, error) { 38 return func(string) (kubernetes.Interface, error) { 39 return m, nil 40 } 41 } 42 43 func mockDynamicClient(m dynamic.Interface) func(string) (dynamic.Interface, error) { 44 return func(string) (dynamic.Interface, error) { 45 return m, nil 46 } 47 } 48 49 func TestApplyLabels(t *testing.T) { 50 tests := []struct { 51 description string 52 existingLabels map[string]string 53 appliedLabels map[string]string 54 expectedLabels map[string]string 55 }{ 56 { 57 description: "set labels", 58 existingLabels: map[string]string{}, 59 appliedLabels: map[string]string{ 60 "key1": "value1", 61 "key2": "value2", 62 }, 63 expectedLabels: map[string]string{ 64 "key1": "value1", 65 "key2": "value2", 66 }, 67 }, 68 { 69 description: "add labels", 70 existingLabels: map[string]string{ 71 "key0": "value0", 72 }, 73 appliedLabels: map[string]string{ 74 "key0": "should-be-ignored", 75 "key1": "value1", 76 }, 77 expectedLabels: map[string]string{ 78 "key0": "value0", 79 "key1": "value1", 80 }, 81 }, 82 } 83 84 for _, test := range tests { 85 testutil.Run(t, test.description, func(t *testutil.T) { 86 dep := &v1.Deployment{ 87 TypeMeta: metav1.TypeMeta{ 88 APIVersion: "apps/v1", 89 Kind: "Deployment", 90 }, 91 ObjectMeta: metav1.ObjectMeta{ 92 Name: "foo", 93 Labels: test.existingLabels, 94 }, 95 } 96 97 // Mock Kubernetes 98 client := fakeclient.NewSimpleClientset(dep) 99 client.Resources = append(client.Resources, &metav1.APIResourceList{ 100 GroupVersion: dep.APIVersion, 101 APIResources: []metav1.APIResource{{ 102 Kind: dep.Kind, 103 Name: "deployments", 104 }}, 105 }) 106 t.Override(&kubernetesclient.Client, mockClient(client)) 107 dynClient := fakedynclient.NewSimpleDynamicClientWithCustomListKinds(scheme.Scheme, nil, dep) 108 t.Override(&kubernetesclient.DynamicClient, mockDynamicClient(dynClient)) 109 110 // Patch labels 111 Apply(context.Background(), test.appliedLabels, []types.Artifact{{Obj: dep}}, "") 112 113 // Check modified value 114 modified, err := dynClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}).Get(context.Background(), "foo", metav1.GetOptions{}) 115 t.CheckNoError(err) 116 t.CheckDeepEqual(test.expectedLabels, modified.GetLabels()) 117 }) 118 } 119 }