github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/config/create_test.go (about)

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