mirror of https://github.com/docker/docs.git
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:
commit
092c36c31a
|
@ -164,13 +164,15 @@ func runCommand(command func(commandLine CommandLine, api libmachine.API) error)
|
||||||
if crashErr, ok := err.(crashreport.CrashError); ok {
|
if crashErr, ok := err.(crashreport.CrashError); ok {
|
||||||
crashReporter := crashreport.NewCrashReporter(mcndirs.GetBaseDir(), context.GlobalString("bugsnag-api-token"))
|
crashReporter := crashreport.NewCrashReporter(mcndirs.GetBaseDir(), context.GlobalString("bugsnag-api-token"))
|
||||||
crashReporter.Send(crashErr)
|
crashReporter.Send(crashErr)
|
||||||
|
|
||||||
|
if _, ok := crashErr.Cause.(mcnerror.ErrDuringPreCreate); ok {
|
||||||
|
osExit(3)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := err.(mcnerror.ErrDuringPreCreate); ok {
|
osExit(1)
|
||||||
osExit(3)
|
return
|
||||||
} else {
|
|
||||||
osExit(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -200,31 +200,38 @@ func TestReturnExitCode1onError(t *testing.T) {
|
||||||
|
|
||||||
exitCode := checkErrorCodeForCommand(command)
|
exitCode := checkErrorCodeForCommand(command)
|
||||||
|
|
||||||
assert.Equal(t, exitCode, 1)
|
assert.Equal(t, 1, exitCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReturnExitCode3onErrorDuringPreCreate(t *testing.T) {
|
func TestReturnExitCode3onErrorDuringPreCreate(t *testing.T) {
|
||||||
command := func(commandLine CommandLine, api libmachine.API) error {
|
command := func(commandLine CommandLine, api libmachine.API) error {
|
||||||
return mcnerror.ErrDuringPreCreate{
|
return crashreport.CrashError{
|
||||||
Cause: errors.New("foo is not bar"),
|
Cause: mcnerror.ErrDuringPreCreate{
|
||||||
|
Cause: errors.New("foo is not bar"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCode := checkErrorCodeForCommand(command)
|
exitCode := checkErrorCodeForCommand(command)
|
||||||
|
|
||||||
assert.Equal(t, exitCode, 3)
|
assert.Equal(t, 3, exitCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkErrorCodeForCommand(command func(commandLine CommandLine, api libmachine.API) error) int {
|
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) {
|
osExit = func(code int) {
|
||||||
exitCode = code
|
setExitCode = code
|
||||||
}
|
}
|
||||||
|
|
||||||
context := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
|
context := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
|
||||||
runCommand(command)(context)
|
runCommand(command)(context)
|
||||||
|
|
||||||
return exitCode
|
return setExitCode
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue