summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/kpod/wait.go21
-rw-r--r--libpod/container.go33
-rw-r--r--test/kpod_wait.bats55
3 files changed, 49 insertions, 60 deletions
diff --git a/cmd/kpod/wait.go b/cmd/kpod/wait.go
index 5e8a50e13..6e22f54e5 100644
--- a/cmd/kpod/wait.go
+++ b/cmd/kpod/wait.go
@@ -5,7 +5,6 @@ import (
"os"
"github.com/pkg/errors"
- "github.com/projectatomic/libpod/libkpod"
"github.com/urfave/cli"
)
@@ -31,23 +30,23 @@ func waitCmd(c *cli.Context) error {
return errors.Errorf("you must provide at least one container name or id")
}
- config, err := getConfig(c)
+ runtime, err := getRuntime(c)
if err != nil {
- return errors.Wrapf(err, "could not get config")
- }
- server, err := libkpod.New(config)
- if err != nil {
- return errors.Wrapf(err, "could not get container server")
+ return errors.Wrapf(err, "error creating libpod runtime")
}
- defer server.Shutdown()
- err = server.Update()
+ defer runtime.Shutdown(false)
+
if err != nil {
- return errors.Wrapf(err, "could not update list of containers")
+ return errors.Wrapf(err, "could not get config")
}
var lastError error
for _, container := range c.Args() {
- returnCode, err := server.ContainerWait(container)
+ ctr, err := runtime.LookupContainer(container)
+ if err != nil {
+ return errors.Wrapf(err, "unable to find container %s", container)
+ }
+ returnCode, err := ctr.Wait()
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
diff --git a/libpod/container.go b/libpod/container.go
index 8bd1a0abf..9ec0fb121 100644
--- a/libpod/container.go
+++ b/libpod/container.go
@@ -19,6 +19,7 @@ import (
crioAnnotations "github.com/projectatomic/libpod/pkg/annotations"
"github.com/sirupsen/logrus"
"github.com/ulule/deepcopier"
+ "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/remotecommand"
)
@@ -639,3 +640,35 @@ func (c *Container) Export(path string) error {
func (c *Container) Commit() (*storage.Image, error) {
return nil, ErrNotImplemented
}
+
+// Wait blocks on a container to exit and returns its exit code
+func (c *Container) Wait() (int32, error) {
+ err := wait.PollImmediateInfinite(1,
+ func() (bool, error) {
+ stopped, err := c.isStopped()
+ if err != nil {
+ return false, err
+ }
+ if !stopped {
+ return false, nil
+ } else { // nolint
+ return true, nil // nolint
+ } // nolint
+ },
+ )
+ if err != nil {
+ return 0, err
+ }
+ exitCode := c.state.ExitCode
+ return exitCode, nil
+}
+
+func (c *Container) isStopped() (bool, error) {
+ c.lock.Lock()
+ defer c.lock.Unlock()
+ err := c.syncContainer()
+ if err != nil {
+ return true, err
+ }
+ return c.state.State == ContainerStateStopped, nil
+}
diff --git a/test/kpod_wait.bats b/test/kpod_wait.bats
index beb2c246d..b7bcb072c 100644
--- a/test/kpod_wait.bats
+++ b/test/kpod_wait.bats
@@ -2,74 +2,31 @@
load helpers
-IMAGE="redis:alpine"
function setup() {
copy_images
}
-# Returns the POD ID
-function pod_run_from_template(){
- #1=name, 2=uid, 3=namespace) {
- NAME=$1 CUID=$2 NAMESPACE=$3 envsubst < ${TESTDATA}/template_sandbox_config.json > ${TESTDIR}/pod-${1}.json
- crioctl pod run --config ${TESTDIR}/pod-${1}.json
-}
-
-# Returns the container ID
-function container_create_from_template() {
- #1=name, 2=image, 3=command, 4=id) {
- NAME=$1 IMAGE=$2 COMMAND=$3 envsubst < ${TESTDATA}/template_container_config.json > ${TESTDIR}/ctr-${1}.json
- crioctl ctr create --config ${TESTDIR}/ctr-${1}.json --pod "$4"
-}
-
-function container_start() {
- #1=id
- crioctl ctr start --id "$1"
-
-}
@test "wait on a bogus container" {
- skip "Needs to be converted to kpod run"
- start_crio
- run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait 12343
+ run ${KPOD_BINARY} ${KPOD_OPTIONS} wait 12343
echo $output
+ echo $status
[ "$status" -eq 1 ]
- stop_crio
}
@test "wait on a stopped container" {
- skip "Needs to be converted to kpod run"
- run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} pull docker.io/library/busybox:latest
+ run ${KPOD_BINARY} ${KPOD_OPTIONS} run -d ${ALPINE} ls
echo $output
[ "$status" -eq 0 ]
- start_crio
- pod_id=$( pod_run_from_template "test" "test" "test1-1" )
- echo $pod_id
- ctr_id=$(container_create_from_template "test-CTR" "docker.io/library/busybox:latest" '["ls"]' "${pod_id}")
- echo $ctr_id
- container_start $ctr_id
+ ctr_id=${output}
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait $ctr_id
[ "$status" -eq 0 ]
- cleanup_ctrs
- cleanup_pods
- stop_crio
}
@test "wait on a sleeping container" {
- skip "Needs to be converted to kpod run"
- run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} pull docker.io/library/busybox:latest
- echo $output
- [ "$status" -eq 0 ]
- start_crio
- pod_id=$( pod_run_from_template "test" "test" "test1-1" )
- echo $pod_id
- ctr_id=$(container_create_from_template "test-CTR" "docker.io/library/busybox:latest" '["sleep", "5"]' "${pod_id}")
- echo $ctr_id
- run container_start $ctr_id
+ run ${KPOD_BINARY} ${KPOD_OPTIONS} run -d ${ALPINE} sleep 10
echo $output
[ "$status" -eq 0 ]
+ ctr_id=${output}
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait $ctr_id
- echo $output
[ "$status" -eq 0 ]
- cleanup_ctrs
- cleanup_pods
- stop_crio
}