moby/integration/network/inspect_test.go
Albin Kerouanton 78479b1915
libnet: Make sure network names are unique
Fixes #18864, #20648, #33561, #40901.

[This GH comment][1] makes clear network name uniqueness has never been
enforced due to the eventually consistent nature of Classic Swarm
datastores:

> there is no guaranteed way to check for duplicates across a cluster of
> docker hosts.

And this is further confirmed by other comments made by @mrjana in that
same issue, eg. [this one][2]:

> we want to adopt a schema which can pave the way in the future for a
> completely decentralized cluster of docker hosts (if scalability is
> needed).

This decentralized model is what Classic Swarm was trying to be. It's
been superseded since then by Docker Swarm, which has a centralized
control plane.

To circumvent this drawback, the `NetworkCreate` endpoint accepts a
`CheckDuplicate` flag. However it's not perfectly reliable as it won't
catch concurrent requests.

Due to this design decision, API clients like Compose have to implement
workarounds to make sure names are really unique (eg.
docker/compose#9585). And the daemon itself has seen a string of issues
due to that decision, including some that aren't fixed to this day (for
instance moby/moby#40901):

> The problem is, that if you specify a network for a container using
> the ID, it will add that network to the container but it will then
> change it to reference the network by using the name.

To summarize, this "feature" is broken, has no practical use and is a
source of pain for Docker users and API consumers. So let's just remove
it for _all_ API versions.

[1]: https://github.com/moby/moby/issues/18864#issuecomment-167201414
[2]: https://github.com/moby/moby/issues/18864#issuecomment-167202589

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-09-12 10:40:13 +02:00

103 lines
2.6 KiB
Go

package network // import "github.com/docker/docker/integration/network"
import (
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/integration/internal/network"
"github.com/docker/docker/integration/internal/swarm"
"github.com/docker/docker/testutil"
"gotest.tools/v3/assert"
"gotest.tools/v3/poll"
"gotest.tools/v3/skip"
)
func TestInspectNetwork(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "FIXME")
skip.If(t, testEnv.IsRootless, "rootless mode doesn't support Swarm-mode")
ctx := setupTest(t)
d := swarm.NewSwarm(ctx, t, testEnv)
defer d.Stop(t)
c := d.NewClientT(t)
defer c.Close()
networkName := "Overlay" + t.Name()
overlayID := network.CreateNoError(ctx, t, c, networkName,
network.WithDriver("overlay"),
)
var instances uint64 = 2
serviceName := "TestService" + t.Name()
serviceID := swarm.CreateService(ctx, t, d,
swarm.ServiceWithReplicas(instances),
swarm.ServiceWithName(serviceName),
swarm.ServiceWithNetwork(networkName),
)
poll.WaitOn(t, swarm.RunningTasksCount(ctx, c, serviceID, instances), swarm.ServicePoll)
tests := []struct {
name string
network string
opts types.NetworkInspectOptions
}{
{
name: "full network id",
network: overlayID,
opts: types.NetworkInspectOptions{
Verbose: true,
},
},
{
name: "partial network id",
network: overlayID[0:11],
opts: types.NetworkInspectOptions{
Verbose: true,
},
},
{
name: "network name",
network: networkName,
opts: types.NetworkInspectOptions{
Verbose: true,
},
},
{
name: "network name and swarm scope",
network: networkName,
opts: types.NetworkInspectOptions{
Verbose: true,
Scope: "swarm",
},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
ctx := testutil.StartSpan(ctx, t)
nw, err := c.NetworkInspect(ctx, tc.network, tc.opts)
assert.NilError(t, err)
if service, ok := nw.Services[serviceName]; ok {
assert.Equal(t, len(service.Tasks), int(instances))
}
assert.Assert(t, nw.IPAM.Config != nil)
for _, cfg := range nw.IPAM.Config {
assert.Assert(t, cfg.Gateway != "")
assert.Assert(t, cfg.Subnet != "")
}
})
}
// TODO find out why removing networks is needed; other tests fail if the network is not removed, even though they run on a new daemon.
err := c.ServiceRemove(ctx, serviceID)
assert.NilError(t, err)
poll.WaitOn(t, swarm.NoTasksForService(ctx, c, serviceID), swarm.ServicePoll)
err = c.NetworkRemove(ctx, overlayID)
assert.NilError(t, err)
poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll)
}