Merge pull request #2979 from nathanleclaire/exit_code_3

Exit with code 3 if error is during pre-create check
This commit is contained in:
Jean-Laurent de Morlhon 2016-02-02 16:54:04 -08:00
commit 092c36c31a
2 changed files with 22 additions and 13 deletions

View File

@ -164,13 +164,15 @@ func runCommand(command func(commandLine CommandLine, api libmachine.API) error)
if crashErr, ok := err.(crashreport.CrashError); ok {
crashReporter := crashreport.NewCrashReporter(mcndirs.GetBaseDir(), context.GlobalString("bugsnag-api-token"))
crashReporter.Send(crashErr)
if _, ok := crashErr.Cause.(mcnerror.ErrDuringPreCreate); ok {
osExit(3)
return
}
}
if _, ok := err.(mcnerror.ErrDuringPreCreate); ok {
osExit(3)
} else {
osExit(1)
}
osExit(1)
return
}
}
}

View File

@ -200,31 +200,38 @@ func TestReturnExitCode1onError(t *testing.T) {
exitCode := checkErrorCodeForCommand(command)
assert.Equal(t, exitCode, 1)
assert.Equal(t, 1, exitCode)
}
func TestReturnExitCode3onErrorDuringPreCreate(t *testing.T) {
command := func(commandLine CommandLine, api libmachine.API) error {
return mcnerror.ErrDuringPreCreate{
Cause: errors.New("foo is not bar"),
return crashreport.CrashError{
Cause: mcnerror.ErrDuringPreCreate{
Cause: errors.New("foo is not bar"),
},
}
}
exitCode := checkErrorCodeForCommand(command)
assert.Equal(t, exitCode, 3)
assert.Equal(t, 3, exitCode)
}
func checkErrorCodeForCommand(command func(commandLine CommandLine, api libmachine.API) error) int {
var exitCode int
var setExitCode int
originalOSExit := osExit
defer func() {
osExit = originalOSExit
}()
defer func(fnOsExit func(code int)) { osExit = fnOsExit }(osExit)
osExit = func(code int) {
exitCode = code
setExitCode = code
}
context := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
runCommand(command)(context)
return exitCode
return setExitCode
}