From 69c4be48ec59c499cbe2f6808fe0514c65659ed0 Mon Sep 17 00:00:00 2001 From: Colin Hoglund Date: Sat, 10 Feb 2018 00:58:41 -0500 Subject: [PATCH 1/5] fix issue trimming repo string --- cmd/drone-docker-ecr/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/drone-docker-ecr/main.go b/cmd/drone-docker-ecr/main.go index 3f518b0..54a6913 100644 --- a/cmd/drone-docker-ecr/main.go +++ b/cmd/drone-docker-ecr/main.go @@ -55,9 +55,9 @@ func main() { } if create { - err = ensureRepoExists(svc, strings.TrimPrefix(repo, registry)) + err = ensureRepoExists(svc, strings.SplitN(repo, "/", 2)[1]) if err != nil { - os.Exit(1) + log.Fatal(fmt.Sprintf("error creating ECR repo: %v", err)) } } From 21c54aea3d0b7ece118ed4fb382993865126cda8 Mon Sep 17 00:00:00 2001 From: Colin Hoglund Date: Sat, 10 Feb 2018 11:41:07 -0500 Subject: [PATCH 2/5] 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) + } + } +} From a8da259d52d1fd99f176fa10ce86bd7a0b23ee91 Mon Sep 17 00:00:00 2001 From: Colin Hoglund Date: Sat, 10 Feb 2018 11:42:19 -0500 Subject: [PATCH 3/5] run tests as part of build step --- .drone.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.drone.yml b/.drone.yml index 31c33a9..18335e5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,6 +8,7 @@ pipeline: commands: - go get -u github.com/golang/dep/cmd/dep - dep ensure + - go test ./... - sh .drone.sh publish: From cfb097f4078f7dc5e5d30c247299f7c6d8f0170e Mon Sep 17 00:00:00 2001 From: Colin Hoglund Date: Sat, 10 Feb 2018 17:41:49 -0500 Subject: [PATCH 4/5] use Trim functions to avoid index out of range panic --- cmd/drone-docker-ecr/main.go | 8 +++++--- cmd/drone-docker-ecr/main_test.go | 11 ++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/drone-docker-ecr/main.go b/cmd/drone-docker-ecr/main.go index 35b86b4..a090e6c 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, getRepoName(repo)) + err = ensureRepoExists(svc, trimHostname(repo, registry)) if err != nil { log.Fatal(fmt.Sprintf("error creating ECR repo: %v", err)) } @@ -75,8 +75,10 @@ func main() { } } -func getRepoName(repo string) string { - return strings.SplitN(repo, "/", 2)[1] +func trimHostname(repo, registry string) string { + repo = strings.TrimPrefix(repo, registry) + repo = strings.TrimLeft(repo, "/") + return repo } func ensureRepoExists(svc *ecr.ECR, name string) (err error) { diff --git a/cmd/drone-docker-ecr/main_test.go b/cmd/drone-docker-ecr/main_test.go index fed5abc..5c50ba5 100644 --- a/cmd/drone-docker-ecr/main_test.go +++ b/cmd/drone-docker-ecr/main_test.go @@ -2,16 +2,17 @@ package main import "testing" -func TestGetRepoName(t *testing.T) { +func TestTrimHostname(t *testing.T) { + registry := "000000000000.dkr.ecr.us-east-1.amazonaws.com" // 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", + registry + "/repo": "repo", + registry + "/namespace/repo": "namespace/repo", + registry + "/namespace/namespace/repo": "namespace/namespace/repo", } for repo, name := range repos { - splitName := getRepoName(repo) + splitName := trimHostname(repo, registry) if splitName != name { t.Errorf("%s is not equal to %s.", splitName, name) } From 047967e4da9be166188ae127888dfd870176226c Mon Sep 17 00:00:00 2001 From: Colin Hoglund Date: Tue, 13 Feb 2018 23:49:47 -0500 Subject: [PATCH 5/5] align build stage testing with other plugins --- .drone.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 18335e5..0fc3103 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,7 +8,8 @@ pipeline: commands: - go get -u github.com/golang/dep/cmd/dep - dep ensure - - go test ./... + - go vet ./... + - go test -cover ./... - sh .drone.sh publish: