Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecr): add get and put lifecycle policy functions #1198

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions modules/aws/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -97,3 +98,46 @@ func NewECRClientE(t testing.TestingT, region string) (*ecr.ECR, error) {
}
return ecr.New(sess), nil
}

// GetECRRepoLifecyclePolicy gets the policies for the given ECR repository.
// This will fail the test and stop execution if there is an error.
func GetECRRepoLifecyclePolicy(t testing.TestingT, region string, repo *ecr.Repository) string {
policy, err := GetECRRepoLifecyclePolicyE(t, region, repo)
require.NoError(t, err)
return policy
}

// GetECRRepoLifecyclePolicyE gets the policies for the given ECR repository.
func GetECRRepoLifecyclePolicyE(t testing.TestingT, region string, repo *ecr.Repository) (string, error) {
client := NewECRClient(t, region)
resp, err := client.GetLifecyclePolicy(&ecr.GetLifecyclePolicyInput{RepositoryName: repo.RepositoryName})
if err != nil {
return "", err
}
return *resp.LifecyclePolicyText, nil
}

// PutECRRepoLifecyclePolicy puts the given policy for the given ECR repository.
// This will fail the test and stop execution if there is an error.
func PutECRRepoLifecyclePolicy(t testing.TestingT, region string, repo *ecr.Repository, policy string) {
err := PutECRRepoLifecyclePolicyE(t, region, repo, policy)
require.NoError(t, err)
}

// PutEcrRepoLifecyclePolicy puts the given policy for the given ECR repository.
func PutECRRepoLifecyclePolicyE(t testing.TestingT, region string, repo *ecr.Repository, policy string) error {
logger.Logf(t, "Applying policy for repository %s in %s", *repo.RepositoryName, region)

client, err := NewECRClientE(t, region)
if err != nil {
return err
}

input := &ecr.PutLifecyclePolicyInput{
RepositoryName: repo.RepositoryName,
LifecyclePolicyText: aws.String(policy),
}

_, err = client.PutLifecyclePolicy(input)
return err
}
49 changes: 49 additions & 0 deletions modules/aws/ecr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,52 @@ func TestEcrRepo(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, ecrRepoName, aws.StringValue(repo2.RepositoryName))
}

func TestGetEcrRepoLifecyclePolicyError(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
ecrRepoName := fmt.Sprintf("terratest%s", strings.ToLower(random.UniqueId()))
repo1, err := CreateECRRepoE(t, region, ecrRepoName)
defer DeleteECRRepo(t, region, repo1)
require.NoError(t, err)

assert.Equal(t, ecrRepoName, aws.StringValue(repo1.RepositoryName))

_, err = GetECRRepoLifecyclePolicyE(t, region, repo1)
require.Error(t, err)
}

func TestCanSetECRRepoLifecyclePolicyWithSingleRule(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
ecrRepoName := fmt.Sprintf("terratest%s", strings.ToLower(random.UniqueId()))
repo1, err := CreateECRRepoE(t, region, ecrRepoName)
defer DeleteECRRepo(t, region, repo1)
require.NoError(t, err)

lifecyclePolicy := `{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}`

err = PutECRRepoLifecyclePolicyE(t, region, repo1, lifecyclePolicy)
require.NoError(t, err)

policy := GetECRRepoLifecyclePolicy(t, region, repo1)
assert.JSONEq(t, lifecyclePolicy, policy)
}