1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

integration/secret: TestTemplatedSecret: simplify task code

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-08-19 14:57:21 +02:00
parent de78663181
commit 3c6f018f94
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -15,6 +15,7 @@ import (
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/poll"
"gotest.tools/v3/skip" "gotest.tools/v3/skip"
) )
@ -301,21 +302,12 @@ func TestTemplatedSecret(t *testing.T) {
swarm.ServiceWithName(serviceName), swarm.ServiceWithName(serviceName),
) )
var tasks []swarmtypes.Task poll.WaitOn(t, swarm.RunningTasksCount(c, serviceID, 1), swarm.ServicePoll, poll.WithTimeout(1*time.Minute))
waitAndAssert(t, 60*time.Second, func(t *testing.T) bool {
tasks = swarm.GetRunningTasks(t, c, serviceID)
return len(tasks) > 0
})
task := tasks[0] tasks := swarm.GetRunningTasks(t, c, serviceID)
waitAndAssert(t, 60*time.Second, func(t *testing.T) bool { assert.Assert(t, len(tasks) > 0, "no running tasks found for service %s", serviceID)
if task.NodeID == "" || (task.Status.ContainerStatus == nil || task.Status.ContainerStatus.ContainerID == "") {
task, _, _ = c.TaskInspectWithRaw(context.Background(), task.ID)
}
return task.NodeID != "" && task.Status.ContainerStatus != nil && task.Status.ContainerStatus.ContainerID != ""
})
attach := swarm.ExecTask(t, d, task, types.ExecConfig{ attach := swarm.ExecTask(t, d, tasks[0], types.ExecConfig{
Cmd: []string{"/bin/cat", "/run/secrets/templated_secret"}, Cmd: []string{"/bin/cat", "/run/secrets/templated_secret"},
AttachStdout: true, AttachStdout: true,
AttachStderr: true, AttachStderr: true,
@ -326,7 +318,7 @@ func TestTemplatedSecret(t *testing.T) {
"this is a config\n" "this is a config\n"
assertAttachedStream(t, attach, expect) assertAttachedStream(t, attach, expect)
attach = swarm.ExecTask(t, d, task, types.ExecConfig{ attach = swarm.ExecTask(t, d, tasks[0], types.ExecConfig{
Cmd: []string{"mount"}, Cmd: []string{"mount"},
AttachStdout: true, AttachStdout: true,
AttachStderr: true, AttachStderr: true,
@ -391,22 +383,6 @@ func assertAttachedStream(t *testing.T, attach types.HijackedResponse, expect st
assert.Check(t, is.Contains(buf.String(), expect)) assert.Check(t, is.Contains(buf.String(), expect))
} }
func waitAndAssert(t *testing.T, timeout time.Duration, f func(*testing.T) bool) {
t.Helper()
after := time.After(timeout)
for {
select {
case <-after:
t.Fatal("timed out waiting for condition")
default:
}
if f(t) {
return
}
time.Sleep(100 * time.Millisecond)
}
}
func secretNamesFromList(entries []swarmtypes.Secret) []string { func secretNamesFromList(entries []swarmtypes.Secret) []string {
var values []string var values []string
for _, entry := range entries { for _, entry := range entries {