github.com/oam-dev/kubevela@v1.9.11/pkg/utils/apply/patch_test.go (about) 1 /* 2 Copyright 2021 The KubeVela 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 apply 18 19 import ( 20 "fmt" 21 "testing" 22 23 corev1 "k8s.io/api/core/v1" 24 25 "github.com/oam-dev/kubevela/pkg/oam" 26 27 "github.com/crossplane/crossplane-runtime/pkg/test" 28 "github.com/google/go-cmp/cmp" 29 "github.com/pkg/errors" 30 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 31 "k8s.io/apimachinery/pkg/runtime" 32 ) 33 34 func TestAddLastAppliedConfig(t *testing.T) { 35 cases := map[string]struct { 36 reason string 37 obj runtime.Object 38 wantObj runtime.Object 39 wantErr error 40 }{ 41 "ErrCannotAccessMetadata": { 42 reason: "An error should be returned if cannot access metadata", 43 obj: testNoMetaObject{}, 44 wantObj: testNoMetaObject{}, 45 wantErr: errors.Wrap(fmt.Errorf("object does not implement the Object interfaces"), "cannot access metadata.annotations"), 46 }, 47 } 48 49 for caseName, tc := range cases { 50 t.Run(caseName, func(t *testing.T) { 51 err := addLastAppliedConfigAnnotation(tc.obj) 52 if diff := cmp.Diff(tc.wantObj, tc.obj); diff != "" { 53 t.Errorf("\n%s\ngetModifiedConfig(...): -want , +got \n%s\n", tc.reason, diff) 54 } 55 if diff := cmp.Diff(tc.wantErr, err, test.EquateErrors()); diff != "" { 56 t.Errorf("\n%s\ngetModifiedConfig(...): -want , +got \n%s\n", tc.reason, diff) 57 } 58 }) 59 } 60 } 61 62 func TestGetOriginalConfig(t *testing.T) { 63 objNoAnno := &unstructured.Unstructured{} 64 objNoAnno.SetAnnotations(make(map[string]string)) 65 66 objHasAnno := &unstructured.Unstructured{} 67 annoMap := make(map[string]string) 68 annoMap[oam.AnnotationLastAppliedConfig] = "oam obj record" 69 annoMap[corev1.LastAppliedConfigAnnotation] = "kubectl obj record" 70 objHasAnno.SetAnnotations(annoMap) 71 72 objOnlyHasKubectlAnno := &unstructured.Unstructured{} 73 annoOnlyKubectlMap := make(map[string]string) 74 annoOnlyKubectlMap[corev1.LastAppliedConfigAnnotation] = "kubectl obj record" 75 objOnlyHasKubectlAnno.SetAnnotations(annoOnlyKubectlMap) 76 77 cases := map[string]struct { 78 reason string 79 obj runtime.Object 80 wantConfig string 81 wantErr error 82 }{ 83 "ErrCannotAccessMetadata": { 84 reason: "An error should be returned if cannot access metadata", 85 obj: testNoMetaObject{}, 86 wantErr: errors.Wrap(fmt.Errorf("object does not implement the Object interfaces"), "cannot access metadata.annotations"), 87 }, 88 "NoAnnotations": { 89 reason: "No error should be returned if cannot find last-applied-config annotaion", 90 obj: &unstructured.Unstructured{}, 91 }, 92 "LastAppliedConfigAnnotationNotFound": { 93 reason: "No error should be returned if cannot find last-applied-config annotaion", 94 obj: objNoAnno, 95 }, 96 "OAMLastAppliedConfigAnnotationFound": { 97 reason: "No error should be returned if find oam last-applied-config annotaion ", 98 obj: objHasAnno, 99 wantConfig: "oam obj record", 100 }, 101 "KubectlLastAppliedConfigAnnotationFound": { 102 reason: "No error should be returned if find last-applied-config annotaion, prefer oam annotations, followed by kubectl ", 103 obj: objOnlyHasKubectlAnno, 104 wantConfig: "kubectl obj record", 105 }, 106 } 107 108 for caseName, tc := range cases { 109 t.Run(caseName, func(t *testing.T) { 110 r, err := getOriginalConfiguration(tc.obj) 111 if diff := cmp.Diff(tc.wantConfig, string(r)); diff != "" { 112 t.Errorf("\n%s\ngetModifiedConfig(...): -want , +got \n%s\n", tc.reason, diff) 113 } 114 if diff := cmp.Diff(tc.wantErr, err, test.EquateErrors()); diff != "" { 115 t.Errorf("\n%s\ngetModifiedConfig(...): -want , +got \n%s\n", tc.reason, diff) 116 } 117 }) 118 } 119 }