forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a
cli
package for test-integration
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
- Loading branch information
1 parent
ad530ff
commit 50c4475
Showing
33 changed files
with
572 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package build | ||
|
||
import ( | ||
"strings" | ||
|
||
icmd "github.com/docker/docker/pkg/testutil/cmd" | ||
) | ||
|
||
// WithDockerfile creates / returns a CmdOperator to set the Dockerfile for a build operation | ||
func WithDockerfile(dockerfile string) func(*icmd.Cmd) func() { | ||
return func(cmd *icmd.Cmd) func() { | ||
cmd.Command = append(cmd.Command, "-") | ||
cmd.Stdin = strings.NewReader(dockerfile) | ||
return nil | ||
} | ||
} | ||
|
||
// WithoutCache makes the build ignore cache | ||
func WithoutCache(cmd *icmd.Cmd) func() { | ||
cmd.Command = append(cmd.Command, "--no-cache") | ||
return nil | ||
} | ||
|
||
// WithContextPath set the build context path | ||
func WithContextPath(path string) func(*icmd.Cmd) func() { | ||
// WithContextPath sets the build context path | ||
return func(cmd *icmd.Cmd) func() { | ||
cmd.Command = append(cmd.Command, path) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/docker/docker/integration-cli/daemon" | ||
"github.com/docker/docker/integration-cli/environment" | ||
icmd "github.com/docker/docker/pkg/testutil/cmd" | ||
) | ||
|
||
var ( | ||
testEnv *environment.Execution | ||
onlyOnce sync.Once | ||
) | ||
|
||
// EnsureTestEnvIsLoaded make sure the test environment is loaded for this package | ||
func EnsureTestEnvIsLoaded(t testingT) { | ||
var doIt bool | ||
var err error | ||
onlyOnce.Do(func() { | ||
doIt = true | ||
}) | ||
|
||
if !doIt { | ||
return | ||
} | ||
testEnv, err = environment.New() | ||
if err != nil { | ||
t.Fatalf("error loading testenv : %v", err) | ||
} | ||
} | ||
|
||
// CmdOperator defines functions that can modify a command | ||
type CmdOperator func(*icmd.Cmd) func() | ||
|
||
type testingT interface { | ||
Fatalf(string, ...interface{}) | ||
} | ||
|
||
// DockerCmd executes the specified docker command and expect a success | ||
func DockerCmd(t testingT, command string, args ...string) *icmd.Result { | ||
return Docker(Cmd(command, args...)).Assert(t, icmd.Success) | ||
} | ||
|
||
// BuildCmd executes the specified docker build command and expect a success | ||
func BuildCmd(t testingT, name string, cmdOperators ...CmdOperator) *icmd.Result { | ||
return Docker(Build(name), cmdOperators...).Assert(t, icmd.Success) | ||
} | ||
|
||
// InspectCmd executes the specified docker inspect command and expect a success | ||
func InspectCmd(t testingT, name string, cmdOperators ...CmdOperator) *icmd.Result { | ||
return Docker(Inspect(name), cmdOperators...).Assert(t, icmd.Success) | ||
} | ||
|
||
// Docker executes the specified docker command | ||
func Docker(cmd icmd.Cmd, cmdOperators ...CmdOperator) *icmd.Result { | ||
for _, op := range cmdOperators { | ||
deferFn := op(&cmd) | ||
if deferFn != nil { | ||
defer deferFn() | ||
} | ||
} | ||
appendDocker(&cmd) | ||
return icmd.RunCmd(cmd) | ||
} | ||
|
||
// 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() { | ||
cmd.Command = append( | ||
[]string{cmd.Command[0]}, | ||
append([]string{"--format", fmt.Sprintf("{{%s}}", format)}, cmd.Command[1:]...)..., | ||
) | ||
return nil | ||
} | ||
} | ||
|
||
func appendDocker(cmd *icmd.Cmd) { | ||
cmd.Command = append([]string{testEnv.DockerBinary()}, cmd.Command...) | ||
} | ||
|
||
// Cmd build an icmd.Cmd struct from the specified command and arguments | ||
func Cmd(command string, args ...string) icmd.Cmd { | ||
return icmd.Command(command, args...) | ||
} | ||
|
||
// Daemon points to the specified daemon | ||
func Daemon(d *daemon.Daemon) func(*icmd.Cmd) func() { | ||
return func(cmd *icmd.Cmd) func() { | ||
cmd.Command = append([]string{"--host", d.Sock()}, cmd.Command...) | ||
return nil | ||
} | ||
} | ||
|
||
// WithTimeout sets the timeout for the command to run | ||
func WithTimeout(timeout time.Duration) func(cmd *icmd.Cmd) func() { | ||
return func(cmd *icmd.Cmd) func() { | ||
cmd.Timeout = timeout | ||
return nil | ||
} | ||
} | ||
|
||
// WithEnvironmentVariables sets the specified environment variables for the command to run | ||
func WithEnvironmentVariables(envs ...string) func(cmd *icmd.Cmd) func() { | ||
return func(cmd *icmd.Cmd) func() { | ||
cmd.Env = envs | ||
return nil | ||
} | ||
} | ||
|
||
// WithFlags sets the specified flags for the command to run | ||
func WithFlags(flags ...string) func(*icmd.Cmd) func() { | ||
return func(cmd *icmd.Cmd) func() { | ||
cmd.Command = append(cmd.Command, flags...) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.