github.com/moby/docker@v26.1.3+incompatible/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" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types/container" 13 "github.com/docker/docker/errdefs" 14 "gotest.tools/v3/assert" 15 is "gotest.tools/v3/assert/cmp" 16 ) 17 18 func TestContainerRemoveError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{}) 23 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 24 } 25 26 func TestContainerRemoveNotFoundError(t *testing.T) { 27 client := &Client{ 28 client: newMockClient(errorMock(http.StatusNotFound, "no such container: container_id")), 29 } 30 err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{}) 31 assert.Check(t, is.ErrorContains(err, "no such container: container_id")) 32 assert.Check(t, is.ErrorType(err, errdefs.IsNotFound)) 33 } 34 35 func TestContainerRemove(t *testing.T) { 36 expectedURL := "/containers/container_id" 37 client := &Client{ 38 client: newMockClient(func(req *http.Request) (*http.Response, error) { 39 if !strings.HasPrefix(req.URL.Path, expectedURL) { 40 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 41 } 42 query := req.URL.Query() 43 volume := query.Get("v") 44 if volume != "1" { 45 return nil, fmt.Errorf("v (volume) not set in URL query properly. Expected '1', got %s", volume) 46 } 47 force := query.Get("force") 48 if force != "1" { 49 return nil, fmt.Errorf("force not set in URL query properly. Expected '1', got %s", force) 50 } 51 link := query.Get("link") 52 if link != "" { 53 return nil, fmt.Errorf("link should have not be present in query, go %s", link) 54 } 55 return &http.Response{ 56 StatusCode: http.StatusOK, 57 Body: io.NopCloser(bytes.NewReader([]byte(""))), 58 }, nil 59 }), 60 } 61 62 err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{ 63 RemoveVolumes: true, 64 Force: true, 65 }) 66 assert.Check(t, err) 67 }