From 1b2d4f3d60a8ae6181f56c6f2fdca81daa0aea90 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Tue, 6 Dec 2016 12:39:38 +0100 Subject: [PATCH] server: sync ctr state before checking it Signed-off-by: Antonio Murdaca --- server/container_stop.go | 11 +++++++-- server/sandbox_stop.go | 3 +++ test/ctr.bats | 30 ++++++++++++++++++++++++ test/pod.bats | 50 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/server/container_stop.go b/server/container_stop.go index 762c3d9a..1aba8801 100644 --- a/server/container_stop.go +++ b/server/container_stop.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/Sirupsen/logrus" + "github.com/kubernetes-incubator/cri-o/oci" "golang.org/x/net/context" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" ) @@ -16,8 +17,14 @@ func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest return nil, err } - if err := s.runtime.StopContainer(c); err != nil { - return nil, fmt.Errorf("failed to stop container %s: %v", c.ID(), err) + if err := s.runtime.UpdateStatus(c); err != nil { + return nil, err + } + cStatus := s.runtime.ContainerStatus(c) + if cStatus.Status != oci.ContainerStateStopped { + if err := s.runtime.StopContainer(c); err != nil { + return nil, fmt.Errorf("failed to stop container %s: %v", c.ID(), err) + } } resp := &pb.StopContainerResponse{} diff --git a/server/sandbox_stop.go b/server/sandbox_stop.go index 42dac4ca..9853b17c 100644 --- a/server/sandbox_stop.go +++ b/server/sandbox_stop.go @@ -34,6 +34,9 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque containers = append(containers, podInfraContainer) for _, c := range containers { + if err := s.runtime.UpdateStatus(c); err != nil { + return nil, err + } cStatus := s.runtime.ContainerStatus(c) if cStatus.Status != oci.ContainerStateStopped { if err := s.runtime.StopContainer(c); err != nil { diff --git a/test/ctr.bats b/test/ctr.bats index e4857853..e44287ee 100644 --- a/test/ctr.bats +++ b/test/ctr.bats @@ -388,3 +388,33 @@ function teardown() { cleanup_pods stop_ocid } + +@test "ctr stop idempotent" { + # this test requires docker, thus it can't yet be run in a container + if [ "$TRAVIS" = "true" ]; then # instead of $TRAVIS, add a function is_containerized to skip here + skip "cannot yet run this test in a container, use sudo make localintegration" + fi + + start_ocid + run ocic pod create --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run ocic ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + ctr_id="$output" + run ocic ctr start --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic ctr stop --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic ctr stop --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + + cleanup_ctrs + cleanup_pods + stop_ocid +} diff --git a/test/pod.bats b/test/pod.bats index e33db963..087fa274 100644 --- a/test/pod.bats +++ b/test/pod.bats @@ -227,3 +227,53 @@ function teardown() { cleanup_pods stop_ocid } + +@test "pod stop idempotent" { + # this test requires docker, thus it can't yet be run in a container + if [ "$TRAVIS" = "true" ]; then # instead of $TRAVIS, add a function is_containerized to skip here + skip "cannot yet run this test in a container, use sudo make localintegration" + fi + + start_ocid + run ocic pod create --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run ocic pod stop --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic pod stop --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + + cleanup_ctrs + cleanup_pods + stop_ocid +} + +@test "pod stop idempotent with ctrs already stopped" { + # this test requires docker, thus it can't yet be run in a container + if [ "$TRAVIS" = "true" ]; then # instead of $TRAVIS, add a function is_containerized to skip here + skip "cannot yet run this test in a container, use sudo make localintegration" + fi + + start_ocid + run ocic pod create --config "$TESTDATA"/sandbox_config.json + echo "$output" + [ "$status" -eq 0 ] + pod_id="$output" + run ocic ctr create --config "$TESTDATA"/container_config.json --pod "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + ctr_id="$output" + run ocic ctr start --id "$ctr_id" + echo "$output" + [ "$status" -eq 0 ] + run ocic pod stop --id "$pod_id" + echo "$output" + [ "$status" -eq 0 ] + + cleanup_ctrs + cleanup_pods + stop_ocid +}