2018-02-05 16:05:59 -05:00
|
|
|
package service // import "github.com/docker/docker/integration/service"
|
2017-06-14 17:32:15 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-06-14 17:32:15 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-08-07 10:07:17 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2018-02-02 17:36:59 -05:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2018-02-09 13:13:26 -05:00
|
|
|
"github.com/docker/docker/integration/internal/swarm"
|
2017-12-22 16:30:49 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-06-14 17:32:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInspect(t *testing.T) {
|
2019-01-03 06:19:15 -05:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon)
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2017-08-30 12:01:01 -04:00
|
|
|
defer setupTest(t)()
|
2018-02-02 17:36:59 -05:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
2017-06-14 17:32:15 -04:00
|
|
|
defer d.Stop(t)
|
2018-04-13 11:02:56 -04:00
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
2017-06-14 17:32:15 -04:00
|
|
|
|
2017-12-22 16:30:49 -05:00
|
|
|
var now = time.Now()
|
2017-06-14 17:32:15 -04:00
|
|
|
var instances uint64 = 2
|
2019-10-27 19:42:47 -04:00
|
|
|
serviceSpec := fullSwarmServiceSpec("test-service-inspect"+t.Name(), instances)
|
2017-06-14 17:32:15 -04:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
resp, err := client.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{
|
|
|
|
QueryRegistry: false,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-14 17:32:15 -04:00
|
|
|
|
|
|
|
id := resp.ID
|
2019-09-09 11:48:12 -04:00
|
|
|
poll.WaitOn(t, swarm.RunningTasksCount(client, id, instances))
|
2017-06-14 17:32:15 -04:00
|
|
|
|
|
|
|
service, _, err := client.ServiceInspectWithRaw(ctx, id, types.ServiceInspectOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-22 16:30:49 -05:00
|
|
|
|
|
|
|
expected := swarmtypes.Service{
|
|
|
|
ID: id,
|
|
|
|
Spec: serviceSpec,
|
|
|
|
Meta: swarmtypes.Meta{
|
|
|
|
Version: swarmtypes.Version{Index: uint64(11)},
|
|
|
|
CreatedAt: now,
|
|
|
|
UpdatedAt: now,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Check(t, is.DeepEqual(service, expected, cmpServiceOpts()))
|
|
|
|
}
|
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
// TODO: use helpers from gotest.tools/assert/opt when available
|
2017-12-22 16:30:49 -05:00
|
|
|
func cmpServiceOpts() cmp.Option {
|
|
|
|
const threshold = 20 * time.Second
|
|
|
|
|
|
|
|
metaTimeFields := func(path cmp.Path) bool {
|
|
|
|
switch path.String() {
|
|
|
|
case "Meta.CreatedAt", "Meta.UpdatedAt":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
withinThreshold := cmp.Comparer(func(x, y time.Time) bool {
|
|
|
|
delta := x.Sub(y)
|
|
|
|
return delta < threshold && delta > -threshold
|
|
|
|
})
|
|
|
|
|
|
|
|
return cmp.FilterPath(metaTimeFields, withinThreshold)
|
2017-06-14 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2018-02-02 17:36:59 -05:00
|
|
|
func fullSwarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
|
2017-06-14 17:32:15 -04:00
|
|
|
restartDelay := 100 * time.Millisecond
|
|
|
|
maxAttempts := uint64(4)
|
|
|
|
|
2018-02-02 17:36:59 -05:00
|
|
|
return swarmtypes.ServiceSpec{
|
|
|
|
Annotations: swarmtypes.Annotations{
|
2017-06-14 17:32:15 -04:00
|
|
|
Name: name,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"service-label": "service-label-value",
|
|
|
|
},
|
|
|
|
},
|
2018-02-02 17:36:59 -05:00
|
|
|
TaskTemplate: swarmtypes.TaskSpec{
|
|
|
|
ContainerSpec: &swarmtypes.ContainerSpec{
|
2017-06-14 17:32:15 -04:00
|
|
|
Image: "busybox:latest",
|
|
|
|
Labels: map[string]string{"container-label": "container-value"},
|
|
|
|
Command: []string{"/bin/top"},
|
|
|
|
Args: []string{"-u", "root"},
|
|
|
|
Hostname: "hostname",
|
|
|
|
Env: []string{"envvar=envvalue"},
|
|
|
|
Dir: "/work",
|
|
|
|
User: "root",
|
|
|
|
StopSignal: "SIGINT",
|
|
|
|
StopGracePeriod: &restartDelay,
|
|
|
|
Hosts: []string{"8.8.8.8 google"},
|
2018-02-02 17:36:59 -05:00
|
|
|
DNSConfig: &swarmtypes.DNSConfig{
|
2017-06-14 17:32:15 -04:00
|
|
|
Nameservers: []string{"8.8.8.8"},
|
|
|
|
Search: []string{"somedomain"},
|
|
|
|
},
|
2017-08-07 10:07:17 -04:00
|
|
|
Isolation: container.IsolationDefault,
|
2017-06-14 17:32:15 -04:00
|
|
|
},
|
2018-02-02 17:36:59 -05:00
|
|
|
RestartPolicy: &swarmtypes.RestartPolicy{
|
2017-06-14 17:32:15 -04:00
|
|
|
Delay: &restartDelay,
|
2018-02-02 17:36:59 -05:00
|
|
|
Condition: swarmtypes.RestartPolicyConditionOnFailure,
|
2017-06-14 17:32:15 -04:00
|
|
|
MaxAttempts: &maxAttempts,
|
|
|
|
},
|
2018-02-02 17:36:59 -05:00
|
|
|
Runtime: swarmtypes.RuntimeContainer,
|
2017-06-14 17:32:15 -04:00
|
|
|
},
|
2018-02-02 17:36:59 -05:00
|
|
|
Mode: swarmtypes.ServiceMode{
|
|
|
|
Replicated: &swarmtypes.ReplicatedService{
|
2017-06-14 17:32:15 -04:00
|
|
|
Replicas: &replicas,
|
|
|
|
},
|
|
|
|
},
|
2018-02-02 17:36:59 -05:00
|
|
|
UpdateConfig: &swarmtypes.UpdateConfig{
|
2017-06-14 17:32:15 -04:00
|
|
|
Parallelism: 2,
|
|
|
|
Delay: 200 * time.Second,
|
2018-02-02 17:36:59 -05:00
|
|
|
FailureAction: swarmtypes.UpdateFailureActionContinue,
|
2017-06-14 17:32:15 -04:00
|
|
|
Monitor: 2 * time.Second,
|
|
|
|
MaxFailureRatio: 0.2,
|
2018-02-02 17:36:59 -05:00
|
|
|
Order: swarmtypes.UpdateOrderStopFirst,
|
2017-06-14 17:32:15 -04:00
|
|
|
},
|
2018-02-02 17:36:59 -05:00
|
|
|
RollbackConfig: &swarmtypes.UpdateConfig{
|
2017-06-14 17:32:15 -04:00
|
|
|
Parallelism: 3,
|
|
|
|
Delay: 300 * time.Second,
|
2018-02-02 17:36:59 -05:00
|
|
|
FailureAction: swarmtypes.UpdateFailureActionPause,
|
2017-06-14 17:32:15 -04:00
|
|
|
Monitor: 3 * time.Second,
|
|
|
|
MaxFailureRatio: 0.3,
|
2018-02-02 17:36:59 -05:00
|
|
|
Order: swarmtypes.UpdateOrderStartFirst,
|
2017-06-14 17:32:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|