Returns exit code 3 on pre-create check

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2016-01-11 18:57:23 +01:00
parent 1b30e7bdec
commit 45a8f5e7a6
4 changed files with 51 additions and 2 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/docker/machine/libmachine/crashreport"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnerror"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/persist"
"github.com/docker/machine/libmachine/ssh"
@ -122,7 +123,11 @@ func runCommand(command func(commandLine CommandLine, api libmachine.API) error)
crashReporter.Send(crashErr)
}
osExit(1)
if _, ok := err.(mcnerror.ErrDuringPreCreate); ok {
osExit(3)
} else {
osExit(1)
}
}
}
}

View File

@ -12,6 +12,7 @@ import (
"github.com/docker/machine/libmachine/crashreport"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/hosttest"
"github.com/docker/machine/libmachine/mcnerror"
"github.com/docker/machine/libmachine/state"
"github.com/stretchr/testify/assert"
)
@ -191,3 +192,37 @@ func TestSendCrashReport(t *testing.T) {
assert.Equal(t, test.sent, mockCrashReporter.sent, test.description)
}
}
func TestReturnExitCode1onError(t *testing.T) {
command := func(commandLine CommandLine, api libmachine.API) error {
return errors.New("foo is not bar")
}
exitCode := checkErrorCodeForCommand(command)
assert.Equal(t, exitCode, 1)
}
func TestReturnExitCode3onErrorDuringPreCreate(t *testing.T) {
command := func(commandLine CommandLine, api libmachine.API) error {
return mcnerror.ErrDuringPreCreate{errors.New("foo is not bar")}
}
exitCode := checkErrorCodeForCommand(command)
assert.Equal(t, exitCode, 3)
}
func checkErrorCodeForCommand(command func(commandLine CommandLine, api libmachine.API) error) int {
var exitCode int
defer func(fnOsExit func(code int)) { osExit = fnOsExit }(osExit)
osExit = func(code int) {
exitCode = code
}
context := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
runCommand(command)(context)
return exitCode
}

View File

@ -19,6 +19,7 @@ import (
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnerror"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/persist"
"github.com/docker/machine/libmachine/provision"
@ -124,7 +125,7 @@ func (api *Client) Create(h *host.Host) error {
log.Info("Running pre-create checks...")
if err := h.Driver.PreCreateCheck(); err != nil {
return fmt.Errorf("Error with pre-create check: %s", err)
return mcnerror.ErrDuringPreCreate{err}
}
if err := api.Save(h); err != nil {

View File

@ -24,3 +24,11 @@ type ErrHostAlreadyExists struct {
func (e ErrHostAlreadyExists) Error() string {
return fmt.Sprintf("Host already exists: %q", e.Name)
}
type ErrDuringPreCreate struct {
Cause error
}
func (e ErrDuringPreCreate) Error() string {
return fmt.Sprintf("Error with pre-create check: %q", e.Cause)
}