github.com/argoproj/argo-cd@v1.8.7/util/argo/normalizers/diff_normalizer_test.go (about) 1 package normalizers 2 3 import ( 4 "testing" 5 6 "github.com/ghodss/yaml" 7 "github.com/stretchr/testify/assert" 8 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 9 10 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 11 "github.com/argoproj/argo-cd/test" 12 ) 13 14 func TestNormalizeObjectWithMatchedGroupKind(t *testing.T) { 15 normalizer, err := NewIgnoreNormalizer([]v1alpha1.ResourceIgnoreDifferences{{ 16 Group: "apps", 17 Kind: "Deployment", 18 JSONPointers: []string{"/not-matching-path", "/spec/template/spec/containers"}, 19 }}, make(map[string]v1alpha1.ResourceOverride)) 20 21 assert.Nil(t, err) 22 23 deployment := test.NewDeployment() 24 25 _, has, err := unstructured.NestedSlice(deployment.Object, "spec", "template", "spec", "containers") 26 assert.Nil(t, err) 27 assert.True(t, has) 28 29 err = normalizer.Normalize(deployment) 30 assert.Nil(t, err) 31 _, has, err = unstructured.NestedSlice(deployment.Object, "spec", "template", "spec", "containers") 32 assert.Nil(t, err) 33 assert.False(t, has) 34 } 35 36 func TestNormalizeNoMatchedGroupKinds(t *testing.T) { 37 normalizer, err := NewIgnoreNormalizer([]v1alpha1.ResourceIgnoreDifferences{{ 38 Group: "", 39 Kind: "Service", 40 JSONPointers: []string{"/spec"}, 41 }}, make(map[string]v1alpha1.ResourceOverride)) 42 43 assert.Nil(t, err) 44 45 deployment := test.NewDeployment() 46 47 err = normalizer.Normalize(deployment) 48 assert.Nil(t, err) 49 50 _, hasSpec, err := unstructured.NestedMap(deployment.Object, "spec") 51 assert.Nil(t, err) 52 assert.True(t, hasSpec) 53 } 54 55 func TestNormalizeMatchedResourceOverrides(t *testing.T) { 56 normalizer, err := NewIgnoreNormalizer([]v1alpha1.ResourceIgnoreDifferences{}, map[string]v1alpha1.ResourceOverride{ 57 "apps/Deployment": { 58 IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{JSONPointers: []string{"/spec/template/spec/containers"}}, 59 }, 60 }) 61 62 assert.Nil(t, err) 63 64 deployment := test.NewDeployment() 65 66 _, has, err := unstructured.NestedSlice(deployment.Object, "spec", "template", "spec", "containers") 67 assert.Nil(t, err) 68 assert.True(t, has) 69 70 err = normalizer.Normalize(deployment) 71 assert.Nil(t, err) 72 _, has, err = unstructured.NestedSlice(deployment.Object, "spec", "template", "spec", "containers") 73 assert.Nil(t, err) 74 assert.False(t, has) 75 } 76 77 const testCRDYAML = ` 78 apiVersion: apiextensions.k8s.io/v1beta1 79 kind: CustomResourceDefinition 80 metadata: 81 name: certificates.cert-manager.io 82 spec: 83 group: cert-manager.io 84 names: 85 kind: Certificate 86 listKind: CertificateList 87 plural: certificates 88 shortNames: 89 - cert 90 - certs 91 singular: certificate 92 scope: Namespaced 93 version: v1alpha1` 94 95 func TestNormalizeMissingJsonPointer(t *testing.T) { 96 normalizer, err := NewIgnoreNormalizer([]v1alpha1.ResourceIgnoreDifferences{}, map[string]v1alpha1.ResourceOverride{ 97 "apps/Deployment": { 98 IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{JSONPointers: []string{"/garbage"}}, 99 }, 100 "apiextensions.k8s.io/CustomResourceDefinition": { 101 IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{JSONPointers: []string{"/spec/additionalPrinterColumns/0/priority"}}, 102 }, 103 }) 104 assert.NoError(t, err) 105 106 deployment := test.NewDeployment() 107 108 err = normalizer.Normalize(deployment) 109 assert.NoError(t, err) 110 111 crd := unstructured.Unstructured{} 112 err = yaml.Unmarshal([]byte(testCRDYAML), &crd) 113 assert.NoError(t, err) 114 115 err = normalizer.Normalize(&crd) 116 assert.NoError(t, err) 117 } 118 119 func TestNormalizeGlobMatch(t *testing.T) { 120 normalizer, err := NewIgnoreNormalizer([]v1alpha1.ResourceIgnoreDifferences{}, map[string]v1alpha1.ResourceOverride{ 121 "*/*": { 122 IgnoreDifferences: v1alpha1.OverrideIgnoreDiff{JSONPointers: []string{"/spec/template/spec/containers"}}, 123 }, 124 }) 125 126 assert.Nil(t, err) 127 128 deployment := test.NewDeployment() 129 130 _, has, err := unstructured.NestedSlice(deployment.Object, "spec", "template", "spec", "containers") 131 assert.Nil(t, err) 132 assert.True(t, has) 133 134 err = normalizer.Normalize(deployment) 135 assert.Nil(t, err) 136 _, has, err = unstructured.NestedSlice(deployment.Object, "spec", "template", "spec", "containers") 137 assert.Nil(t, err) 138 assert.False(t, has) 139 }