|
| 1 | +/* |
| 2 | +Copyright 2023 The Flux 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 diff |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + extjsondiff "github.com/wI2L/jsondiff" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | + |
| 25 | + "github.com/fluxcd/pkg/ssa/jsondiff" |
| 26 | +) |
| 27 | + |
| 28 | +func TestSummarizeDiffSet(t *testing.T) { |
| 29 | + diffSet := jsondiff.DiffSet{ |
| 30 | + &jsondiff.Diff{ |
| 31 | + GroupVersionKind: schema.GroupVersionKind{ |
| 32 | + Kind: "ConfigMap", |
| 33 | + }, |
| 34 | + Namespace: "namespace-1", |
| 35 | + Name: "config", |
| 36 | + Type: jsondiff.DiffTypeNone, |
| 37 | + }, |
| 38 | + &jsondiff.Diff{ |
| 39 | + GroupVersionKind: schema.GroupVersionKind{ |
| 40 | + Kind: "Secret", |
| 41 | + }, |
| 42 | + Namespace: "namespace-x", |
| 43 | + Name: "naughty", |
| 44 | + Type: jsondiff.DiffTypeCreate, |
| 45 | + }, |
| 46 | + &jsondiff.Diff{ |
| 47 | + GroupVersionKind: schema.GroupVersionKind{ |
| 48 | + Kind: "StatefulSet", |
| 49 | + }, |
| 50 | + Namespace: "default", |
| 51 | + Name: "hello-world", |
| 52 | + Type: jsondiff.DiffTypeExclude, |
| 53 | + }, |
| 54 | + &jsondiff.Diff{ |
| 55 | + GroupVersionKind: schema.GroupVersionKind{ |
| 56 | + Kind: "Deployment", |
| 57 | + }, |
| 58 | + Namespace: "tenant-y", |
| 59 | + Name: "touched-me", |
| 60 | + Type: jsondiff.DiffTypeUpdate, |
| 61 | + Patch: extjsondiff.Patch{ |
| 62 | + {Type: extjsondiff.OperationAdd}, |
| 63 | + {Type: extjsondiff.OperationReplace}, |
| 64 | + {Type: extjsondiff.OperationReplace}, |
| 65 | + {Type: extjsondiff.OperationReplace}, |
| 66 | + {Type: extjsondiff.OperationRemove}, |
| 67 | + {Type: extjsondiff.OperationRemove}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + include []jsondiff.DiffType |
| 75 | + want string |
| 76 | + }{ |
| 77 | + { |
| 78 | + name: "default", |
| 79 | + include: nil, |
| 80 | + want: `Secret/namespace-x/naughty removed |
| 81 | +StatefulSet/default/hello-world excluded |
| 82 | +Deployment/tenant-y/touched-me changed (1 additions, 3 changes, 2 removals)`, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "include unchanged", |
| 86 | + include: []jsondiff.DiffType{ |
| 87 | + jsondiff.DiffTypeNone, |
| 88 | + }, |
| 89 | + want: "ConfigMap/namespace-1/config unchanged", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "include removed", |
| 93 | + include: []jsondiff.DiffType{ |
| 94 | + jsondiff.DiffTypeCreate, |
| 95 | + }, |
| 96 | + want: "Secret/namespace-x/naughty removed", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "include excluded", |
| 100 | + include: []jsondiff.DiffType{ |
| 101 | + jsondiff.DiffTypeExclude, |
| 102 | + }, |
| 103 | + want: "StatefulSet/default/hello-world excluded", |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "include changed", |
| 107 | + include: []jsondiff.DiffType{ |
| 108 | + jsondiff.DiffTypeUpdate, |
| 109 | + }, |
| 110 | + want: "Deployment/tenant-y/touched-me changed (1 additions, 3 changes, 2 removals)", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "include multiple types", |
| 114 | + include: []jsondiff.DiffType{ |
| 115 | + jsondiff.DiffTypeNone, |
| 116 | + jsondiff.DiffTypeUpdate, |
| 117 | + }, |
| 118 | + want: `ConfigMap/namespace-1/config unchanged |
| 119 | +Deployment/tenant-y/touched-me changed (1 additions, 3 changes, 2 removals)`, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "empty set", |
| 123 | + include: []jsondiff.DiffType{}, |
| 124 | + want: "", |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for _, tt := range tests { |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + got := SummarizeDiffSet(diffSet, tt.include...) |
| 131 | + if got != tt.want { |
| 132 | + t.Errorf("SummarizeDiffSet() =\n\n%v\n\nwant\n\n%v", got, tt.want) |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestSummarizeDiffSetBrief(t *testing.T) { |
| 139 | + diffSet := jsondiff.DiffSet{ |
| 140 | + &jsondiff.Diff{Type: jsondiff.DiffTypeCreate}, |
| 141 | + &jsondiff.Diff{Type: jsondiff.DiffTypeUpdate}, |
| 142 | + &jsondiff.Diff{Type: jsondiff.DiffTypeExclude}, |
| 143 | + &jsondiff.Diff{Type: jsondiff.DiffTypeNone}, |
| 144 | + &jsondiff.Diff{Type: jsondiff.DiffTypeNone}, |
| 145 | + } |
| 146 | + |
| 147 | + tests := []struct { |
| 148 | + name string |
| 149 | + include []jsondiff.DiffType |
| 150 | + want string |
| 151 | + }{ |
| 152 | + { |
| 153 | + name: "default include", |
| 154 | + include: nil, |
| 155 | + want: "removed: 1, changed: 1, excluded: 1", |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "include create and update", |
| 159 | + include: []jsondiff.DiffType{ |
| 160 | + jsondiff.DiffTypeCreate, |
| 161 | + jsondiff.DiffTypeUpdate, |
| 162 | + }, |
| 163 | + want: "removed: 1, changed: 1", |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "include all types", |
| 167 | + include: []jsondiff.DiffType{ |
| 168 | + jsondiff.DiffTypeCreate, |
| 169 | + jsondiff.DiffTypeUpdate, |
| 170 | + jsondiff.DiffTypeExclude, |
| 171 | + jsondiff.DiffTypeNone, |
| 172 | + }, |
| 173 | + want: "removed: 1, changed: 1, excluded: 1, unchanged: 2", |
| 174 | + }, |
| 175 | + { |
| 176 | + name: "include none", |
| 177 | + include: []jsondiff.DiffType{}, |
| 178 | + want: "", |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + for _, tt := range tests { |
| 183 | + t.Run(tt.name, func(t *testing.T) { |
| 184 | + got := SummarizeDiffSetBrief(diffSet, tt.include...) |
| 185 | + if got != tt.want { |
| 186 | + t.Errorf("SummarizeDiffSetBrief() = %v, want %v", got, tt.want) |
| 187 | + } |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
0 commit comments