TestCreateServiceSysctls tests that a service created with sysctl options in the ContainerSpec correctly applies those options. To test this, we're going to create a service with the sysctl option {"net.ipv4.ip_nonlocal_bind": "0"} We'll get the service's tasks to get the container ID, and then
(t *testing.T)
| 333 | // confident won't be modified by the container runtime, and won't blow |
| 334 | // anything up in the test environment |
| 335 | func TestCreateServiceSysctls(t *testing.T) { |
| 336 | ctx := setupTest(t) |
| 337 | |
| 338 | d := swarm.NewSwarm(ctx, t, testEnv) |
| 339 | defer d.Stop(t) |
| 340 | apiClient := d.NewClientT(t) |
| 341 | defer apiClient.Close() |
| 342 | |
| 343 | // run this block twice, so that no matter what the default value of |
| 344 | // net.ipv4.ip_nonlocal_bind is, we can verify that setting the sysctl |
| 345 | // options works |
| 346 | for _, expected := range []string{"0", "1"} { |
| 347 | // store the map we're going to be using everywhere. |
| 348 | expectedSysctls := map[string]string{"net.ipv4.ip_nonlocal_bind": expected} |
| 349 | |
| 350 | // Create the service with the sysctl options |
| 351 | var instances uint64 = 1 |
| 352 | serviceID := swarm.CreateService(ctx, t, d, |
| 353 | swarm.ServiceWithSysctls(expectedSysctls), |
| 354 | ) |
| 355 | |
| 356 | // wait for the service to converge to 1 running task as expected |
| 357 | poll.WaitOn(t, swarm.RunningTasksCount(ctx, apiClient, serviceID, instances)) |
| 358 | |
| 359 | // we're going to check 3 things: |
| 360 | // |
| 361 | // 1. Does the container, when inspected, have the sysctl option set? |
| 362 | // 2. Does the task have the sysctl in the spec? |
| 363 | // 3. Does the service have the sysctl in the spec? |
| 364 | // |
| 365 | // if all 3 of these things are true, we know that the sysctl has been |
| 366 | // plumbed correctly through the engine. |
| 367 | // |
| 368 | // We don't actually have to get inside the container and check its |
| 369 | // logs or anything. If we see the sysctl set on the container inspect, |
| 370 | // we know that the sysctl is plumbed correctly. everything below that |
| 371 | // level has been tested elsewhere. (thanks @thaJeztah, because an |
| 372 | // earlier version of this test had to get container logs and was much |
| 373 | // more complex) |
| 374 | |
| 375 | // get all tasks of the service, so we can get the container |
| 376 | taskList, err := apiClient.TaskList(ctx, client.TaskListOptions{ |
| 377 | Filters: make(client.Filters).Add("service", serviceID), |
| 378 | }) |
| 379 | assert.NilError(t, err) |
| 380 | assert.Check(t, is.Equal(len(taskList.Items), 1)) |
| 381 | |
| 382 | // verify that the container has the sysctl option set |
| 383 | inspect, err := apiClient.ContainerInspect(ctx, taskList.Items[0].Status.ContainerStatus.ContainerID, client.ContainerInspectOptions{}) |
| 384 | assert.NilError(t, err) |
| 385 | assert.DeepEqual(t, inspect.Container.HostConfig.Sysctls, expectedSysctls) |
| 386 | |
| 387 | // verify that the task has the sysctl option set in the task object |
| 388 | assert.DeepEqual(t, taskList.Items[0].Spec.ContainerSpec.Sysctls, expectedSysctls) |
| 389 | |
| 390 | // verify that the service also has the sysctl set in the spec. |
| 391 | result, err := apiClient.ServiceInspect(ctx, serviceID, client.ServiceInspectOptions{}) |
| 392 | assert.NilError(t, err) |
nothing calls this directly
no test coverage detected
searching dependent graphs…