client: PluginInspectWithRaw: refactor and rename to PluginInspect

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-21 23:14:08 +02:00
parent 2401bd1e12
commit ebc92e015a
11 changed files with 63 additions and 58 deletions

View File

@@ -154,7 +154,7 @@ type PluginAPIClient interface {
PluginUpgrade(ctx context.Context, name string, options PluginInstallOptions) (io.ReadCloser, error)
PluginPush(ctx context.Context, name string, registryAuth string) (io.ReadCloser, error)
PluginSet(ctx context.Context, name string, args []string) error
PluginInspectWithRaw(ctx context.Context, name string) (*plugin.Plugin, []byte, error)
PluginInspect(ctx context.Context, name string, options PluginInspectOptions) (PluginInspectResult, error)
PluginCreate(ctx context.Context, createContext io.Reader, options PluginCreateOptions) error
}

View File

@@ -1,32 +1,35 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"github.com/moby/moby/api/types/plugin"
)
// PluginInspectWithRaw inspects an existing plugin
func (cli *Client) PluginInspectWithRaw(ctx context.Context, name string) (*plugin.Plugin, []byte, error) {
// PluginInspectOptions holds parameters to inspect a plugin.
type PluginInspectOptions struct {
// Add future optional parameters here
}
// PluginInspectResult holds the result from the [Client.PluginInspect] method.
type PluginInspectResult struct {
Raw []byte
Plugin plugin.Plugin
}
// PluginInspect inspects an existing plugin
func (cli *Client) PluginInspect(ctx context.Context, name string, options PluginInspectOptions) (PluginInspectResult, error) {
name, err := trimID("plugin", name)
if err != nil {
return nil, nil, err
return PluginInspectResult{}, err
}
resp, err := cli.get(ctx, "/plugins/"+name+"/json", nil, nil)
defer ensureReaderClosed(resp)
if err != nil {
return nil, nil, err
return PluginInspectResult{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
var p plugin.Plugin
rdr := bytes.NewReader(body)
err = json.NewDecoder(rdr).Decode(&p)
return &p, body, err
var out PluginInspectResult
out.Raw, err = decodeWithRaw(resp, &out.Plugin)
return out, err
}

View File

@@ -2,7 +2,6 @@ package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
@@ -19,7 +18,7 @@ func TestPluginInspectError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, _, err = client.PluginInspectWithRaw(context.Background(), "nothing")
_, err = client.PluginInspect(t.Context(), "nothing", PluginInspectOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
@@ -28,11 +27,11 @@ func TestPluginInspectWithEmptyID(t *testing.T) {
return nil, errors.New("should not make request")
}))
assert.NilError(t, err)
_, _, err = client.PluginInspectWithRaw(context.Background(), "")
_, err = client.PluginInspect(t.Context(), "", PluginInspectOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, _, err = client.PluginInspectWithRaw(context.Background(), " ")
_, err = client.PluginInspect(t.Context(), " ", PluginInspectOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
@@ -56,7 +55,7 @@ func TestPluginInspect(t *testing.T) {
}))
assert.NilError(t, err)
pluginInspect, _, err := client.PluginInspectWithRaw(context.Background(), "plugin_name")
resp, err := client.PluginInspect(t.Context(), "plugin_name", PluginInspectOptions{})
assert.NilError(t, err)
assert.Check(t, is.Equal(pluginInspect.ID, "plugin_id"))
assert.Check(t, is.Equal(resp.Plugin.ID, "plugin_id"))
}

View File

@@ -67,12 +67,12 @@ func (d *Daemon) CheckServiceUpdateState(ctx context.Context, service string) fu
func (d *Daemon) CheckPluginRunning(ctx context.Context, plugin string) func(c *testing.T) (any, string) {
return func(t *testing.T) (any, string) {
apiclient := d.NewClientT(t)
resp, _, err := apiclient.PluginInspectWithRaw(ctx, plugin)
resp, err := apiclient.PluginInspect(ctx, plugin, client.PluginInspectOptions{})
if cerrdefs.IsNotFound(err) {
return false, fmt.Sprintf("%v", err)
}
assert.NilError(t, err)
return resp.Enabled, fmt.Sprintf("%+v", resp)
return resp.Plugin.Enabled, fmt.Sprintf("%+v", resp.Plugin)
}
}
@@ -80,12 +80,12 @@ func (d *Daemon) CheckPluginRunning(ctx context.Context, plugin string) func(c *
func (d *Daemon) CheckPluginImage(ctx context.Context, plugin string) func(c *testing.T) (any, string) {
return func(t *testing.T) (any, string) {
apiclient := d.NewClientT(t)
resp, _, err := apiclient.PluginInspectWithRaw(ctx, plugin)
resp, err := apiclient.PluginInspect(ctx, plugin, client.PluginInspectOptions{})
if cerrdefs.IsNotFound(err) {
return false, fmt.Sprintf("%v", err)
}
assert.NilError(t, err)
return resp.PluginReference, fmt.Sprintf("%+v", resp)
return resp.Plugin.PluginReference, fmt.Sprintf("%+v", resp.Plugin)
}
}

View File

@@ -2225,11 +2225,11 @@ func (s *DockerDaemonSuite) TestFailedPluginRemove(c *testing.T) {
ctx, cancel = context.WithTimeout(testutil.GetContext(c), 30*time.Second)
defer cancel()
p, _, err := apiClient.PluginInspectWithRaw(ctx, name)
res, err := apiClient.PluginInspect(ctx, name, client.PluginInspectOptions{})
assert.NilError(c, err)
// simulate a bad/partial removal by removing the plugin config.
configPath := filepath.Join(d.Root, "plugins", p.ID, "config.json")
configPath := filepath.Join(d.Root, "plugins", res.Plugin.ID, "config.json")
assert.NilError(c, os.Remove(configPath))
d.Restart(c)
@@ -2238,7 +2238,7 @@ func (s *DockerDaemonSuite) TestFailedPluginRemove(c *testing.T) {
_, err = apiClient.Ping(ctx)
assert.NilError(c, err)
_, _, err = apiClient.PluginInspectWithRaw(ctx, name)
_, err = apiClient.PluginInspect(ctx, name, client.PluginInspectOptions{})
// plugin should be gone since the config.json is gone
assert.ErrorContains(c, err, "")
}

View File

@@ -120,7 +120,7 @@ func TestPluginInstall(t *testing.T) {
_, err = io.Copy(io.Discard, rdr)
assert.NilError(t, err)
_, _, err = apiclient.PluginInspectWithRaw(ctx, repo)
_, err = apiclient.PluginInspect(ctx, repo, client.PluginInspectOptions{})
assert.NilError(t, err)
})
@@ -163,7 +163,7 @@ func TestPluginInstall(t *testing.T) {
_, err = io.Copy(io.Discard, rdr)
assert.NilError(t, err)
_, _, err = apiclient.PluginInspectWithRaw(ctx, repo)
_, err = apiclient.PluginInspect(ctx, repo, client.PluginInspectOptions{})
assert.NilError(t, err)
})
@@ -192,7 +192,7 @@ func TestPluginInstall(t *testing.T) {
_, err = io.Copy(io.Discard, rdr)
assert.NilError(t, err)
_, _, err = apiclient.PluginInspectWithRaw(ctx, repo)
_, err = apiclient.PluginInspect(ctx, repo, client.PluginInspectOptions{})
assert.NilError(t, err)
})
t.Run("with insecure", func(t *testing.T) {
@@ -234,15 +234,15 @@ func TestPluginInstall(t *testing.T) {
repo := path.Join(regURL, name+":latest")
assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil, plugin.WithInsecureRegistry(regURL)))
apiclient := d.NewClientT(t)
rdr, err := apiclient.PluginInstall(ctx, repo, client.PluginInstallOptions{Disabled: true, RemoteRef: repo})
apiClient := d.NewClientT(t)
rdr, err := apiClient.PluginInstall(ctx, repo, client.PluginInstallOptions{Disabled: true, RemoteRef: repo})
assert.NilError(t, err)
defer rdr.Close()
_, err = io.Copy(io.Discard, rdr)
assert.NilError(t, err)
_, _, err = apiclient.PluginInspectWithRaw(ctx, repo)
_, err = apiClient.PluginInspect(ctx, repo, client.PluginInspectOptions{})
assert.NilError(t, err)
})
// TODO: test insecure registry with https

