Skip to content

Commit 59e0456

Browse files
Ackararekkas
authored andcommitted
Add method to build and run an image (#104)
Signed-off-by: Sylvain Cleymans <[email protected]>
1 parent 596a4ae commit 59e0456

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

dockertest.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package dockertest
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"os"
7+
"path/filepath"
68
"runtime"
79
"strings"
810
"time"
@@ -133,6 +135,32 @@ type RunOptions struct {
133135
PortBindings map[dc.Port][]dc.PortBinding
134136
}
135137

138+
// BuildAndRunWithOptions builds and starts a docker container
139+
func (d *Pool) BuildAndRunWithOptions(dockerfilePath string, opts *RunOptions) (*Resource, error) {
140+
// Set the Dockerfile folder as build context
141+
dir, file := filepath.Split(dockerfilePath)
142+
143+
err := d.Client.BuildImage(dc.BuildImageOptions{
144+
Name: opts.Name,
145+
Dockerfile: file,
146+
OutputStream: ioutil.Discard,
147+
ContextDir: dir,
148+
})
149+
150+
if err != nil {
151+
return nil, errors.Wrap(err, "")
152+
}
153+
154+
opts.Repository = opts.Name
155+
156+
return d.RunWithOptions(opts)
157+
}
158+
159+
// BuildAndRun builds and starts a docker container
160+
func (d *Pool) BuildAndRun(name, dockerfilePath string, env []string) (*Resource, error) {
161+
return d.BuildAndRunWithOptions(dockerfilePath, &RunOptions{Name: name, Env: env})
162+
}
163+
136164
// RunWithOptions starts a docker container.
137165
//
138166
// pool.Run(&RunOptions{Repository: "mongo", Cmd: []string{"mongod", "--smallfiles"}})

dockertest_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package dockertest
33
import (
44
"database/sql"
55
"fmt"
6+
"io/ioutil"
67
"log"
78
"net/http"
89
"os"
910
"testing"
1011

12+
dc "github.com/fsouza/go-dockerclient"
1113
_ "github.com/lib/pq"
1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
14-
dc "github.com/fsouza/go-dockerclient"
1516
)
1617

1718
var docker = os.Getenv("DOCKER_URL")
@@ -77,12 +78,12 @@ func TestMongo(t *testing.T) {
7778
func TestContainerWithName(t *testing.T) {
7879
resource, err := pool.RunWithOptions(
7980
&RunOptions{
80-
Name: "db",
81+
Name: "db",
8182
Repository: "postgres",
82-
Tag: "9.5",
83+
Tag: "9.5",
8384
})
8485
require.Nil(t, err)
85-
assert.Equal(t,"/db", resource.Container.Name)
86+
assert.Equal(t, "/db", resource.Container.Name)
8687

8788
require.Nil(t, pool.Purge(resource))
8889
}
@@ -91,13 +92,31 @@ func TestContainerWithPortBinding(t *testing.T) {
9192
resource, err := pool.RunWithOptions(
9293
&RunOptions{
9394
Repository: "postgres",
94-
Tag: "9.5",
95+
Tag: "9.5",
9596
PortBindings: map[dc.Port][]dc.PortBinding{
9697
"5432/tcp": {{HostIP: "", HostPort: "5433"}},
9798
},
9899
})
99100
require.Nil(t, err)
100-
assert.Equal(t,"5433", resource.GetPort("5432/tcp"))
101+
assert.Equal(t, "5433", resource.GetPort("5432/tcp"))
102+
103+
require.Nil(t, pool.Purge(resource))
104+
}
105+
106+
func TestBuildImage(t *testing.T) {
107+
// Create Dockerfile in temp dir
108+
dir, _ := ioutil.TempDir("", "dockertest")
109+
defer os.RemoveAll(dir)
110+
111+
dockerfilePath := dir + "/Dockerfile"
112+
ioutil.WriteFile(dockerfilePath,
113+
[]byte("FROM postgres:9.5"),
114+
0644,
115+
)
116+
117+
resource, err := pool.BuildAndRun("postgres-test", dockerfilePath, nil)
118+
require.Nil(t, err)
101119

120+
assert.Equal(t, "/postgres-test", resource.Container.Name)
102121
require.Nil(t, pool.Purge(resource))
103-
}
122+
}

examples/BuildDockerfile.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
`./db/image/Dockerfile`
2+
```Dockerfile
3+
FROM postgres:latest
4+
5+
# Add your customizations here
6+
```
7+
8+
`./db_test.go`
9+
```go
10+
pool, err := dockertest.NewPool("")
11+
if err != nil {
12+
log.Fatalf("Could not connect to docker: %s", err)
13+
}
14+
15+
// Build and run the given Dockerfile
16+
resource, err := pool.BuildAndRun("my-postgres-test-image", "./db/image/Dockerfile", []string{})
17+
if err != nil {
18+
log.Fatalf("Could not start resource: %s", err)
19+
}
20+
21+
if err = pool.Retry(func() error {
22+
var err error
23+
db, err = sql.Open("postgres", fmt.Sprintf("postgres://postgres:secret@localhost:%s/%s?sslmode=disable", resource.GetPort("5432/tcp"), database))
24+
if err != nil {
25+
return err
26+
}
27+
return db.Ping()
28+
}); err != nil {
29+
log.Fatalf("Could not connect to docker: %s", err)
30+
}
31+
32+
// When you're done, kill and remove the container
33+
err = pool.Purge(resource)
34+
```

0 commit comments

Comments
 (0)