From 21c54aea3d0b7ece118ed4fb382993865126cda8 Mon Sep 17 00:00:00 2001 From: Colin Hoglund Date: Sat, 10 Feb 2018 11:41:07 -0500 Subject: [PATCH] break out repo split and add unit test --- cmd/drone-docker-ecr/main.go | 6 +++++- cmd/drone-docker-ecr/main_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 cmd/drone-docker-ecr/main_test.go diff --git a/cmd/drone-docker-ecr/main.go b/cmd/drone-docker-ecr/main.go index 54a6913..35b86b4 100644 --- a/cmd/drone-docker-ecr/main.go +++ b/cmd/drone-docker-ecr/main.go @@ -55,7 +55,7 @@ func main() { } if create { - err = ensureRepoExists(svc, strings.SplitN(repo, "/", 2)[1]) + err = ensureRepoExists(svc, getRepoName(repo)) if err != nil { log.Fatal(fmt.Sprintf("error creating ECR repo: %v", err)) } @@ -75,6 +75,10 @@ func main() { } } +func getRepoName(repo string) string { + return strings.SplitN(repo, "/", 2)[1] +} + func ensureRepoExists(svc *ecr.ECR, name string) (err error) { input := &ecr.CreateRepositoryInput{} input.SetRepositoryName(name) diff --git a/cmd/drone-docker-ecr/main_test.go b/cmd/drone-docker-ecr/main_test.go new file mode 100644 index 0000000..fed5abc --- /dev/null +++ b/cmd/drone-docker-ecr/main_test.go @@ -0,0 +1,19 @@ +package main + +import "testing" + +func TestGetRepoName(t *testing.T) { + // map full repo path to expected repo name + repos := map[string]string{ + "000000000000.dkr.ecr.us-east-1.amazonaws.com/repo": "repo", + "000000000000.dkr.ecr.us-east-1.amazonaws.com/namespace/repo": "namespace/repo", + "000000000000.dkr.ecr.us-east-1.amazonaws.com/namespace/namespace/repo": "namespace/namespace/repo", + } + + for repo, name := range repos { + splitName := getRepoName(repo) + if splitName != name { + t.Errorf("%s is not equal to %s.", splitName, name) + } + } +}