Skip to content

Commit ae1ca7d

Browse files
authored
Add option to pull image before running tests (#19)
* Add option to pull image before running tests * update docs * fix travis * fix bazel build * dont allow pulling when not using docker driver
1 parent ada3aad commit ae1ca7d

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ go_test(
4545
deps = [
4646
"//drivers:go_default_library",
4747
"//vendor/github.com/ghodss/yaml:go_default_library",
48+
"//vendor/github.com/fsouza/go-dockerclient:go_default_library",
4849
],
4950
)
5051

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ To use container structure tests to validate your containers, you need the follo
1818
- A container image to test against
1919
- A test .yaml or .json file with user defined structure tests to run inside of the specified container image
2020

21+
Note that the test framework looks for the provided image in the local Docker
22+
daemon (if it is not provided as a tar). The `-pull` flag can optionally
23+
be provided to force a pull of a remote image before running the tests.
24+
2125
## Example Run
2226
An example run of the test framework:
2327
```shell

drivers/driver.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import (
2222
"github.com/GoogleCloudPlatform/container-structure-test/types/unversioned"
2323
)
2424

25+
const (
26+
Docker = "docker"
27+
Tar = "tar"
28+
)
29+
2530
type Driver interface {
2631
Setup(t *testing.T, envVars []unversioned.EnvVar, fullCommand []unversioned.Command)
2732

@@ -45,9 +50,9 @@ type Driver interface {
4550
func InitDriverImpl(driver string) func(string) (Driver, error) {
4651
switch driver {
4752
// future drivers will be added here
48-
case "docker":
53+
case Docker:
4954
return NewDockerDriver
50-
case "tar":
55+
case Tar:
5156
return NewTarDriver
5257
default:
5358
return nil

structure_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"testing"
2727

2828
"github.com/GoogleCloudPlatform/container-structure-test/drivers"
29+
docker "github.com/fsouza/go-dockerclient"
2930
"github.com/ghodss/yaml"
3031
)
3132

@@ -94,11 +95,13 @@ func Parse(t *testing.T, fp string) (StructureTest, error) {
9495
var configFiles arrayFlags
9596

9697
var imagePath, driver string
98+
var pull bool
9799
var driverImpl func(string) (drivers.Driver, error)
98100

99101
func TestMain(m *testing.M) {
100102
flag.StringVar(&imagePath, "image", "", "path to test image")
101103
flag.StringVar(&driver, "driver", "docker", "driver to use when running tests")
104+
flag.BoolVar(&pull, "pull", false, "force a pull of the image before running tests")
102105

103106
flag.Parse()
104107
configFiles = flag.Args()
@@ -115,6 +118,30 @@ func TestMain(m *testing.M) {
115118

116119
var err error
117120

121+
if pull {
122+
if driver != drivers.Docker {
123+
fmt.Println("Image pull not supported when not using Docker driver")
124+
os.Exit(1)
125+
}
126+
var repository, tag string
127+
parts := strings.Split(imagePath, ":")
128+
repository = parts[0]
129+
if len(parts) < 2 {
130+
fmt.Println("Please provide specific tag for image")
131+
os.Exit(1)
132+
}
133+
tag = parts[1]
134+
client, err := docker.NewClientFromEnv()
135+
if err = client.PullImage(docker.PullImageOptions{
136+
Repository: repository,
137+
Tag: tag,
138+
OutputStream: os.Stdout,
139+
}, docker.AuthConfiguration{}); err != nil {
140+
fmt.Printf("Error pulling remote image %s: %s", imagePath, err.Error())
141+
os.Exit(1)
142+
}
143+
}
144+
118145
driverImpl = drivers.InitDriverImpl(driver)
119146
if driverImpl == nil {
120147
fmt.Printf("Unsupported driver type: %s", driver)

0 commit comments

Comments
 (0)