mirror of https://github.com/docker/cli.git
Move common e2e things into an internal package.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
683b6226ed
commit
677d17150a
|
@ -5,22 +5,13 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/docker/cli/internal/test/environment"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
if err := setupTestEnv(); err != nil {
|
if err := environment.Setup(); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move to shared internal package
|
|
||||||
func setupTestEnv() error {
|
|
||||||
dockerHost := os.Getenv("TEST_DOCKER_HOST")
|
|
||||||
if dockerHost == "" {
|
|
||||||
return errors.New("$TEST_DOCKER_HOST must be set")
|
|
||||||
}
|
|
||||||
return os.Setenv("DOCKER_HOST", dockerHost)
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,14 +4,17 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"github.com/docker/cli/internal/test/environment"
|
||||||
shlex "github.com/flynn-archive/go-shlex"
|
shlex "github.com/flynn-archive/go-shlex"
|
||||||
"github.com/gotestyourself/gotestyourself/golden"
|
"github.com/gotestyourself/gotestyourself/golden"
|
||||||
"github.com/gotestyourself/gotestyourself/icmd"
|
"github.com/gotestyourself/gotestyourself/icmd"
|
||||||
|
"github.com/gotestyourself/gotestyourself/poll"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var pollSettings = environment.DefaultPollSettings
|
||||||
|
|
||||||
func TestRemove(t *testing.T) {
|
func TestRemove(t *testing.T) {
|
||||||
stackname := "test-stack-remove"
|
stackname := "test-stack-remove"
|
||||||
deployFullStack(t, stackname)
|
deployFullStack(t, stackname)
|
||||||
|
@ -29,21 +32,24 @@ func deployFullStack(t *testing.T, stackname string) {
|
||||||
"docker stack deploy --compose-file=./testdata/full-stack.yml %s", stackname))
|
"docker stack deploy --compose-file=./testdata/full-stack.yml %s", stackname))
|
||||||
result.Assert(t, icmd.Success)
|
result.Assert(t, icmd.Success)
|
||||||
|
|
||||||
waitOn(t, taskCount(stackname, 2), 0)
|
poll.WaitOn(t, taskCount(stackname, 2), pollSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanupFullStack(t *testing.T, stackname string) {
|
func cleanupFullStack(t *testing.T, stackname string) {
|
||||||
result := icmd.RunCmd(shell(t, "docker stack rm %s", stackname))
|
result := icmd.RunCmd(shell(t, "docker stack rm %s", stackname))
|
||||||
result.Assert(t, icmd.Success)
|
result.Assert(t, icmd.Success)
|
||||||
waitOn(t, taskCount(stackname, 0), 0)
|
poll.WaitOn(t, taskCount(stackname, 0), pollSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskCount(stackname string, expected int) func() (bool, error) {
|
func taskCount(stackname string, expected int) func(t poll.LogT) poll.Result {
|
||||||
return func() (bool, error) {
|
return func(poll.LogT) poll.Result {
|
||||||
result := icmd.RunCommand(
|
result := icmd.RunCommand(
|
||||||
"docker", "stack", "ps", "-f=desired-state=running", stackname)
|
"docker", "stack", "ps", "-f=desired-state=running", stackname)
|
||||||
count := lines(result.Stdout()) - 1
|
count := lines(result.Stdout()) - 1
|
||||||
return count == expected, nil
|
if count == expected {
|
||||||
|
return poll.Success()
|
||||||
|
}
|
||||||
|
return poll.Continue("task count is %d waiting for %d", count, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,33 +63,3 @@ func shell(t *testing.T, format string, args ...interface{}) icmd.Cmd {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return icmd.Cmd{Command: cmd}
|
return icmd.Cmd{Command: cmd}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move to gotestyourself
|
|
||||||
func waitOn(t *testing.T, check func() (bool, error), timeout time.Duration) {
|
|
||||||
if timeout == time.Duration(0) {
|
|
||||||
timeout = defaultTimeout()
|
|
||||||
}
|
|
||||||
|
|
||||||
after := time.After(timeout)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-after:
|
|
||||||
// TODO: include check function name in error message
|
|
||||||
t.Fatalf("timeout hit after %s", timeout)
|
|
||||||
default:
|
|
||||||
// TODO: maybe return a failure message as well?
|
|
||||||
done, err := check()
|
|
||||||
if done {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func defaultTimeout() time.Duration {
|
|
||||||
// TODO: support override from environment variable
|
|
||||||
return 10 * time.Second
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package environment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gotestyourself/gotestyourself/poll"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Setup a new environment
|
||||||
|
func Setup() error {
|
||||||
|
dockerHost := os.Getenv("TEST_DOCKER_HOST")
|
||||||
|
if dockerHost == "" {
|
||||||
|
return errors.New("$TEST_DOCKER_HOST must be set")
|
||||||
|
}
|
||||||
|
return os.Setenv("DOCKER_HOST", dockerHost)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultPollSettings used with gotestyourself/poll
|
||||||
|
var DefaultPollSettings = poll.WithDelay(100 * time.Millisecond)
|
Loading…
Reference in New Issue