plugin_list_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/filters"
  13. "github.com/docker/docker/errdefs"
  14. )
  15. func TestPluginListError(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  18. }
  19. _, err := client.PluginList(context.Background(), filters.NewArgs())
  20. if !errdefs.IsSystem(err) {
  21. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  22. }
  23. }
  24. func TestPluginList(t *testing.T) {
  25. expectedURL := "/plugins"
  26. enabledFilters := filters.NewArgs()
  27. enabledFilters.Add("enabled", "true")
  28. capabilityFilters := filters.NewArgs()
  29. capabilityFilters.Add("capability", "volumedriver")
  30. capabilityFilters.Add("capability", "authz")
  31. listCases := []struct {
  32. filters filters.Args
  33. expectedQueryParams map[string]string
  34. }{
  35. {
  36. filters: filters.NewArgs(),
  37. expectedQueryParams: map[string]string{
  38. "all": "",
  39. "filter": "",
  40. "filters": "",
  41. },
  42. },
  43. {
  44. filters: enabledFilters,
  45. expectedQueryParams: map[string]string{
  46. "all": "",
  47. "filter": "",
  48. "filters": `{"enabled":{"true":true}}`,
  49. },
  50. },
  51. {
  52. filters: capabilityFilters,
  53. expectedQueryParams: map[string]string{
  54. "all": "",
  55. "filter": "",
  56. "filters": `{"capability":{"authz":true,"volumedriver":true}}`,
  57. },
  58. },
  59. }
  60. for _, listCase := range listCases {
  61. client := &Client{
  62. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  63. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  64. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  65. }
  66. query := req.URL.Query()
  67. for key, expected := range listCase.expectedQueryParams {
  68. actual := query.Get(key)
  69. if actual != expected {
  70. return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
  71. }
  72. }
  73. content, err := json.Marshal([]*types.Plugin{
  74. {
  75. ID: "plugin_id1",
  76. },
  77. {
  78. ID: "plugin_id2",
  79. },
  80. })
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &http.Response{
  85. StatusCode: http.StatusOK,
  86. Body: io.NopCloser(bytes.NewReader(content)),
  87. }, nil
  88. }),
  89. }
  90. plugins, err := client.PluginList(context.Background(), listCase.filters)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. if len(plugins) != 2 {
  95. t.Fatalf("expected 2 plugins, got %v", plugins)
  96. }
  97. }
  98. }