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"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-03-14 06:21:21 -04:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2018-01-11 22:05:18 -05:00
|
|
|
"github.com/docker/docker/client"
|
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"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/poll"
|
2018-04-19 05:14:15 -04:00
|
|
|
"gotest.tools/skip"
|
2018-01-11 22:05:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultSwarmPort = 2477
|
|
|
|
|
|
|
|
func TestInspectNetwork(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
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)
|
2018-04-13 11:02:56 -04:00
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
overlayName := "overlay1"
|
2018-06-11 12:11:22 -04:00
|
|
|
overlayID := network.CreateNoError(t, context.Background(), client, overlayName,
|
|
|
|
network.WithDriver("overlay"),
|
|
|
|
network.WithCheckDuplicate(),
|
|
|
|
)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
var instances uint64 = 4
|
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),
|
|
|
|
swarm.ServiceWithNetwork(overlayName),
|
|
|
|
)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2018-03-27 00:06:46 -04:00
|
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2018-06-11 12:11:22 -04:00
|
|
|
_, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
// Test inspect verbose with full NetworkID
|
|
|
|
networkVerbose, err := client.NetworkInspect(context.Background(), overlayID, types.NetworkInspectOptions{
|
|
|
|
Verbose: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, validNetworkVerbose(networkVerbose, serviceName, instances))
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
// Test inspect verbose with partial NetworkID
|
|
|
|
networkVerbose, err = client.NetworkInspect(context.Background(), overlayID[0:11], types.NetworkInspectOptions{
|
|
|
|
Verbose: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, validNetworkVerbose(networkVerbose, serviceName, instances))
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
// Test inspect verbose with Network name and swarm scope
|
|
|
|
networkVerbose, err = client.NetworkInspect(context.Background(), overlayName, types.NetworkInspectOptions{
|
|
|
|
Verbose: true,
|
|
|
|
Scope: "swarm",
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, validNetworkVerbose(networkVerbose, serviceName, instances))
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
err = client.ServiceRemove(context.Background(), serviceID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2018-03-27 00:06:46 -04:00
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2018-04-05 17:26:29 -04:00
|
|
|
serviceID2 := swarm.CreateService(t, d,
|
|
|
|
swarm.ServiceWithReplicas(instances),
|
|
|
|
swarm.ServiceWithName(serviceName),
|
|
|
|
swarm.ServiceWithNetwork(overlayName),
|
|
|
|
)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2018-03-27 00:06:46 -04:00
|
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
err = client.ServiceRemove(context.Background(), serviceID2)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
2018-03-27 00:06:46 -04:00
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID2), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
err = client.NetworkRemove(context.Background(), overlayID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-11 22:05:18 -05:00
|
|
|
|
|
|
|
poll.WaitOn(t, networkIsRemoved(client, overlayID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("service", serviceID)
|
|
|
|
tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return poll.Error(err)
|
|
|
|
case len(tasks) == int(instances):
|
|
|
|
for _, task := range tasks {
|
2018-03-14 06:21:21 -04:00
|
|
|
if task.Status.State != swarmtypes.TaskStateRunning {
|
2018-01-11 22:05:18 -05:00
|
|
|
return poll.Continue("waiting for tasks to enter run state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
default:
|
|
|
|
return poll.Continue("task count at %d waiting for %d", len(tasks), instances)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkIsRemoved(client client.NetworkAPIClient, networkID string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
_, err := client.NetworkInspect(context.Background(), networkID, types.NetworkInspectOptions{})
|
|
|
|
if err == nil {
|
|
|
|
return poll.Continue("waiting for network %s to be removed", networkID)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceIsRemoved(client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("service", serviceID)
|
|
|
|
_, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return poll.Continue("waiting for service %s to be deleted", serviceID)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func noTasks(client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return poll.Error(err)
|
|
|
|
case len(tasks) == 0:
|
|
|
|
return poll.Success()
|
|
|
|
default:
|
|
|
|
return poll.Continue("task count at %d waiting for 0", len(tasks))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if Service and Tasks info are part of the inspect verbose response
|
|
|
|
func validNetworkVerbose(network types.NetworkResource, service string, instances uint64) bool {
|
|
|
|
if service, ok := network.Services[service]; ok {
|
2018-05-11 17:37:01 -04:00
|
|
|
if len(service.Tasks) != int(instances) {
|
|
|
|
return false
|
2018-01-11 22:05:18 -05:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 17:37:01 -04:00
|
|
|
|
|
|
|
if network.IPAM.Config == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cfg := range network.IPAM.Config {
|
|
|
|
if cfg.Gateway == "" || cfg.Subnet == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2018-01-11 22:05:18 -05:00
|
|
|
}
|