github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/compose/convert/compose_test.go (about)

     1  package convert
     2  
     3  import (
     4  	"testing"
     5  
     6  	composetypes "github.com/docker/cli/cli/compose/types"
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/api/types/network"
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  	"gotest.tools/v3/fs"
    12  )
    13  
    14  func TestNamespaceScope(t *testing.T) {
    15  	scoped := Namespace{name: "foo"}.Scope("bar")
    16  	assert.Check(t, is.Equal("foo_bar", scoped))
    17  }
    18  
    19  func TestAddStackLabel(t *testing.T) {
    20  	labels := map[string]string{
    21  		"something": "labeled",
    22  	}
    23  	actual := AddStackLabel(Namespace{name: "foo"}, labels)
    24  	expected := map[string]string{
    25  		"something":    "labeled",
    26  		LabelNamespace: "foo",
    27  	}
    28  	assert.Check(t, is.DeepEqual(expected, actual))
    29  }
    30  
    31  func TestNetworks(t *testing.T) {
    32  	namespace := Namespace{name: "foo"}
    33  	serviceNetworks := map[string]struct{}{
    34  		"normal":        {},
    35  		"outside":       {},
    36  		"default":       {},
    37  		"attachablenet": {},
    38  		"named":         {},
    39  	}
    40  	source := networkMap{
    41  		"normal": composetypes.NetworkConfig{
    42  			Driver: "overlay",
    43  			DriverOpts: map[string]string{
    44  				"opt": "value",
    45  			},
    46  			Ipam: composetypes.IPAMConfig{
    47  				Driver: "driver",
    48  				Config: []*composetypes.IPAMPool{
    49  					{
    50  						Subnet: "10.0.0.0",
    51  					},
    52  				},
    53  			},
    54  			Labels: map[string]string{
    55  				"something": "labeled",
    56  			},
    57  		},
    58  		"outside": composetypes.NetworkConfig{
    59  			External: composetypes.External{External: true},
    60  			Name:     "special",
    61  		},
    62  		"attachablenet": composetypes.NetworkConfig{
    63  			Driver:     "overlay",
    64  			Attachable: true,
    65  		},
    66  		"named": composetypes.NetworkConfig{
    67  			Name: "othername",
    68  		},
    69  	}
    70  	expected := map[string]types.NetworkCreate{
    71  		"foo_default": {
    72  			Labels: map[string]string{
    73  				LabelNamespace: "foo",
    74  			},
    75  		},
    76  		"foo_normal": {
    77  			Driver: "overlay",
    78  			IPAM: &network.IPAM{
    79  				Driver: "driver",
    80  				Config: []network.IPAMConfig{
    81  					{
    82  						Subnet: "10.0.0.0",
    83  					},
    84  				},
    85  			},
    86  			Options: map[string]string{
    87  				"opt": "value",
    88  			},
    89  			Labels: map[string]string{
    90  				LabelNamespace: "foo",
    91  				"something":    "labeled",
    92  			},
    93  		},
    94  		"foo_attachablenet": {
    95  			Driver:     "overlay",
    96  			Attachable: true,
    97  			Labels: map[string]string{
    98  				LabelNamespace: "foo",
    99  			},
   100  		},
   101  		"othername": {
   102  			Labels: map[string]string{LabelNamespace: "foo"},
   103  		},
   104  	}
   105  
   106  	networks, externals := Networks(namespace, source, serviceNetworks)
   107  	assert.DeepEqual(t, expected, networks)
   108  	assert.DeepEqual(t, []string{"special"}, externals)
   109  }
   110  
   111  func TestSecrets(t *testing.T) {
   112  	namespace := Namespace{name: "foo"}
   113  
   114  	secretText := "this is the first secret"
   115  	secretFile := fs.NewFile(t, "convert-secrets", fs.WithContent(secretText))
   116  	defer secretFile.Remove()
   117  
   118  	source := map[string]composetypes.SecretConfig{
   119  		"one": {
   120  			File:   secretFile.Path(),
   121  			Labels: map[string]string{"monster": "mash"},
   122  		},
   123  		"ext": {
   124  			External: composetypes.External{
   125  				External: true,
   126  			},
   127  		},
   128  	}
   129  
   130  	specs, err := Secrets(namespace, source)
   131  	assert.NilError(t, err)
   132  	assert.Assert(t, is.Len(specs, 1))
   133  	secret := specs[0]
   134  	assert.Check(t, is.Equal("foo_one", secret.Name))
   135  	assert.Check(t, is.DeepEqual(map[string]string{
   136  		"monster":      "mash",
   137  		LabelNamespace: "foo",
   138  	}, secret.Labels))
   139  	assert.Check(t, is.DeepEqual([]byte(secretText), secret.Data))
   140  }
   141  
   142  func TestConfigs(t *testing.T) {
   143  	namespace := Namespace{name: "foo"}
   144  
   145  	configText := "this is the first config"
   146  	configFile := fs.NewFile(t, "convert-configs", fs.WithContent(configText))
   147  	defer configFile.Remove()
   148  
   149  	source := map[string]composetypes.ConfigObjConfig{
   150  		"one": {
   151  			File:   configFile.Path(),
   152  			Labels: map[string]string{"monster": "mash"},
   153  		},
   154  		"ext": {
   155  			External: composetypes.External{
   156  				External: true,
   157  			},
   158  		},
   159  	}
   160  
   161  	specs, err := Configs(namespace, source)
   162  	assert.NilError(t, err)
   163  	assert.Assert(t, is.Len(specs, 1))
   164  	config := specs[0]
   165  	assert.Check(t, is.Equal("foo_one", config.Name))
   166  	assert.Check(t, is.DeepEqual(map[string]string{
   167  		"monster":      "mash",
   168  		LabelNamespace: "foo",
   169  	}, config.Labels))
   170  	assert.Check(t, is.DeepEqual([]byte(configText), config.Data))
   171  }