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"},
|
Fix flaky TestInspect
This test has been flaky for a long time, failing with:
--- FAIL: TestInspect (12.04s)
inspect_test.go:39: timeout hit after 10s: waiting for tasks to enter run state. task failed with error: task: non-zero exit (1)
While looking through logs, noticed tasks were started, entering RUNNING stage,
and then exited, to be started again.
state.transition="STARTING->RUNNING"
...
msg="fatal task error" error="task: non-zero exit (1)"
...
state.transition="RUNNING->FAILED"
Looking for possible reasons, first considering network issues (possibly we ran
out of IP addresses or networking not cleaned up), then I spotted the issue.
The service is started with;
Command: []string{"/bin/top"},
Args: []string{"-u", "root"},
The `-u root` is not an argument for the service, but for `/bin/top`. While the
Ubuntu/Debian/GNU version `top` has a -u/-U option;
docker run --rm ubuntu:20.04 top -h 2>&1 | grep '\-u'
top -hv | -bcEHiOSs1 -d secs -n max -u|U user -p pid(s) -o field -w [cols]
The *busybox* version of top does not:
docker run --rm busybox top --help 2>&1 | grep '\-u'
So running `top -u root` would cause the task to fail;
docker run --rm busybox top -u root
top: invalid option -- u
...
echo $?
1
As a result, the service went into a crash-loop, and because the `poll.WaitOn()`
was running with a short interval, in many cases would _just_ find the RUNNING
state, perform the `service inspect`, and pass, but in other cases, it would not
be that lucky, and continue polling untill we reached the 10 seconds timeout,
and mark the test as failed.
Looking for history of this option (was it previously using a different image?) I
found this was added in 6cd6d8646a90fa2013416bc8f11bd78d72c4180d, but probably
just missed during review.
Given that the option is only set to have "something" to inspect, I replaced
the `-u root` with `-d 5`, which makes top refresh with a 5 second interval.
Note that there is another test (`TestServiceListWithStatuses) that uses the same
spec, however, that test is skipped based on API version of the test-daemon, and
(to be looked into), when performing that check, no API version is known, causing
the test to (always?) be skipped:
=== RUN TestServiceListWithStatuses
--- SKIP: TestServiceListWithStatuses (0.00s)
list_test.go:34: versions.LessThan(testEnv.DaemonInfo.ServerVersion, "1.41")
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-04-03 13:24:12 -04:00
|
|
|
Args: []string{"-d", "5"},
|
2017-06-14 17:32:15 -04:00
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|