Skip to content

Commit

Permalink
Adds tests around run_config
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowiem committed May 25, 2020
1 parent a07b2ec commit 5dd1594
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 3 additions & 4 deletions cmd/ecs_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions cmd/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions cmd/run_config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 5dd1594

Please sign in to comment.