Skip to content

Commit

Permalink
Add CMD to docker component
Browse files Browse the repository at this point in the history
  • Loading branch information
everesio committed Jan 25, 2018
1 parent e5612f0 commit c6c44e7
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 7 deletions.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.DEFAULT_GOAL := build
.PHONY: test.exmaples

SOURCES = $(shell find . -name '*.go')
GOPKGS = $(shell go list ./... | grep -v /vendor/)

build: $(SOURCES)
go build .

fmt:
go fmt $(GOPKGS)

check:
golint $(GOPKGS)
go vet $(GOPKGS)

test: build
go test -v ./

test.exmaples: build
go test -v ./test-examples/...

25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/grepplabs/docker-it/wait"
"github.com/grepplabs/docker-it/wait/elastic"
"github.com/grepplabs/docker-it/wait/redis"
"github.com/grepplabs/docker-it/wait/http"
"testing"
"time"
)
Expand Down Expand Up @@ -88,11 +89,33 @@ func TestWithDocker(t *testing.T) {
},
),
},
dit.DockerComponent{
Name: "it-vault",
Image: "vault:0.9.1",
ForcePull: true,
FollowLogs: false,
ExposedPorts: []dit.Port{
{
ContainerPort: 8200,
},
},
EnvironmentVariables: map[string]string{
"VAULT_ADDR": "http://127.0.0.1:8200",
},

Cmd: []string{
"server", "-dev",
},
AfterStart: http.NewHttpWait(
`http://{{ value . "it-vault.Host"}}:{{ value . "it-vault.Port"}}/v1/sys/seal-status`,
http.Options{},
),
},
)
if err != nil {
panic(err)
}
if err != env.StartParallel("it-redis", "it-es") {
if err != env.StartParallel("it-redis", "it-es", "it-vault") {
panic(err)
}

Expand Down
5 changes: 3 additions & 2 deletions docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"github.com/docker/docker/api/types"
typesContainer "github.com/docker/docker/api/types/container"
typesFilters "github.com/docker/docker/api/types/filters"
typesStrslice "github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/go-connections/nat"
"io"
"io/ioutil"
)


type dockerClient struct {
client *client.Client
}
Expand Down 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) (string, error) {
func (r *dockerClient) CreateContainer(containerName string, image string, env []string, portSpecs []string, cmd []string) (string, error) {
// ip:public:private/proto
exposedPorts, portBindings, err := nat.ParsePortSpecs(portSpecs)
if err != nil {
Expand All @@ -118,6 +118,7 @@ func (r *dockerClient) CreateContainer(containerName string, image string, env [
Image: image,
Env: env,
ExposedPorts: exposedPorts,
Cmd: typesStrslice.StrSlice(cmd),
}
hostConfig := typesContainer.HostConfig{
PortBindings: portBindings,
Expand Down
3 changes: 2 additions & 1 deletion docker_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func TestDockerCommands(t *testing.T) {
containerName := fmt.Sprintf("test-busybox-%s", salt)

env := []string{}
containerID, err := dc.CreateContainer(containerName, testImage, env, portSpecs)
cmd := []string{}
containerID, err := dc.CreateContainer(containerName, testImage, env, portSpecs, cmd)
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 @@ -14,6 +14,8 @@ type DockerComponent struct {
ExposedPorts []Port
// Container environment variables
EnvironmentVariables map[string]string
// Command to run when starting the container
Cmd []string
// Follow container log output
FollowLogs bool
// Callback invoked after start container command was invoked.
Expand Down
9 changes: 7 additions & 2 deletions docker_lifecycle_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ func (r *dockerLifecycleHandler) createDockerContainer(container *dockerContaine
env = append(env, k+"="+v)
}
}
r.context.logger.Info.Println("Creating container for", container.Name, "name", containerName, "env", env, "portSpecs", portSpecs)
containerID, err := r.dockerClient.CreateContainer(containerName, container.Image, env, portSpecs)
cmd := make([]string, 0)
if container.Cmd != nil {
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)
if err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions test-examples/setup_docker_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestMain(m *testing.M) {
"it-mysql",
"it-kafka",
"it-es",
"it-vault",
}
if err := dockerEnvironment.StartParallel(components...); err != nil {
dockerEnvironment.Shutdown()
Expand Down Expand Up @@ -160,6 +161,28 @@ func newDockerEnvironment() *dit.DockerEnvironment {
},
),
},
dit.DockerComponent{
Name: "it-vault",
Image: "vault:0.9.1",
ForcePull: true,
FollowLogs: false,
ExposedPorts: []dit.Port{
{
ContainerPort: 8200,
},
},
EnvironmentVariables: map[string]string{
"VAULT_ADDR": "http://127.0.0.1:8200",
},

Cmd: []string{
"server", "-dev",
},
AfterStart: http.NewHttpWait(
`http://{{ value . "it-vault.Host"}}:{{ value . "it-vault.Port"}}/v1/sys/seal-status`,
http.Options{},
),
},
)
if err != nil {
panic(err)
Expand Down
37 changes: 37 additions & 0 deletions test-examples/vault_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package testexamples

import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"testing"
"time"
)

func TestVaultCall(t *testing.T) {
a := assert.New(t)

host := dockerEnvironment.Host()
port, err := dockerEnvironment.Port("it-vault", "")
a.Nil(err)

// 503 for a sealed vault
url := fmt.Sprintf("http://%s:%d/v1/sys/leader", host, port)
fmt.Println(url)

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, err := http.NewRequest("GET", url, nil)
a.Nil(err)

req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
a.Nil(err)
defer resp.Body.Close()

a.EqualValues(resp.StatusCode, http.StatusOK)
_, err = ioutil.ReadAll(resp.Body)
a.Nil(err)
}
2 changes: 1 addition & 1 deletion wait/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package redis

import (
"fmt"
"github.com/garyburd/redigo/redis"
dit "github.com/grepplabs/docker-it"
"github.com/grepplabs/docker-it/wait"
"github.com/garyburd/redigo/redis"
"time"
)

Expand Down

0 comments on commit c6c44e7

Please sign in to comment.