plugin_push_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/errdefs"
  11. )
  12. func TestPluginPushError(t *testing.T) {
  13. client := &Client{
  14. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  15. }
  16. _, err := client.PluginPush(context.Background(), "plugin_name", "")
  17. if err == nil || err.Error() != "Error response from daemon: Server error" {
  18. t.Fatalf("expected a Server Error, got %v", err)
  19. }
  20. if !errdefs.IsSystem(err) {
  21. t.Fatalf("expected a Server Error, got %T", err)
  22. }
  23. }
  24. func TestPluginPush(t *testing.T) {
  25. expectedURL := "/plugins/plugin_name"
  26. client := &Client{
  27. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  28. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  29. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  30. }
  31. if req.Method != "POST" {
  32. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  33. }
  34. auth := req.Header.Get("X-Registry-Auth")
  35. if auth != "authtoken" {
  36. return nil, fmt.Errorf("Invalid auth header : expected 'authtoken', got %s", auth)
  37. }
  38. return &http.Response{
  39. StatusCode: http.StatusOK,
  40. Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
  41. }, nil
  42. }),
  43. }
  44. _, err := client.PluginPush(context.Background(), "plugin_name", "authtoken")
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. }