config_create_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  32. }
  33. func TestConfigCreate(t *testing.T) {
  34. expectedURL := "/v1.30/configs/create"
  35. client := &Client{
  36. version: "1.30",
  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. if req.Method != http.MethodPost {
  42. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  43. }
  44. b, err := json.Marshal(types.ConfigCreateResponse{
  45. ID: "test_config",
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &http.Response{
  51. StatusCode: http.StatusCreated,
  52. Body: io.NopCloser(bytes.NewReader(b)),
  53. }, nil
  54. }),
  55. }
  56. r, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if r.ID != "test_config" {
  61. t.Fatalf("expected `test_config`, got %s", r.ID)
  62. }
  63. }