github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration/config/config_test.go (about) 1 package config 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "sort" 7 "testing" 8 "time" 9 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/api/types/filters" 12 swarmtypes "github.com/docker/docker/api/types/swarm" 13 "github.com/docker/docker/client" 14 "github.com/docker/docker/integration/internal/swarm" 15 "github.com/docker/docker/internal/testutil" 16 "github.com/docker/docker/pkg/stdcopy" 17 "github.com/gotestyourself/gotestyourself/assert" 18 is "github.com/gotestyourself/gotestyourself/assert/cmp" 19 "github.com/gotestyourself/gotestyourself/skip" 20 "golang.org/x/net/context" 21 ) 22 23 func TestConfigList(t *testing.T) { 24 skip.If(t, testEnv.DaemonInfo.OSType != "linux") 25 26 defer setupTest(t)() 27 d := swarm.NewSwarm(t, testEnv) 28 defer d.Stop(t) 29 client := d.NewClientT(t) 30 defer client.Close() 31 32 ctx := context.Background() 33 34 // This test case is ported from the original TestConfigsEmptyList 35 configs, err := client.ConfigList(ctx, types.ConfigListOptions{}) 36 assert.NilError(t, err) 37 assert.Check(t, is.Equal(len(configs), 0)) 38 39 testName0 := "test0" 40 testName1 := "test1" 41 testNames := []string{testName0, testName1} 42 sort.Strings(testNames) 43 44 // create config test0 45 createConfig(ctx, t, client, testName0, []byte("TESTINGDATA0"), map[string]string{"type": "test"}) 46 47 config1ID := createConfig(ctx, t, client, testName1, []byte("TESTINGDATA1"), map[string]string{"type": "production"}) 48 49 names := func(entries []swarmtypes.Config) []string { 50 values := []string{} 51 for _, entry := range entries { 52 values = append(values, entry.Spec.Name) 53 } 54 sort.Strings(values) 55 return values 56 } 57 58 // test by `config ls` 59 entries, err := client.ConfigList(ctx, types.ConfigListOptions{}) 60 assert.NilError(t, err) 61 assert.Check(t, is.DeepEqual(names(entries), testNames)) 62 63 testCases := []struct { 64 filters filters.Args 65 expected []string 66 }{ 67 // test filter by name `config ls --filter name=xxx` 68 { 69 filters: filters.NewArgs(filters.Arg("name", testName0)), 70 expected: []string{testName0}, 71 }, 72 // test filter by id `config ls --filter id=xxx` 73 { 74 filters: filters.NewArgs(filters.Arg("id", config1ID)), 75 expected: []string{testName1}, 76 }, 77 // test filter by label `config ls --filter label=xxx` 78 { 79 filters: filters.NewArgs(filters.Arg("label", "type")), 80 expected: testNames, 81 }, 82 { 83 filters: filters.NewArgs(filters.Arg("label", "type=test")), 84 expected: []string{testName0}, 85 }, 86 { 87 filters: filters.NewArgs(filters.Arg("label", "type=production")), 88 expected: []string{testName1}, 89 }, 90 } 91 for _, tc := range testCases { 92 entries, err = client.ConfigList(ctx, types.ConfigListOptions{ 93 Filters: tc.filters, 94 }) 95 assert.NilError(t, err) 96 assert.Check(t, is.DeepEqual(names(entries), tc.expected)) 97 98 } 99 } 100 101 func createConfig(ctx context.Context, t *testing.T, client client.APIClient, name string, data []byte, labels map[string]string) string { 102 config, err := client.ConfigCreate(ctx, swarmtypes.ConfigSpec{ 103 Annotations: swarmtypes.Annotations{ 104 Name: name, 105 Labels: labels, 106 }, 107 Data: data, 108 }) 109 assert.NilError(t, err) 110 assert.Check(t, config.ID != "") 111 return config.ID 112 } 113 114 func TestConfigsCreateAndDelete(t *testing.T) { 115 skip.If(t, testEnv.DaemonInfo.OSType != "linux") 116 117 defer setupTest(t)() 118 d := swarm.NewSwarm(t, testEnv) 119 defer d.Stop(t) 120 client := d.NewClientT(t) 121 defer client.Close() 122 123 ctx := context.Background() 124 125 testName := "test_config" 126 127 // This test case is ported from the original TestConfigsCreate 128 configID := createConfig(ctx, t, client, testName, []byte("TESTINGDATA"), nil) 129 130 insp, _, err := client.ConfigInspectWithRaw(ctx, configID) 131 assert.NilError(t, err) 132 assert.Check(t, is.Equal(insp.Spec.Name, testName)) 133 134 // This test case is ported from the original TestConfigsDelete 135 err = client.ConfigRemove(ctx, configID) 136 assert.NilError(t, err) 137 138 insp, _, err = client.ConfigInspectWithRaw(ctx, configID) 139 testutil.ErrorContains(t, err, "No such config") 140 } 141 142 func TestConfigsUpdate(t *testing.T) { 143 skip.If(t, testEnv.DaemonInfo.OSType != "linux") 144 145 defer setupTest(t)() 146 d := swarm.NewSwarm(t, testEnv) 147 defer d.Stop(t) 148 client := d.NewClientT(t) 149 defer client.Close() 150 151 ctx := context.Background() 152 153 testName := "test_config" 154 155 // This test case is ported from the original TestConfigsCreate 156 configID := createConfig(ctx, t, client, testName, []byte("TESTINGDATA"), nil) 157 158 insp, _, err := client.ConfigInspectWithRaw(ctx, configID) 159 assert.NilError(t, err) 160 assert.Check(t, is.Equal(insp.ID, configID)) 161 162 // test UpdateConfig with full ID 163 insp.Spec.Labels = map[string]string{"test": "test1"} 164 err = client.ConfigUpdate(ctx, configID, insp.Version, insp.Spec) 165 assert.NilError(t, err) 166 167 insp, _, err = client.ConfigInspectWithRaw(ctx, configID) 168 assert.NilError(t, err) 169 assert.Check(t, is.Equal(insp.Spec.Labels["test"], "test1")) 170 171 // test UpdateConfig with full name 172 insp.Spec.Labels = map[string]string{"test": "test2"} 173 err = client.ConfigUpdate(ctx, testName, insp.Version, insp.Spec) 174 assert.NilError(t, err) 175 176 insp, _, err = client.ConfigInspectWithRaw(ctx, configID) 177 assert.NilError(t, err) 178 assert.Check(t, is.Equal(insp.Spec.Labels["test"], "test2")) 179 180 // test UpdateConfig with prefix ID 181 insp.Spec.Labels = map[string]string{"test": "test3"} 182 err = client.ConfigUpdate(ctx, configID[:1], insp.Version, insp.Spec) 183 assert.NilError(t, err) 184 185 insp, _, err = client.ConfigInspectWithRaw(ctx, configID) 186 assert.NilError(t, err) 187 assert.Check(t, is.Equal(insp.Spec.Labels["test"], "test3")) 188 189 // test UpdateConfig in updating Data which is not supported in daemon 190 // this test will produce an error in func UpdateConfig 191 insp.Spec.Data = []byte("TESTINGDATA2") 192 err = client.ConfigUpdate(ctx, configID, insp.Version, insp.Spec) 193 testutil.ErrorContains(t, err, "only updates to Labels are allowed") 194 } 195 196 func TestTemplatedConfig(t *testing.T) { 197 d := swarm.NewSwarm(t, testEnv) 198 defer d.Stop(t) 199 client := d.NewClientT(t) 200 defer client.Close() 201 ctx := context.Background() 202 203 referencedSecretSpec := swarmtypes.SecretSpec{ 204 Annotations: swarmtypes.Annotations{ 205 Name: "referencedsecret", 206 }, 207 Data: []byte("this is a secret"), 208 } 209 referencedSecret, err := client.SecretCreate(ctx, referencedSecretSpec) 210 assert.Check(t, err) 211 212 referencedConfigSpec := swarmtypes.ConfigSpec{ 213 Annotations: swarmtypes.Annotations{ 214 Name: "referencedconfig", 215 }, 216 Data: []byte("this is a config"), 217 } 218 referencedConfig, err := client.ConfigCreate(ctx, referencedConfigSpec) 219 assert.Check(t, err) 220 221 configSpec := swarmtypes.ConfigSpec{ 222 Annotations: swarmtypes.Annotations{ 223 Name: "templated_config", 224 }, 225 Templating: &swarmtypes.Driver{ 226 Name: "golang", 227 }, 228 Data: []byte("SERVICE_NAME={{.Service.Name}}\n" + 229 "{{secret \"referencedsecrettarget\"}}\n" + 230 "{{config \"referencedconfigtarget\"}}\n"), 231 } 232 233 templatedConfig, err := client.ConfigCreate(ctx, configSpec) 234 assert.Check(t, err) 235 236 serviceID := swarm.CreateService(t, d, 237 swarm.ServiceWithConfig( 238 &swarmtypes.ConfigReference{ 239 File: &swarmtypes.ConfigReferenceFileTarget{ 240 Name: "/templated_config", 241 UID: "0", 242 GID: "0", 243 Mode: 0600, 244 }, 245 ConfigID: templatedConfig.ID, 246 ConfigName: "templated_config", 247 }, 248 ), 249 swarm.ServiceWithConfig( 250 &swarmtypes.ConfigReference{ 251 File: &swarmtypes.ConfigReferenceFileTarget{ 252 Name: "referencedconfigtarget", 253 UID: "0", 254 GID: "0", 255 Mode: 0600, 256 }, 257 ConfigID: referencedConfig.ID, 258 ConfigName: "referencedconfig", 259 }, 260 ), 261 swarm.ServiceWithSecret( 262 &swarmtypes.SecretReference{ 263 File: &swarmtypes.SecretReferenceFileTarget{ 264 Name: "referencedsecrettarget", 265 UID: "0", 266 GID: "0", 267 Mode: 0600, 268 }, 269 SecretID: referencedSecret.ID, 270 SecretName: "referencedsecret", 271 }, 272 ), 273 swarm.ServiceWithName("svc"), 274 ) 275 276 var tasks []swarmtypes.Task 277 waitAndAssert(t, 60*time.Second, func(t *testing.T) bool { 278 tasks = swarm.GetRunningTasks(t, d, serviceID) 279 return len(tasks) > 0 280 }) 281 282 task := tasks[0] 283 waitAndAssert(t, 60*time.Second, func(t *testing.T) bool { 284 if task.NodeID == "" || (task.Status.ContainerStatus == nil || task.Status.ContainerStatus.ContainerID == "") { 285 task, _, _ = client.TaskInspectWithRaw(context.Background(), task.ID) 286 } 287 return task.NodeID != "" && task.Status.ContainerStatus != nil && task.Status.ContainerStatus.ContainerID != "" 288 }) 289 290 attach := swarm.ExecTask(t, d, task, types.ExecConfig{ 291 Cmd: []string{"/bin/cat", "/templated_config"}, 292 AttachStdout: true, 293 AttachStderr: true, 294 }) 295 296 expect := "SERVICE_NAME=svc\n" + 297 "this is a secret\n" + 298 "this is a config\n" 299 assertAttachedStream(t, attach, expect) 300 301 attach = swarm.ExecTask(t, d, task, types.ExecConfig{ 302 Cmd: []string{"mount"}, 303 AttachStdout: true, 304 AttachStderr: true, 305 }) 306 assertAttachedStream(t, attach, "tmpfs on /templated_config type tmpfs") 307 } 308 309 func assertAttachedStream(t *testing.T, attach types.HijackedResponse, expect string) { 310 buf := bytes.NewBuffer(nil) 311 _, err := stdcopy.StdCopy(buf, buf, attach.Reader) 312 assert.NilError(t, err) 313 assert.Check(t, is.Contains(buf.String(), expect)) 314 } 315 316 func waitAndAssert(t *testing.T, timeout time.Duration, f func(*testing.T) bool) { 317 t.Helper() 318 after := time.After(timeout) 319 for { 320 select { 321 case <-after: 322 t.Fatalf("timed out waiting for condition") 323 default: 324 } 325 if f(t) { 326 return 327 } 328 time.Sleep(100 * time.Millisecond) 329 } 330 } 331 332 func TestConfigInspect(t *testing.T) { 333 skip.If(t, testEnv.DaemonInfo.OSType != "linux") 334 335 defer setupTest(t)() 336 d := swarm.NewSwarm(t, testEnv) 337 defer d.Stop(t) 338 client := d.NewClientT(t) 339 defer client.Close() 340 341 ctx := context.Background() 342 343 testName := t.Name() 344 configID := createConfig(ctx, t, client, testName, []byte("TESTINGDATA"), nil) 345 346 insp, body, err := client.ConfigInspectWithRaw(ctx, configID) 347 assert.NilError(t, err) 348 assert.Check(t, is.Equal(insp.Spec.Name, testName)) 349 350 var config swarmtypes.Config 351 err = json.Unmarshal(body, &config) 352 assert.NilError(t, err) 353 assert.Check(t, is.DeepEqual(config, insp)) 354 }