Fix handling of exit codes

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>

Closes: #183
Approved by: TomSweeneyRedHat
This commit is contained in:
Daniel J Walsh
2018-01-04 08:53:46 -05:00
committed by Atomic Bot
parent 137e5c8ffd
commit b231e3412e
15 changed files with 103 additions and 21 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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
}

View File

@ -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 {

View File

@ -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 ]
}

View File

@ -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" {

View File

@ -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 ]
}

View File

@ -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 ]

View File

@ -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" {

46
test/podman_run_exit.bats Normal file
View File

@ -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 ]
}

View File

@ -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 ]
}

View File

@ -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" {

View File

@ -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" {

View File

@ -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" {

View File

@ -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" {