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