|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cloudposse/test-helpers/pkg/atmos" |
| 10 | + helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper" |
| 11 | + "github.com/gruntwork-io/terratest/modules/aws" |
| 12 | + "github.com/gruntwork-io/terratest/modules/random" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +type LifecyclePolicyRuleSelection struct { |
| 17 | + TagStatus string `json:"tagStatus"` |
| 18 | + TagPrefixList []string `json:"tagPrefixList"` |
| 19 | + CountType string `json:"countType"` |
| 20 | + CountNumber int `json:"countNumber"` |
| 21 | +} |
| 22 | + |
| 23 | +type LifecyclePolicyRule struct { |
| 24 | + RulePriority int `json:"rulePriority"` |
| 25 | + Description string `json:"description"` |
| 26 | + Selection LifecyclePolicyRuleSelection `json:"selection"` |
| 27 | + Action map[string]string `json:"action"` |
| 28 | +} |
| 29 | + |
| 30 | +type LifecyclePolicy struct { |
| 31 | + Rules []LifecyclePolicyRule `json:"rules"` |
| 32 | +} |
| 33 | + |
| 34 | +type ComponentSuite struct { |
| 35 | + helper.TestSuite |
| 36 | +} |
| 37 | + |
| 38 | +func (s *ComponentSuite) TestBasic() { |
| 39 | + const component = "ecr/basic" |
| 40 | + const stack = "default-test" |
| 41 | + const awsRegion = "us-east-2" |
| 42 | + |
| 43 | + suffix := strings.ToLower(random.UniqueId()) |
| 44 | + |
| 45 | + inputs := map[string]interface{}{ |
| 46 | + "images" : []string{ |
| 47 | + fmt.Sprintf("infrastructure-%s", suffix), |
| 48 | + fmt.Sprintf("microservice-a-%s", suffix), |
| 49 | + fmt.Sprintf("microservice-b-%s", suffix), |
| 50 | + fmt.Sprintf("microservice-c-%s", suffix), |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs) |
| 55 | + options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs) |
| 56 | + assert.NotNil(s.T(), options) |
| 57 | + |
| 58 | + awsAccountId := aws.GetAccountId(s.T()) |
| 59 | + |
| 60 | + repositoryHost := atmos.Output(s.T(), options, "repository_host") |
| 61 | + assert.Equal(s.T(), fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", awsAccountId, awsRegion), repositoryHost) |
| 62 | + |
| 63 | + assert.Empty(s.T(), atmos.Output(s.T(), options, "ecr_user_name")) |
| 64 | + assert.Empty(s.T(), atmos.Output(s.T(), options, "ecr_user_arn")) |
| 65 | + assert.Empty(s.T(), atmos.Output(s.T(), options, "ecr_user_unique_id")) |
| 66 | + |
| 67 | + arnMaps := map[string]string{} |
| 68 | + atmos.OutputStruct(s.T(), options, "ecr_repo_arn_map", &arnMaps) |
| 69 | + |
| 70 | + urlMaps := map[string]string{} |
| 71 | + atmos.OutputStruct(s.T(), options, "ecr_repo_url_map", &urlMaps) |
| 72 | + |
| 73 | + for name, arn := range arnMaps { |
| 74 | + repository := aws.GetECRRepo(s.T(), awsRegion, name) |
| 75 | + assert.Equal(s.T(), name, *repository.RepositoryName) |
| 76 | + assert.Equal(s.T(), arn, *repository.RepositoryArn) |
| 77 | + assert.Equal(s.T(), urlMaps[name], *repository.RepositoryUri) |
| 78 | + assert.EqualValues(s.T(), "IMMUTABLE", repository.ImageTagMutability) |
| 79 | + assert.True(s.T(), repository.ImageScanningConfiguration.ScanOnPush) |
| 80 | + assert.EqualValues(s.T(), "AES256", repository.EncryptionConfiguration.EncryptionType) |
| 81 | + |
| 82 | + lifecyclePolicyString := aws.GetECRRepoLifecyclePolicy(s.T(), awsRegion, repository) |
| 83 | + lifecyclePolicy := LifecyclePolicy{} |
| 84 | + json.Unmarshal([]byte(lifecyclePolicyString), &lifecyclePolicy) |
| 85 | + |
| 86 | + expectedLifecyclePolicy := LifecyclePolicy{ |
| 87 | + Rules: []LifecyclePolicyRule{ |
| 88 | + { |
| 89 | + RulePriority: 1, |
| 90 | + Description: "Protects images tagged with prod", |
| 91 | + Selection: LifecyclePolicyRuleSelection{ |
| 92 | + TagStatus: "tagged", |
| 93 | + TagPrefixList: []string{"prod"}, |
| 94 | + CountType: "imageCountMoreThan", |
| 95 | + CountNumber: 999999, |
| 96 | + }, |
| 97 | + Action: map[string]string{ |
| 98 | + "type": "expire", |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + RulePriority: 2, |
| 103 | + Description: "Remove untagged images", |
| 104 | + Selection: LifecyclePolicyRuleSelection{ |
| 105 | + TagStatus: "untagged", |
| 106 | + CountType: "imageCountMoreThan", |
| 107 | + CountNumber: 1, |
| 108 | + }, |
| 109 | + Action: map[string]string{ |
| 110 | + "type": "expire", |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + RulePriority: 3, |
| 115 | + Description: "Rotate images when reach 500 images stored", |
| 116 | + Selection: LifecyclePolicyRuleSelection{ |
| 117 | + TagStatus: "any", |
| 118 | + CountType: "imageCountMoreThan", |
| 119 | + CountNumber: 500, |
| 120 | + }, |
| 121 | + Action: map[string]string{ |
| 122 | + "type": "expire", |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + assert.EqualValues(s.T(), expectedLifecyclePolicy, lifecyclePolicy) |
| 128 | + } |
| 129 | + |
| 130 | + s.DriftTest(component, stack, &inputs) |
| 131 | +} |
| 132 | + |
| 133 | +func (s *ComponentSuite) TestEnabledFlag() { |
| 134 | + const component = "ecr/disabled" |
| 135 | + const stack = "default-test" |
| 136 | + const awsRegion = "us-east-2" |
| 137 | + |
| 138 | + s.VerifyEnabledFlag(component, stack, nil) |
| 139 | +} |
| 140 | + |
| 141 | +func TestRunSuite(t *testing.T) { |
| 142 | + suite := new(ComponentSuite) |
| 143 | + helper.Run(t, suite) |
| 144 | +} |
0 commit comments