Skip to content

Commit b9520c8

Browse files
s-interrubenhoenle
authored andcommitted
feat(secrets-manager): test new instance update features
1 parent c010c40 commit b9520c8

File tree

1 file changed

+95
-30
lines changed

1 file changed

+95
-30
lines changed

internal/cmd/secrets-manager/instance/update/update_test.go

Lines changed: 95 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,10 @@ func TestParseInput(t *testing.T) {
200200
isValid: false,
201201
},
202202
{
203-
description: "acl flag conflicts with kms flags",
203+
description: "kms flags without name flag",
204204
argValues: fixtureArgValues(),
205205
flagValues: map[string]string{
206206
projectIdFlag: testProjectId,
207-
aclFlag: testACL1,
208207
kmsKeyIdFlag: "key-id",
209208
kmsKeyringIdFlag: "keyring-id",
210209
kmsKeyVersionFlag: "1",
@@ -236,10 +235,37 @@ func TestParseInput(t *testing.T) {
236235
}),
237236
},
238237
{
239-
description: "kms flags",
238+
description: "name flag only",
239+
argValues: fixtureArgValues(),
240+
flagValues: map[string]string{
241+
projectIdFlag: testProjectId,
242+
instanceNameFlag: "updated-name",
243+
},
244+
isValid: true,
245+
expectedModel: fixtureInputModel(func(model *inputModel) {
246+
model.Acls = nil
247+
model.InstanceName = utils.Ptr("updated-name")
248+
}),
249+
},
250+
{
251+
description: "name and acl flags",
252+
argValues: fixtureArgValues(),
253+
flagValues: map[string]string{
254+
projectIdFlag: testProjectId,
255+
instanceNameFlag: testInstanceName,
256+
aclFlag: testACL1,
257+
},
258+
isValid: true,
259+
expectedModel: fixtureInputModel(func(model *inputModel) {
260+
model.InstanceName = utils.Ptr(testInstanceName)
261+
}),
262+
},
263+
{
264+
description: "kms flags with name",
240265
argValues: fixtureArgValues(),
241266
flagValues: map[string]string{
242267
projectIdFlag: testProjectId,
268+
instanceNameFlag: testInstanceName,
243269
kmsKeyIdFlag: testKmsKeyId,
244270
kmsKeyringIdFlag: testKmsKeyringId,
245271
kmsKeyVersionFlag: "1",
@@ -248,6 +274,28 @@ func TestParseInput(t *testing.T) {
248274
isValid: true,
249275
expectedModel: fixtureInputModel(func(model *inputModel) {
250276
model.Acls = nil
277+
model.InstanceName = utils.Ptr(testInstanceName)
278+
model.KmsKeyId = utils.Ptr(testKmsKeyId)
279+
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
280+
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
281+
model.KmsServiceAccountEmail = utils.Ptr(testKmsServiceAccountEmail)
282+
}),
283+
},
284+
{
285+
description: "name, acl and kms flags together",
286+
argValues: fixtureArgValues(),
287+
flagValues: map[string]string{
288+
projectIdFlag: testProjectId,
289+
instanceNameFlag: testInstanceName,
290+
aclFlag: testACL1,
291+
kmsKeyIdFlag: testKmsKeyId,
292+
kmsKeyringIdFlag: testKmsKeyringId,
293+
kmsKeyVersionFlag: "1",
294+
kmsServiceAccountEmailFlag: testKmsServiceAccountEmail,
295+
},
296+
isValid: true,
297+
expectedModel: fixtureInputModel(func(model *inputModel) {
298+
model.InstanceName = utils.Ptr(testInstanceName)
251299
model.KmsKeyId = utils.Ptr(testKmsKeyId)
252300
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
253301
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
@@ -264,7 +312,7 @@ func TestParseInput(t *testing.T) {
264312
})
265313
}
266314
}
267-
func TestBuildRequest(t *testing.T) {
315+
func TestBuildUpdateACLsRequest(t *testing.T) {
268316
tests := []struct {
269317
description string
270318
model *inputModel
@@ -290,13 +338,9 @@ func TestBuildRequest(t *testing.T) {
290338

291339
for _, tt := range tests {
292340
t.Run(tt.description, func(t *testing.T) {
293-
request := buildRequest(testCtx, tt.model, testInstanceName, testClient)
294-
aclRequest, ok := request.(secretsmanager.ApiUpdateACLsRequest)
295-
if !ok {
296-
t.Fatalf("expected ACL update request, got %T", request)
297-
}
341+
request := buildUpdateACLsRequest(testCtx, tt.model, testClient)
298342

299-
diff := cmp.Diff(aclRequest, tt.expectedRequest,
343+
diff := cmp.Diff(request, tt.expectedRequest,
300344
cmp.AllowUnexported(tt.expectedRequest),
301345
cmpopts.EquateComparable(testCtx),
302346
)
@@ -307,27 +351,48 @@ func TestBuildRequest(t *testing.T) {
307351
}
308352
}
309353

310-
func TestBuildRequestKms(t *testing.T) {
311-
model := fixtureInputModel(func(model *inputModel) {
312-
model.Acls = nil
313-
model.KmsKeyId = utils.Ptr(testKmsKeyId)
314-
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
315-
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
316-
model.KmsServiceAccountEmail = utils.Ptr(testKmsServiceAccountEmail)
317-
})
318-
319-
request := buildRequest(testCtx, model, testInstanceName, testClient)
320-
updateRequest, ok := request.(secretsmanager.ApiUpdateInstanceRequest)
321-
if !ok {
322-
t.Fatalf("expected instance update request, got %T", request)
354+
func TestBuildUpdateInstanceRequest(t *testing.T) {
355+
tests := []struct {
356+
description string
357+
model *inputModel
358+
expectedRequest secretsmanager.ApiUpdateInstanceRequest
359+
}{
360+
{
361+
description: "with name only",
362+
model: fixtureInputModel(func(model *inputModel) {
363+
model.Acls = nil
364+
model.InstanceName = utils.Ptr(testInstanceName)
365+
}),
366+
expectedRequest: testClient.UpdateInstance(testCtx, testProjectId, testInstanceId).
367+
UpdateInstancePayload(secretsmanager.UpdateInstancePayload{
368+
Name: utils.Ptr(testInstanceName),
369+
}),
370+
},
371+
{
372+
description: "with KMS settings",
373+
model: fixtureInputModel(func(model *inputModel) {
374+
model.Acls = nil
375+
model.InstanceName = utils.Ptr(testInstanceName)
376+
model.KmsKeyId = utils.Ptr(testKmsKeyId)
377+
model.KmsKeyringId = utils.Ptr(testKmsKeyringId)
378+
model.KmsKeyVersion = utils.Ptr(testKmsKeyVersion)
379+
model.KmsServiceAccountEmail = utils.Ptr(testKmsServiceAccountEmail)
380+
}),
381+
expectedRequest: fixtureUpdateInstanceRequest(),
382+
},
323383
}
324384

325-
expectedRequest := fixtureUpdateInstanceRequest()
326-
diff := cmp.Diff(updateRequest, expectedRequest,
327-
cmp.AllowUnexported(expectedRequest),
328-
cmpopts.EquateComparable(testCtx),
329-
)
330-
if diff != "" {
331-
t.Fatalf("Data does not match: %s", diff)
385+
for _, tt := range tests {
386+
t.Run(tt.description, func(t *testing.T) {
387+
request := buildUpdateInstanceRequest(testCtx, tt.model, testClient)
388+
389+
diff := cmp.Diff(request, tt.expectedRequest,
390+
cmp.AllowUnexported(tt.expectedRequest),
391+
cmpopts.EquateComparable(testCtx),
392+
)
393+
if diff != "" {
394+
t.Fatalf("Data does not match: %s", diff)
395+
}
396+
})
332397
}
333398
}

0 commit comments

Comments
 (0)