mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
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
This commit is contained in:
committed by
Shentubot
parent
57d0fcbdbf
commit
4c1167de4e
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user