Skip to content

Commit

Permalink
Introduce a cli package for test-integration
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
  • Loading branch information
vdemeester committed Mar 23, 2017
1 parent ad530ff commit 50c4475
Show file tree
Hide file tree
Showing 33 changed files with 572 additions and 414 deletions.
12 changes: 3 additions & 9 deletions integration-cli/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/docker/docker/api/types/swarm"
cliconfig "github.com/docker/docker/cli/config"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/daemon"
"github.com/docker/docker/integration-cli/environment"
"github.com/docker/docker/integration-cli/registry"
Expand Down Expand Up @@ -51,15 +52,7 @@ func init() {
}

func TestMain(m *testing.M) {
var err error
if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
dockerBinary = dockerBin
}
dockerBinary, err = exec.LookPath(dockerBinary)
if err != nil {
fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
os.Exit(1)
}
dockerBinary = testEnv.DockerBinary()

if testEnv.LocalDaemon() {
fmt.Println("INFO: Testing against a local daemon")
Expand All @@ -71,6 +64,7 @@ func TestMain(m *testing.M) {
}

func Test(t *testing.T) {
cli.EnsureTestEnvIsLoaded(t)
cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
cmd.Env = appendBaseEnv(true)
out, err := cmd.CombinedOutput()
Expand Down
31 changes: 31 additions & 0 deletions integration-cli/cli/build/build.go
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
}
}
129 changes: 129 additions & 0 deletions integration-cli/cli/cli.go
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
}
}
3 changes: 2 additions & 1 deletion integration-cli/docker_api_containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
mounttypes "github.com/docker/docker/api/types/mount"
networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/mount"
Expand Down Expand Up @@ -1768,7 +1769,7 @@ func (s *DockerSuite) TestContainersAPICreateMountsCreate(c *check.C) {
)
if testEnv.DaemonPlatform() != "windows" {
testImg = "test-mount-config"
buildImageSuccessfully(c, testImg, withDockerfile(`
buildImageSuccessfully(c, testImg, build.WithDockerfile(`
FROM busybox
RUN mkdir `+destPath+` && touch `+destPath+slash+`bar
CMD cat `+destPath+slash+`bar
Expand Down
10 changes: 6 additions & 4 deletions integration-cli/docker_api_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/request"
"github.com/go-check/check"
)
Expand Down Expand Up @@ -53,7 +55,7 @@ func (s *DockerSuite) TestAPIImagesSaveAndLoad(c *check.C) {
// TODO Windows to Windows CI: Investigate further why this test fails.
testRequires(c, Network)
testRequires(c, DaemonIsLinux)
buildImageSuccessfully(c, "saveandload", withDockerfile("FROM busybox\nENV FOO bar"))
buildImageSuccessfully(c, "saveandload", build.WithDockerfile("FROM busybox\nENV FOO bar"))
id := getIDByName(c, "saveandload")

res, body, err := request.Get("/images/" + id + "/get")
Expand All @@ -68,7 +70,7 @@ func (s *DockerSuite) TestAPIImagesSaveAndLoad(c *check.C) {
defer loadBody.Close()
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)

inspectOut := inspectField(c, id, "Id")
inspectOut := cli.InspectCmd(c, id, cli.Format(".Id")).Combined()
c.Assert(strings.TrimSpace(string(inspectOut)), checker.Equals, id, check.Commentf("load did not work properly"))
}

Expand All @@ -77,7 +79,7 @@ func (s *DockerSuite) TestAPIImagesDelete(c *check.C) {
testRequires(c, Network)
}
name := "test-api-images-delete"
buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nENV FOO bar"))
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
id := getIDByName(c, name)

dockerCmd(c, "tag", name, "test:tag1")
Expand All @@ -100,7 +102,7 @@ func (s *DockerSuite) TestAPIImagesHistory(c *check.C) {
testRequires(c, Network)
}
name := "test-api-images-history"
buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nENV FOO bar"))
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
id := getIDByName(c, name)

status, body, err := request.SockRequest("GET", "/images/"+id+"/history", nil, daemonHost())
Expand Down
Loading

0 comments on commit 50c4475

Please sign in to comment.