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

65 lines
1.9 KiB
Go

package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/plugin"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestPluginInspectError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, _, err = client.PluginInspectWithRaw(context.Background(), "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestPluginInspectWithEmptyID(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("should not make request")
}))
assert.NilError(t, err)
_, _, err = client.PluginInspectWithRaw(context.Background(), "")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, _, err = client.PluginInspectWithRaw(context.Background(), " ")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
func TestPluginInspect(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)
}
content, err := json.Marshal(plugin.Plugin{
ID: "plugin_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
}))
assert.NilError(t, err)
pluginInspect, _, err := client.PluginInspectWithRaw(context.Background(), "plugin_name")
assert.NilError(t, err)
assert.Check(t, is.Equal(pluginInspect.ID, "plugin_id"))
}