-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1064 from Dewey-Ding/master
test:add container remove and stop test
- Loading branch information
Showing
5 changed files
with
140 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
) | ||
|
||
// ContainerRemove removes a container. | ||
func (client *APIClient) ContainerRemove(ctx context.Context, name string, force bool) error { | ||
q := url.Values{} | ||
if force { | ||
q.Set("force", "true") | ||
} | ||
|
||
resp, err := client.delete(ctx, "/containers/"+name, q, nil) | ||
if err != nil { | ||
return err | ||
} | ||
ensureCloseReader(resp) | ||
return nil | ||
} |
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,56 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestContainerRemoveError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
err := client.ContainerRemove(context.Background(), "nothing", true) | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerRemoveNotFoundError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusNotFound, "Not Found")), | ||
} | ||
err := client.ContainerRemove(context.Background(), "no contaienr", true) | ||
if err == nil || !strings.Contains(err.Error(), "Not Found") { | ||
t.Fatalf("expected a Not Found Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerRemove(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) | ||
} | ||
force := req.URL.Query().Get("force") | ||
if force != "true" { | ||
return nil, fmt.Errorf("force not set in URL properly. Expected 'true', got %s", force) | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusNoContent, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), | ||
}, nil | ||
}) | ||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
err := client.ContainerRemove(context.Background(), "container_id", true) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
) | ||
|
||
// ContainerStop stops a container. | ||
func (client *APIClient) ContainerStop(ctx context.Context, name string, timeout string) error { | ||
q := url.Values{} | ||
q.Add("t", timeout) | ||
|
||
resp, err := client.post(ctx, "/containers/"+name+"/stop", q, nil, nil) | ||
ensureCloseReader(resp) | ||
|
||
return 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,46 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestContainerStopError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
err := client.ContainerStop(context.Background(), "nothing", "10") | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerStop(t *testing.T) { | ||
expectedURL := "/containers/container_id/stop" | ||
|
||
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) | ||
} | ||
timeout := req.URL.Query().Get("t") | ||
if timeout != "10" { | ||
return nil, fmt.Errorf("timeout not set in URL properly. Expected '10', got %s", timeout) | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), | ||
}, nil | ||
}) | ||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
err := client.ContainerStop(context.Background(), "container_id", "10") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |