github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/compare/patch_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package compare 19 20 import ( 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "github.com/google/go-cmp/cmp/cmpopts" 25 26 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 27 ) 28 29 var ( 30 ignoreOpts = []cmp.Option{ 31 cmpopts.IgnoreUnexported(bundlev1.Patch{}), 32 cmpopts.IgnoreUnexported(bundlev1.PatchMeta{}), 33 cmpopts.IgnoreUnexported(bundlev1.PatchSpec{}), 34 cmpopts.IgnoreUnexported(bundlev1.PatchRule{}), 35 cmpopts.IgnoreUnexported(bundlev1.PatchSecret{}), 36 cmpopts.IgnoreUnexported(bundlev1.PatchSelector{}), 37 cmpopts.IgnoreUnexported(bundlev1.PatchSelectorMatchPath{}), 38 cmpopts.IgnoreUnexported(bundlev1.PatchPackage{}), 39 cmpopts.IgnoreUnexported(bundlev1.PatchOperation{}), 40 } 41 ) 42 43 func TestToPatch(t *testing.T) { 44 type args struct { 45 oplog []DiffItem 46 } 47 tests := []struct { 48 name string 49 args args 50 want *bundlev1.Patch 51 wantErr bool 52 }{ 53 { 54 name: "nil", 55 wantErr: true, 56 }, 57 { 58 name: "empty", 59 args: args{ 60 oplog: []DiffItem{}, 61 }, 62 wantErr: true, 63 }, 64 { 65 name: "package added", 66 args: args{ 67 oplog: []DiffItem{ 68 {Operation: Add, Type: "package", Path: "application/test"}, 69 }, 70 }, 71 wantErr: false, 72 want: &bundlev1.Patch{ 73 ApiVersion: "harp.elastic.co/v1", 74 Kind: "BundlePatch", 75 Meta: &bundlev1.PatchMeta{ 76 Name: "autogenerated-patch", 77 Description: "Patch generated from oplog", 78 }, 79 Spec: &bundlev1.PatchSpec{ 80 Rules: []*bundlev1.PatchRule{}, 81 }, 82 }, 83 }, 84 { 85 name: "package removed", 86 args: args{ 87 oplog: []DiffItem{ 88 {Operation: Remove, Type: "package", Path: "application/test"}, 89 }, 90 }, 91 wantErr: false, 92 want: &bundlev1.Patch{ 93 ApiVersion: "harp.elastic.co/v1", 94 Kind: "BundlePatch", 95 Meta: &bundlev1.PatchMeta{ 96 Name: "autogenerated-patch", 97 Description: "Patch generated from oplog", 98 }, 99 Spec: &bundlev1.PatchSpec{ 100 Rules: []*bundlev1.PatchRule{ 101 { 102 Selector: &bundlev1.PatchSelector{ 103 MatchPath: &bundlev1.PatchSelectorMatchPath{ 104 Strict: "application/test", 105 }, 106 }, 107 Package: &bundlev1.PatchPackage{ 108 Remove: true, 109 }, 110 }, 111 }, 112 }, 113 }, 114 }, 115 { 116 name: "secret added", 117 args: args{ 118 oplog: []DiffItem{ 119 {Operation: Add, Type: "secret", Path: "application/test#key1", Value: "payload"}, 120 {Operation: Add, Type: "secret", Path: "application/test#key2", Value: "payload"}, 121 }, 122 }, 123 wantErr: false, 124 want: &bundlev1.Patch{ 125 ApiVersion: "harp.elastic.co/v1", 126 Kind: "BundlePatch", 127 Meta: &bundlev1.PatchMeta{ 128 Name: "autogenerated-patch", 129 Description: "Patch generated from oplog", 130 }, 131 Spec: &bundlev1.PatchSpec{ 132 Rules: []*bundlev1.PatchRule{ 133 { 134 Selector: &bundlev1.PatchSelector{ 135 MatchPath: &bundlev1.PatchSelectorMatchPath{ 136 Strict: "application/test", 137 }, 138 }, 139 Package: &bundlev1.PatchPackage{ 140 Data: &bundlev1.PatchSecret{ 141 Kv: &bundlev1.PatchOperation{ 142 Add: map[string]string{ 143 "key1": "payload", 144 "key2": "payload", 145 }, 146 }, 147 }, 148 }, 149 }, 150 }, 151 }, 152 }, 153 }, 154 { 155 name: "secret updated", 156 args: args{ 157 oplog: []DiffItem{ 158 {Operation: Add, Type: "secret", Path: "application/test#key1", Value: "payload"}, 159 {Operation: Replace, Type: "secret", Path: "application/test#key2", Value: "payload"}, 160 }, 161 }, 162 wantErr: false, 163 want: &bundlev1.Patch{ 164 ApiVersion: "harp.elastic.co/v1", 165 Kind: "BundlePatch", 166 Meta: &bundlev1.PatchMeta{ 167 Name: "autogenerated-patch", 168 Description: "Patch generated from oplog", 169 }, 170 Spec: &bundlev1.PatchSpec{ 171 Rules: []*bundlev1.PatchRule{ 172 { 173 Selector: &bundlev1.PatchSelector{ 174 MatchPath: &bundlev1.PatchSelectorMatchPath{ 175 Strict: "application/test", 176 }, 177 }, 178 Package: &bundlev1.PatchPackage{ 179 Data: &bundlev1.PatchSecret{ 180 Kv: &bundlev1.PatchOperation{ 181 Add: map[string]string{ 182 "key1": "payload", 183 }, 184 Update: map[string]string{ 185 "key2": "payload", 186 }, 187 }, 188 }, 189 }, 190 }, 191 }, 192 }, 193 }, 194 }, 195 { 196 name: "secret removed", 197 args: args{ 198 oplog: []DiffItem{ 199 {Operation: Add, Type: "secret", Path: "application/test#key1", Value: "payload"}, 200 {Operation: Replace, Type: "secret", Path: "application/test#key2", Value: "payload"}, 201 {Operation: Remove, Type: "secret", Path: "application/test#key3"}, 202 }, 203 }, 204 wantErr: false, 205 want: &bundlev1.Patch{ 206 ApiVersion: "harp.elastic.co/v1", 207 Kind: "BundlePatch", 208 Meta: &bundlev1.PatchMeta{ 209 Name: "autogenerated-patch", 210 Description: "Patch generated from oplog", 211 }, 212 Spec: &bundlev1.PatchSpec{ 213 Rules: []*bundlev1.PatchRule{ 214 { 215 Selector: &bundlev1.PatchSelector{ 216 MatchPath: &bundlev1.PatchSelectorMatchPath{ 217 Strict: "application/test", 218 }, 219 }, 220 Package: &bundlev1.PatchPackage{ 221 Data: &bundlev1.PatchSecret{ 222 Kv: &bundlev1.PatchOperation{ 223 Add: map[string]string{ 224 "key1": "payload", 225 }, 226 Update: map[string]string{ 227 "key2": "payload", 228 }, 229 Remove: []string{ 230 "key3", 231 }, 232 }, 233 }, 234 }, 235 }, 236 }, 237 }, 238 }, 239 }, 240 } 241 for _, tt := range tests { 242 t.Run(tt.name, func(t *testing.T) { 243 got, err := ToPatch(tt.args.oplog) 244 if (err != nil) != tt.wantErr { 245 t.Errorf("ToPatch() error = %v, wantErr %v", err, tt.wantErr) 246 return 247 } 248 if diff := cmp.Diff(got, tt.want, ignoreOpts...); diff != "" { 249 t.Errorf("%q. ToPatch():\n-got/+want\ndiff %s", tt.name, diff) 250 } 251 }) 252 } 253 }