config_list_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/filters"
  12. "github.com/docker/docker/api/types/swarm"
  13. "github.com/stretchr/testify/assert"
  14. "golang.org/x/net/context"
  15. )
  16. func TestConfigListUnsupported(t *testing.T) {
  17. client := &Client{
  18. version: "1.29",
  19. client: &http.Client{},
  20. }
  21. _, err := client.ConfigList(context.Background(), types.ConfigListOptions{})
  22. assert.EqualError(t, err, `"config list" requires API version 1.30, but the Docker daemon API version is 1.29`)
  23. }
  24. func TestConfigListError(t *testing.T) {
  25. client := &Client{
  26. version: "1.30",
  27. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  28. }
  29. _, err := client.ConfigList(context.Background(), types.ConfigListOptions{})
  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 TestConfigList(t *testing.T) {
  35. expectedURL := "/v1.30/configs"
  36. filters := filters.NewArgs()
  37. filters.Add("label", "label1")
  38. filters.Add("label", "label2")
  39. listCases := []struct {
  40. options types.ConfigListOptions
  41. expectedQueryParams map[string]string
  42. }{
  43. {
  44. options: types.ConfigListOptions{},
  45. expectedQueryParams: map[string]string{
  46. "filters": "",
  47. },
  48. },
  49. {
  50. options: types.ConfigListOptions{
  51. Filters: filters,
  52. },
  53. expectedQueryParams: map[string]string{
  54. "filters": `{"label":{"label1":true,"label2":true}}`,
  55. },
  56. },
  57. }
  58. for _, listCase := range listCases {
  59. client := &Client{
  60. version: "1.30",
  61. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  62. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  63. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  64. }
  65. query := req.URL.Query()
  66. for key, expected := range listCase.expectedQueryParams {
  67. actual := query.Get(key)
  68. if actual != expected {
  69. return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
  70. }
  71. }
  72. content, err := json.Marshal([]swarm.Config{
  73. {
  74. ID: "config_id1",
  75. },
  76. {
  77. ID: "config_id2",
  78. },
  79. })
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &http.Response{
  84. StatusCode: http.StatusOK,
  85. Body: ioutil.NopCloser(bytes.NewReader(content)),
  86. }, nil
  87. }),
  88. }
  89. configs, err := client.ConfigList(context.Background(), listCase.options)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if len(configs) != 2 {
  94. t.Fatalf("expected 2 configs, got %v", configs)
  95. }
  96. }
  97. }