Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Fail on missing env var #9

Merged
merged 2 commits into from
May 14, 2021
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
13 changes: 13 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions cmd/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"log"
"os"
"time"
Expand All @@ -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(),
Expand All @@ -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")
Expand Down
28 changes: 27 additions & 1 deletion cmd/plugin/main_test.go
Original file line number Diff line number Diff line change
@@ -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")

}