From 3e8c92241df93e1eef3e3ce49eec454218b2e89a Mon Sep 17 00:00:00 2001 From: mitchya1 Date: Fri, 14 May 2021 13:47:52 -0600 Subject: [PATCH 1/2] add test steps --- .drone.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.drone.yml b/.drone.yml index 454158c..0719fb9 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,6 +41,19 @@ steps: - go test -v ./cmd/plugin - go test -v ./pkg/deploy +- name: vet + image: golang:1.16.3 + pull: if-not-present + commands: + - go vet -race ./cmd/plugin + - go vet -race ./pkg/deploy + +- name: build-amd64 + image: golang:1.16.3 + pull: if-not-present + commands: + - bash build/linux_amd64.sh + --- kind: pipeline name: publish From affd7ee6b21a41e2aee954ef5bec6b1ae4e37181 Mon Sep 17 00:00:00 2001 From: mitchya1 Date: Fri, 14 May 2021 13:48:03 -0600 Subject: [PATCH 2/2] ensure required vars are set --- cmd/plugin/main.go | 25 +++++++++++++++++++++++++ cmd/plugin/main_test.go | 28 +++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/cmd/plugin/main.go b/cmd/plugin/main.go index 3ba8260..307abb6 100644 --- a/cmd/plugin/main.go +++ b/cmd/plugin/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "log" "os" "time" @@ -11,6 +12,25 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ecs" ) +func checkEnvVars() error { + requiredVars := []string{ + "PLUGIN_AWS_REGION", + "PLUGIN_SERVICE", + "PLUGIN_CLUSTER", + "PLUGIN_CONTAINER", + "PLUGIN_IMAGE", + } + + for _, v := range requiredVars { + if os.Getenv(v) == "" { + log.Printf("Required environment variable '%s' is missing\n", v) + return errors.New("env var not set") + } + } + + return nil +} + func newECSClient(region string) *ecs.Client { cfg, err := config.LoadDefaultConfig( context.TODO(), @@ -25,6 +45,11 @@ func newECSClient(region string) *ecs.Client { } func main() { + // Ensure all required env vars are present + if err := checkEnvVars(); err != nil { + os.Exit(1) + } + e := newECSClient(os.Getenv("PLUGIN_AWS_REGION")) service := os.Getenv("PLUGIN_SERVICE") diff --git a/cmd/plugin/main_test.go b/cmd/plugin/main_test.go index 28bdf71..d6a8798 100644 --- a/cmd/plugin/main_test.go +++ b/cmd/plugin/main_test.go @@ -1,7 +1,33 @@ package main -import "testing" +import ( + "os" + "testing" + + "gotest.tools/assert" +) func TestNewECSClient(t *testing.T) { newECSClient("us-east-2") } + +func TestCheckEnvVarsAllVarsSet(t *testing.T) { + os.Setenv("PLUGIN_AWS_REGION", "us-east-2") + os.Setenv("PLUGIN_SERVICE", "some-service") + os.Setenv("PLUGIN_CLUSTER", "some-cluster") + os.Setenv("PLUGIN_CONTAINER", "some-container-name") + os.Setenv("PLUGIN_IMAGE", "some/image:with-tag") + + err := checkEnvVars() + + assert.Equal(t, nil, err) +} + +func TestCheckEnvVarsMissing(t *testing.T) { + os.Unsetenv("PLUGIN_IMAGE") + + err := checkEnvVars() + + assert.Error(t, err, "env var not set") + +}