Merge pull request #43285 from thaJeztah/cleanup_testutils
integration-cli: cleanup some of the test-utilities
This commit is contained in:
commit
eef2d20e23
5 changed files with 44 additions and 83 deletions
|
@ -32,32 +32,27 @@ func DockerCmd(t testing.TB, args ...string) *icmd.Result {
|
|||
|
||||
// BuildCmd executes the specified docker build command and expect a success
|
||||
func BuildCmd(t testing.TB, name string, cmdOperators ...CmdOperator) *icmd.Result {
|
||||
return Docker(Build(name), cmdOperators...).Assert(t, icmd.Success)
|
||||
return Docker(Args("build", "-t", name), cmdOperators...).Assert(t, icmd.Success)
|
||||
}
|
||||
|
||||
// InspectCmd executes the specified docker inspect command and expect a success
|
||||
func InspectCmd(t testing.TB, name string, cmdOperators ...CmdOperator) *icmd.Result {
|
||||
return Docker(Inspect(name), cmdOperators...).Assert(t, icmd.Success)
|
||||
return Docker(Args("inspect", name), cmdOperators...).Assert(t, icmd.Success)
|
||||
}
|
||||
|
||||
// WaitRun will wait for the specified container to be running, maximum 5 seconds.
|
||||
func WaitRun(t testing.TB, name string, cmdOperators ...CmdOperator) {
|
||||
WaitForInspectResult(t, name, "{{.State.Running}}", "true", 5*time.Second, cmdOperators...)
|
||||
waitForInspectResult(t, name, "{{.State.Running}}", "true", 5*time.Second, cmdOperators...)
|
||||
}
|
||||
|
||||
// WaitExited will wait for the specified container to state exit, subject
|
||||
// to a maximum time limit in seconds supplied by the caller
|
||||
func WaitExited(t testing.TB, name string, timeout time.Duration, cmdOperators ...CmdOperator) {
|
||||
WaitForInspectResult(t, name, "{{.State.Status}}", "exited", timeout, cmdOperators...)
|
||||
waitForInspectResult(t, name, "{{.State.Status}}", "exited", timeout, cmdOperators...)
|
||||
}
|
||||
|
||||
// WaitRestart will wait for the specified container to restart once
|
||||
func WaitRestart(t testing.TB, name string, timeout time.Duration, cmdOperators ...CmdOperator) {
|
||||
WaitForInspectResult(t, name, "{{.RestartCount}}", "1", timeout, cmdOperators...)
|
||||
}
|
||||
|
||||
// WaitForInspectResult waits for the specified expression to be equals to the specified expected string in the given time.
|
||||
func WaitForInspectResult(t testing.TB, name, expr, expected string, timeout time.Duration, cmdOperators ...CmdOperator) {
|
||||
// waitForInspectResult waits for the specified expression to be equals to the specified expected string in the given time.
|
||||
func waitForInspectResult(t testing.TB, name, expr, expected string, timeout time.Duration, cmdOperators ...CmdOperator) {
|
||||
after := time.After(timeout)
|
||||
|
||||
args := []string{"inspect", "-f", expr, name}
|
||||
|
@ -100,7 +95,7 @@ func Docker(cmd icmd.Cmd, cmdOperators ...CmdOperator) *icmd.Result {
|
|||
defer deferFn()
|
||||
}
|
||||
}
|
||||
appendDocker(&cmd)
|
||||
cmd.Command = append([]string{testEnv.DockerBinary()}, cmd.Command...)
|
||||
if err := validateArgs(cmd.Command...); err != nil {
|
||||
return &icmd.Result{
|
||||
Error: err,
|
||||
|
@ -127,16 +122,6 @@ func validateArgs(args ...string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Build executes the specified docker build command
|
||||
func Build(name string) icmd.Cmd {
|
||||
return icmd.Command("build", "-t", name)
|
||||
}
|
||||
|
||||
// Inspect executes the specified docker inspect command
|
||||
func Inspect(name string) icmd.Cmd {
|
||||
return icmd.Command("inspect", name)
|
||||
}
|
||||
|
||||
// Format sets the specified format with --format flag
|
||||
func Format(format string) func(*icmd.Cmd) func() {
|
||||
return func(cmd *icmd.Cmd) func() {
|
||||
|
@ -148,20 +133,9 @@ func Format(format string) func(*icmd.Cmd) func() {
|
|||
}
|
||||
}
|
||||
|
||||
func appendDocker(cmd *icmd.Cmd) {
|
||||
cmd.Command = append([]string{testEnv.DockerBinary()}, cmd.Command...)
|
||||
}
|
||||
|
||||
// Args build an icmd.Cmd struct from the specified arguments
|
||||
func Args(args ...string) icmd.Cmd {
|
||||
switch len(args) {
|
||||
case 0:
|
||||
return icmd.Cmd{}
|
||||
case 1:
|
||||
return icmd.Command(args[0])
|
||||
default:
|
||||
return icmd.Command(args[0], args[1:]...)
|
||||
}
|
||||
// Args build an icmd.Cmd struct from the specified (command and) arguments.
|
||||
func Args(commandAndArgs ...string) icmd.Cmd {
|
||||
return icmd.Cmd{Command: commandAndArgs}
|
||||
}
|
||||
|
||||
// Daemon points to the specified daemon
|
||||
|
|
|
@ -384,7 +384,7 @@ func (s *DockerCLIBuildSuite) TestBuildCacheAdd(c *testing.T) {
|
|||
cli.BuildCmd(c, name, build.WithDockerfile(fmt.Sprintf(`FROM scratch
|
||||
ADD %s/robots.txt /`, server.URL())))
|
||||
|
||||
result := cli.Docker(cli.Build(name), build.WithDockerfile(fmt.Sprintf(`FROM scratch
|
||||
result := cli.Docker(cli.Args("build", "-t", name), build.WithDockerfile(fmt.Sprintf(`FROM scratch
|
||||
ADD %s/index.html /`, server.URL())))
|
||||
result.Assert(c, icmd.Success)
|
||||
if strings.Contains(result.Combined(), "Using cache") {
|
||||
|
@ -3777,13 +3777,13 @@ func (s *DockerCLIBuildSuite) TestBuildSpaces(c *testing.T) {
|
|||
ctx := fakecontext.New(c, "", fakecontext.WithDockerfile("FROM busybox\nCOPY\n"))
|
||||
defer ctx.Close()
|
||||
|
||||
result1 := cli.Docker(cli.Build(name), build.WithExternalBuildContext(ctx))
|
||||
result1 := cli.Docker(cli.Args("build", "-t", name), build.WithExternalBuildContext(ctx))
|
||||
result1.Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
})
|
||||
|
||||
ctx.Add("Dockerfile", "FROM busybox\nCOPY ")
|
||||
result2 := cli.Docker(cli.Build(name), build.WithExternalBuildContext(ctx))
|
||||
result2 := cli.Docker(cli.Args("build", "-t", name), build.WithExternalBuildContext(ctx))
|
||||
result2.Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
})
|
||||
|
@ -3802,7 +3802,7 @@ func (s *DockerCLIBuildSuite) TestBuildSpaces(c *testing.T) {
|
|||
}
|
||||
|
||||
ctx.Add("Dockerfile", "FROM busybox\n COPY")
|
||||
result2 = cli.Docker(cli.Build(name), build.WithoutCache, build.WithExternalBuildContext(ctx))
|
||||
result2 = cli.Docker(cli.Args("build", "-t", name), build.WithoutCache, build.WithExternalBuildContext(ctx))
|
||||
result2.Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
})
|
||||
|
@ -3817,7 +3817,7 @@ func (s *DockerCLIBuildSuite) TestBuildSpaces(c *testing.T) {
|
|||
}
|
||||
|
||||
ctx.Add("Dockerfile", "FROM busybox\n COPY ")
|
||||
result2 = cli.Docker(cli.Build(name), build.WithoutCache, build.WithExternalBuildContext(ctx))
|
||||
result2 = cli.Docker(cli.Args("build", "-t", name), build.WithoutCache, build.WithExternalBuildContext(ctx))
|
||||
result2.Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
})
|
||||
|
@ -5677,7 +5677,7 @@ func (s *DockerCLIBuildSuite) TestBuildMultiStageCopyFromErrors(c *testing.T) {
|
|||
"foo": "abc",
|
||||
}))
|
||||
|
||||
cli.Docker(cli.Build("build1"), build.WithExternalBuildContext(ctx)).Assert(c, icmd.Expected{
|
||||
cli.Docker(cli.Args("build", "-t", "build1"), build.WithExternalBuildContext(ctx)).Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
Err: tc.expectedError,
|
||||
})
|
||||
|
@ -5879,7 +5879,7 @@ func (s *DockerCLIBuildSuite) TestBuildCopyFromWindowsIsCaseInsensitive(c *testi
|
|||
COPY --from=0 c:\\fOo c:\\copied
|
||||
RUN type c:\\copied
|
||||
`
|
||||
cli.Docker(cli.Build("copyfrom-windows-insensitive"), build.WithBuildContext(c,
|
||||
cli.Docker(cli.Args("build", "-t", "copyfrom-windows-insensitive"), build.WithBuildContext(c,
|
||||
build.WithFile("Dockerfile", dockerfile),
|
||||
build.WithFile("foo", "hello world"),
|
||||
)).Assert(c, icmd.Expected{
|
||||
|
@ -5932,7 +5932,7 @@ func (s *DockerCLIBuildSuite) TestBuildIntermediateTarget(c *testing.T) {
|
|||
res = cli.InspectCmd(c, "build1", cli.Format("json .Config.Cmd")).Combined()
|
||||
assert.Equal(c, strings.TrimSpace(res), `["/dev"]`)
|
||||
|
||||
result := cli.Docker(cli.Build("build1"), build.WithExternalBuildContext(ctx),
|
||||
result := cli.Docker(cli.Args("build", "-t", "build1"), build.WithExternalBuildContext(ctx),
|
||||
cli.WithFlags("--target", "nosuchtarget"))
|
||||
result.Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
|
@ -6050,7 +6050,7 @@ func (s *DockerCLIBuildSuite) TestBuildLineErrorOnBuild(c *testing.T) {
|
|||
// FIXME(vdemeester) should be a unit test
|
||||
func (s *DockerCLIBuildSuite) TestBuildLineErrorUnknownInstruction(c *testing.T) {
|
||||
name := "test_build_line_error_unknown_instruction"
|
||||
cli.Docker(cli.Build(name), build.WithDockerfile(`FROM busybox
|
||||
cli.Docker(cli.Args("build", "-t", name), build.WithDockerfile(`FROM busybox
|
||||
RUN echo hello world
|
||||
NOINSTRUCTION echo ba
|
||||
RUN echo hello
|
||||
|
@ -6064,7 +6064,7 @@ func (s *DockerCLIBuildSuite) TestBuildLineErrorUnknownInstruction(c *testing.T)
|
|||
// FIXME(vdemeester) should be a unit test
|
||||
func (s *DockerCLIBuildSuite) TestBuildLineErrorWithEmptyLines(c *testing.T) {
|
||||
name := "test_build_line_error_with_empty_lines"
|
||||
cli.Docker(cli.Build(name), build.WithDockerfile(`
|
||||
cli.Docker(cli.Args("build", "-t", name), build.WithDockerfile(`
|
||||
FROM busybox
|
||||
|
||||
RUN echo hello world
|
||||
|
@ -6081,7 +6081,7 @@ func (s *DockerCLIBuildSuite) TestBuildLineErrorWithEmptyLines(c *testing.T) {
|
|||
// FIXME(vdemeester) should be a unit test
|
||||
func (s *DockerCLIBuildSuite) TestBuildLineErrorWithComments(c *testing.T) {
|
||||
name := "test_build_line_error_with_comments"
|
||||
cli.Docker(cli.Build(name), build.WithDockerfile(`FROM busybox
|
||||
cli.Docker(cli.Args("build", "-t", name), build.WithDockerfile(`FROM busybox
|
||||
# This will print hello world
|
||||
# and then ba
|
||||
RUN echo hello world
|
||||
|
@ -6148,7 +6148,7 @@ func (s *DockerCLIBuildSuite) TestBuildIidFileCleanupOnFail(c *testing.T) {
|
|||
err = os.WriteFile(tmpIidFile, []byte("Dummy"), 0666)
|
||||
assert.NilError(c, err)
|
||||
|
||||
cli.Docker(cli.Build("testbuildiidfilecleanuponfail"),
|
||||
cli.Docker(cli.Args("build", "-t", "testbuildiidfilecleanuponfail"),
|
||||
build.WithDockerfile(`FROM `+minimalBaseImage()+`
|
||||
RUN /non/existing/command`),
|
||||
cli.WithFlags("--iidfile", tmpIidFile)).Assert(c, icmd.Expected{
|
||||
|
|
|
@ -106,12 +106,9 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *testing.T) {
|
|||
c.Fatal(err, out)
|
||||
}
|
||||
|
||||
out, err := s.d.Cmd("inspect", "-f", "{{json .Mounts}}", "volrestarttest1")
|
||||
assert.NilError(c, err, out)
|
||||
|
||||
if _, err := inspectMountPointJSON(out, "/foo"); err != nil {
|
||||
c.Fatalf("Expected volume to exist: /foo, error: %v\n", err)
|
||||
}
|
||||
out, err := s.d.Cmd("inspect", "-f", `{{range .Mounts}}{{.Destination}}{{"\n"}}{{end}}`, "volrestarttest1")
|
||||
assert.Check(c, err)
|
||||
assert.Check(c, is.Contains(strings.Split(out, "\n"), "/foo"))
|
||||
}
|
||||
|
||||
// #11008
|
||||
|
@ -181,7 +178,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartOnFailure(c *testing.T) {
|
|||
|
||||
// wait test1 to stop
|
||||
hostArgs := []string{"--host", s.d.Sock()}
|
||||
err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 10*time.Second, hostArgs...)
|
||||
err = daemon.WaitInspectWithArgs(dockerBinary, "test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 10*time.Second, hostArgs...)
|
||||
assert.NilError(c, err, "test1 should exit but not")
|
||||
|
||||
// record last start time
|
||||
|
@ -192,7 +189,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartOnFailure(c *testing.T) {
|
|||
s.d.Restart(c)
|
||||
|
||||
// test1 shouldn't restart at all
|
||||
err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 0, hostArgs...)
|
||||
err = daemon.WaitInspectWithArgs(dockerBinary, "test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 0, hostArgs...)
|
||||
assert.NilError(c, err, "test1 should exit but not")
|
||||
|
||||
// make sure test1 isn't restarted when daemon restart
|
||||
|
|
|
@ -3943,7 +3943,7 @@ func (s *DockerCLIRunSuite) TestRunRm(c *testing.T) {
|
|||
name := "miss-me-when-im-gone"
|
||||
cli.DockerCmd(c, "run", "--name="+name, "--rm", "busybox")
|
||||
|
||||
cli.Docker(cli.Inspect(name), cli.Format(".name")).Assert(c, icmd.Expected{
|
||||
cli.Docker(cli.Args("inspect", name), cli.Format(".name")).Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
Err: "No such object: " + name,
|
||||
})
|
||||
|
@ -3955,7 +3955,7 @@ func (s *DockerCLIRunSuite) TestRunRmPre125Api(c *testing.T) {
|
|||
envs := appendBaseEnv(os.Getenv("DOCKER_TLS_VERIFY") != "", "DOCKER_API_VERSION=1.24")
|
||||
cli.Docker(cli.Args("run", "--name="+name, "--rm", "busybox"), cli.WithEnvironmentVariables(envs...)).Assert(c, icmd.Success)
|
||||
|
||||
cli.Docker(cli.Inspect(name), cli.Format(".name")).Assert(c, icmd.Expected{
|
||||
cli.Docker(cli.Args("inspect", name), cli.Format(".name")).Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
Err: "No such object: " + name,
|
||||
})
|
||||
|
|
|
@ -84,7 +84,7 @@ func inspectFieldAndUnmarshall(c *testing.T, name, field string, output interfac
|
|||
assert.Assert(c, err == nil, "failed to unmarshal: %v", err)
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
// Deprecated: use cli.Docker
|
||||
func inspectFilter(name, filter string) (string, error) {
|
||||
format := fmt.Sprintf("{{%s}}", filter)
|
||||
result := icmd.RunCommand(dockerBinary, "inspect", "-f", format, name)
|
||||
|
@ -94,12 +94,12 @@ func inspectFilter(name, filter string) (string, error) {
|
|||
return strings.TrimSpace(result.Combined()), nil
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
// Deprecated: use cli.Docker
|
||||
func inspectFieldWithError(name, field string) (string, error) {
|
||||
return inspectFilter(name, "."+field)
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
// Deprecated: use cli.Docker
|
||||
func inspectField(c *testing.T, name, field string) string {
|
||||
c.Helper()
|
||||
out, err := inspectFilter(name, "."+field)
|
||||
|
@ -107,7 +107,7 @@ func inspectField(c *testing.T, name, field string) string {
|
|||
return out
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
// Deprecated: use cli.Docker
|
||||
func inspectFieldJSON(c *testing.T, name, field string) string {
|
||||
c.Helper()
|
||||
out, err := inspectFilter(name, "json ."+field)
|
||||
|
@ -115,7 +115,7 @@ func inspectFieldJSON(c *testing.T, name, field string) string {
|
|||
return out
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
// Deprecated: use cli.Docker
|
||||
func inspectFieldMap(c *testing.T, name, path, field string) string {
|
||||
c.Helper()
|
||||
out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
|
||||
|
@ -123,7 +123,7 @@ func inspectFieldMap(c *testing.T, name, path, field string) string {
|
|||
return out
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
// Deprecated: use cli.Docker
|
||||
func inspectMountSourceField(name, destination string) (string, error) {
|
||||
m, err := inspectMountPoint(name, destination)
|
||||
if err != nil {
|
||||
|
@ -132,22 +132,17 @@ func inspectMountSourceField(name, destination string) (string, error) {
|
|||
return m.Source, nil
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
var errMountNotFound = errors.New("mount point not found")
|
||||
|
||||
// Deprecated: use cli.Docker
|
||||
func inspectMountPoint(name, destination string) (types.MountPoint, error) {
|
||||
out, err := inspectFilter(name, "json .Mounts")
|
||||
if err != nil {
|
||||
return types.MountPoint{}, err
|
||||
}
|
||||
|
||||
return inspectMountPointJSON(out, destination)
|
||||
}
|
||||
|
||||
var errMountNotFound = errors.New("mount point not found")
|
||||
|
||||
// Deprecated: use cli.Inspect
|
||||
func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
|
||||
var mp []types.MountPoint
|
||||
if err := json.Unmarshal([]byte(j), &mp); err != nil {
|
||||
if err := json.Unmarshal([]byte(out), &mp); err != nil {
|
||||
return types.MountPoint{}, err
|
||||
}
|
||||
|
||||
|
@ -173,15 +168,15 @@ func getIDByName(c *testing.T, name string) string {
|
|||
return id
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Build
|
||||
// Deprecated: use cli.Docker
|
||||
func buildImageSuccessfully(c *testing.T, name string, cmdOperators ...cli.CmdOperator) {
|
||||
c.Helper()
|
||||
buildImage(name, cmdOperators...).Assert(c, icmd.Success)
|
||||
}
|
||||
|
||||
// Deprecated: use cli.Build
|
||||
// Deprecated: use cli.Docker
|
||||
func buildImage(name string, cmdOperators ...cli.CmdOperator) *icmd.Result {
|
||||
return cli.Docker(cli.Build(name), cmdOperators...)
|
||||
return cli.Docker(cli.Args("build", "-t", name), cmdOperators...)
|
||||
}
|
||||
|
||||
// Write `content` to the file at path `dst`, creating it if necessary,
|
||||
|
@ -316,7 +311,7 @@ func createTmpFile(c *testing.T, content string) string {
|
|||
// waitRun will wait for the specified container to be running, maximum 5 seconds.
|
||||
// Deprecated: use cli.WaitFor
|
||||
func waitRun(contID string) error {
|
||||
return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
|
||||
return daemon.WaitInspectWithArgs(dockerBinary, contID, "{{.State.Running}}", "true", 5*time.Second)
|
||||
}
|
||||
|
||||
// waitInspect will wait for the specified container to have the specified string
|
||||
|
@ -324,12 +319,7 @@ func waitRun(contID string) error {
|
|||
// is reached.
|
||||
// Deprecated: use cli.WaitFor
|
||||
func waitInspect(name, expr, expected string, timeout time.Duration) error {
|
||||
return waitInspectWithArgs(name, expr, expected, timeout)
|
||||
}
|
||||
|
||||
// Deprecated: use cli.WaitFor
|
||||
func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg ...string) error {
|
||||
return daemon.WaitInspectWithArgs(dockerBinary, name, expr, expected, timeout, arg...)
|
||||
return daemon.WaitInspectWithArgs(dockerBinary, name, expr, expected, timeout)
|
||||
}
|
||||
|
||||
func getInspectBody(c *testing.T, version, id string) []byte {
|
||||
|
|
Loading…
Add table
Reference in a new issue