Skip to content

Commit 390cbf2

Browse files
authored
Remove intermediate container/tar archive after creating (#18)
* remove containers after creating * add flag to allow saving intermediate image/tar archive * update docs
1 parent ae1ca7d commit 390cbf2

File tree

8 files changed

+47
-15
lines changed

8 files changed

+47
-15
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ commandTests:
8888
excludedError: [".*Inst.*Security.* | .*Security.*Inst.*"]
8989
```
9090
91+
### Intermediate Artifacts
92+
Each command test run creates either a container (with the `docker` driver) or
93+
tar artifact (with the `tar` driver). By default, these are deleted after the
94+
test run finishes, but the `-save` flag can optionally be passed to keep
95+
these around. This would normally be used for debugging purposes.
96+
9197

9298
## File Existence Tests
9399
File existence tests check to make sure a specific file (or directory) exist

drivers/docker_driver.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ import (
3131
)
3232

3333
type DockerDriver struct {
34-
cli docker.Client
3534
originalImage string
3635
currentImage string
36+
cli docker.Client
3737
env map[string]string
38+
save bool
3839
}
3940

40-
func NewDockerDriver(image string) (Driver, error) {
41+
func NewDockerDriver(image string, save bool) (Driver, error) {
4142
newCli, err := docker.NewClientFromEnv()
4243
if err != nil {
4344
return nil, err
@@ -47,6 +48,7 @@ func NewDockerDriver(image string) (Driver, error) {
4748
currentImage: image,
4849
cli: *newCli,
4950
env: nil,
51+
save: save,
5052
}, nil
5153
}
5254

@@ -257,6 +259,14 @@ func (d *DockerDriver) runAndCommit(t *testing.T, env []string, command []string
257259
t.Errorf("Error committing container: %s", err.Error())
258260
}
259261

262+
if !d.save {
263+
if err = d.cli.RemoveContainer(docker.RemoveContainerOptions{
264+
ID: container.ID,
265+
}); err != nil {
266+
t.Logf("Error when removing container %s: %s", container.ID, err.Error())
267+
}
268+
}
269+
260270
d.currentImage = image.ID
261271
return image.ID
262272
}
@@ -302,6 +312,14 @@ func (d *DockerDriver) exec(t *testing.T, env []string, command []string) (strin
302312
t.Errorf("Error retrieving container logs: %s", err.Error())
303313
}
304314

315+
if !d.save {
316+
if err = d.cli.RemoveContainer(docker.RemoveContainerOptions{
317+
ID: container.ID,
318+
}); err != nil {
319+
t.Logf("Error when removing container %s: %s", container.ID, err.Error())
320+
}
321+
}
322+
305323
return stdout.String(), stderr.String(), exitCode
306324
}
307325

drivers/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Driver interface {
4747
Destroy()
4848
}
4949

50-
func InitDriverImpl(driver string) func(string) (Driver, error) {
50+
func InitDriverImpl(driver string) func(string, bool) (Driver, error) {
5151
switch driver {
5252
// future drivers will be added here
5353
case Docker:

drivers/tar_driver.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import (
2727

2828
type TarDriver struct {
2929
Image pkgutil.Image
30+
Save bool
3031
}
3132

32-
func NewTarDriver(imageName string) (Driver, error) {
33+
func NewTarDriver(imageName string, save bool) (Driver, error) {
3334
// if the image is in the local daemon, we should be using the docker driver anyway.
3435
// only try remote.
3536

@@ -55,7 +56,9 @@ func NewTarDriver(imageName string) (Driver, error) {
5556
}
5657

5758
func (d *TarDriver) Destroy() {
58-
pkgutil.CleanupImage(d.Image)
59+
if !d.Save {
60+
pkgutil.CleanupImage(d.Image)
61+
}
5962
}
6063

6164
func (d *TarDriver) Setup(t *testing.T, envVars []unversioned.EnvVar, fullCommand []unversioned.Command) {

structure_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,21 @@ func Parse(t *testing.T, fp string) (StructureTest, error) {
8888
if !ok {
8989
return nil, errors.New("Error encountered when type casting Structure Test interface")
9090
}
91-
tests.SetDriverImpl(driverImpl, imagePath)
91+
tests.SetDriverImpl(driverImpl, imagePath, save)
9292
return tests, nil
9393
}
9494

9595
var configFiles arrayFlags
9696

9797
var imagePath, driver string
98-
var pull bool
99-
var driverImpl func(string) (drivers.Driver, error)
98+
var save, pull bool
99+
var driverImpl func(string, bool) (drivers.Driver, error)
100100

101101
func TestMain(m *testing.M) {
102102
flag.StringVar(&imagePath, "image", "", "path to test image")
103103
flag.StringVar(&driver, "driver", "docker", "driver to use when running tests")
104104
flag.BoolVar(&pull, "pull", false, "force a pull of the image before running tests")
105+
flag.BoolVar(&save, "save", false, "preserve created containers after test run")
105106

106107
flag.Parse()
107108
configFiles = flag.Args()

types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
type StructureTest interface {
26-
SetDriverImpl(func(string) (drivers.Driver, error), string)
26+
SetDriverImpl(func(string, bool) (drivers.Driver, error), string, bool)
2727
NewDriver() (drivers.Driver, error)
2828
RunAll(t *testing.T) int
2929
}

types/v1/structure.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525
)
2626

2727
type StructureTest struct {
28-
DriverImpl func(string) (drivers.Driver, error)
28+
DriverImpl func(string, bool) (drivers.Driver, error)
2929
Image string
30+
Save bool
3031
GlobalEnvVars []unversioned.EnvVar
3132
CommandTests []CommandTest
3233
FileExistenceTests []FileExistenceTest
@@ -35,12 +36,13 @@ type StructureTest struct {
3536
}
3637

3738
func (st *StructureTest) NewDriver() (drivers.Driver, error) {
38-
return st.DriverImpl(st.Image)
39+
return st.DriverImpl(st.Image, st.Save)
3940
}
4041

41-
func (st *StructureTest) SetDriverImpl(f func(string) (drivers.Driver, error), image string) {
42+
func (st *StructureTest) SetDriverImpl(f func(string, bool) (drivers.Driver, error), image string, save bool) {
4243
st.DriverImpl = f
4344
st.Image = image
45+
st.Save = save
4446
}
4547

4648
func (st *StructureTest) RunAll(t *testing.T) int {

types/v2/structure.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525
)
2626

2727
type StructureTest struct {
28-
DriverImpl func(string) (drivers.Driver, error)
28+
DriverImpl func(string, bool) (drivers.Driver, error)
2929
Image string
30+
Save bool
3031
GlobalEnvVars []unversioned.EnvVar
3132
CommandTests []CommandTest
3233
FileExistenceTests []FileExistenceTest
@@ -36,12 +37,13 @@ type StructureTest struct {
3637
}
3738

3839
func (st *StructureTest) NewDriver() (drivers.Driver, error) {
39-
return st.DriverImpl(st.Image)
40+
return st.DriverImpl(st.Image, st.Save)
4041
}
4142

42-
func (st *StructureTest) SetDriverImpl(f func(string) (drivers.Driver, error), image string) {
43+
func (st *StructureTest) SetDriverImpl(f func(string, bool) (drivers.Driver, error), image string, save bool) {
4344
st.DriverImpl = f
4445
st.Image = image
46+
st.Save = save
4547
}
4648

4749
func (st *StructureTest) RunAll(t *testing.T) int {

0 commit comments

Comments
 (0)