plugin_inspect_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/errdefs"
  13. "github.com/pkg/errors"
  14. )
  15. func TestPluginInspectError(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  18. }
  19. _, _, err := client.PluginInspectWithRaw(context.Background(), "nothing")
  20. if !errdefs.IsSystem(err) {
  21. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  22. }
  23. }
  24. func TestPluginInspectWithEmptyID(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. return nil, errors.New("should not make request")
  28. }),
  29. }
  30. _, _, err := client.PluginInspectWithRaw(context.Background(), "")
  31. if !IsErrNotFound(err) {
  32. t.Fatalf("Expected NotFoundError, got %v", err)
  33. }
  34. }
  35. func TestPluginInspect(t *testing.T) {
  36. expectedURL := "/plugins/plugin_name"
  37. client := &Client{
  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. content, err := json.Marshal(types.Plugin{
  43. ID: "plugin_id",
  44. })
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &http.Response{
  49. StatusCode: http.StatusOK,
  50. Body: io.NopCloser(bytes.NewReader(content)),
  51. }, nil
  52. }),
  53. }
  54. pluginInspect, _, err := client.PluginInspectWithRaw(context.Background(), "plugin_name")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if pluginInspect.ID != "plugin_id" {
  59. t.Fatalf("expected `plugin_id`, got %s", pluginInspect.ID)
  60. }
  61. }