diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2018-01-04 08:53:46 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-01-04 21:05:10 +0000 |
commit | b231e3412e7d37d9680229e3782e28161792c7f8 (patch) | |
tree | da0d214d45bbb13b3254dfe450b73ddc24e44351 | |
parent | 137e5c8ffd5e1d7abe47398d909c9611f19bfcbb (diff) | |
download | podman-b231e3412e7d37d9680229e3782e28161792c7f8.tar.gz podman-b231e3412e7d37d9680229e3782e28161792c7f8.tar.bz2 podman-b231e3412e7d37d9680229e3782e28161792c7f8.zip |
Fix handling of exit codes
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
Closes: #183
Approved by: TomSweeneyRedHat
-rw-r--r-- | cmd/podman/main.go | 14 | ||||
-rw-r--r-- | cmd/podman/run.go | 12 | ||||
-rw-r--r-- | cmd/podman/start.go | 5 | ||||
-rw-r--r-- | libpod/container.go | 7 | ||||
-rw-r--r-- | test/podman_attach.bats | 6 | ||||
-rw-r--r-- | test/podman_exec.bats | 4 | ||||
-rw-r--r-- | test/podman_kill.bats | 2 | ||||
-rw-r--r-- | test/podman_pause.bats | 10 | ||||
-rw-r--r-- | test/podman_rmi.bats | 2 | ||||
-rw-r--r-- | test/podman_run_exit.bats | 46 | ||||
-rw-r--r-- | test/podman_start.bats | 4 | ||||
-rw-r--r-- | test/podman_stats.bats | 2 | ||||
-rw-r--r-- | test/podman_stop.bats | 2 | ||||
-rw-r--r-- | test/podman_top.bats | 6 | ||||
-rw-r--r-- | test/podman_wait.bats | 2 |
15 files changed, 103 insertions, 21 deletions
diff --git a/cmd/podman/main.go b/cmd/podman/main.go index b27409e4c..0fd30fa71 100644 --- a/cmd/podman/main.go +++ b/cmd/podman/main.go @@ -13,7 +13,10 @@ import ( // This is populated by the Makefile from the VERSION file // in the repository -var podmanVersion = "" +var ( + podmanVersion = "" + exitCode = 125 +) func main() { debug := false @@ -152,5 +155,14 @@ func main() { } else { fmt.Fprintln(os.Stderr, err.Error()) } + } else { + // The exitCode modified from 125, indicates an application + // running inside of a container failed, as opposed to the + // podman command failed. Must exit with that exit code + // otherwise command exited correctly. + if exitCode == 125 { + exitCode = 0 + } } + os.Exit(exitCode) } diff --git a/cmd/podman/run.go b/cmd/podman/run.go index 654b7a47e..45a428f39 100644 --- a/cmd/podman/run.go +++ b/cmd/podman/run.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "strings" "sync" "github.com/pkg/errors" @@ -62,6 +63,11 @@ func runCmd(c *cli.Context) error { logrus.Debug("new container created ", ctr.ID()) if err := ctr.Init(); err != nil { + // This means the command did not exist + exitCode = 126 + if strings.Index(err.Error(), "permission denied") > -1 { + exitCode = 127 + } return err } logrus.Debugf("container storage created for %q", ctr.ID()) @@ -109,9 +115,15 @@ func runCmd(c *cli.Context) error { } if createConfig.Detach { fmt.Printf("%s\n", ctr.ID()) + exitCode = 0 return nil } wg.Wait() + if ecode, err := ctr.ExitCode(); err != nil { + logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err) + } else { + exitCode = int(ecode) + } if createConfig.Rm { return runtime.RemoveContainer(ctr, true) diff --git a/cmd/podman/start.go b/cmd/podman/start.go index 33bc354bb..d4ddfbbe7 100644 --- a/cmd/podman/start.go +++ b/cmd/podman/start.go @@ -126,6 +126,11 @@ func startCmd(c *cli.Context) error { fmt.Println(ctr.ID()) } wg.Wait() + if ecode, err := ctr.ExitCode(); err != nil { + logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err) + } else { + exitCode = int(ecode) + } } return lastError } diff --git a/libpod/container.go b/libpod/container.go index 2b70afec2..efb4873b0 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -558,6 +558,13 @@ func (c *Container) Init() (err error) { if err := c.mountStorage(); err != nil { return err } + defer func() { + if err != nil { + if err2 := c.cleanupStorage(); err2 != nil { + logrus.Errorf("Error cleaning up storage for container %s: %v", c.ID(), err2) + } + } + }() // Make a network namespace for the container if c.config.CreateNetNS && c.state.NetNS == nil { diff --git a/test/podman_attach.bats b/test/podman_attach.bats index 9baaf339c..8676b2e43 100644 --- a/test/podman_attach.bats +++ b/test/podman_attach.bats @@ -13,14 +13,14 @@ function setup() { @test "attach to a bogus container" { run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} attach foobar" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "attach to non-running container" { ${PODMAN_BINARY} ${PODMAN_OPTIONS} create --name foobar -d -i ${ALPINE} ls run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} attach foobar" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "attach to multiple containers" { @@ -28,5 +28,5 @@ function setup() { ${PODMAN_BINARY} ${PODMAN_OPTIONS} run --name foobar2 -d -i ${ALPINE} /bin/sh run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} attach foobar1 foobar2" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } diff --git a/test/podman_exec.bats b/test/podman_exec.bats index 76114aa0a..2d6a42704 100644 --- a/test/podman_exec.bats +++ b/test/podman_exec.bats @@ -13,13 +13,13 @@ function setup() { @test "exec into a bogus container" { run ${PODMAN_BINARY} ${PODMAN_OPTIONS} exec foobar ls echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "exec without command should fail" { run ${PODMAN_BINARY} ${PODMAN_OPTIONS} exec foobar echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "exec simple command" { diff --git a/test/podman_kill.bats b/test/podman_kill.bats index 449603905..1ccf373bb 100644 --- a/test/podman_kill.bats +++ b/test/podman_kill.bats @@ -58,7 +58,7 @@ function setup() { run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} ps" [ "$status" -eq 0 ] run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} kill -s foobar $ctr_id" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} ps --no-trunc" [ "$status" -eq 0 ] } diff --git a/test/podman_pause.bats b/test/podman_pause.bats index 52add0397..49db0c2aa 100644 --- a/test/podman_pause.bats +++ b/test/podman_pause.bats @@ -13,13 +13,13 @@ function teardown() { @test "pause a bogus container" { run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} pause foobar" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "unpause a bogus container" { run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} unpause foobar" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "pause a created container by id" { @@ -61,7 +61,7 @@ function teardown() { ctr_id=`echo "$output" | tail -n 1` run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} unpause $ctr_id" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} rm -f $ctr_id" echo "$output" [ "$status" -eq 0 ] @@ -78,7 +78,7 @@ function teardown() { [ "$status" -eq 0 ] run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} rm $ctr_id" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} rm --force $ctr_id" echo "$output" [ "$status" -eq 0 ] @@ -94,7 +94,7 @@ function teardown() { [ "$status" -eq 0 ] run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} stop $ctr_id" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} unpause $ctr_id" echo "$output" [ "$status" -eq 0 ] diff --git a/test/podman_rmi.bats b/test/podman_rmi.bats index dabf2f0e8..e800a0388 100644 --- a/test/podman_rmi.bats +++ b/test/podman_rmi.bats @@ -19,7 +19,7 @@ function pullImages() { @test "podman rmi bogus image" { run ${PODMAN_BINARY} $PODMAN_OPTIONS rmi debian:6.0.10 echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "podman rmi image with fq name" { diff --git a/test/podman_run_exit.bats b/test/podman_run_exit.bats new file mode 100644 index 000000000..02ccb56ec --- /dev/null +++ b/test/podman_run_exit.bats @@ -0,0 +1,46 @@ +#!/usr/bin/env bats + +load helpers + +function teardown() { + cleanup_test +} + +function setup() { + copy_images +} + +@test "run exit125 test" { + run ${PODMAN_BINARY} ${PODMAN_OPTIONS} run --foobar ${ALPINE} ls $tmp + echo $output + echo $status != 125 + [ $status -eq 125 ] +} + +@test "run exit126 test" { + run ${PODMAN_BINARY} ${PODMAN_OPTIONS} run ${ALPINE} foobar + echo $output + echo $status != 126 + [ "$status" -eq 126 ] +} + +@test "run exit127 test" { + run ${PODMAN_BINARY} ${PODMAN_OPTIONS} run ${ALPINE} /etc + echo $output + echo $status != 127 + [ "$status" -eq 127 ] +} + +@test "run exit0 test" { + run ${PODMAN_BINARY} ${PODMAN_OPTIONS} run ${ALPINE} ps + echo $output + echo $status != 0 + [ "$status" -eq 0 ] +} + +@test "run exit50 test" { + run ${PODMAN_BINARY} ${PODMAN_OPTIONS} run ${ALPINE} sh -c "exit 50" + echo $output + echo $status != 50 + [ "$status" -eq 50 ] +} diff --git a/test/podman_start.bats b/test/podman_start.bats index 50cbf43dc..71afc83ff 100644 --- a/test/podman_start.bats +++ b/test/podman_start.bats @@ -13,7 +13,7 @@ function teardown() { @test "start bogus container" { run ${PODMAN_BINARY} ${PODMAN_OPTIONS} start 1234 echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "start single container by id" { @@ -46,5 +46,5 @@ function teardown() { ${PODMAN_BINARY} ${PODMAN_OPTIONS} create --name foobar2 -d ${ALPINE} ls run ${PODMAN_BINARY} ${PODMAN_OPTIONS} start -a foobar1 foobar2 echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } diff --git a/test/podman_stats.bats b/test/podman_stats.bats index 91ef62f6b..7bdd0ee63 100644 --- a/test/podman_stats.bats +++ b/test/podman_stats.bats @@ -19,7 +19,7 @@ function setup() { @test "stats with bogus container id" { run ${PODMAN_BINARY} ${PODMAN_OPTIONS} stats --no-stream 123 echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "stats on a running container" { diff --git a/test/podman_stop.bats b/test/podman_stop.bats index b505d8827..498d71838 100644 --- a/test/podman_stop.bats +++ b/test/podman_stop.bats @@ -13,7 +13,7 @@ function setup() { @test "stop a bogus container" { run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} stop foobar" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "stop a running container by id" { diff --git a/test/podman_top.bats b/test/podman_top.bats index e592a0e22..a8b92cd44 100644 --- a/test/podman_top.bats +++ b/test/podman_top.bats @@ -13,13 +13,13 @@ function setup() { @test "top without container name or id" { run ${PODMAN_BINARY} ${PODMAN_OPTIONS} top echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "top a bogus container" { run ${PODMAN_BINARY} ${PODMAN_OPTIONS} top foobar echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "top non-running container by id with defaults" { @@ -28,7 +28,7 @@ function setup() { ctr_id="$output" run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} top $ctr_id" echo "$output" - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "top running container by id with defaults" { diff --git a/test/podman_wait.bats b/test/podman_wait.bats index 2ebf1d58d..3109ce91e 100644 --- a/test/podman_wait.bats +++ b/test/podman_wait.bats @@ -14,7 +14,7 @@ function teardown() { run ${PODMAN_BINARY} ${PODMAN_OPTIONS} wait 12343 echo $output echo $status - [ "$status" -eq 1 ] + [ "$status" -eq 125 ] } @test "wait on a stopped container" { |