github.com/argoproj/argo-cd@v1.8.7/util/kube/kube_test.go (about) 1 package kube 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "log" 7 "testing" 8 9 "github.com/ghodss/yaml" 10 11 "github.com/stretchr/testify/assert" 12 apiv1 "k8s.io/api/core/v1" 13 extv1beta1 "k8s.io/api/extensions/v1beta1" 14 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 15 16 "github.com/argoproj/argo-cd/common" 17 ) 18 19 const depWithoutSelector = ` 20 apiVersion: extensions/v1beta1 21 kind: Deployment 22 metadata: 23 name: nginx-deployment 24 spec: 25 template: 26 metadata: 27 labels: 28 app: nginx 29 spec: 30 containers: 31 - image: nginx:1.7.9 32 name: nginx 33 ports: 34 - containerPort: 80 35 ` 36 37 const depWithSelector = ` 38 apiVersion: extensions/v1beta1 39 kind: Deployment 40 metadata: 41 name: nginx-deployment 42 spec: 43 selector: 44 matchLabels: 45 app: nginx 46 template: 47 metadata: 48 labels: 49 app: nginx 50 spec: 51 containers: 52 - image: nginx:1.7.9 53 name: nginx 54 ports: 55 - containerPort: 80 56 ` 57 58 func TestSetLabels(t *testing.T) { 59 for _, yamlStr := range []string{depWithoutSelector, depWithSelector} { 60 var obj unstructured.Unstructured 61 err := yaml.Unmarshal([]byte(yamlStr), &obj) 62 assert.Nil(t, err) 63 64 err = SetAppInstanceLabel(&obj, common.LabelKeyAppInstance, "my-app") 65 assert.Nil(t, err) 66 67 manifestBytes, err := json.MarshalIndent(obj.Object, "", " ") 68 assert.Nil(t, err) 69 log.Println(string(manifestBytes)) 70 71 var depV1Beta1 extv1beta1.Deployment 72 err = json.Unmarshal(manifestBytes, &depV1Beta1) 73 assert.Nil(t, err) 74 75 // the following makes sure we are not falling into legacy code which injects labels 76 if yamlStr == depWithoutSelector { 77 assert.Nil(t, depV1Beta1.Spec.Selector) 78 } else if yamlStr == depWithSelector { 79 assert.Equal(t, 1, len(depV1Beta1.Spec.Selector.MatchLabels)) 80 assert.Equal(t, "nginx", depV1Beta1.Spec.Selector.MatchLabels["app"]) 81 } 82 assert.Equal(t, 1, len(depV1Beta1.Spec.Template.Labels)) 83 assert.Equal(t, "nginx", depV1Beta1.Spec.Template.Labels["app"]) 84 } 85 } 86 87 func TestSetLegacyLabels(t *testing.T) { 88 for _, yamlStr := range []string{depWithoutSelector, depWithSelector} { 89 var obj unstructured.Unstructured 90 err := yaml.Unmarshal([]byte(yamlStr), &obj) 91 assert.Nil(t, err) 92 93 err = SetAppInstanceLabel(&obj, common.LabelKeyLegacyApplicationName, "my-app") 94 assert.Nil(t, err) 95 96 manifestBytes, err := json.MarshalIndent(obj.Object, "", " ") 97 assert.Nil(t, err) 98 log.Println(string(manifestBytes)) 99 100 var depV1Beta1 extv1beta1.Deployment 101 err = json.Unmarshal(manifestBytes, &depV1Beta1) 102 assert.Nil(t, err) 103 assert.Equal(t, 1, len(depV1Beta1.Spec.Selector.MatchLabels)) 104 assert.Equal(t, "nginx", depV1Beta1.Spec.Selector.MatchLabels["app"]) 105 assert.Equal(t, 2, len(depV1Beta1.Spec.Template.Labels)) 106 assert.Equal(t, "nginx", depV1Beta1.Spec.Template.Labels["app"]) 107 assert.Equal(t, "my-app", depV1Beta1.Spec.Template.Labels[common.LabelKeyLegacyApplicationName]) 108 } 109 } 110 111 func TestSetLegacyJobLabel(t *testing.T) { 112 yamlBytes, err := ioutil.ReadFile("testdata/job.yaml") 113 assert.Nil(t, err) 114 var obj unstructured.Unstructured 115 err = yaml.Unmarshal(yamlBytes, &obj) 116 assert.Nil(t, err) 117 err = SetAppInstanceLabel(&obj, common.LabelKeyLegacyApplicationName, "my-app") 118 assert.Nil(t, err) 119 120 manifestBytes, err := json.MarshalIndent(obj.Object, "", " ") 121 assert.Nil(t, err) 122 log.Println(string(manifestBytes)) 123 124 job := unstructured.Unstructured{} 125 err = json.Unmarshal(manifestBytes, &job) 126 assert.Nil(t, err) 127 128 labels := job.GetLabels() 129 assert.Equal(t, "my-app", labels[common.LabelKeyLegacyApplicationName]) 130 131 templateLabels, ok, err := unstructured.NestedMap(job.UnstructuredContent(), "spec", "template", "metadata", "labels") 132 assert.True(t, ok) 133 assert.Nil(t, err) 134 assert.Equal(t, "my-app", templateLabels[common.LabelKeyLegacyApplicationName]) 135 } 136 137 func TestSetSvcLabel(t *testing.T) { 138 yamlBytes, err := ioutil.ReadFile("testdata/svc.yaml") 139 assert.Nil(t, err) 140 var obj unstructured.Unstructured 141 err = yaml.Unmarshal(yamlBytes, &obj) 142 assert.Nil(t, err) 143 err = SetAppInstanceLabel(&obj, common.LabelKeyAppInstance, "my-app") 144 assert.Nil(t, err) 145 146 manifestBytes, err := json.MarshalIndent(obj.Object, "", " ") 147 assert.Nil(t, err) 148 log.Println(string(manifestBytes)) 149 150 var s apiv1.Service 151 err = json.Unmarshal(manifestBytes, &s) 152 assert.Nil(t, err) 153 154 log.Println(s.Name) 155 log.Println(s.ObjectMeta) 156 assert.Equal(t, "my-app", s.ObjectMeta.Labels[common.LabelKeyAppInstance]) 157 }