Skip to content

Commit

Permalink
add config validation & more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elsesiy committed Jun 30, 2019
1 parent 72008fc commit 6470933
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
5 changes: 5 additions & 0 deletions donner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

// ErrUndefinedCommand is thrown if a command specified can't be found in the yaml definition
var ErrUndefinedCommand = errors.New("the command you're trying to run doesn't exist in the yaml definition")

// ErrMissingCommand is thrown if no handler for execution is provided
var ErrMissingCommand = errors.New("no command for execution specified")

Expand Down Expand Up @@ -87,6 +88,10 @@ func ExecCommand(cfg *Cfg, params []string) error {
cliArgs = append(cliArgs, "--rm")
}

if designatedHandler.Service != "" {
cliArgs = append(cliArgs, designatedHandler.Service)
}

if designatedHandler.Image != "" {
cliArgs = append(cliArgs, designatedHandler.Image)
}
Expand Down
26 changes: 24 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import (
"gopkg.in/yaml.v2"
)

// Current handler implementations
var availableHandlers = map[string]ExecHandler{"docker_compose_run": {"docker-compose", "run"}, "docker_compose_exec": {"docker-compose", "exec"}, "docker_run": {"docker", "run"}}

// ErrInvalidHandler is thrown if any handler that is unknown to the program is specified
var ErrInvalidHandler = errors.New("configuration specifies unknown handler")

// Current handler implementations
var availableHandlers = map[string]ExecHandler{"docker_compose_run": {"docker-compose", "run"}, "docker_compose_exec": {"docker-compose", "exec"}, "docker_run": {"docker", "run"}}
// ErrNoCommandsSpecified is thrown if the yaml file doesn't contain any commands
var ErrNoCommandsSpecified = errors.New("the specified yaml file doesn't contain any commands")

// ErrNoStrategiesSpecified is thrown if the yaml file doesn't contain any strategies
var ErrNoStrategiesSpecified = errors.New("the specified yaml file doesn't contain any strategies")

// ExecHandler is the desired OS exec
type ExecHandler struct {
Expand All @@ -32,6 +38,18 @@ type Strategy struct {
Image string
}

func (c *Cfg) Validate() error {
if len(c.Strategies) == 0 {
return ErrNoStrategiesSpecified
}

if len(c.Commands) == 0 {
return ErrNoCommandsSpecified
}

return nil
}

// Validate checks whether a strategy specifies only valid handlers
func (s *Strategy) Validate() error {
_, ok := availableHandlers[s.Handler]
Expand All @@ -54,6 +72,10 @@ func ParseFile(file []byte) (*Cfg, error) {
return nil, err
}

if err := cfg.Validate(); err != nil {
return nil, err
}

for _, strat := range cfg.Strategies {
if err := strat.Validate(); err != nil {
return nil, err
Expand Down
32 changes: 26 additions & 6 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,45 @@ import (
"github.com/stretchr/testify/assert"
)

var data = `
var missingCommandsYaml = `
strategies:
exec:
handler: docker_compose_exec
service: app
`

var missingStrategiesYml = `
commands:
rails: exec
rspec: exec
`

var fullYaml = `
strategies:
run:
handler: docker_compose_run
service: app
remove: true
exec:
handler: docker_compose_exec
service: app
run_with_docker:
handler: docker_run
image: alpine:latest
default_strategy: run
commands:
ls: run_with_docker
bundle: run
`

func TestParseFile(t *testing.T) {
// TODO add many more tests
tests := map[string]struct {
input string
exp *Cfg
expErr error
}{
"partial valid yaml": {data, &Cfg{Strategies: map[string]Strategy{"exec": Strategy{Handler: "docker_compose_exec", Service: "app", Remove: false}, "run": Strategy{Handler: "docker_compose_run", Service: "app", Remove: true}}, DefaultStrategy: "", Commands: map[string]Command(nil)}, nil},
"yaml without commands": {input: missingCommandsYaml, expErr: ErrNoCommandsSpecified},
"yaml without strategies": {input: missingStrategiesYml, expErr: ErrNoStrategiesSpecified},
"full yaml spec": {input: fullYaml, exp: &Cfg{Strategies: map[string]Strategy{"run": {Handler: "docker_compose_run", Service: "app", Remove: true}, "run_with_docker": {Handler: "docker_run", Image: "alpine:latest"}}, DefaultStrategy: "run", Commands: map[string]Command{"ls": "run_with_docker", "bundle": "run"}}},
}

for name, test := range tests {
Expand Down

0 comments on commit 6470933

Please sign in to comment.