cli/command/secret: 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/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
                                      ^
    cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
                                              ^
    cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
                                    ^
    cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
    func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
                                      ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-03-30 16:22:06 +02:00
parent 66c66bdce7
commit 9dd012aa5d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 39 additions and 35 deletions

View File

@ -10,36 +10,36 @@ import (
type fakeClient struct { type fakeClient struct {
client.Client client.Client
secretCreateFunc func(swarm.SecretSpec) (types.SecretCreateResponse, error) secretCreateFunc func(context.Context, swarm.SecretSpec) (types.SecretCreateResponse, error)
secretInspectFunc func(string) (swarm.Secret, []byte, error) secretInspectFunc func(context.Context, string) (swarm.Secret, []byte, error)
secretListFunc func(types.SecretListOptions) ([]swarm.Secret, error) secretListFunc func(context.Context, types.SecretListOptions) ([]swarm.Secret, error)
secretRemoveFunc func(string) error secretRemoveFunc func(context.Context, string) error
} }
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) { func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if c.secretCreateFunc != nil { if c.secretCreateFunc != nil {
return c.secretCreateFunc(spec) return c.secretCreateFunc(ctx, spec)
} }
return types.SecretCreateResponse{}, nil return types.SecretCreateResponse{}, nil
} }
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) { func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
if c.secretInspectFunc != nil { if c.secretInspectFunc != nil {
return c.secretInspectFunc(id) return c.secretInspectFunc(ctx, id)
} }
return swarm.Secret{}, nil, nil return swarm.Secret{}, nil, nil
} }
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) { func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
if c.secretListFunc != nil { if c.secretListFunc != nil {
return c.secretListFunc(options) return c.secretListFunc(ctx, options)
} }
return []swarm.Secret{}, nil return []swarm.Secret{}, nil
} }
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error { func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
if c.secretRemoveFunc != nil { if c.secretRemoveFunc != nil {
return c.secretRemoveFunc(name) return c.secretRemoveFunc(ctx, name)
} }
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package secret package secret
import ( import (
"context"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -21,7 +22,7 @@ const secretDataFile = "secret-create-with-name.golden"
func TestSecretCreateErrors(t *testing.T) { func TestSecretCreateErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
args []string args []string
secretCreateFunc func(swarm.SecretSpec) (types.SecretCreateResponse, error) secretCreateFunc func(context.Context, swarm.SecretSpec) (types.SecretCreateResponse, error)
expectedError string expectedError string
}{ }{
{ {
@ -34,7 +35,7 @@ func TestSecretCreateErrors(t *testing.T) {
}, },
{ {
args: []string{"name", filepath.Join("testdata", secretDataFile)}, args: []string{"name", filepath.Join("testdata", secretDataFile)},
secretCreateFunc: func(secretSpec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(_ context.Context, secretSpec swarm.SecretSpec) (types.SecretCreateResponse, error) {
return types.SecretCreateResponse{}, errors.Errorf("error creating secret") return types.SecretCreateResponse{}, errors.Errorf("error creating secret")
}, },
expectedError: "error creating secret", expectedError: "error creating secret",
@ -66,7 +67,7 @@ func TestSecretCreateWithName(t *testing.T) {
} }
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if !reflect.DeepEqual(spec, expected) { if !reflect.DeepEqual(spec, expected) {
return types.SecretCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec) return types.SecretCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
} }
@ -89,7 +90,7 @@ func TestSecretCreateWithDriver(t *testing.T) {
name := "foo" name := "foo"
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
} }
@ -118,7 +119,7 @@ func TestSecretCreateWithTemplatingDriver(t *testing.T) {
name := "foo" name := "foo"
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
} }
@ -148,7 +149,7 @@ func TestSecretCreateWithLabels(t *testing.T) {
name := "foo" name := "foo"
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
} }

View File

@ -1,6 +1,7 @@
package secret package secret
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"testing" "testing"
@ -18,7 +19,7 @@ func TestSecretInspectErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
args []string args []string
flags map[string]string flags map[string]string
secretInspectFunc func(secretID string) (swarm.Secret, []byte, error) secretInspectFunc func(ctx context.Context, secretID string) (swarm.Secret, []byte, error)
expectedError string expectedError string
}{ }{
{ {
@ -26,7 +27,7 @@ func TestSecretInspectErrors(t *testing.T) {
}, },
{ {
args: []string{"foo"}, args: []string{"foo"},
secretInspectFunc: func(secretID string) (swarm.Secret, []byte, error) { secretInspectFunc: func(_ context.Context, secretID string) (swarm.Secret, []byte, error) {
return swarm.Secret{}, nil, errors.Errorf("error while inspecting the secret") return swarm.Secret{}, nil, errors.Errorf("error while inspecting the secret")
}, },
expectedError: "error while inspecting the secret", expectedError: "error while inspecting the secret",
@ -40,7 +41,7 @@ func TestSecretInspectErrors(t *testing.T) {
}, },
{ {
args: []string{"foo", "bar"}, args: []string{"foo", "bar"},
secretInspectFunc: func(secretID string) (swarm.Secret, []byte, error) { secretInspectFunc: func(_ context.Context, secretID string) (swarm.Secret, []byte, error) {
if secretID == "foo" { if secretID == "foo" {
return *Secret(SecretName("foo")), nil, nil return *Secret(SecretName("foo")), nil, nil
} }
@ -68,12 +69,12 @@ func TestSecretInspectWithoutFormat(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
args []string args []string
secretInspectFunc func(secretID string) (swarm.Secret, []byte, error) secretInspectFunc func(ctx context.Context, secretID string) (swarm.Secret, []byte, error)
}{ }{
{ {
name: "single-secret", name: "single-secret",
args: []string{"foo"}, args: []string{"foo"},
secretInspectFunc: func(name string) (swarm.Secret, []byte, error) { secretInspectFunc: func(_ context.Context, name string) (swarm.Secret, []byte, error) {
if name != "foo" { if name != "foo" {
return swarm.Secret{}, nil, errors.Errorf("Invalid name, expected %s, got %s", "foo", name) return swarm.Secret{}, nil, errors.Errorf("Invalid name, expected %s, got %s", "foo", name)
} }
@ -83,7 +84,7 @@ func TestSecretInspectWithoutFormat(t *testing.T) {
{ {
name: "multiple-secrets-with-labels", name: "multiple-secrets-with-labels",
args: []string{"foo", "bar"}, args: []string{"foo", "bar"},
secretInspectFunc: func(name string) (swarm.Secret, []byte, error) { secretInspectFunc: func(_ context.Context, name string) (swarm.Secret, []byte, error) {
return *Secret(SecretID("ID-"+name), SecretName(name), SecretLabels(map[string]string{ return *Secret(SecretID("ID-"+name), SecretName(name), SecretLabels(map[string]string{
"label1": "label-foo", "label1": "label-foo",
})), nil, nil })), nil, nil
@ -102,7 +103,7 @@ func TestSecretInspectWithoutFormat(t *testing.T) {
} }
func TestSecretInspectWithFormat(t *testing.T) { func TestSecretInspectWithFormat(t *testing.T) {
secretInspectFunc := func(name string) (swarm.Secret, []byte, error) { secretInspectFunc := func(_ context.Context, name string) (swarm.Secret, []byte, error) {
return *Secret(SecretName("foo"), SecretLabels(map[string]string{ return *Secret(SecretName("foo"), SecretLabels(map[string]string{
"label1": "label-foo", "label1": "label-foo",
})), nil, nil })), nil, nil
@ -111,7 +112,7 @@ func TestSecretInspectWithFormat(t *testing.T) {
name string name string
format string format string
args []string args []string
secretInspectFunc func(name string) (swarm.Secret, []byte, error) secretInspectFunc func(_ context.Context, name string) (swarm.Secret, []byte, error)
}{ }{
{ {
name: "simple-template", name: "simple-template",
@ -141,11 +142,11 @@ func TestSecretInspectWithFormat(t *testing.T) {
func TestSecretInspectPretty(t *testing.T) { func TestSecretInspectPretty(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
secretInspectFunc func(string) (swarm.Secret, []byte, error) secretInspectFunc func(context.Context, string) (swarm.Secret, []byte, error)
}{ }{
{ {
name: "simple", name: "simple",
secretInspectFunc: func(id string) (swarm.Secret, []byte, error) { secretInspectFunc: func(_ context.Context, id string) (swarm.Secret, []byte, error) {
return *Secret( return *Secret(
SecretLabels(map[string]string{ SecretLabels(map[string]string{
"lbl1": "value1", "lbl1": "value1",

View File

@ -1,6 +1,7 @@
package secret package secret
import ( import (
"context"
"io" "io"
"testing" "testing"
"time" "time"
@ -19,7 +20,7 @@ import (
func TestSecretListErrors(t *testing.T) { func TestSecretListErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
args []string args []string
secretListFunc func(types.SecretListOptions) ([]swarm.Secret, error) secretListFunc func(context.Context, types.SecretListOptions) ([]swarm.Secret, error)
expectedError string expectedError string
}{ }{
{ {
@ -27,7 +28,7 @@ func TestSecretListErrors(t *testing.T) {
expectedError: "accepts no argument", expectedError: "accepts no argument",
}, },
{ {
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{}, errors.Errorf("error listing secrets") return []swarm.Secret{}, errors.Errorf("error listing secrets")
}, },
expectedError: "error listing secrets", expectedError: "error listing secrets",
@ -47,7 +48,7 @@ func TestSecretListErrors(t *testing.T) {
func TestSecretList(t *testing.T) { func TestSecretList(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-1-foo"), *Secret(SecretID("ID-1-foo"),
SecretName("1-foo"), SecretName("1-foo"),
@ -79,7 +80,7 @@ func TestSecretList(t *testing.T) {
func TestSecretListWithQuietOption(t *testing.T) { func TestSecretListWithQuietOption(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{ *Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
@ -96,7 +97,7 @@ func TestSecretListWithQuietOption(t *testing.T) {
func TestSecretListWithConfigFormat(t *testing.T) { func TestSecretListWithConfigFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{ *Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
@ -115,7 +116,7 @@ func TestSecretListWithConfigFormat(t *testing.T) {
func TestSecretListWithFormat(t *testing.T) { func TestSecretListWithFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{ *Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
@ -132,7 +133,7 @@ func TestSecretListWithFormat(t *testing.T) {
func TestSecretListWithFilter(t *testing.T) { func TestSecretListWithFilter(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0]), "foo") assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0]), "foo")
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.Secret{ return []swarm.Secret{

View File

@ -1,6 +1,7 @@
package secret package secret
import ( import (
"context"
"io" "io"
"strings" "strings"
"testing" "testing"
@ -14,7 +15,7 @@ import (
func TestSecretRemoveErrors(t *testing.T) { func TestSecretRemoveErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
args []string args []string
secretRemoveFunc func(string) error secretRemoveFunc func(context.Context, string) error
expectedError string expectedError string
}{ }{
{ {
@ -23,7 +24,7 @@ func TestSecretRemoveErrors(t *testing.T) {
}, },
{ {
args: []string{"foo"}, args: []string{"foo"},
secretRemoveFunc: func(name string) error { secretRemoveFunc: func(_ context.Context, name string) error {
return errors.Errorf("error removing secret") return errors.Errorf("error removing secret")
}, },
expectedError: "error removing secret", expectedError: "error removing secret",
@ -45,7 +46,7 @@ func TestSecretRemoveWithName(t *testing.T) {
names := []string{"foo", "bar"} names := []string{"foo", "bar"}
var removedSecrets []string var removedSecrets []string
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretRemoveFunc: func(name string) error { secretRemoveFunc: func(_ context.Context, name string) error {
removedSecrets = append(removedSecrets, name) removedSecrets = append(removedSecrets, name)
return nil return nil
}, },
@ -62,7 +63,7 @@ func TestSecretRemoveContinueAfterError(t *testing.T) {
var removedSecrets []string var removedSecrets []string
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretRemoveFunc: func(name string) error { secretRemoveFunc: func(_ context.Context, name string) error {
removedSecrets = append(removedSecrets, name) removedSecrets = append(removedSecrets, name)
if name == "foo" { if name == "foo" {
return errors.Errorf("error removing secret: %s", name) return errors.Errorf("error removing secret: %s", name)