|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | "github.com/docker/docker/api/types" |
| 8 | + "github.com/docker/docker/api/types/filters" |
8 | 9 | swarmtypes "github.com/docker/docker/api/types/swarm" |
| 10 | + "github.com/docker/docker/api/types/versions" |
9 | 11 | "github.com/docker/docker/client" |
10 | 12 | "github.com/docker/docker/integration/internal/network" |
11 | 13 | "github.com/docker/docker/integration/internal/swarm" |
@@ -248,6 +250,91 @@ func TestServiceUpdateNetwork(t *testing.T) { |
248 | 250 | assert.NilError(t, err) |
249 | 251 | } |
250 | 252 |
|
| 253 | +// TestServiceUpdatePidsLimit tests creating and updating a service with PidsLimit |
| 254 | +func TestServiceUpdatePidsLimit(t *testing.T) { |
| 255 | + skip.If( |
| 256 | + t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.41"), |
| 257 | + "setting pidslimit for services is not supported before api v1.41", |
| 258 | + ) |
| 259 | + skip.If(t, testEnv.DaemonInfo.OSType != "linux") |
| 260 | + tests := []struct { |
| 261 | + name string |
| 262 | + pidsLimit int64 |
| 263 | + expected int64 |
| 264 | + }{ |
| 265 | + { |
| 266 | + name: "create service with PidsLimit 300", |
| 267 | + pidsLimit: 300, |
| 268 | + expected: 300, |
| 269 | + }, |
| 270 | + { |
| 271 | + name: "unset PidsLimit to 0", |
| 272 | + pidsLimit: 0, |
| 273 | + expected: 0, |
| 274 | + }, |
| 275 | + { |
| 276 | + name: "update PidsLimit to 100", |
| 277 | + pidsLimit: 100, |
| 278 | + expected: 100, |
| 279 | + }, |
| 280 | + } |
| 281 | + |
| 282 | + defer setupTest(t)() |
| 283 | + d := swarm.NewSwarm(t, testEnv) |
| 284 | + defer d.Stop(t) |
| 285 | + cli := d.NewClientT(t) |
| 286 | + defer func() { _ = cli.Close() }() |
| 287 | + ctx := context.Background() |
| 288 | + var ( |
| 289 | + serviceID string |
| 290 | + service swarmtypes.Service |
| 291 | + ) |
| 292 | + for i, tc := range tests { |
| 293 | + t.Run(tc.name, func(t *testing.T) { |
| 294 | + if i == 0 { |
| 295 | + serviceID = swarm.CreateService(t, d, swarm.ServiceWithPidsLimit(tc.pidsLimit)) |
| 296 | + } else { |
| 297 | + service = getService(t, cli, serviceID) |
| 298 | + service.Spec.TaskTemplate.ContainerSpec.PidsLimit = tc.pidsLimit |
| 299 | + _, err := cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) |
| 300 | + assert.NilError(t, err) |
| 301 | + poll.WaitOn(t, serviceIsUpdated(cli, serviceID), swarm.ServicePoll) |
| 302 | + } |
| 303 | + |
| 304 | + poll.WaitOn(t, swarm.RunningTasksCount(cli, serviceID, 1), swarm.ServicePoll) |
| 305 | + service = getService(t, cli, serviceID) |
| 306 | + container := getServiceTaskContainer(ctx, t, cli, serviceID) |
| 307 | + assert.Equal(t, service.Spec.TaskTemplate.ContainerSpec.PidsLimit, tc.expected) |
| 308 | + if tc.expected == 0 { |
| 309 | + if container.HostConfig.Resources.PidsLimit != nil { |
| 310 | + t.Fatalf("Expected container.HostConfig.Resources.PidsLimit to be nil") |
| 311 | + } |
| 312 | + } else { |
| 313 | + assert.Assert(t, container.HostConfig.Resources.PidsLimit != nil) |
| 314 | + assert.Equal(t, *container.HostConfig.Resources.PidsLimit, tc.expected) |
| 315 | + } |
| 316 | + }) |
| 317 | + } |
| 318 | + |
| 319 | + err := cli.ServiceRemove(ctx, serviceID) |
| 320 | + assert.NilError(t, err) |
| 321 | +} |
| 322 | + |
| 323 | +func getServiceTaskContainer(ctx context.Context, t *testing.T, cli client.APIClient, serviceID string) types.ContainerJSON { |
| 324 | + t.Helper() |
| 325 | + filter := filters.NewArgs() |
| 326 | + filter.Add("service", serviceID) |
| 327 | + filter.Add("desired-state", "running") |
| 328 | + tasks, err := cli.TaskList(ctx, types.TaskListOptions{Filters: filter}) |
| 329 | + assert.NilError(t, err) |
| 330 | + assert.Assert(t, len(tasks) > 0) |
| 331 | + |
| 332 | + ctr, err := cli.ContainerInspect(ctx, tasks[0].Status.ContainerStatus.ContainerID) |
| 333 | + assert.NilError(t, err) |
| 334 | + assert.Equal(t, ctr.State.Running, true) |
| 335 | + return ctr |
| 336 | +} |
| 337 | + |
251 | 338 | func getService(t *testing.T, cli client.ServiceAPIClient, serviceID string) swarmtypes.Service { |
252 | 339 | t.Helper() |
253 | 340 | service, _, err := cli.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) |
|
0 commit comments