kpod_wait

Convert to libpod container backend

Signed-off-by: baude <bbaude@redhat.com>

Closes: #70
Approved by: rhatdan
This commit is contained in:
baude 2017-11-24 09:08:59 -06:00 committed by Atomic Bot
parent 99f905243b
commit 52ea0deee6
3 changed files with 50 additions and 61 deletions

View File

@ -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, "error creating libpod runtime")
}
defer runtime.Shutdown(false)
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")
}
defer server.Shutdown()
err = server.Update()
if err != nil {
return errors.Wrapf(err, "could not update list of containers")
}
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)

View File

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

View File

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