container_rename_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/errdefs"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. func TestContainerRenameError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. err := client.ContainerRename(context.Background(), "nothing", "newNothing")
  19. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  20. }
  21. func TestContainerRename(t *testing.T) {
  22. expectedURL := "/containers/container_id/rename"
  23. client := &Client{
  24. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  25. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  26. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  27. }
  28. name := req.URL.Query().Get("name")
  29. if name != "newName" {
  30. return nil, fmt.Errorf("name not set in URL query properly. Expected 'newName', got %s", name)
  31. }
  32. return &http.Response{
  33. StatusCode: http.StatusOK,
  34. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  35. }, nil
  36. }),
  37. }
  38. err := client.ContainerRename(context.Background(), "container_id", "newName")
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. }