Skip to content

Commit

Permalink
Add tests and validations for the docker_compose_run handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrer committed Jul 10, 2019
1 parent b482635 commit 63e7495
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
35 changes: 30 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package main

import "github.com/mitchellh/mapstructure"
import (
"errors"
"fmt"
"strings"

"github.com/mitchellh/mapstructure"
)

// DockerRunHandler wraps a command with `docker container run`
type DockerRunHandler struct {
Expand All @@ -24,7 +30,9 @@ func (handler *DockerRunHandler) WrapCommand(command []string) []string {
}

func (handler *DockerRunHandler) validate() error {
// TODO: Perform some actual validations
if handler.Image == "" {
return errors.New("field image required but not set")
}
return nil
}

Expand Down Expand Up @@ -64,24 +72,39 @@ func (handler *ComposeRunHandler) WrapCommand(command []string) []string {
}

func (handler *ComposeRunHandler) validate() error {
// TODO: Perform some actual validations
if handler.Service == "" {
return errors.New("field service required but not set")
}
return nil
}

// InitComposeRunHandler generates a ComposeRunHandler
func InitComposeRunHandler(settings map[string]interface{}) (Handler, error) {
var handler *ComposeRunHandler
if err := mapstructure.Decode(settings, &handler); err != nil {
var parsingMetadata *mapstructure.Metadata = &mapstructure.Metadata{}

if err := mapstructure.DecodeMetadata(settings, &handler, parsingMetadata); err != nil {
return handler, err
}

if err := handler.validate(); err != nil {
return handler, err
}

if err := ensureNoAdditionalFields(parsingMetadata); err != nil {
return handler, err
}

return handler, nil
}

func ensureNoAdditionalFields(m *mapstructure.Metadata) error {
if len(m.Unused) > 0 {
return fmt.Errorf("additonal field(s) detected: %v", strings.Join(m.Unused, ", "))
}
return nil
}

// ComposeExecHandler wraps a command with `docker-compose run`
type ComposeExecHandler struct {
Service string
Expand All @@ -99,7 +122,9 @@ func (handler *ComposeExecHandler) WrapCommand(command []string) []string {
}

func (handler *ComposeExecHandler) validate() error {
// TODO: Perform some actual validations
if handler.Service == "" {
return errors.New("field service required but not set")
}
return nil
}

Expand Down
12 changes: 7 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"fmt"
)

// ErrInvalidHandler is thrown if any handler that is unknown to the program is specified
Expand Down Expand Up @@ -47,7 +48,11 @@ func GenerateConfig(file []byte) (*Cfg, error) {
return nil, error
}

cfg := &Cfg{}
cfg := &Cfg{
handler: map[string]Handler{},
commands: map[string]Handler{},
}

error = cfg.configFromYaml(yamlConfig)

return cfg, error
Expand Down Expand Up @@ -83,9 +88,6 @@ func (cfg *Cfg) configFromYaml(yaml *yamlCfg) error {
return ErrNoCommandsSpecified
}

cfg.handler = make(map[string]Handler)
cfg.commands = make(map[string]Handler)

for name, settings := range yaml.Strategies {
if err := cfg.generateHandler(name, settings); err != nil {
return err
Expand Down Expand Up @@ -127,7 +129,7 @@ func (cfg *Cfg) generateHandler(name string, settings map[string]interface{}) er
if handler, err := handlerFactory(settings); err == nil {
cfg.handler[name] = handler
} else {
return err
return fmt.Errorf("Error in strategy %v: %v", name, err.Error())
}

return nil
Expand Down
29 changes: 28 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@ func TestListCommands(t *testing.T) {
assert.ElementsMatch(t, cfg.ListCommands(), []string{"ls", "bundle"})
}

func TestGenerateHandler(t *testing.T) {
cfg := &Cfg{handler: map[string]Handler{}}
settings := map[string]interface{}{
"handler": "docker_compose_run",
"service": "app",
}
err := cfg.generateHandler("test", settings)
assert.NoError(t, err)
assert.NotNil(t, cfg.handler["test"])
}

func TestGenerateHandlerError(t *testing.T) {
cfg := &Cfg{}
err := cfg.generateHandler("test", map[string]interface{}{
"handler": "docker_compose_run",
})
assert.Error(t, err)
assert.Equal(t, err.Error(), "Error in strategy test: field service required but not set")

err = cfg.generateHandler("foo", map[string]interface{}{
"handler": "docker_compose_run",
"service": "test",
"other": "field",
})
assert.Error(t, err)
assert.Equal(t, err.Error(), "Error in strategy foo: additonal field(s) detected: other")
}

func TestGetHandlerFor(t *testing.T) {
var handler Handler
var err error
Expand All @@ -33,5 +61,4 @@ func TestGetHandlerFor(t *testing.T) {
handler, err = cfg.GetHandlerFor("some-cmd", false)
assert.NoError(t, err)
assert.NotNil(t, handler)

}

0 comments on commit 63e7495

Please sign in to comment.