Skip to content

Commit

Permalink
(#25) Add a test verifying that the container receives the env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Apr 16, 2020
1 parent 06ada3c commit a320e66
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 42 additions & 0 deletions compose_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package testcontainers

import (
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLocalDockerCompose(t *testing.T) {
Expand All @@ -24,8 +27,47 @@ func TestLocalDockerCompose(t *testing.T) {
defer destroyFn()
}

func TestLocalDockerComposeWithEnvironment(t *testing.T) {
path := "./testresources/docker-compose.yml"

identifier := strings.ToLower(RandomString(6))

compose := NewLocalDockerCompose(path, identifier)
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
}
defer destroyFn()

err := compose.
WithCommand([]string{"up", "-d"}).
WithEnv(map[string]string{
"foo": "BAR",
}).
Invoke()
checkIfError(t, err)

args := []string{
"exec", compose.Identifier + "_nginx_1", "env",
}

output, err := executeAndGetOutput("docker", args)
checkIfError(t, err)
assert.Contains(t, output, "bar=BAR")
}

func checkIfError(t *testing.T, err ExecError) {
if err.Error != nil || err.Stdout != nil || err.Stderr != nil {
t.Fatal(err)
}
}

func executeAndGetOutput(command string, args []string) (string, ExecError) {
cmd := exec.Command(command, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return "", ExecError{Error: err}
}

return string(out), ExecError{Error: nil}
}
4 changes: 3 additions & 1 deletion testresources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
version: '3'
services:
nginx:
image: nginx:stable-alpine
image: nginx:stable-alpine
environment:
bar: ${foo}

0 comments on commit a320e66

Please sign in to comment.