View File

@@ -54,7 +54,7 @@ func TestPluginWithDevMounts(t *testing.T) {
assert.Check(t, err)
}()
p, _, err := c.PluginInspectWithRaw(ctx, "test")
resp, err := c.PluginInspect(ctx, "test", client.PluginInspectOptions{})
assert.NilError(t, err)
assert.Assert(t, p.Enabled)
assert.Assert(t, resp.Plugin.Enabled)
}

View File

@@ -72,10 +72,10 @@ func TestServicePlugin(t *testing.T) {
t.Log("No tasks found for plugin service")
t.Fail()
}
p, _, err := d1.NewClientT(t).PluginInspectWithRaw(ctx, name)
res, err := d1.NewClientT(t).PluginInspect(ctx, name, client.PluginInspectOptions{})
assert.NilError(t, err, "Error inspecting service plugin")
found := false
for _, env := range p.Settings.Env {
for _, env := range res.Plugin.Settings.Env {
assert.Equal(t, strings.HasPrefix(env, "baz"), false, "Environment variable entry %q is invalid and should not be present", "baz")
if strings.HasPrefix(env, "foo=") {
found = true

View File

@@ -33,7 +33,7 @@ func (d *Daemon) PluginIsNotRunning(t testing.TB, name string) func(poll.LogT) p
// PluginIsNotPresent provides a poller to check if the specified plugin is not present
func (d *Daemon) PluginIsNotPresent(t testing.TB, name string) func(poll.LogT) poll.Result {
return withClient(t, d, func(c client.APIClient, t poll.LogT) poll.Result {
_, _, err := c.PluginInspectWithRaw(context.Background(), name)
_, err := c.PluginInspect(context.Background(), name, client.PluginInspectOptions{})
if cerrdefs.IsNotFound(err) {
return poll.Success()
}
@@ -56,14 +56,14 @@ func (d *Daemon) PluginReferenceIs(t testing.TB, name, expectedRef string) func(
func withPluginInspect(name string, f func(*plugin.Plugin, poll.LogT) poll.Result) func(client.APIClient, poll.LogT) poll.Result {
return func(c client.APIClient, t poll.LogT) poll.Result {
p, _, err := c.PluginInspectWithRaw(context.Background(), name)
res, err := c.PluginInspect(context.Background(), name, client.PluginInspectOptions{})
if cerrdefs.IsNotFound(err) {
return poll.Continue("plugin %q not found", name)
}
if err != nil {
return poll.Error(err)
}
return f(p, t)
return f(&res.Plugin, t)
}
}

View File

@@ -154,7 +154,7 @@ type PluginAPIClient interface {
PluginUpgrade(ctx context.Context, name string, options PluginInstallOptions) (io.ReadCloser, error)
PluginPush(ctx context.Context, name string, registryAuth string) (io.ReadCloser, error)
PluginSet(ctx context.Context, name string, args []string) error
PluginInspectWithRaw(ctx context.Context, name string) (*plugin.Plugin, []byte, error)
PluginInspect(ctx context.Context, name string, options PluginInspectOptions) (PluginInspectResult, error)
PluginCreate(ctx context.Context, createContext io.Reader, options PluginCreateOptions) error
}

View File

@@ -1,32 +1,35 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"github.com/moby/moby/api/types/plugin"
)
// PluginInspectWithRaw inspects an existing plugin
func (cli *Client) PluginInspectWithRaw(ctx context.Context, name string) (*plugin.Plugin, []byte, error) {
// PluginInspectOptions holds parameters to inspect a plugin.
type PluginInspectOptions struct {
// Add future optional parameters here
}
// PluginInspectResult holds the result from the [Client.PluginInspect] method.
type PluginInspectResult struct {
Raw []byte
Plugin plugin.Plugin
}
// PluginInspect inspects an existing plugin
func (cli *Client) PluginInspect(ctx context.Context, name string, options PluginInspectOptions) (PluginInspectResult, error) {
name, err := trimID("plugin", name)
if err != nil {
return nil, nil, err
return PluginInspectResult{}, err
}
resp, err := cli.get(ctx, "/plugins/"+name+"/json", nil, nil)
defer ensureReaderClosed(resp)
if err != nil {
return nil, nil, err
return PluginInspectResult{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
var p plugin.Plugin
rdr := bytes.NewReader(body)
err = json.NewDecoder(rdr).Decode(&p)
return &p, body, err
var out PluginInspectResult
out.Raw, err = decodeWithRaw(resp, &out.Plugin)
return out, err
}