Skip to content

Commit

Permalink
Add volume binds
Browse files Browse the repository at this point in the history
  • Loading branch information
everesio committed Jan 25, 2018
1 parent bf96983 commit 5b7e098
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ test: build
test.exmaples: build
go test -v ./test-examples/...

test.all: test test.exmaples
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This utility library allows you to create a test environment based on docker con
* Full control of the container lifecycle - you can stop and restart a container to test connectivity problems
* Follow container log output
* Define a wait for container application startup before your tests start
* Bind mounts
* Use DOCKER_API_VERSION environment variable to set API version

Prerequisites
Expand Down Expand Up @@ -102,9 +103,11 @@ func TestWithDocker(t *testing.T) {
EnvironmentVariables: map[string]string{
"VAULT_ADDR": "http://127.0.0.1:8200",
},

Cmd: []string{
"server", "-dev",
"server", "-dev", "-config=/etc/vault/vault_config.hcl",
},
Binds: []string{
"/tmp/vault_config.hcl", "/etc/vault",
},
AfterStart: http.NewHttpWait(
`http://{{ value . "it-vault.Host"}}:{{ value . "it-vault.Port"}}/v1/sys/seal-status`,
Expand Down Expand Up @@ -247,6 +250,7 @@ Run [test-examples](https://github.com/grepplabs/docker-it/tree/master/test-exam
* Postgres
* Kafka
* Redis
* Vault

```bash
go test -v ./test-examples/...
Expand Down
3 changes: 2 additions & 1 deletion docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (r *dockerClient) PullImage(imageName string) error {
}

// CreateContainer creates a new container.
func (r *dockerClient) CreateContainer(containerName string, image string, env []string, portSpecs []string, cmd []string) (string, error) {
func (r *dockerClient) CreateContainer(containerName string, image string, env []string, portSpecs []string, cmd []string, binds []string) (string, error) {
// ip:public:private/proto
exposedPorts, portBindings, err := nat.ParsePortSpecs(portSpecs)
if err != nil {
Expand All @@ -122,6 +122,7 @@ func (r *dockerClient) CreateContainer(containerName string, image string, env [
}
hostConfig := typesContainer.HostConfig{
PortBindings: portBindings,
Binds: binds,
}

body, err := r.client.ContainerCreate(context.Background(), &config, &hostConfig, nil, containerName)
Expand Down
3 changes: 2 additions & 1 deletion docker_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func TestDockerCommands(t *testing.T) {

env := []string{}
cmd := []string{}
containerID, err := dc.CreateContainer(containerName, testImage, env, portSpecs, cmd)
binds := []string{}
containerID, err := dc.CreateContainer(containerName, testImage, env, portSpecs, cmd, binds)
a.Nil(err)

container, err := dc.GetContainerByID(containerID)
Expand Down
2 changes: 2 additions & 0 deletions docker_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type DockerComponent struct {
EnvironmentVariables map[string]string
// Command to run when starting the container
Cmd []string
// List of volume bindings for this container
Binds []string
// Follow container log output
FollowLogs bool
// Callback invoked after start container command was invoked.
Expand Down
4 changes: 2 additions & 2 deletions docker_lifecycle_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ func (r *dockerLifecycleHandler) createDockerContainer(container *dockerContaine
cmd = append(cmd, container.Cmd...)
}

r.context.logger.Info.Println("Creating container for", container.Name, "name", containerName, "env", env, "portSpecs", portSpecs, "cmd", cmd)
containerID, err := r.dockerClient.CreateContainer(containerName, container.Image, env, portSpecs, cmd)
r.context.logger.Info.Println("Creating container for", container.Name, "name", containerName, "env", env, "portSpecs", portSpecs, "cmd", cmd, "binds", container.Binds)
containerID, err := r.dockerClient.CreateContainer(containerName, container.Image, env, portSpecs, cmd, container.Binds)
if err != nil {
return err
}
Expand Down
36 changes: 30 additions & 6 deletions test-examples/setup_docker_environment_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testexamples

import (
"fmt"
dit "github.com/grepplabs/docker-it"
"github.com/grepplabs/docker-it/wait"
"github.com/grepplabs/docker-it/wait/elastic"
Expand All @@ -10,6 +11,9 @@ import (
"github.com/grepplabs/docker-it/wait/postgres"
"github.com/grepplabs/docker-it/wait/redis"
"os"
"os/signal"
"path/filepath"
"syscall"
"testing"
"time"
)
Expand All @@ -32,6 +36,9 @@ func TestMain(m *testing.M) {
"it-es",
"it-vault",
}

go handleInterrupt()

if err := dockerEnvironment.StartParallel(components...); err != nil {
dockerEnvironment.Shutdown()
panic(err)
Expand All @@ -50,6 +57,15 @@ func TestMain(m *testing.M) {
}

func newDockerEnvironment() *dit.DockerEnvironment {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
_, err = os.Stat(filepath.Join(pwd, "vault_config.hcl"))
if os.IsNotExist(err) {
panic(err)
}

env, err := dit.NewDockerEnvironment(
dit.DockerComponent{
Name: "it-redis",
Expand Down Expand Up @@ -165,18 +181,17 @@ func newDockerEnvironment() *dit.DockerEnvironment {
Name: "it-vault",
Image: "vault:0.9.1",
ForcePull: true,
FollowLogs: false,
FollowLogs: true,
ExposedPorts: []dit.Port{
{
ContainerPort: 8200,
},
},
EnvironmentVariables: map[string]string{
"VAULT_ADDR": "http://127.0.0.1:8200",
},

Cmd: []string{
"server", "-dev",
"server", "-dev", "-config=/etc/vault/vault_config.hcl",
},
Binds: []string{
fmt.Sprintf("%s:%s", pwd, "/etc/vault"),
},
AfterStart: http.NewHttpWait(
`http://{{ value . "it-vault.Host"}}:{{ value . "it-vault.Port"}}/v1/sys/seal-status`,
Expand Down Expand Up @@ -211,3 +226,12 @@ func newDockerEnvironment2() *dit.DockerEnvironment {
}
return env
}

func handleInterrupt() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
println("FATAL: Stop signal was received")
dockerEnvironment.Shutdown()
os.Exit(1)
}
1 change: 1 addition & 0 deletions test-examples/vault_config.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugin_directory = "/tmp"

0 comments on commit 5b7e098

Please sign in to comment.