plugin_push_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types/registry"
  11. "github.com/docker/docker/errdefs"
  12. "gotest.tools/v3/assert"
  13. is "gotest.tools/v3/assert/cmp"
  14. )
  15. func TestPluginPushError(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  18. }
  19. _, err := client.PluginPush(context.Background(), "plugin_name", "")
  20. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  21. }
  22. func TestPluginPush(t *testing.T) {
  23. expectedURL := "/plugins/plugin_name"
  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. if req.Method != http.MethodPost {
  30. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  31. }
  32. auth := req.Header.Get(registry.AuthHeader)
  33. if auth != "authtoken" {
  34. return nil, fmt.Errorf("invalid auth header: expected 'authtoken', got %s", auth)
  35. }
  36. return &http.Response{
  37. StatusCode: http.StatusOK,
  38. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  39. }, nil
  40. }),
  41. }
  42. _, err := client.PluginPush(context.Background(), "plugin_name", "authtoken")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. }