From 2f5c0afbbcea06452b3c56d8d928f5036452b681 Mon Sep 17 00:00:00 2001 From: Wang Long Date: Sat, 24 Sep 2016 13:25:13 +0000 Subject: [PATCH] pause and resume multi-containers With this patch, `runc pasue` and `runc resume` can pause and resume multi-containers. Signed-off-by: Wang Long --- man/runc-pause.8.md | 2 +- man/runc-resume.8.md | 2 +- pause.go | 61 +++++++++++++++++++++++----- tests/integration/pause.bats | 78 ++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 11 deletions(-) diff --git a/man/runc-pause.8.md b/man/runc-pause.8.md index 4a4265e8..489bb907 100644 --- a/man/runc-pause.8.md +++ b/man/runc-pause.8.md @@ -2,7 +2,7 @@ runc pause - pause suspends all processes inside the container # SYNOPSIS - runc pause + runc pause [container-id...] Where "" is the name for the instance of the container to be paused. diff --git a/man/runc-resume.8.md b/man/runc-resume.8.md index 627885f1..d864a542 100644 --- a/man/runc-resume.8.md +++ b/man/runc-resume.8.md @@ -2,7 +2,7 @@ runc resume - resumes all processes that have been previously paused # SYNOPSIS - runc resume + runc resume [container-id...] Where "" is the name for the instance of the container to be resumed. diff --git a/pause.go b/pause.go index 22c2164d..ffdd984e 100644 --- a/pause.go +++ b/pause.go @@ -2,12 +2,17 @@ package main -import "github.com/urfave/cli" +import ( + "fmt" + "os" + + "github.com/urfave/cli" +) var pauseCommand = cli.Command{ Name: "pause", Usage: "pause suspends all processes inside the container", - ArgsUsage: ` + ArgsUsage: ` [container-id...] Where "" is the name for the instance of the container to be paused. `, @@ -15,12 +20,31 @@ paused. `, Use runc list to identiy instances of containers and their current status.`, Action: func(context *cli.Context) error { - container, err := getContainer(context) + hasError := false + if !context.Args().Present() { + return fmt.Errorf("runc: \"pause\" requires a minimum of 1 argument") + } + + factory, err := loadFactory(context) if err != nil { return err } - if err := container.Pause(); err != nil { - return err + + for _, id := range context.Args() { + container, err := factory.Load(id) + if err != nil { + fmt.Fprintf(os.Stderr, "container %s is not exist\n", id) + hasError = true + continue + } + if err := container.Pause(); err != nil { + fmt.Fprintf(os.Stderr, "pause container %s : %s\n", id, err) + hasError = true + } + } + + if hasError { + return fmt.Errorf("one or more of container pause failed") } return nil }, @@ -29,7 +53,7 @@ Use runc list to identiy instances of containers and their current status.`, var resumeCommand = cli.Command{ Name: "resume", Usage: "resumes all processes that have been previously paused", - ArgsUsage: ` + ArgsUsage: ` [container-id...] Where "" is the name for the instance of the container to be resumed.`, @@ -37,12 +61,31 @@ resumed.`, Use runc list to identiy instances of containers and their current status.`, Action: func(context *cli.Context) error { - container, err := getContainer(context) + hasError := false + if !context.Args().Present() { + return fmt.Errorf("runc: \"resume\" requires a minimum of 1 argument") + } + + factory, err := loadFactory(context) if err != nil { return err } - if err := container.Resume(); err != nil { - return err + + for _, id := range context.Args() { + container, err := factory.Load(id) + if err != nil { + fmt.Fprintf(os.Stderr, "container %s is not exist\n", id) + hasError = true + continue + } + if err := container.Resume(); err != nil { + fmt.Fprintf(os.Stderr, "resume container %s : %s\n", id, err) + hasError = true + } + } + + if hasError { + return fmt.Errorf("one or more of container resume failed") } return nil }, diff --git a/tests/integration/pause.bats b/tests/integration/pause.bats index ed12f4a6..02642f43 100644 --- a/tests/integration/pause.bats +++ b/tests/integration/pause.bats @@ -32,3 +32,81 @@ function teardown() { # test state of busybox is back to running testcontainer test_busybox running } + +@test "runc pause and resume with multi-container" { + # run test_busybox1 detached + runc run -d --console /dev/pts/ptmx test_busybox1 + [ "$status" -eq 0 ] + + wait_for_container 15 1 test_busybox1 + + # run test_busybox2 detached + runc run -d --console /dev/pts/ptmx test_busybox2 + [ "$status" -eq 0 ] + + wait_for_container 15 1 test_busybox2 + + # pause test_busybox1 and test_busybox2 + runc pause test_busybox1 test_busybox2 + [ "$status" -eq 0 ] + + # test state of test_busybox1 and test_busybox2 is paused + testcontainer test_busybox1 paused + testcontainer test_busybox2 paused + + # resume test_busybox1 and test_busybox2 + runc resume test_busybox1 test_busybox2 + [ "$status" -eq 0 ] + + # test state of two containers is back to running + testcontainer test_busybox1 running + testcontainer test_busybox2 running + + # delete test_busybox1 and test_busybox2 + runc delete --force test_busybox1 test_busybox2 + + runc state test_busybox1 + [ "$status" -ne 0 ] + + runc state test_busybox2 + [ "$status" -ne 0 ] +} + +@test "runc pause and resume with nonexist container" { + # run test_busybox1 detached + runc run -d --console /dev/pts/ptmx test_busybox1 + [ "$status" -eq 0 ] + + wait_for_container 15 1 test_busybox1 + + # run test_busybox2 detached + runc run -d --console /dev/pts/ptmx test_busybox2 + [ "$status" -eq 0 ] + + wait_for_container 15 1 test_busybox2 + + # pause test_busybox1, test_busybox2 and nonexistant container + runc pause test_busybox1 test_busybox2 nonexistant + [ "$status" -ne 0 ] + + # test state of test_busybox1 and test_busybox2 is paused + testcontainer test_busybox1 paused + testcontainer test_busybox2 paused + + # resume test_busybox1, test_busybox2 and nonexistant container + runc resume test_busybox1 test_busybox2 nonexistant + [ "$status" -ne 0 ] + + # test state of two containers is back to running + testcontainer test_busybox1 running + testcontainer test_busybox2 running + + # delete test_busybox1 and test_busybox2 + runc delete --force test_busybox1 test_busybox2 + + runc state test_busybox1 + [ "$status" -ne 0 ] + + runc state test_busybox2 + [ "$status" -ne 0 ] +}