Skip to content

Commit

Permalink
Merge pull request #131 from rieske/tmpfs
Browse files Browse the repository at this point in the history
Add ability to configure tmpfs for a container
  • Loading branch information
gianarb committed Feb 3, 2020
2 parents 4468424 + 1ea057b commit 2654b8d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type ContainerRequest struct {
Labels map[string]string
BindMounts map[string]string
VolumeMounts map[string]string
Tmpfs map[string]string
RegistryCred string
WaitingFor wait.Strategy
Name string // for specifying container name
Expand Down
1 change: 1 addition & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
hostConfig := &container.HostConfig{
PortBindings: exposedPortMap,
Mounts: mounts,
Tmpfs: req.Tmpfs,
AutoRemove: req.AutoRemove,
Privileged: req.Privileged,
NetworkMode: req.NetworkMode,
Expand Down
53 changes: 52 additions & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package testcontainers
import (
"context"
"fmt"
"github.com/docker/docker/api/types/volume"
"net/http"
"path/filepath"
"testing"
"time"

"github.com/docker/docker/api/types/volume"

"database/sql"
// Import mysql into the scope of this package (required)
_ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -987,3 +988,53 @@ func TestContainerCreationWithBindAndVolume(t *testing.T) {
}
}()
}

func TestContainerWithTmpFs(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Image: "busybox",
Cmd: []string{"sleep", "10"},
Tmpfs: map[string]string{"/testtmpfs": "rw"},
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
t.Fatal(err)
}
defer func() {
t.Log("terminating container")
err := container.Terminate(ctx)
if err != nil {
t.Fatal(err)
}
}()

var path = "/testtmpfs/test.file"

c, err := container.Exec(ctx, []string{"ls", path})
if err != nil {
t.Fatal(err)
}
if c != 1 {
t.Fatalf("File %s should not have existed, expected return code 1, got %v", path, c)
}

c, err = container.Exec(ctx, []string{"touch", path})
if err != nil {
t.Fatal(err)
}
if c != 0 {
t.Fatalf("File %s should have been created successfully, expected return code 0, got %v", path, c)
}

c, err = container.Exec(ctx, []string{"ls", path})
if err != nil {
t.Fatal(err)
}
if c != 0 {
t.Fatalf("File %s should exist, expected return code 0, got %v", path, c)
}
}

0 comments on commit 2654b8d

Please sign in to comment.