-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c29c40
commit 303d023
Showing
14 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// waitDescription is used to describe wait command in detail and auto generate command doc. | ||
var waitDescription = "Block until one or more containers stop, then print their exit codes. " + | ||
"If container state is already stopped, the command will return exit code immediately. " + | ||
"On a successful stop, the exit code of the container is returned. " | ||
|
||
// WaitCommand is used to implement 'wait' command. | ||
type WaitCommand struct { | ||
baseCommand | ||
} | ||
|
||
// Init initialize wait command. | ||
func (wait *WaitCommand) Init(c *Cli) { | ||
wait.cli = c | ||
wait.cmd = &cobra.Command{ | ||
Use: "wait CONTAINER [CONTAINER...]", | ||
Short: "Block until one or more containers stop, then print their exit codes", | ||
Long: waitDescription, | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return wait.runWait(args) | ||
}, | ||
Example: waitExamples(), | ||
} | ||
} | ||
|
||
// runWait is the entry of wait command. | ||
func (wait *WaitCommand) runWait(args []string) error { | ||
ctx := context.Background() | ||
apiClient := wait.cli.Client() | ||
|
||
var errs []string | ||
for _, name := range args { | ||
status, err := apiClient.ContainerWait(ctx, name) | ||
if err != nil { | ||
errs = append(errs, name) | ||
continue | ||
} | ||
fmt.Printf("%d\n", status) | ||
} | ||
|
||
if len(errs) > 0 { | ||
return errors.New(strings.Join(errs, "\n")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// waitExamples shows examples in wait command, and is used in auto-generated cli docs. | ||
func waitExamples() string { | ||
return `$ pouch ps | ||
Name ID Status Created Image Runtime | ||
foo f6717e Up 2 seconds 3 seconds ago registry.hub.docker.com/library/busybox:latest runc | ||
$ pouch stop foo | ||
$ pouch ps -a | ||
Name ID Status Created Image Runtime | ||
foo f6717e Stopped (0) 1 minute 2 minutes ago registry.hub.docker.com/library/busybox:latest runc | ||
$ pouch wait foo | ||
0` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
// ContainerWait pauses execution until a container exits. | ||
// It returns the API status code as response of its readiness. | ||
func (client *APIClient) ContainerWait(ctx context.Context, name string) (int64, error) { | ||
resp, err := client.post(ctx, "/containers/"+name+"/wait", nil, nil, nil) | ||
if err != nil { | ||
return -1, err | ||
} | ||
|
||
var response types.ContainerWaitOKBody | ||
err = decodeBody(&response, resp.Body) | ||
ensureCloseReader(resp) | ||
return response.StatusCode, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/alibaba/pouch/test/environment" | ||
"github.com/alibaba/pouch/test/request" | ||
|
||
"github.com/go-check/check" | ||
) | ||
|
||
// APIContainerWaitSuite is the test suite for container wait API. | ||
type APIContainerWaitSuite struct{} | ||
|
||
func init() { | ||
check.Suite(&APIContainerWaitSuite{}) | ||
} | ||
|
||
// SetUpTest does common setup in the beginning of each test. | ||
func (suite *APIContainerWaitSuite) SetUpTest(c *check.C) { | ||
SkipIfFalse(c, environment.IsLinux) | ||
PullImage(c, busyboxImage) | ||
} | ||
|
||
// TestWaitOk tests waiting a stopped container return 200. | ||
func (suite *APIContainerWaitSuite) TestWaitOk(c *check.C) { | ||
cname := "TestWaitOk" | ||
|
||
CreateBusyboxContainerOk(c, cname) | ||
StartContainerOk(c, cname) | ||
StopContainerOk(c, cname) | ||
|
||
resp, err := request.Post("/containers/" + cname + "/wait") | ||
c.Assert(err, check.IsNil) | ||
CheckRespStatus(c, resp, 200) | ||
|
||
DelContainerForceOk(c, cname) | ||
} | ||
|
||
// TestNonExistingContainer tests waiting a non-existing container return 404. | ||
func (suite *APIContainerWaitSuite) TestNonExistingContainer(c *check.C) { | ||
cname := "TestNonExistingContainer" | ||
resp, err := request.Post("/containers/" + cname + "/wait") | ||
c.Assert(err, check.IsNil) | ||
CheckRespStatus(c, resp, 404) | ||
} |
Oops, something went wrong.