cli/command/config: fakeClient: include context in fake client (revive)

I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)

.   cli/command/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
                                      ^
    cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
                                              ^
    cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
                                    ^
    cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
                                      ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 66c66bdce7)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-03-30 16:14:47 +02:00
parent dccbbfc4c7
commit d741c64884
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 32 additions and 29 deletions

View File

@ -10,34 +10,34 @@ import (
type fakeClient struct { type fakeClient struct {
client.Client client.Client
configCreateFunc func(swarm.ConfigSpec) (types.ConfigCreateResponse, error) configCreateFunc func(context.Context, swarm.ConfigSpec) (types.ConfigCreateResponse, error)
configInspectFunc func(string) (swarm.Config, []byte, error) configInspectFunc func(context.Context, string) (swarm.Config, []byte, error)
configListFunc func(types.ConfigListOptions) ([]swarm.Config, error) configListFunc func(context.Context, types.ConfigListOptions) ([]swarm.Config, error)
configRemoveFunc func(string) error configRemoveFunc func(string) error
} }
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if c.configCreateFunc != nil { if c.configCreateFunc != nil {
return c.configCreateFunc(spec) return c.configCreateFunc(ctx, spec)
} }
return types.ConfigCreateResponse{}, nil return types.ConfigCreateResponse{}, nil
} }
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) { func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
if c.configInspectFunc != nil { if c.configInspectFunc != nil {
return c.configInspectFunc(id) return c.configInspectFunc(ctx, id)
} }
return swarm.Config{}, nil, nil return swarm.Config{}, nil, nil
} }
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) { func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
if c.configListFunc != nil { if c.configListFunc != nil {
return c.configListFunc(options) return c.configListFunc(ctx, options)
} }
return []swarm.Config{}, nil return []swarm.Config{}, nil
} }
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error { func (c *fakeClient) ConfigRemove(_ context.Context, name string) error {
if c.configRemoveFunc != nil { if c.configRemoveFunc != nil {
return c.configRemoveFunc(name) return c.configRemoveFunc(name)
} }

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"context"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -22,7 +23,7 @@ const configDataFile = "config-create-with-name.golden"
func TestConfigCreateErrors(t *testing.T) { func TestConfigCreateErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
args []string args []string
configCreateFunc func(swarm.ConfigSpec) (types.ConfigCreateResponse, error) configCreateFunc func(context.Context, swarm.ConfigSpec) (types.ConfigCreateResponse, error)
expectedError string expectedError string
}{ }{
{ {
@ -35,7 +36,7 @@ func TestConfigCreateErrors(t *testing.T) {
}, },
{ {
args: []string{"name", filepath.Join("testdata", configDataFile)}, args: []string{"name", filepath.Join("testdata", configDataFile)},
configCreateFunc: func(configSpec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { configCreateFunc: func(_ context.Context, configSpec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
return types.ConfigCreateResponse{}, errors.Errorf("error creating config") return types.ConfigCreateResponse{}, errors.Errorf("error creating config")
}, },
expectedError: "error creating config", expectedError: "error creating config",
@ -57,7 +58,7 @@ func TestConfigCreateWithName(t *testing.T) {
name := "foo" name := "foo"
var actual []byte var actual []byte
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { configCreateFunc: func(_ context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
} }
@ -96,7 +97,7 @@ func TestConfigCreateWithLabels(t *testing.T) {
} }
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { configCreateFunc: func(_ context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if !reflect.DeepEqual(spec, expected) { if !reflect.DeepEqual(spec, expected) {
return types.ConfigCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec) return types.ConfigCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
} }
@ -122,7 +123,7 @@ func TestConfigCreateWithTemplatingDriver(t *testing.T) {
name := "foo" name := "foo"
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { configCreateFunc: func(_ context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
} }

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"testing" "testing"
@ -18,7 +19,7 @@ func TestConfigInspectErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
args []string args []string
flags map[string]string flags map[string]string
configInspectFunc func(configID string) (swarm.Config, []byte, error) configInspectFunc func(_ context.Context, configID string) (swarm.Config, []byte, error)
expectedError string expectedError string
}{ }{
{ {
@ -26,7 +27,7 @@ func TestConfigInspectErrors(t *testing.T) {
}, },
{ {
args: []string{"foo"}, args: []string{"foo"},
configInspectFunc: func(configID string) (swarm.Config, []byte, error) { configInspectFunc: func(_ context.Context, configID string) (swarm.Config, []byte, error) {
return swarm.Config{}, nil, errors.Errorf("error while inspecting the config") return swarm.Config{}, nil, errors.Errorf("error while inspecting the config")
}, },
expectedError: "error while inspecting the config", expectedError: "error while inspecting the config",
@ -40,7 +41,7 @@ func TestConfigInspectErrors(t *testing.T) {
}, },
{ {
args: []string{"foo", "bar"}, args: []string{"foo", "bar"},
configInspectFunc: func(configID string) (swarm.Config, []byte, error) { configInspectFunc: func(_ context.Context, configID string) (swarm.Config, []byte, error) {
if configID == "foo" { if configID == "foo" {
return *Config(ConfigName("foo")), nil, nil return *Config(ConfigName("foo")), nil, nil
} }
@ -68,12 +69,12 @@ func TestConfigInspectWithoutFormat(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
args []string args []string
configInspectFunc func(configID string) (swarm.Config, []byte, error) configInspectFunc func(_ context.Context, configID string) (swarm.Config, []byte, error)
}{ }{
{ {
name: "single-config", name: "single-config",
args: []string{"foo"}, args: []string{"foo"},
configInspectFunc: func(name string) (swarm.Config, []byte, error) { configInspectFunc: func(_ context.Context, name string) (swarm.Config, []byte, error) {
if name != "foo" { if name != "foo" {
return swarm.Config{}, nil, errors.Errorf("Invalid name, expected %s, got %s", "foo", name) return swarm.Config{}, nil, errors.Errorf("Invalid name, expected %s, got %s", "foo", name)
} }
@ -83,7 +84,7 @@ func TestConfigInspectWithoutFormat(t *testing.T) {
{ {
name: "multiple-configs-with-labels", name: "multiple-configs-with-labels",
args: []string{"foo", "bar"}, args: []string{"foo", "bar"},
configInspectFunc: func(name string) (swarm.Config, []byte, error) { configInspectFunc: func(_ context.Context, name string) (swarm.Config, []byte, error) {
return *Config(ConfigID("ID-"+name), ConfigName(name), ConfigLabels(map[string]string{ return *Config(ConfigID("ID-"+name), ConfigName(name), ConfigLabels(map[string]string{
"label1": "label-foo", "label1": "label-foo",
})), nil, nil })), nil, nil
@ -100,7 +101,7 @@ func TestConfigInspectWithoutFormat(t *testing.T) {
} }
func TestConfigInspectWithFormat(t *testing.T) { func TestConfigInspectWithFormat(t *testing.T) {
configInspectFunc := func(name string) (swarm.Config, []byte, error) { configInspectFunc := func(_ context.Context, name string) (swarm.Config, []byte, error) {
return *Config(ConfigName("foo"), ConfigLabels(map[string]string{ return *Config(ConfigName("foo"), ConfigLabels(map[string]string{
"label1": "label-foo", "label1": "label-foo",
})), nil, nil })), nil, nil
@ -109,7 +110,7 @@ func TestConfigInspectWithFormat(t *testing.T) {
name string name string
format string format string
args []string args []string
configInspectFunc func(name string) (swarm.Config, []byte, error) configInspectFunc func(_ context.Context, name string) (swarm.Config, []byte, error)
}{ }{
{ {
name: "simple-template", name: "simple-template",
@ -139,11 +140,11 @@ func TestConfigInspectWithFormat(t *testing.T) {
func TestConfigInspectPretty(t *testing.T) { func TestConfigInspectPretty(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
configInspectFunc func(string) (swarm.Config, []byte, error) configInspectFunc func(context.Context, string) (swarm.Config, []byte, error)
}{ }{
{ {
name: "simple", name: "simple",
configInspectFunc: func(id string) (swarm.Config, []byte, error) { configInspectFunc: func(_ context.Context, id string) (swarm.Config, []byte, error) {
return *Config( return *Config(
ConfigLabels(map[string]string{ ConfigLabels(map[string]string{
"lbl1": "value1", "lbl1": "value1",

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"context"
"io" "io"
"testing" "testing"
"time" "time"
@ -19,7 +20,7 @@ import (
func TestConfigListErrors(t *testing.T) { func TestConfigListErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
args []string args []string
configListFunc func(types.ConfigListOptions) ([]swarm.Config, error) configListFunc func(context.Context, types.ConfigListOptions) ([]swarm.Config, error)
expectedError string expectedError string
}{ }{
{ {
@ -27,7 +28,7 @@ func TestConfigListErrors(t *testing.T) {
expectedError: "accepts no argument", expectedError: "accepts no argument",
}, },
{ {
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{}, errors.Errorf("error listing configs") return []swarm.Config{}, errors.Errorf("error listing configs")
}, },
expectedError: "error listing configs", expectedError: "error listing configs",
@ -47,7 +48,7 @@ func TestConfigListErrors(t *testing.T) {
func TestConfigList(t *testing.T) { func TestConfigList(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-1-foo"), *Config(ConfigID("ID-1-foo"),
ConfigName("1-foo"), ConfigName("1-foo"),
@ -77,7 +78,7 @@ func TestConfigList(t *testing.T) {
func TestConfigListWithQuietOption(t *testing.T) { func TestConfigListWithQuietOption(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), ConfigName("foo")), *Config(ConfigID("ID-foo"), ConfigName("foo")),
*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{ *Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
@ -94,7 +95,7 @@ func TestConfigListWithQuietOption(t *testing.T) {
func TestConfigListWithConfigFormat(t *testing.T) { func TestConfigListWithConfigFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), ConfigName("foo")), *Config(ConfigID("ID-foo"), ConfigName("foo")),
*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{ *Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
@ -113,7 +114,7 @@ func TestConfigListWithConfigFormat(t *testing.T) {
func TestConfigListWithFormat(t *testing.T) { func TestConfigListWithFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), ConfigName("foo")), *Config(ConfigID("ID-foo"), ConfigName("foo")),
*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{ *Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
@ -130,7 +131,7 @@ func TestConfigListWithFormat(t *testing.T) {
func TestConfigListWithFilter(t *testing.T) { func TestConfigListWithFilter(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0])) assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0]))
assert.Check(t, is.Equal("lbl1=Label-bar", options.Filters.Get("label")[0])) assert.Check(t, is.Equal("lbl1=Label-bar", options.Filters.Get("label")[0]))
return []swarm.Config{ return []swarm.Config{