-
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.
feature: add wait client command for pouch
Signed-off-by: xiechengsheng <[email protected]>
- Loading branch information
1 parent
dfdb8fa
commit 6fbcf9c
Showing
16 changed files
with
512 additions
and
1 deletion.
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 initializes 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 { | ||
response, err := apiClient.ContainerWait(ctx, name) | ||
if err != nil { | ||
errs = append(errs, err.Error()) | ||
continue | ||
} | ||
fmt.Printf("%d\n", response.StatusCode) | ||
} | ||
|
||
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,22 @@ | ||
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) (types.ContainerWaitOKBody, error) { | ||
resp, err := client.post(ctx, "/containers/"+name+"/wait", nil, nil, nil) | ||
|
||
if err != nil { | ||
return types.ContainerWaitOKBody{}, err | ||
} | ||
|
||
var response types.ContainerWaitOKBody | ||
err = decodeBody(&response, resp.Body) | ||
ensureCloseReader(resp) | ||
return response, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
func TestContainerWaitError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
_, err := client.ContainerWait(context.Background(), "nothing") | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerWaitNotFoundError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusNotFound, "Not Found")), | ||
} | ||
_, err := client.ContainerWait(context.Background(), "no container") | ||
if err == nil || !strings.Contains(err.Error(), "Not Found") { | ||
t.Fatalf("expected a Not Found Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerWait(t *testing.T) { | ||
expectedURL := "/containers/container_id" | ||
|
||
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) { | ||
if !strings.HasPrefix(req.URL.Path, expectedURL) { | ||
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) | ||
} | ||
waitJSON := types.ContainerWaitOKBody{ | ||
Error: "", | ||
StatusCode: 0, | ||
} | ||
b, err := json.Marshal(waitJSON) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), | ||
}, nil | ||
}) | ||
|
||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
|
||
_, err := client.ContainerWait(context.Background(), "container_id") | ||
if err != nil { | ||
t.Fatal(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
Oops, something went wrong.