2018-02-05 16:05:59 -05:00
|
|
|
package network // import "github.com/docker/docker/integration/network"
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2018-01-11 22:05:18 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-06-11 12:11:22 -04:00
|
|
|
"github.com/docker/docker/integration/internal/network"
|
2018-03-14 06:21:21 -04:00
|
|
|
"github.com/docker/docker/integration/internal/swarm"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-01-11 22:05:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInspectNetwork(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2020-03-13 09:37:09 -04:00
|
|
|
skip.If(t, testEnv.IsRootless, "rootless mode doesn't support Swarm-mode")
|
2018-01-11 22:05:18 -05:00
|
|
|
defer setupTest(t)()
|
2018-03-14 06:21:21 -04:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
2018-01-11 22:05:18 -05:00
|
|
|
defer d.Stop(t)
|
2019-01-13 19:47:17 -05:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
defer c.Close()
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2019-01-13 19:47:17 -05:00
|
|
|
networkName := "Overlay" + t.Name()
|
2019-06-06 06:00:19 -04:00
|
|
|
overlayID := network.CreateNoError(context.Background(), t, c, networkName,
|
2018-06-11 12:11:22 -04:00
|
|
|
network.WithDriver("overlay"),
|
|
|
|
network.WithCheckDuplicate(),
|
|
|
|
)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2019-01-13 19:47:17 -05:00
|
|
|
var instances uint64 = 2
|
2018-05-28 18:57:52 -04:00
|
|
|
serviceName := "TestService" + t.Name()
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2018-04-05 17:26:29 -04:00
|
|
|
serviceID := swarm.CreateService(t, d,
|
|
|
|
swarm.ServiceWithReplicas(instances),
|
|
|
|
swarm.ServiceWithName(serviceName),
|
2019-01-13 19:47:17 -05:00
|
|
|
swarm.ServiceWithNetwork(networkName),
|
2018-04-05 17:26:29 -04:00
|
|
|
)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2019-01-19 13:54:32 -05:00
|
|
|
poll.WaitOn(t, swarm.RunningTasksCount(c, serviceID, instances), swarm.ServicePoll)
|
2019-01-13 19:47:17 -05:00
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.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))
|
|
|
|
}
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2019-01-13 19:47:17 -05:00
|
|
|
assert.Assert(t, nw.IPAM.Config != nil)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2019-01-13 19:47:17 -05:00
|
|
|
for _, cfg := range nw.IPAM.Config {
|
|
|
|
assert.Assert(t, cfg.Gateway != "")
|
|
|
|
assert.Assert(t, cfg.Subnet != "")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2019-01-13 19:47:17 -05:00
|
|
|
// 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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-01-13 20:20:09 -05:00
|
|
|
poll.WaitOn(t, swarm.NoTasksForService(ctx, c, serviceID), swarm.ServicePoll)
|
2019-01-13 19:47:17 -05:00
|
|
|
err = c.NetworkRemove(ctx, overlayID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-01-13 19:47:17 -05:00
|
|
|
poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll)
|
2018-01-11 22:05:18 -05:00
|
|
|
}
|