config_create_test.go 1.8 KB

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