Skip to content

Commit

Permalink
Merge branch 'pull-request-635' of https://github.com/rguthriemsft/te…
Browse files Browse the repository at this point in the history
…rratest into pull-request-635
  • Loading branch information
Dave Seepersad committed Oct 13, 2020
2 parents 2115a7a + 46d59fc commit 034d902
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/docker-compose-stdout-example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# website::tag::1:: Build a simple Docker image that contains a text file with the contents "Hello, World!"
FROM ubuntu:20.04
COPY ./bash_script.sh /usr/local/bin/bash_script.sh
5 changes: 5 additions & 0 deletions examples/docker-compose-stdout-example/bash_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e

echo "stdout: message"
>&2 echo -e "stderr: error"
6 changes: 6 additions & 0 deletions examples/docker-compose-stdout-example/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '2.0'
services:
bash_script:
build:
context: .
entrypoint: bash_script.sh
7 changes: 7 additions & 0 deletions examples/packer-docker-example/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ services:
# Expose the sample app's port on the host OS
ports:
- "${SERVER_PORT}:${SERVER_PORT}"
bash_script:
# The name we use for the Docker image in build.json
image: gruntwork/packer-docker-example

volumes:
- ./bash_script.sh:/home/ubuntu/bash_script.sh

entrypoint: bash_script.sh
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vdemeester/k8s-pkg-credentialprovider v0.0.0-20200107171650-7c61ffa44238/go.mod h1:JwQJCMWpUDqjZrB5jpw0f5VbN7U95zxFy1ZDpoEarGo=
github.com/vmware/govmomi v0.20.3/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59bHWk6aFU=
Expand Down
17 changes: 16 additions & 1 deletion modules/docker/docker_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/shell"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)

// Options are Docker options.
Expand All @@ -16,15 +17,26 @@ type Options struct {

// RunDockerCompose runs docker-compose with the given arguments and options and return stdout/stderr.
func RunDockerCompose(t testing.TestingT, options *Options, args ...string) string {
out, err := RunDockerComposeE(t, options, args...)
out, err := runDockerComposeE(t, false, options, args...)
if err != nil {
t.Fatal(err)
}
return out
}

// RunDockerComposeAndGetStdout runs docker-compose with the given arguments and options and returns only stdout.
func RunDockerComposeAndGetStdOut(t testing.TestingT, options *Options, args ...string) string {
out, err := runDockerComposeE(t, true, options, args...)
require.NoError(t, err)
return out
}

// RunDockerComposeE runs docker-compose with the given arguments and options and return stdout/stderr.
func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (string, error) {
return runDockerComposeE(t, false, options, args...)
}

func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args ...string) (string, error) {
cmd := shell.Command{
Command: "docker-compose",
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
Expand All @@ -35,5 +47,8 @@ func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (st
Logger: options.Logger,
}

if stdout {
return shell.RunCommandAndGetStdOut(t, cmd), nil
}
return shell.RunCommandAndGetOutputE(t, cmd)
}
37 changes: 37 additions & 0 deletions test/docker_stdout_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package test

import (
"testing"

"github.com/gruntwork-io/terratest/modules/docker"
"github.com/stretchr/testify/assert"
)

func TestDockerComposeStdoutExample(t *testing.T) {
t.Parallel()
dockerComposeFile := "../examples/docker-compose-stdout-example/docker-compose.yml"

// Run the build step first so that the build output doesn't go to stdout during the compose step.
docker.RunDockerCompose(
t,
&docker.Options{},
"-f",
dockerComposeFile,
"build",
)

// Run the Docker image, read the stdout from it, and make sure it contains the expected output.
// The script must be run using `run bash_script` rather than `up`, so that the echo output from the script
// is the only thing that outputs to stdout.
output := docker.RunDockerComposeAndGetStdOut(
t,
&docker.Options{},
"-f",
dockerComposeFile,
"run",
"bash_script",
)

assert.Contains(t, output, "stdout: message")
assert.NotContains(t, output, "stderr: error")
}

0 comments on commit 034d902

Please sign in to comment.