Files
moby/client/plugin_push_test.go
2025-08-29 15:17:23 +02:00

58 lines
1.7 KiB
Go

package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/registry"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestPluginPushError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.PluginPush(context.Background(), "plugin_name", "")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.PluginPush(context.Background(), "", "")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, err = client.PluginPush(context.Background(), " ", "")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
func TestPluginPush(t *testing.T) {
expectedURL := "/plugins/plugin_name"
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}
auth := req.Header.Get(registry.AuthHeader)
if auth != "authtoken" {
return nil, fmt.Errorf("invalid auth header: expected 'authtoken', got %s", auth)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}))
assert.NilError(t, err)
_, err = client.PluginPush(context.Background(), "plugin_name", "authtoken")
assert.NilError(t, err)
}