From 5dd1594b199da16d62bcecd501d1600d02364cdc Mon Sep 17 00:00:00 2001 From: Matt Gowie Date: Mon, 25 May 2020 00:48:39 -0600 Subject: [PATCH] Adds tests around run_config --- README.md | 7 ++-- cmd/ecs_client.go | 7 ++-- cmd/run_config.go | 5 +-- cmd/run_config_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 cmd/run_config_test.go diff --git a/README.md b/README.md index 04f8adc..baa0dd9 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,10 @@ Easily run one-off tasks against a ECS Task Definition # TODO -- [] Support project config file and use home dir config file as backup -- [] Support Dryrun Flag +- [] Support basic CLI usage +- [] Support local config file +- [] Support `--dryrun` Flag - [] Support selection of resources similar to gossm (cluster, task def, task def version, etc etc) - [] Support log group / stream tailing of initiated task +- [] Support validation of given params: cluster, definition name, revision, subnet ID, SG ID, ect. +- [] Support EC2 usage. diff --git a/cmd/ecs_client.go b/cmd/ecs_client.go index b354506..8192572 100644 --- a/cmd/ecs_client.go +++ b/cmd/ecs_client.go @@ -33,8 +33,7 @@ func newClient(client ecsiface.ECSAPI, config *RunConfig) ECSClient { } func (c *ecsClient) BuildRunTaskInput() *ecs.RunTaskInput { - - runInput := &ecs.RunTaskInput{ + return &ecs.RunTaskInput{ Cluster: &c.config.Cluster, TaskDefinition: &c.config.TaskDefinition, Count: &c.config.Count, @@ -55,11 +54,11 @@ func (c *ecsClient) BuildRunTaskInput() *ecs.RunTaskInput { }, }, } - - return runInput } func (c *ecsClient) RunTask(runTaskInput *ecs.RunTaskInput) (*ecs.RunTaskOutput, error) { + log.Debug("Input: ", runTaskInput) + output, err := c.client.RunTask(runTaskInput) if err != nil { log.Fatal("Received error when invoking RunTask.", err) diff --git a/cmd/run_config.go b/cmd/run_config.go index 46a62ce..857d2e6 100644 --- a/cmd/run_config.go +++ b/cmd/run_config.go @@ -55,8 +55,9 @@ func BuildRunConfig(session *session.Session) *RunConfig { func getNormalizedCmd() []*string { result := []*string{} - for _, v := range viper.GetStringSlice("cmd") { - result = append(result, &v) + original := viper.GetStringSlice("cmd") + for idx := range original { + result = append(result, &original[idx]) } return result diff --git a/cmd/run_config_test.go b/cmd/run_config_test.go new file mode 100644 index 0000000..92bc44f --- /dev/null +++ b/cmd/run_config_test.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "testing" + + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestGetNormalizedCmd(t *testing.T) { + assert := assert.New(t) + + expected1 := []*string{} + viper.Set("cmd", []string{}) + actual1 := getNormalizedCmd() + assert.Equal(expected1, actual1) + + echo := "echo" + helloWorld := "hello world" + expected2 := []*string{&echo, &helloWorld} + viper.Set("cmd", []string{echo, helloWorld}) + actual2 := getNormalizedCmd() + assert.Equal(expected2, actual2) +} + +func TestGetAssignPublicIP(t *testing.T) { + assert := assert.New(t) + + expected1 := "ENABLED" + viper.Set("public", true) + actual1 := getAssignPublicIP() + assert.Equal(expected1, actual1) + + expected2 := "DISABLED" + viper.Set("public", false) + actual2 := getAssignPublicIP() + assert.Equal(expected2, actual2) +} + +func TestGetContainerName(t *testing.T) { + assert := assert.New(t) + + expected1 := "task-def-name" + viper.Set("task", "task-def-name") + actual1 := getContainerName() + assert.Equal(expected1, actual1) + + expected2 := "container-name" + viper.Set("task", "task-def-name") + viper.Set("name", "container-name") + actual2 := getContainerName() + assert.Equal(expected2, actual2) +} + +func TestGetTaskDefinition(t *testing.T) { + assert := assert.New(t) + + expected1 := "task-def-name" + viper.Set("task", "task-def-name") + actual1 := getTaskDefinition() + assert.Equal(expected1, actual1) + + expected2 := "task-def-name" + viper.Set("task", "task-def-name") + viper.Set("revision", "") + actual2 := getTaskDefinition() + assert.Equal(expected2, actual2) + + expected3 := "task-def-name:5" + viper.Set("task", "task-def-name") + viper.Set("revision", "5") + actual3 := getTaskDefinition() + assert.Equal(expected3, actual3) +}