Skip to content

Commit

Permalink
core: possibility to pass the command that will be executed on the co…
Browse files Browse the repository at this point in the history
…ntainer (#86)

* Add possibility to pass the command that will be executed on the container

* Rename test to follow existing pattern

Signed-off-by: Roberto Rodrigues Junior <robertogyn19@gmail.com>

* Check http status on mongo test

Signed-off-by: Roberto Rodrigues Junior <robertogyn19@gmail.com>
  • Loading branch information
robertogyn19 authored and arekkas committed Mar 25, 2017
1 parent 007aac5 commit 4e6e9b5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
34 changes: 27 additions & 7 deletions dockertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ func (r *Resource) GetPort(id string) string {
return m[0].HostPort
}

// NewTLSPool creates a new pool given an endpoint and the certificate path. This is required for endpoints that
// NewTLSPool creates a new pool given an endpoint and the certificate path. This is required for endpoints that
// require TLS communication.
func NewTLSPool(endpoint, certpath string) (*Pool, error) {
ca := fmt.Sprintf("%s/ca.pem", certpath)
cert := fmt.Sprintf("%s/cert.pem", certpath)
key := fmt.Sprintf("%s/key.pem", certpath)

client, err := dc.NewTLSClient(endpoint, cert, key, ca)
if err != nil {
return nil, errors.Wrap(err, "")
}

return &Pool{
Client: client,
}, nil
Expand All @@ -77,7 +77,7 @@ func NewPool(endpoint string) (*Pool, error) {
endpoint = "unix:///var/run/docker.sock"
}
}

client, err := dc.NewClient(endpoint)
if err != nil {
return nil, errors.Wrap(err, "")
Expand All @@ -88,10 +88,22 @@ func NewPool(endpoint string) (*Pool, error) {
}, nil
}

// Run starts a docker container.
type RunOptions struct {
Repository string
Tag string
Env []string
Cmd []string
}

// RunWithOptions starts a docker container.
//
// pool.Run("mysql", "5.3", []string{"FOO=BAR", "BAR=BAZ"})
func (d *Pool) Run(repository, tag string, env []string) (*Resource, error) {
// pool.Run(RunOptions{Repository: "mongo", Cmd: []string{"mongod", "--smallfiles"}})
func (d *Pool) RunWithOptions(opts RunOptions) (*Resource, error) {
repository := opts.Repository
tag := opts.Tag
env := opts.Env
cmd := opts.Cmd

if tag == "" {
tag = "latest"
}
Expand All @@ -110,6 +122,7 @@ func (d *Pool) Run(repository, tag string, env []string) (*Resource, error) {
Config: &dc.Config{
Image: fmt.Sprintf("%s:%s", repository, tag),
Env: env,
Cmd: cmd,
},
HostConfig: &dc.HostConfig{
PublishAllPorts: true,
Expand All @@ -133,6 +146,13 @@ func (d *Pool) Run(repository, tag string, env []string) (*Resource, error) {
}, nil
}

// Run starts a docker container.
//
// pool.Run("mysql", "5.3", []string{"FOO=BAR", "BAR=BAZ"})
func (d *Pool) Run(repository, tag string, env []string) (*Resource, error) {
return d.RunWithOptions(RunOptions{Repository: repository, Tag: tag, Env: env})
}

// Purge removes a container and linked volumes from docker.
func (d *Pool) Purge(r *Resource) error {
if err := d.Client.KillContainer(dc.KillContainerOptions{ID: r.Container.ID}); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions dockertest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"log"
"net/http"
"os"
"testing"
)
Expand Down Expand Up @@ -55,3 +56,31 @@ func TestPostgres(t *testing.T) {
require.Nil(t, err)
require.Nil(t, pool.Purge(resource))
}

func TestMongo(t *testing.T) {
options := RunOptions{
Repository: "mongo",
Tag: "3.3.12",
Cmd: []string{"mongod", "--smallfiles"},
}
resource, err := pool.RunWithOptions(options)
require.Nil(t, err)
port := resource.GetPort("27017/tcp")
assert.NotEmpty(t, port)

err = pool.Retry(func() error {
response, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s", port))

if err != nil {
return err
}

if response.StatusCode != http.StatusOK {
return fmt.Errorf("Could not connect to resource.")
}

return nil
})
require.Nil(t, err)
require.Nil(t, pool.Purge(resource))
}

0 comments on commit 4e6e9b5

Please sign in to comment.