plugin_list_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. "golang.org/x/net/context"
  12. )
  13. func TestPluginListError(t *testing.T) {
  14. client := &Client{
  15. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  16. }
  17. _, err := client.PluginList(context.Background())
  18. if err == nil || err.Error() != "Error response from daemon: Server error" {
  19. t.Fatalf("expected a Server Error, got %v", err)
  20. }
  21. }
  22. func TestPluginList(t *testing.T) {
  23. expectedURL := "/plugins"
  24. client := &Client{
  25. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  26. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  27. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  28. }
  29. content, err := json.Marshal([]*types.Plugin{
  30. {
  31. ID: "plugin_id1",
  32. },
  33. {
  34. ID: "plugin_id2",
  35. },
  36. })
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &http.Response{
  41. StatusCode: http.StatusOK,
  42. Body: ioutil.NopCloser(bytes.NewReader(content)),
  43. }, nil
  44. }),
  45. }
  46. plugins, err := client.PluginList(context.Background())
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. if len(plugins) != 2 {
  51. t.Fatalf("expected 2 plugins, got %v", plugins)
  52. }
  53. }