config_create_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/swarm"
  13. "github.com/docker/docker/errdefs"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. )
  17. func TestConfigCreateUnsupported(t *testing.T) {
  18. client := &Client{
  19. version: "1.29",
  20. client: &http.Client{},
  21. }
  22. _, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
  23. assert.Check(t, is.Error(err, `"config create" requires API version 1.30, but the Docker daemon API version is 1.29`))
  24. }
  25. func TestConfigCreateError(t *testing.T) {
  26. client := &Client{
  27. version: "1.30",
  28. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  29. }
  30. _, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
  31. if !errdefs.IsSystem(err) {
  32. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  33. }
  34. }
  35. func TestConfigCreate(t *testing.T) {
  36. expectedURL := "/v1.30/configs/create"
  37. client := &Client{
  38. version: "1.30",
  39. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  40. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  41. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  42. }
  43. if req.Method != http.MethodPost {
  44. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  45. }
  46. b, err := json.Marshal(types.ConfigCreateResponse{
  47. ID: "test_config",
  48. })
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &http.Response{
  53. StatusCode: http.StatusCreated,
  54. Body: io.NopCloser(bytes.NewReader(b)),
  55. }, nil
  56. }),
  57. }
  58. r, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if r.ID != "test_config" {
  63. t.Fatalf("expected `test_config`, got %s", r.ID)
  64. }
  65. }