github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/config/create_test.go (about)

     1  package config
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"reflect"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/cli/internal/test"
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/api/types/swarm"
    13  	"github.com/pkg/errors"
    14  	"gotest.tools/v3/assert"
    15  	is "gotest.tools/v3/assert/cmp"
    16  	"gotest.tools/v3/golden"
    17  )
    18  
    19  const configDataFile = "config-create-with-name.golden"
    20  
    21  func TestConfigCreateErrors(t *testing.T) {
    22  	testCases := []struct {
    23  		args             []string
    24  		configCreateFunc func(swarm.ConfigSpec) (types.ConfigCreateResponse, error)
    25  		expectedError    string
    26  	}{
    27  		{
    28  			args:          []string{"too_few"},
    29  			expectedError: "requires exactly 2 arguments",
    30  		},
    31  		{args: []string{"too", "many", "arguments"},
    32  			expectedError: "requires exactly 2 arguments",
    33  		},
    34  		{
    35  			args: []string{"name", filepath.Join("testdata", configDataFile)},
    36  			configCreateFunc: func(configSpec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
    37  				return types.ConfigCreateResponse{}, errors.Errorf("error creating config")
    38  			},
    39  			expectedError: "error creating config",
    40  		},
    41  	}
    42  	for _, tc := range testCases {
    43  		cmd := newConfigCreateCommand(
    44  			test.NewFakeCli(&fakeClient{
    45  				configCreateFunc: tc.configCreateFunc,
    46  			}),
    47  		)
    48  		cmd.SetArgs(tc.args)
    49  		cmd.SetOut(ioutil.Discard)
    50  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    51  	}
    52  }
    53  
    54  func TestConfigCreateWithName(t *testing.T) {
    55  	name := "foo"
    56  	var actual []byte
    57  	cli := test.NewFakeCli(&fakeClient{
    58  		configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
    59  			if spec.Name != name {
    60  				return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
    61  			}
    62  
    63  			actual = spec.Data
    64  
    65  			return types.ConfigCreateResponse{
    66  				ID: "ID-" + spec.Name,
    67  			}, nil
    68  		},
    69  	})
    70  
    71  	cmd := newConfigCreateCommand(cli)
    72  	cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
    73  	assert.NilError(t, cmd.Execute())
    74  	golden.Assert(t, string(actual), configDataFile)
    75  	assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
    76  }
    77  
    78  func TestConfigCreateWithLabels(t *testing.T) {
    79  	expectedLabels := map[string]string{
    80  		"lbl1": "Label-foo",
    81  		"lbl2": "Label-bar",
    82  	}
    83  	name := "foo"
    84  
    85  	data, err := ioutil.ReadFile(filepath.Join("testdata", configDataFile))
    86  	assert.NilError(t, err)
    87  
    88  	expected := swarm.ConfigSpec{
    89  		Annotations: swarm.Annotations{
    90  			Name:   name,
    91  			Labels: expectedLabels,
    92  		},
    93  		Data: data,
    94  	}
    95  
    96  	cli := test.NewFakeCli(&fakeClient{
    97  		configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
    98  			if !reflect.DeepEqual(spec, expected) {
    99  				return types.ConfigCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
   100  			}
   101  
   102  			return types.ConfigCreateResponse{
   103  				ID: "ID-" + spec.Name,
   104  			}, nil
   105  		},
   106  	})
   107  
   108  	cmd := newConfigCreateCommand(cli)
   109  	cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
   110  	cmd.Flags().Set("label", "lbl1=Label-foo")
   111  	cmd.Flags().Set("label", "lbl2=Label-bar")
   112  	assert.NilError(t, cmd.Execute())
   113  	assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
   114  }
   115  
   116  func TestConfigCreateWithTemplatingDriver(t *testing.T) {
   117  	expectedDriver := &swarm.Driver{
   118  		Name: "template-driver",
   119  	}
   120  	name := "foo"
   121  
   122  	cli := test.NewFakeCli(&fakeClient{
   123  		configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
   124  			if spec.Name != name {
   125  				return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
   126  			}
   127  
   128  			if spec.Templating.Name != expectedDriver.Name {
   129  				return types.ConfigCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
   130  			}
   131  
   132  			return types.ConfigCreateResponse{
   133  				ID: "ID-" + spec.Name,
   134  			}, nil
   135  		},
   136  	})
   137  
   138  	cmd := newConfigCreateCommand(cli)
   139  	cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
   140  	cmd.Flags().Set("template-driver", expectedDriver.Name)
   141  	assert.NilError(t, cmd.Execute())
   142  	assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
   143  }