github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/container_remove_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "gotest.tools/assert" 14 is "gotest.tools/assert/cmp" 15 ) 16 17 func TestContainerRemoveError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{}) 22 assert.Check(t, is.Error(err, "Error response from daemon: Server error")) 23 } 24 25 func TestContainerRemoveNotFoundError(t *testing.T) { 26 client := &Client{ 27 client: newMockClient(errorMock(http.StatusNotFound, "missing")), 28 } 29 err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{}) 30 assert.Check(t, is.Error(err, "Error: No such container: container_id")) 31 assert.Check(t, IsErrNotFound(err)) 32 } 33 34 func TestContainerRemove(t *testing.T) { 35 expectedURL := "/containers/container_id" 36 client := &Client{ 37 client: newMockClient(func(req *http.Request) (*http.Response, error) { 38 if !strings.HasPrefix(req.URL.Path, expectedURL) { 39 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 40 } 41 query := req.URL.Query() 42 volume := query.Get("v") 43 if volume != "1" { 44 return nil, fmt.Errorf("v (volume) not set in URL query properly. Expected '1', got %s", volume) 45 } 46 force := query.Get("force") 47 if force != "1" { 48 return nil, fmt.Errorf("force not set in URL query properly. Expected '1', got %s", force) 49 } 50 link := query.Get("link") 51 if link != "" { 52 return nil, fmt.Errorf("link should have not be present in query, go %s", link) 53 } 54 return &http.Response{ 55 StatusCode: http.StatusOK, 56 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 57 }, nil 58 }), 59 } 60 61 err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{ 62 RemoveVolumes: true, 63 Force: true, 64 }) 65 assert.Check(t, err) 66 }