github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/secret/create_test.go (about)

     1  package secret
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/cli/internal/test"
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/api/types/swarm"
    14  	"github.com/pkg/errors"
    15  	"gotest.tools/v3/assert"
    16  	is "gotest.tools/v3/assert/cmp"
    17  )
    18  
    19  const secretDataFile = "secret-create-with-name.golden"
    20  
    21  func TestSecretCreateErrors(t *testing.T) {
    22  	testCases := []struct {
    23  		args             []string
    24  		secretCreateFunc func(swarm.SecretSpec) (types.SecretCreateResponse, error)
    25  		expectedError    string
    26  	}{
    27  		{
    28  			args:          []string{"too", "many", "arguments"},
    29  			expectedError: "requires at least 1 and at most 2 arguments",
    30  		},
    31  		{
    32  			args:          []string{"create", "--driver", "driver", "-"},
    33  			expectedError: "secret data must be empty",
    34  		},
    35  		{
    36  			args: []string{"name", filepath.Join("testdata", secretDataFile)},
    37  			secretCreateFunc: func(secretSpec swarm.SecretSpec) (types.SecretCreateResponse, error) {
    38  				return types.SecretCreateResponse{}, errors.Errorf("error creating secret")
    39  			},
    40  			expectedError: "error creating secret",
    41  		},
    42  	}
    43  	for _, tc := range testCases {
    44  		cmd := newSecretCreateCommand(
    45  			test.NewFakeCli(&fakeClient{
    46  				secretCreateFunc: tc.secretCreateFunc,
    47  			}),
    48  		)
    49  		cmd.SetArgs(tc.args)
    50  		cmd.SetOut(io.Discard)
    51  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    52  	}
    53  }
    54  
    55  func TestSecretCreateWithName(t *testing.T) {
    56  	name := "foo"
    57  	data, err := os.ReadFile(filepath.Join("testdata", secretDataFile))
    58  	assert.NilError(t, err)
    59  
    60  	expected := swarm.SecretSpec{
    61  		Annotations: swarm.Annotations{
    62  			Name:   name,
    63  			Labels: make(map[string]string),
    64  		},
    65  		Data: data,
    66  	}
    67  
    68  	cli := test.NewFakeCli(&fakeClient{
    69  		secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
    70  			if !reflect.DeepEqual(spec, expected) {
    71  				return types.SecretCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
    72  			}
    73  			return types.SecretCreateResponse{
    74  				ID: "ID-" + spec.Name,
    75  			}, nil
    76  		},
    77  	})
    78  
    79  	cmd := newSecretCreateCommand(cli)
    80  	cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
    81  	assert.NilError(t, cmd.Execute())
    82  	assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
    83  }
    84  
    85  func TestSecretCreateWithDriver(t *testing.T) {
    86  	expectedDriver := &swarm.Driver{
    87  		Name: "secret-driver",
    88  	}
    89  	name := "foo"
    90  
    91  	cli := test.NewFakeCli(&fakeClient{
    92  		secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
    93  			if spec.Name != name {
    94  				return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
    95  			}
    96  
    97  			if spec.Driver.Name != expectedDriver.Name {
    98  				return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
    99  			}
   100  
   101  			return types.SecretCreateResponse{
   102  				ID: "ID-" + spec.Name,
   103  			}, nil
   104  		},
   105  	})
   106  
   107  	cmd := newSecretCreateCommand(cli)
   108  	cmd.SetArgs([]string{name})
   109  	cmd.Flags().Set("driver", expectedDriver.Name)
   110  	assert.NilError(t, cmd.Execute())
   111  	assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
   112  }
   113  
   114  func TestSecretCreateWithTemplatingDriver(t *testing.T) {
   115  	expectedDriver := &swarm.Driver{
   116  		Name: "template-driver",
   117  	}
   118  	name := "foo"
   119  
   120  	cli := test.NewFakeCli(&fakeClient{
   121  		secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
   122  			if spec.Name != name {
   123  				return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
   124  			}
   125  
   126  			if spec.Templating.Name != expectedDriver.Name {
   127  				return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
   128  			}
   129  
   130  			return types.SecretCreateResponse{
   131  				ID: "ID-" + spec.Name,
   132  			}, nil
   133  		},
   134  	})
   135  
   136  	cmd := newSecretCreateCommand(cli)
   137  	cmd.SetArgs([]string{name})
   138  	cmd.Flags().Set("template-driver", expectedDriver.Name)
   139  	assert.NilError(t, cmd.Execute())
   140  	assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
   141  }
   142  
   143  func TestSecretCreateWithLabels(t *testing.T) {
   144  	expectedLabels := map[string]string{
   145  		"lbl1": "Label-foo",
   146  		"lbl2": "Label-bar",
   147  	}
   148  	name := "foo"
   149  
   150  	cli := test.NewFakeCli(&fakeClient{
   151  		secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
   152  			if spec.Name != name {
   153  				return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
   154  			}
   155  
   156  			if !reflect.DeepEqual(spec.Labels, expectedLabels) {
   157  				return types.SecretCreateResponse{}, errors.Errorf("expected labels %v, got %v", expectedLabels, spec.Labels)
   158  			}
   159  
   160  			return types.SecretCreateResponse{
   161  				ID: "ID-" + spec.Name,
   162  			}, nil
   163  		},
   164  	})
   165  
   166  	cmd := newSecretCreateCommand(cli)
   167  	cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
   168  	cmd.Flags().Set("label", "lbl1=Label-foo")
   169  	cmd.Flags().Set("label", "lbl2=Label-bar")
   170  	assert.NilError(t, cmd.Execute())
   171  	assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
   172  }