github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/openshift/change_test.go (about)

     1  package openshift
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestDiff(t *testing.T) {
     9  
    10  	diffs := map[string]struct {
    11  		currentAnnotations []byte
    12  		currentData        []byte
    13  		desiredAnnotations []byte
    14  		desiredData        []byte
    15  		expectedDiff       string
    16  	}{
    17  		"Modifying a data field": {
    18  			currentAnnotations: []byte("{}"),
    19  			currentData:        []byte("{foo: bar}"),
    20  			desiredAnnotations: []byte("{}"),
    21  			desiredData:        []byte("{foo: baz}"),
    22  			expectedDiff: `--- Current State (OpenShift cluster)
    23  +++ Desired State (Processed template)
    24  @@ -1,6 +1,6 @@
    25   apiVersion: v1
    26   data:
    27  -  foo: bar
    28  +  foo: baz
    29   kind: ConfigMap
    30   metadata:
    31     annotations: {}
    32  `,
    33  		},
    34  		"Adding a data field": {
    35  			currentAnnotations: []byte("{}"),
    36  			currentData:        []byte("{foo: bar}"),
    37  			desiredAnnotations: []byte("{}"),
    38  			desiredData:        []byte("{foo: bar, baz: qux}"),
    39  			expectedDiff: `--- Current State (OpenShift cluster)
    40  +++ Desired State (Processed template)
    41  @@ -1,5 +1,6 @@
    42   apiVersion: v1
    43   data:
    44  +  baz: qux
    45     foo: bar
    46   kind: ConfigMap
    47   metadata:
    48  `,
    49  		},
    50  		"Removing a data field": {
    51  			currentAnnotations: []byte("{}"),
    52  			currentData:        []byte("{foo: bar}"),
    53  			desiredAnnotations: []byte("{}"),
    54  			desiredData:        []byte("{}"),
    55  			expectedDiff: `--- Current State (OpenShift cluster)
    56  +++ Desired State (Processed template)
    57  @@ -1,6 +1,5 @@
    58   apiVersion: v1
    59  -data:
    60  -  foo: bar
    61  +data: {}
    62   kind: ConfigMap
    63   metadata:
    64     annotations: {}
    65  `,
    66  		},
    67  		"Adding an annotation": {
    68  			currentAnnotations: []byte("{}"),
    69  			currentData:        []byte("{}"),
    70  			desiredAnnotations: []byte("{foo: bar}"),
    71  			desiredData:        []byte("{}"),
    72  			expectedDiff: `--- Current State (OpenShift cluster)
    73  +++ Desired State (Processed template)
    74  @@ -2,7 +2,8 @@
    75   data: {}
    76   kind: ConfigMap
    77   metadata:
    78  -  annotations: {}
    79  +  annotations:
    80  +    foo: bar
    81     labels:
    82       app: bar
    83     name: bar
    84  `,
    85  		},
    86  		"Removing an annotation": {
    87  			currentAnnotations: []byte(`{foo: bar, kubectl.kubernetes.io/last-applied-configuration: '{"apiVersion":"v1","kind":"ConfigMap","metadata":{"annotations":{"foo":"bar"}}}'}`),
    88  			currentData:        []byte("{}"),
    89  			desiredAnnotations: []byte("{}"),
    90  			desiredData:        []byte("{}"),
    91  			expectedDiff: `--- Current State (OpenShift cluster)
    92  +++ Desired State (Processed template)
    93  @@ -2,8 +2,7 @@
    94   data: {}
    95   kind: ConfigMap
    96   metadata:
    97  -  annotations:
    98  -    foo: bar
    99  +  annotations: {}
   100     labels:
   101       app: bar
   102     name: bar
   103  `,
   104  		},
   105  		"Modifying an annotation": {
   106  			currentAnnotations: []byte(`{foo: bar, kubectl.kubernetes.io/last-applied-configuration: '{"apiVersion":"v1","kind":"ConfigMap","metadata":{"annotations":{"foo":"bar"}}}'}`),
   107  			currentData:        []byte("{}"),
   108  			desiredAnnotations: []byte("{foo: baz}"),
   109  			desiredData:        []byte("{}"),
   110  			expectedDiff: `--- Current State (OpenShift cluster)
   111  +++ Desired State (Processed template)
   112  @@ -3,7 +3,7 @@
   113   kind: ConfigMap
   114   metadata:
   115     annotations:
   116  -    foo: bar
   117  +    foo: baz
   118     labels:
   119       app: bar
   120     name: bar
   121  `,
   122  		},
   123  		"Modifying a non-managed annotation": {
   124  			currentAnnotations: []byte(`{foo: bar, baz: qux, kubectl.kubernetes.io/last-applied-configuration: '{"apiVersion":"v1","kind":"ConfigMap","metadata":{"annotations":{"foo":"bar"}}}'}`),
   125  			currentData:        []byte("{}"),
   126  			desiredAnnotations: []byte("{foo: bar, baz: zab}"),
   127  			desiredData:        []byte("{}"),
   128  			expectedDiff: `--- Current State (OpenShift cluster)
   129  +++ Desired State (Processed template)
   130  @@ -3,7 +3,7 @@
   131   kind: ConfigMap
   132   metadata:
   133     annotations:
   134  -    baz: qux
   135  +    baz: zab
   136       foo: bar
   137     labels:
   138       app: bar
   139  `,
   140  		},
   141  	}
   142  
   143  	for name, tt := range diffs {
   144  		t.Run(name, func(t *testing.T) {
   145  			currentItem := getItem(
   146  				t,
   147  				getConfigMapForDiff(tt.currentAnnotations, tt.currentData),
   148  				"platform",
   149  			)
   150  			desiredItem := getItem(
   151  				t,
   152  				getConfigMapForDiff(tt.desiredAnnotations, tt.desiredData),
   153  				"template",
   154  			)
   155  			changes, err := calculateChanges(desiredItem, currentItem, []string{}, true)
   156  			if err != nil {
   157  				t.Fatal(err)
   158  			}
   159  			change := changes[0]
   160  			actualDiff := change.Diff(true)
   161  			if actualDiff != tt.expectedDiff {
   162  				t.Fatalf(
   163  					"Diff()\n===== expected =====\n%s\n===== actual =====\n%s",
   164  					tt.expectedDiff,
   165  					actualDiff,
   166  				)
   167  			}
   168  		})
   169  	}
   170  }
   171  
   172  func getConfigMapForDiff(annotations, data []byte) []byte {
   173  	config := []byte(
   174  		`apiVersion: v1
   175  kind: ConfigMap
   176  metadata:
   177    labels:
   178      app: bar
   179    annotations: ANNOTATIONS
   180    name: bar
   181  data: DATA`)
   182  	config = bytes.Replace(config, []byte("ANNOTATIONS"), annotations, -1)
   183  	return bytes.Replace(config, []byte("DATA"), data, -1)
   184  }