Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to pass the command that will be executed on the container #86

Merged
merged 3 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
}