From 2bc76ebc04e9a25f3cc208d7a7f42721f210ddcb Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Mon, 28 Jan 2019 23:34:59 -0800 Subject: [PATCH] Add test for tagRegistryCredentials() --- ecs-cli/modules/cli/regcreds/regcreds_app.go | 6 ++-- .../modules/cli/regcreds/regcreds_app_test.go | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ecs-cli/modules/cli/regcreds/regcreds_app.go b/ecs-cli/modules/cli/regcreds/regcreds_app.go index 0d8988845..80019fb46 100644 --- a/ecs-cli/modules/cli/regcreds/regcreds_app.go +++ b/ecs-cli/modules/cli/regcreds/regcreds_app.go @@ -116,7 +116,8 @@ func Up(c *cli.Context) { } if len(tags) > 0 { - err = tagRegistryCredentials(credentialOutput, roleName, tags, commandConfig) + taggingClient := tagging.NewTaggingClient(commandConfig) + err = tagRegistryCredentials(credentialOutput, tags, taggingClient) if err != nil { log.Fatal("Failed to tag resources: ", err) } @@ -326,14 +327,13 @@ func validateOutputOptions(outputDir string, skipOutput bool) error { return nil } -func tagRegistryCredentials(creds map[string]regcredio.CredsOutputEntry, roleName string, tags map[string]*string, commandConfig *config.CommandConfig) error { +func tagRegistryCredentials(creds map[string]regcredio.CredsOutputEntry, tags map[string]*string, taggingClient tagging.Client) error { var arns []*string for _, credInfo := range creds { arns = append(arns, aws.String(credInfo.CredentialARN)) } - taggingClient := tagging.NewTaggingClient(commandConfig) input := &resourcegroupstaggingapi.TagResourcesInput{ ResourceARNList: arns, Tags: tags, diff --git a/ecs-cli/modules/cli/regcreds/regcreds_app_test.go b/ecs-cli/modules/cli/regcreds/regcreds_app_test.go index 00a2965f0..d00a9108e 100644 --- a/ecs-cli/modules/cli/regcreds/regcreds_app_test.go +++ b/ecs-cli/modules/cli/regcreds/regcreds_app_test.go @@ -21,9 +21,11 @@ import ( "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/iam/mock" "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/kms/mock" "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/secretsmanager/mock" + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/tagging/mock" "github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/regcredio" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/kms" + "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi" secretsmanager "github.com/aws/aws-sdk-go/service/secretsmanager" "github.com/golang/mock/gomock" "github.com/pkg/errors" @@ -216,6 +218,34 @@ func TestGetOrCreateRegistryCredentials_ErrorOnUpdate(t *testing.T) { assert.Error(t, err) } +func TestTagRegistryCredentials(t *testing.T) { + creds := map[string]regcredio.CredsOutputEntry{ + "the-who-registry.com": regcredio.CredsOutputEntry{ + CredentialARN: "arn:aws:secretsmanager:eu-west-1:111111111111:secret:path/whoareyou-1978", + }, + } + + tags := map[string]*string{ + "Baba": aws.String("O'riley"), + "Eminence": aws.String("Front"), + "My": aws.String("Generation"), + } + + ctrl := gomock.NewController(t) + + mockTagging := mock_tagging.NewMockClient(ctrl) + + gomock.InOrder( + mockTagging.EXPECT().TagResources(gomock.Any()).Do(func(x interface{}) { + input := x.(*resourcegroupstaggingapi.TagResourcesInput) + assert.Equal(t, tags, input.Tags, "Expected tags to match") + }).Return(&resourcegroupstaggingapi.TagResourcesOutput{}, nil), + ) + + err := tagRegistryCredentials(creds, tags, mockTagging) + assert.NoError(t, err, "Unexpected error calling tagRegistryCredentials") +} + func TestValidateCredsInput_ErrorEmptyCreds(t *testing.T) { emptyCredMap := make(map[string]regcredio.RegistryCredEntry) emptyCredsInput := regcredio.ECSRegCredsInput{