moby/integration/network/inspect_test.go
Yong Tang e485a60e2b Move serviceRunningTasksCount to integration/internal/swarm
This fix moves multiple places of serviceRunningTasksCount
to one location in integration/internal/swarm, so that
code duplication could be removed.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
2019-01-19 18:54:32 +00:00

102 lines
2.5 KiB
Go

package network // import "github.com/docker/docker/integration/network"
import (
"context"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/integration/internal/network"
"github.com/docker/docker/integration/internal/swarm"
"gotest.tools/assert"
"gotest.tools/poll"
"gotest.tools/skip"
)
func TestInspectNetwork(t *testing.T) {
skip.If(t, testEnv.OSType == "windows", "FIXME")
defer setupTest(t)()
d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t)
c := d.NewClientT(t)
defer c.Close()
networkName := "Overlay" + t.Name()
overlayID := network.CreateNoError(t, context.Background(), c, networkName,
network.WithDriver("overlay"),
network.WithCheckDuplicate(),
)
var instances uint64 = 2
serviceName := "TestService" + t.Name()
serviceID := swarm.CreateService(t, d,
swarm.ServiceWithReplicas(instances),
swarm.ServiceWithName(serviceName),
swarm.ServiceWithNetwork(networkName),
)
poll.WaitOn(t, swarm.RunningTasksCount(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",
},
},
}
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))
}
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)
}