summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFabricio Voznika <fvoznika@google.com>2018-08-02 12:40:29 -0700
committerShentubot <shentubot@google.com>2018-08-02 12:42:07 -0700
commit4c1167de4ee2aa7b71729ff8b1c742b4183168d1 (patch)
tree13cedfb93a66e617fafd0cc1f6feaac764fb764b
parent57d0fcbdbf7e9d2d573ce8d4ca2f72b82f778d63 (diff)
Isolate image pulling time from container startup
mysql image test is timing out sporadically and it's hard to tell where the slow down in coming from. PiperOrigin-RevId: 207147237 Change-Id: I05a4d2c116292695d63cf861f3b89cd1c54b6106
-rw-r--r--runsc/test/image/image_test.go9
-rw-r--r--runsc/test/image/python_test.go3
-rw-r--r--runsc/test/image/tomcat_test.go3
-rw-r--r--runsc/test/testutil/docker.go39
4 files changed, 38 insertions, 16 deletions
diff --git a/runsc/test/image/image_test.go b/runsc/test/image/image_test.go
index 04c334d92..248934484 100644
--- a/runsc/test/image/image_test.go
+++ b/runsc/test/image/image_test.go
@@ -82,6 +82,9 @@ func testHTTPServer(port int) error {
}
func TestHttpd(t *testing.T) {
+ if out, err := testutil.Pull("httpd"); err != nil {
+ t.Fatalf("docker pull failed: %v\nout: %s", err, out)
+ }
d := testutil.MakeDocker("http-test")
dir, err := testutil.PrepareFiles("latin10k.txt")
@@ -112,6 +115,9 @@ func TestHttpd(t *testing.T) {
}
func TestNginx(t *testing.T) {
+ if out, err := testutil.Pull("nginx"); err != nil {
+ t.Fatalf("docker pull failed: %v\nout: %s", err, out)
+ }
d := testutil.MakeDocker("net-test")
dir, err := testutil.PrepareFiles("latin10k.txt")
@@ -142,6 +148,9 @@ func TestNginx(t *testing.T) {
}
func TestMysql(t *testing.T) {
+ if out, err := testutil.Pull("mysql"); err != nil {
+ t.Fatalf("docker pull failed: %v\nout: %s", err, out)
+ }
d := testutil.MakeDocker("mysql-test")
// Start the container.
diff --git a/runsc/test/image/python_test.go b/runsc/test/image/python_test.go
index e7324e83e..b77a6ec87 100644
--- a/runsc/test/image/python_test.go
+++ b/runsc/test/image/python_test.go
@@ -24,6 +24,9 @@ import (
)
func TestPythonHello(t *testing.T) {
+ if out, err := testutil.Pull("google/python-hello"); err != nil {
+ t.Fatalf("docker pull failed: %v\nout: %s", err, out)
+ }
d := testutil.MakeDocker("python-hello-test")
if out, err := d.Run("-p", "8080", "google/python-hello"); err != nil {
t.Fatalf("docker run failed: %v\nout: %s", err, out)
diff --git a/runsc/test/image/tomcat_test.go b/runsc/test/image/tomcat_test.go
index 578385ca7..dd47ab6da 100644
--- a/runsc/test/image/tomcat_test.go
+++ b/runsc/test/image/tomcat_test.go
@@ -24,6 +24,9 @@ import (
)
func TestTomcat(t *testing.T) {
+ if out, err := testutil.Pull("tomcat:8.0"); err != nil {
+ t.Fatalf("docker pull failed: %v\nout: %s", err, out)
+ }
d := testutil.MakeDocker("tomcat-test")
if out, err := d.Run("-p", "8080", "tomcat:8.0"); err != nil {
t.Fatalf("docker run failed: %v\nout: %s", err, out)
diff --git a/runsc/test/testutil/docker.go b/runsc/test/testutil/docker.go
index 4eb049591..ec5ff850b 100644
--- a/runsc/test/testutil/docker.go
+++ b/runsc/test/testutil/docker.go
@@ -94,6 +94,24 @@ func getLocalPath(file string) string {
return path.Join(".", file)
}
+// do executes docker command.
+func do(args ...string) (string, error) {
+ fmt.Printf("Running: docker %s\n", args)
+ cmd := exec.Command("docker", args...)
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ return "", fmt.Errorf("error executing docker %s: %v", args, err)
+ }
+ return string(out), nil
+}
+
+// Pull pulls a docker image. This is used in tests to isolate the
+// time to pull the image off the network from the time to actually
+// start the container, to avoid timeouts over slow networks.
+func Pull(image string) (string, error) {
+ return do("pull", image)
+}
+
// Docker contains the name and the runtime of a docker container.
type Docker struct {
Runtime string
@@ -107,30 +125,19 @@ func MakeDocker(namePrefix string) Docker {
return Docker{Name: namePrefix + suffix, Runtime: runtime()}
}
-// Do executes docker command.
-func (d *Docker) Do(args ...string) (string, error) {
- fmt.Printf("Running: docker %s\n", args)
- cmd := exec.Command("docker", args...)
- out, err := cmd.CombinedOutput()
- if err != nil {
- return "", fmt.Errorf("error executing docker %s: %v", args, err)
- }
- return string(out), nil
-}
-
// Run calls 'docker run' with the arguments provided.
func (d *Docker) Run(args ...string) (string, error) {
a := []string{"run", "--runtime", d.Runtime, "--name", d.Name, "-d"}
a = append(a, args...)
- return d.Do(a...)
+ return do(a...)
}
// CleanUp kills and deletes the container.
func (d *Docker) CleanUp() error {
- if _, err := d.Do("kill", d.Name); err != nil {
+ if _, err := do("kill", d.Name); err != nil {
return fmt.Errorf("error killing container %q: %v", d.Name, err)
}
- if _, err := d.Do("rm", d.Name); err != nil {
+ if _, err := do("rm", d.Name); err != nil {
return fmt.Errorf("error deleting container %q: %v", d.Name, err)
}
return nil
@@ -140,7 +147,7 @@ func (d *Docker) CleanUp() error {
// docker to allocate a free port in the host and prevent conflicts.
func (d *Docker) FindPort(sandboxPort int) (int, error) {
format := fmt.Sprintf(`{{ (index (index .NetworkSettings.Ports "%d/tcp") 0).HostPort }}`, sandboxPort)
- out, err := d.Do("inspect", "-f", format, d.Name)
+ out, err := do("inspect", "-f", format, d.Name)
if err != nil {
return -1, fmt.Errorf("error retrieving port: %v", err)
}
@@ -158,7 +165,7 @@ func (d *Docker) WaitForOutput(pattern string, timeout time.Duration) error {
var out string
for exp := time.Now().Add(timeout); time.Now().Before(exp); {
var err error
- out, err = d.Do("logs", d.Name)
+ out, err = do("logs", d.Name)
if err != nil {
return err
}