migrate TestAPINetworkConnectDisconnect to integration test#51568
migrate TestAPINetworkConnectDisconnect to integration test#515682003Aditya wants to merge 1 commit intomoby:masterfrom
Conversation
3082e80 to
b49de7f
Compare
integration/network/network_test.go
Outdated
| createNet, err := apiClient.NetworkCreate(ctx, name, client.NetworkCreateOptions{}) | ||
| assert.NilError(t, err) | ||
|
|
||
| nr, err := apiClient.NetworkInspect(ctx, createNet.ID, client.NetworkInspectOptions{}) | ||
| assert.NilError(t, err) | ||
|
|
||
| assert.Equal(t, nr.Network.Name, name) | ||
| assert.Equal(t, nr.Network.ID, createNet.ID) | ||
| assert.Equal(t, len(nr.Network.Containers), 0) | ||
|
|
||
| t.Cleanup(func() { | ||
| _, _ = apiClient.NetworkRemove(ctx, nr.Network.ID, client.NetworkRemoveOptions{}) | ||
| }) |
There was a problem hiding this comment.
We've, surprisingly, got a lot of test client utils that make the tests shorter ... there are examples in bridge_linux_test.go and others.
Putting the cleanup after the assertions that follow the NetworkCreate mean the network won't be deleted if any of those assertions fails.
So, this would be ...
network.CreateNoError(ctx, t, apiClient, name)
defer network.RemoveNoError(ctx, t, apiClient, name)
<the inspect code>
integration/network/network_test.go
Outdated
| createResp, err := apiClient.ContainerCreate(ctx, client.ContainerCreateOptions{ | ||
| Name: containerName, | ||
| Config: &container.Config{ | ||
| Image: "busybox", | ||
| Cmd: []string{"top"}, | ||
| }, | ||
| }) | ||
| assert.NilError(t, err) | ||
|
|
||
| t.Cleanup(func() { | ||
| _, _ = apiClient.ContainerRemove(ctx, createResp.ID, client.ContainerRemoveOptions{}) | ||
| }) | ||
|
|
||
| _, err = apiClient.ContainerStart(ctx, createResp.ID, client.ContainerStartOptions{}) | ||
| assert.NilError(t, err) |
There was a problem hiding this comment.
This can be ...
id := container.Run(ctx, t, apiClient, container.WithName(containerName))
defer func() {
_, _ = apiClient.ContainerRemove(ctx, id, client.ContainerRemoveOptions{})
})
The test client defaults to busybox/top if no image/command is specified.
integration/network/network_test.go
Outdated
| assert.Assert(t, found, fmt.Sprintf("%s is not found", networkName)) | ||
| } | ||
|
|
||
| func TestAPINetworkConnectDisconnect(t *testing.T) { |
There was a problem hiding this comment.
We have other tests that connect/disconnect networks - but I think this one is interesting because it creates a container connected to the default bridge, then connects a user-defined network.
We're probably light on coverage for that, and likely have bugs in the area, because it's not allowed in a create/run command ...
$ docker run --rm -ti --network bridge --network n1 alpine
docker: conflicting options: cannot attach both user-defined and non-user-defined network-modes
It'd be worth adding a comment to say that's what it's for. Maybe rename the test to TestBridgeAndCustomNetworkAttach?
|
Oh, for the lint failures - the integration-cli functions can be removed if they're unused ... |
53eaff5 to
171ab7c
Compare
robmry
left a comment
There was a problem hiding this comment.
Thank you @2003Aditya.
A couple of minor nits below - but it's quite hard to see at a glance what the test is doing. I think it'd be worth adding comments describing each of the steps (similar to the original test).
integration/network/network_test.go
Outdated
| nr, err = apiClient.NetworkInspect(ctx, name, client.NetworkInspectOptions{}) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, len(nr.Network.Containers), 0) | ||
|
|
954275e to
98d8e60
Compare
Signed-off-by: Aditya Mishra <mishraaditya675@gmail.com>
98d8e60 to
eae4ec4
Compare
robmry
left a comment
There was a problem hiding this comment.
Thank you - some more comments, to align with conventions we try to stick to in the tests ...
| assert.Equal(t, nr.Network.Name, name) | ||
| assert.Equal(t, len(nr.Network.Containers), 0) | ||
|
|
||
| // starting a container without attaching to the network created above |
There was a problem hiding this comment.
It'd be worth noting this connects the container to the default bridge network - as that's really the value in this test (to have a container connected to both the default bridge and a user-defined network).
| // starting a container without attaching to the network created above | |
| // Start a container on the default bridge network. |
| _, _ = apiClient.ContainerRemove(ctx, id, client.ContainerRemoveOptions{}) | ||
| }() | ||
|
|
||
| // attaching the custom network to the above running conainer . |
There was a problem hiding this comment.
Typo conainer ...
| // attaching the custom network to the above running conainer . | |
| // Attach the container to a custom bridge network. |
| assert.NilError(t, err) | ||
| assert.Equal(t, nr.Network.Name, name) | ||
| assert.Equal(t, len(nr.Network.Containers), 0) |
There was a problem hiding this comment.
It's worth thinking about whether each assertion needs to stop the test here and in the rest of this test (and in-general).
In this case, if the inspect fails, things are probably so broken it's not worth continuing.
But, in the unlikely event there's a container attached to the new network - might as well let the test carry on to see what else happens.
| assert.NilError(t, err) | |
| assert.Equal(t, nr.Network.Name, name) | |
| assert.Equal(t, len(nr.Network.Containers), 0) | |
| assert.NilError(t, err) | |
| assert.Check(t, is.Equal(nr.Network.Name, name)) | |
| assert.Check(t, is.Equal(len(nr.Network.Containers), 0)) |
| var containerIP string | ||
| for n, settings := range contInspect.Container.NetworkSettings.Networks { | ||
| if n == name { | ||
| containerIP = settings.IPAddress.String() | ||
| break | ||
| } | ||
| } | ||
| assert.Assert(t, containerIP != "") | ||
| assert.Equal(t, nr.Network.Containers[id].IPv4Address.Addr().String(), containerIP) |
There was a problem hiding this comment.
Networks is a map, so there's no need to loop - and the addresses have type netip.Addr, it's best not to convert to strings unless necessary ...
| var containerIP string | |
| for n, settings := range contInspect.Container.NetworkSettings.Networks { | |
| if n == name { | |
| containerIP = settings.IPAddress.String() | |
| break | |
| } | |
| } | |
| assert.Assert(t, containerIP != "") | |
| assert.Equal(t, nr.Network.Containers[id].IPv4Address.Addr().String(), containerIP) | |
| n, ok := contInspect.Container.NetworkSettings.Networks[name] | |
| if assert.Check(t, ok) { | |
| assert.Check(t, n.IPAddress.IsValid()) | |
| assert.Check(t, is.Equal(nr.Network.Containers[id].IPv4Address.Addr(), n.IPAddress)) | |
| } |
| // starting a container without attaching to the network created above | ||
| id := container.Run(ctx, t, apiClient, container.WithName(containerName)) | ||
| defer func() { | ||
| _, _ = apiClient.ContainerRemove(ctx, id, client.ContainerRemoveOptions{}) |
There was a problem hiding this comment.
We usually force removal for cleanup, to make sure it happens ...
| _, _ = apiClient.ContainerRemove(ctx, id, client.ContainerRemoveOptions{}) | |
| _, _ = apiClient.ContainerRemove(ctx, id, client.ContainerRemoveOptions{Force: true}) |
- What I did
Migrated the
TestAPINetworkConnectDisconnecttest fromintegration-cli/docker_api_network_test.goto the new integration test framework underintegration/network/network_test.go.