mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Made a few changes to make testutil.Docker easier to use
PiperOrigin-RevId: 215023376 Change-Id: I139569bd15c013e5dd0f60d0c98a64eaa0ba9e8e
This commit is contained in:
committed by
Shentubot
parent
f21dde5666
commit
cfdd418fe2
@@ -40,7 +40,7 @@ import (
|
||||
|
||||
func TestHelloWorld(t *testing.T) {
|
||||
d := testutil.MakeDocker("hello-test")
|
||||
if _, err := d.Run("hello-world"); err != nil {
|
||||
if err := d.Run("hello-world"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
@@ -94,7 +94,8 @@ func TestHttpd(t *testing.T) {
|
||||
}
|
||||
|
||||
// Start the container.
|
||||
if _, err := d.Run("-p", "80", "-v", testutil.MountArg(dir, "/usr/local/apache2/htdocs:ro"), "httpd"); err != nil {
|
||||
mountArg := testutil.MountArg(dir, "/usr/local/apache2/htdocs", testutil.ReadOnly)
|
||||
if err := d.Run("-p", "80", mountArg, "httpd"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
@@ -127,7 +128,8 @@ func TestNginx(t *testing.T) {
|
||||
}
|
||||
|
||||
// Start the container.
|
||||
if _, err := d.Run("-p", "80", "-v", testutil.MountArg(dir, "/usr/share/nginx/html:ro"), "nginx"); err != nil {
|
||||
mountArg := testutil.MountArg(dir, "/usr/share/nginx/html", testutil.ReadOnly)
|
||||
if err := d.Run("-p", "80", mountArg, "nginx"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
@@ -155,7 +157,7 @@ func TestMysql(t *testing.T) {
|
||||
d := testutil.MakeDocker("mysql-test")
|
||||
|
||||
// Start the container.
|
||||
if _, err := d.Run("-e", "MYSQL_ROOT_PASSWORD=foobar123", "mysql"); err != nil {
|
||||
if err := d.Run("-e", "MYSQL_ROOT_PASSWORD=foobar123", "mysql"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
@@ -174,12 +176,12 @@ func TestMysql(t *testing.T) {
|
||||
// Tell mysql client to connect to the server and execute the file in verbose
|
||||
// mode to verify the output.
|
||||
args := []string{
|
||||
"--link", testutil.LinkArg(&d, "mysql"),
|
||||
"-v", testutil.MountArg(dir, "/sql"),
|
||||
testutil.LinkArg(&d, "mysql"),
|
||||
testutil.MountArg(dir, "/sql", testutil.ReadWrite),
|
||||
"mysql",
|
||||
"mysql", "-hmysql", "-uroot", "-pfoobar123", "-v", "-e", "source /sql/mysql.sql",
|
||||
}
|
||||
if _, err := client.Run(args...); err != nil {
|
||||
if err := client.Run(args...); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer client.CleanUp()
|
||||
@@ -198,7 +200,7 @@ func TestPythonHello(t *testing.T) {
|
||||
t.Fatalf("docker pull failed: %v", err)
|
||||
}
|
||||
d := testutil.MakeDocker("python-hello-test")
|
||||
if _, err := d.Run("-p", "8080", "google/python-hello"); err != nil {
|
||||
if err := d.Run("-p", "8080", "google/python-hello"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
@@ -230,7 +232,7 @@ func TestTomcat(t *testing.T) {
|
||||
t.Fatalf("docker pull failed: %v", err)
|
||||
}
|
||||
d := testutil.MakeDocker("tomcat-test")
|
||||
if _, err := d.Run("-p", "8080", "tomcat:8.0"); err != nil {
|
||||
if err := d.Run("-p", "8080", "tomcat:8.0"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestExecCapabilities(t *testing.T) {
|
||||
d := testutil.MakeDocker("exec-test")
|
||||
|
||||
// Start the container.
|
||||
if _, err := d.Run("alpine", "sh", "-c", "cat /proc/self/status; sleep 100"); err != nil {
|
||||
if err := d.Run("alpine", "sh", "-c", "cat /proc/self/status; sleep 100"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
|
||||
@@ -98,8 +98,8 @@ func TestPauseResume(t *testing.T) {
|
||||
t.Fatal("docker pull failed:", err)
|
||||
}
|
||||
d := testutil.MakeDocker("pause-resume-test")
|
||||
if out, err := d.Run("-p", "8080", "google/python-hello"); err != nil {
|
||||
t.Fatalf("docker run failed: %v\nout: %s", err, out)
|
||||
if err := d.Run("-p", "8080", "google/python-hello"); err != nil {
|
||||
t.Fatalf("docker run failed: %v", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
|
||||
@@ -157,7 +157,7 @@ func TestConnectToSelf(t *testing.T) {
|
||||
|
||||
// Creates server that replies "server" and exists. Sleeps at the end because
|
||||
// 'docker exec' gets killed if the init process exists before it can finish.
|
||||
if _, err := d.Run("ubuntu:trusty", "/bin/sh", "-c", "echo server | nc -l -p 8080 && sleep 1"); err != nil {
|
||||
if err := d.Run("ubuntu:trusty", "/bin/sh", "-c", "echo server | nc -l -p 8080 && sleep 1"); err != nil {
|
||||
t.Fatal("docker run failed:", err)
|
||||
}
|
||||
defer d.CleanUp()
|
||||
|
||||
@@ -65,14 +65,35 @@ func EnsureSupportedDockerVersion() {
|
||||
}
|
||||
}
|
||||
|
||||
// MountMode describes if the mount should be ro or rw.
|
||||
type MountMode int
|
||||
|
||||
const (
|
||||
// ReadOnly is what the name says.
|
||||
ReadOnly MountMode = iota
|
||||
// ReadWrite is what the name says.
|
||||
ReadWrite
|
||||
)
|
||||
|
||||
// String returns the mount mode argument for this MountMode.
|
||||
func (m MountMode) String() string {
|
||||
switch m {
|
||||
case ReadOnly:
|
||||
return "ro"
|
||||
case ReadWrite:
|
||||
return "rw"
|
||||
}
|
||||
panic(fmt.Sprintf("invalid mode: %d", m))
|
||||
}
|
||||
|
||||
// MountArg formats the volume argument to mount in the container.
|
||||
func MountArg(source, target string) string {
|
||||
return fmt.Sprintf("%s:%s", source, target)
|
||||
func MountArg(source, target string, mode MountMode) string {
|
||||
return fmt.Sprintf("-v=%s:%s:%v", source, target, mode)
|
||||
}
|
||||
|
||||
// LinkArg formats the link argument.
|
||||
func LinkArg(source *Docker, target string) string {
|
||||
return fmt.Sprintf("%s:%s", source.Name, target)
|
||||
return fmt.Sprintf("--link=%s:%s", source.Name, target)
|
||||
}
|
||||
|
||||
// PrepareFiles creates temp directory to copy files there. The sandbox doesn't
|
||||
@@ -155,11 +176,13 @@ func (d *Docker) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run calls 'docker run' with the arguments provided.
|
||||
func (d *Docker) Run(args ...string) (string, error) {
|
||||
// Run calls 'docker run' with the arguments provided. The container starts
|
||||
// running in the backgroud and the call returns immediately.
|
||||
func (d *Docker) Run(args ...string) error {
|
||||
a := []string{"run", "--runtime", d.Runtime, "--name", d.Name, "-d"}
|
||||
a = append(a, args...)
|
||||
return do(a...)
|
||||
_, err := do(a...)
|
||||
return err
|
||||
}
|
||||
|
||||
// Logs calls 'docker logs'.
|
||||
|
||||
Reference in New Issue
Block a user