2021-06-25 17:08:40 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-06-15 22:24:07 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-25 17:08:40 +02:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2022-06-15 21:55:58 +02:00
|
|
|
|
|
|
|
"github.com/docker/compose/v2/cmd/compose"
|
2021-06-25 17:08:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DockerExecutableName is the OS dependent Docker CLI binary name
|
|
|
|
DockerExecutableName = "docker"
|
2021-12-07 05:49:04 +01:00
|
|
|
|
|
|
|
// DockerComposeExecutableName is the OS dependent Docker CLI binary name
|
|
|
|
DockerComposeExecutableName = "docker-" + compose.PluginName
|
|
|
|
|
|
|
|
// DockerScanExecutableName is the OS dependent Docker CLI binary name
|
|
|
|
DockerScanExecutableName = "docker-scan"
|
2021-06-25 17:08:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
DockerExecutableName = DockerExecutableName + ".exe"
|
2021-12-07 05:49:04 +01:00
|
|
|
DockerComposeExecutableName = DockerComposeExecutableName + ".exe"
|
|
|
|
DockerScanExecutableName = DockerScanExecutableName + ".exe"
|
2021-06-25 17:08:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
// CLI is used to wrap the CLI for end to end testing
|
|
|
|
type CLI struct {
|
2021-06-25 17:08:40 +02:00
|
|
|
ConfigDir string
|
|
|
|
}
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
// NewParallelCLI returns a configured CLI with t.Parallel() set
|
|
|
|
func NewParallelCLI(t *testing.T) *CLI {
|
2021-06-25 17:08:40 +02:00
|
|
|
t.Parallel()
|
2022-06-15 21:55:58 +02:00
|
|
|
return NewCLI(t)
|
2021-06-25 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
// NewCLI returns a CLI to use for E2E tests
|
|
|
|
func NewCLI(t testing.TB) *CLI {
|
2021-06-25 17:08:40 +02:00
|
|
|
d, err := ioutil.TempDir("", "")
|
|
|
|
assert.Check(t, is.Nil(err))
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if t.Failed() {
|
|
|
|
conf, _ := ioutil.ReadFile(filepath.Join(d, "config.json"))
|
|
|
|
t.Errorf("Config: %s\n", string(conf))
|
|
|
|
t.Error("Contents of config dir:")
|
|
|
|
for _, p := range dirContents(d) {
|
|
|
|
t.Errorf(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = os.RemoveAll(d)
|
|
|
|
})
|
|
|
|
|
|
|
|
_ = os.MkdirAll(filepath.Join(d, "cli-plugins"), 0755)
|
2021-12-07 05:49:04 +01:00
|
|
|
composePlugin, err := findExecutable(DockerComposeExecutableName, []string{"../../bin", "../../../bin"})
|
2021-06-25 17:08:40 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fmt.Println("WARNING: docker-compose cli-plugin not found")
|
|
|
|
}
|
|
|
|
if err == nil {
|
2021-12-07 05:49:04 +01:00
|
|
|
err = CopyFile(composePlugin, filepath.Join(d, "cli-plugins", DockerComposeExecutableName))
|
2021-06-25 17:08:40 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// We don't need a functional scan plugin, but a valid plugin binary
|
2021-12-07 05:49:04 +01:00
|
|
|
err = CopyFile(composePlugin, filepath.Join(d, "cli-plugins", DockerScanExecutableName))
|
2021-06-25 17:08:40 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
return &CLI{ConfigDir: d}
|
2021-06-25 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func dirContents(dir string) []string {
|
2021-11-25 19:15:46 +01:00
|
|
|
var res []string
|
2021-06-25 17:08:40 +02:00
|
|
|
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
2021-11-25 19:15:46 +01:00
|
|
|
res = append(res, path)
|
2021-06-25 17:08:40 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func findExecutable(executableName string, paths []string) (string, error) {
|
|
|
|
for _, p := range paths {
|
|
|
|
bin, err := filepath.Abs(path.Join(p, executableName))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(bin); os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.Wrap(os.ErrNotExist, "executable not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyFile copies a file from a sourceFile to a destinationFile setting permissions to 0755
|
|
|
|
func CopyFile(sourceFile string, destinationFile string) error {
|
|
|
|
src, err := os.Open(sourceFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// nolint: errcheck
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
dst, err := os.OpenFile(destinationFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// nolint: errcheck
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-15 22:24:07 +02:00
|
|
|
// BaseEnvironment provides the minimal environment variables used across all
|
|
|
|
// Docker / Compose commands.
|
|
|
|
func (c *CLI) BaseEnvironment() []string {
|
|
|
|
return []string{
|
|
|
|
"DOCKER_CONFIG=" + c.ConfigDir,
|
|
|
|
"KUBECONFIG=invalid",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 17:08:40 +02:00
|
|
|
// NewCmd creates a cmd object configured with the test environment set
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) NewCmd(command string, args ...string) icmd.Cmd {
|
2021-06-25 17:08:40 +02:00
|
|
|
return icmd.Cmd{
|
|
|
|
Command: append([]string{command}, args...),
|
2022-06-15 22:24:07 +02:00
|
|
|
Env: c.BaseEnvironment(),
|
2021-06-25 17:08:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:59:18 +02:00
|
|
|
// NewCmdWithEnv creates a cmd object configured with the test environment set with additional env vars
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) NewCmdWithEnv(envvars []string, command string, args ...string) icmd.Cmd {
|
2022-06-15 22:24:07 +02:00
|
|
|
cmdEnv := append(c.BaseEnvironment(), envvars...)
|
2022-05-31 14:59:18 +02:00
|
|
|
return icmd.Cmd{
|
|
|
|
Command: append([]string{command}, args...),
|
2022-06-15 22:24:07 +02:00
|
|
|
Env: cmdEnv,
|
2022-05-31 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 17:08:40 +02:00
|
|
|
// MetricsSocket get the path where test metrics will be sent
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) MetricsSocket() string {
|
2021-06-25 17:08:40 +02:00
|
|
|
return filepath.Join(c.ConfigDir, "./docker-cli.sock")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerCmd creates a docker cmd without running it
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) NewDockerCmd(args ...string) icmd.Cmd {
|
2021-06-25 17:08:40 +02:00
|
|
|
return c.NewCmd(DockerExecutableName, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunDockerOrExitError runs a docker command and returns a result
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) RunDockerOrExitError(t testing.TB, args ...string) *icmd.Result {
|
|
|
|
fmt.Printf("\t[%s] docker %s\n", t.Name(), strings.Join(args, " "))
|
2021-06-25 17:08:40 +02:00
|
|
|
return icmd.RunCmd(c.NewDockerCmd(args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCmd runs a command, expects no error and returns a result
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) RunCmd(t testing.TB, args ...string) *icmd.Result {
|
|
|
|
fmt.Printf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
|
|
|
|
assert.Assert(t, len(args) >= 1, "require at least one command in parameters")
|
2021-06-25 17:08:40 +02:00
|
|
|
res := icmd.RunCmd(c.NewCmd(args[0], args[1:]...))
|
2022-06-15 21:55:58 +02:00
|
|
|
res.Assert(t, icmd.Success)
|
2021-06-25 17:08:40 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-14 10:48:57 +01:00
|
|
|
// RunCmdInDir runs a command in a given dir, expects no error and returns a result
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) RunCmdInDir(t testing.TB, dir string, args ...string) *icmd.Result {
|
|
|
|
fmt.Printf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
|
|
|
|
assert.Assert(t, len(args) >= 1, "require at least one command in parameters")
|
2021-12-14 10:48:57 +01:00
|
|
|
cmd := c.NewCmd(args[0], args[1:]...)
|
|
|
|
cmd.Dir = dir
|
|
|
|
res := icmd.RunCmd(cmd)
|
2022-06-15 21:55:58 +02:00
|
|
|
res.Assert(t, icmd.Success)
|
2021-12-14 10:48:57 +01:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-25 17:08:40 +02:00
|
|
|
// RunDockerCmd runs a docker command, expects no error and returns a result
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) RunDockerCmd(t testing.TB, args ...string) *icmd.Result {
|
2021-12-07 05:49:04 +01:00
|
|
|
if len(args) > 0 && args[0] == compose.PluginName {
|
2022-06-15 21:55:58 +02:00
|
|
|
t.Fatal("This test called 'RunDockerCmd' for 'compose'. Please prefer 'RunDockerComposeCmd' to be able to test as a plugin and standalone")
|
2021-12-07 05:49:04 +01:00
|
|
|
}
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerOrExitError(t, args...)
|
|
|
|
res.Assert(t, icmd.Success)
|
2021-06-25 17:08:40 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-07 05:49:04 +01:00
|
|
|
// RunDockerComposeCmd runs a docker compose command, expects no error and returns a result
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) RunDockerComposeCmd(t testing.TB, args ...string) *icmd.Result {
|
|
|
|
res := c.RunDockerComposeCmdNoCheck(t, args...)
|
|
|
|
res.Assert(t, icmd.Success)
|
2022-03-30 11:47:34 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunDockerComposeCmdNoCheck runs a docker compose command, don't presume of any expectation and returns a result
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) RunDockerComposeCmdNoCheck(t testing.TB, args ...string) *icmd.Result {
|
2022-06-15 22:24:07 +02:00
|
|
|
return icmd.RunCmd(c.NewDockerComposeCmd(t, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerComposeCmd creates a command object for Compose, either in plugin
|
|
|
|
// or standalone mode (based on build tags).
|
|
|
|
func (c *CLI) NewDockerComposeCmd(t testing.TB, args ...string) icmd.Cmd {
|
|
|
|
t.Helper()
|
2021-12-09 10:30:05 +01:00
|
|
|
if composeStandaloneMode {
|
2022-06-15 22:24:07 +02:00
|
|
|
return c.NewCmd(ComposeStandalonePath(t), args...)
|
2021-12-07 05:49:04 +01:00
|
|
|
}
|
|
|
|
args = append([]string{"compose"}, args...)
|
2022-06-15 22:24:07 +02:00
|
|
|
return c.NewCmd(DockerExecutableName, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComposeStandalonePath returns the path to the locally-built Compose
|
|
|
|
// standalone binary from the repo.
|
|
|
|
//
|
|
|
|
// This function will fail the test immediately if invoked when not running
|
|
|
|
// in standalone test mode.
|
|
|
|
func ComposeStandalonePath(t testing.TB) string {
|
|
|
|
t.Helper()
|
|
|
|
if !composeStandaloneMode {
|
|
|
|
require.Fail(t, "Not running in standalone mode")
|
|
|
|
}
|
|
|
|
composeBinary, err := findExecutable(DockerComposeExecutableName, []string{"../../bin", "../../../bin"})
|
|
|
|
require.NoError(t, err, "Could not find standalone Compose binary (%q)",
|
|
|
|
DockerComposeExecutableName)
|
|
|
|
return composeBinary
|
2021-12-07 05:49:04 +01:00
|
|
|
}
|
|
|
|
|
2021-06-25 17:08:40 +02:00
|
|
|
// StdoutContains returns a predicate on command result expecting a string in stdout
|
|
|
|
func StdoutContains(expected string) func(*icmd.Result) bool {
|
|
|
|
return func(res *icmd.Result) bool {
|
|
|
|
return strings.Contains(res.Stdout(), expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForCmdResult try to execute a cmd until resulting output matches given predicate
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) WaitForCmdResult(t testing.TB, command icmd.Cmd, predicate func(*icmd.Result) bool, timeout time.Duration, delay time.Duration) {
|
|
|
|
assert.Assert(t, timeout.Nanoseconds() > delay.Nanoseconds(), "timeout must be greater than delay")
|
2021-06-25 17:08:40 +02:00
|
|
|
var res *icmd.Result
|
|
|
|
checkStopped := func(logt poll.LogT) poll.Result {
|
2022-06-15 21:55:58 +02:00
|
|
|
fmt.Printf("\t[%s] %s\n", t.Name(), strings.Join(command.Command, " "))
|
2021-06-25 17:08:40 +02:00
|
|
|
res = icmd.RunCmd(command)
|
|
|
|
if !predicate(res) {
|
|
|
|
return poll.Continue("Cmd output did not match requirement: %q", res.Combined())
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
2022-06-15 21:55:58 +02:00
|
|
|
poll.WaitOn(t, checkStopped, poll.WithDelay(delay), poll.WithTimeout(timeout))
|
2021-06-25 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForCondition wait for predicate to execute to true
|
2022-06-15 21:55:58 +02:00
|
|
|
func (c *CLI) WaitForCondition(t testing.TB, predicate func() (bool, string), timeout time.Duration, delay time.Duration) {
|
2021-06-25 17:08:40 +02:00
|
|
|
checkStopped := func(logt poll.LogT) poll.Result {
|
|
|
|
pass, description := predicate()
|
|
|
|
if !pass {
|
|
|
|
return poll.Continue("Condition not met: %q", description)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
2022-06-15 21:55:58 +02:00
|
|
|
poll.WaitOn(t, checkStopped, poll.WithDelay(delay), poll.WithTimeout(timeout))
|
2021-06-25 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 05:49:04 +01:00
|
|
|
// Lines split output into lines
|
2021-06-25 17:08:40 +02:00
|
|
|
func Lines(output string) []string {
|
|
|
|
return strings.Split(strings.TrimSpace(output), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPGetWithRetry performs an HTTP GET on an `endpoint`, using retryDelay also as a request timeout.
|
|
|
|
// In the case of an error or the response status is not the expeted one, it retries the same request,
|
|
|
|
// returning the response body as a string (empty if we could not reach it)
|
2022-06-15 21:55:58 +02:00
|
|
|
func HTTPGetWithRetry(t testing.TB, endpoint string, expectedStatus int, retryDelay time.Duration, timeout time.Duration) string {
|
2021-06-25 17:08:40 +02:00
|
|
|
var (
|
|
|
|
r *http.Response
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: retryDelay,
|
|
|
|
}
|
|
|
|
fmt.Printf("\t[%s] GET %s\n", t.Name(), endpoint)
|
|
|
|
checkUp := func(t poll.LogT) poll.Result {
|
|
|
|
r, err = client.Get(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return poll.Continue("reaching %q: Error %s", endpoint, err.Error())
|
|
|
|
}
|
|
|
|
if r.StatusCode == expectedStatus {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("reaching %q: %d != %d", endpoint, r.StatusCode, expectedStatus)
|
|
|
|
}
|
|
|
|
poll.WaitOn(t, checkUp, poll.WithDelay(retryDelay), poll.WithTimeout(timeout))
|
|
|
|
if r != nil {
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|