2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-06-20 21:37:56 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-10-04 03:43:18 +00:00
|
|
|
// Package image provides end-to-end image tests for runsc.
|
|
|
|
|
2019-09-04 05:01:34 +00:00
|
|
|
// Each test calls docker commands to start up a container, and tests that it
|
|
|
|
// is behaving properly, like connecting to a port or looking at the output.
|
|
|
|
// The container is killed and deleted at the end.
|
2018-10-04 03:43:18 +00:00
|
|
|
//
|
2019-09-04 05:01:34 +00:00
|
|
|
// Setup instruction in test/README.md.
|
2018-06-20 21:37:56 +00:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2019-09-04 05:01:34 +00:00
|
|
|
"flag"
|
2018-06-20 21:37:56 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-05-13 07:49:32 +00:00
|
|
|
"log"
|
2018-06-20 21:37:56 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/test/dockerutil"
|
|
|
|
"gvisor.dev/gvisor/pkg/test/testutil"
|
2018-06-20 21:37:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHelloWorld(t *testing.T) {
|
2020-04-23 18:32:08 +00:00
|
|
|
d := dockerutil.MakeDocker(t)
|
|
|
|
defer d.CleanUp()
|
|
|
|
|
|
|
|
// Run the basic container.
|
|
|
|
out, err := d.Run(dockerutil.RunOpts{
|
|
|
|
Image: "basic/alpine",
|
|
|
|
}, "echo", "Hello world!")
|
|
|
|
if err != nil {
|
2018-08-07 20:47:16 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Check the output.
|
|
|
|
if !strings.Contains(out, "Hello world!") {
|
|
|
|
t.Fatalf("docker didn't say hello: got %s", out)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 07:49:32 +00:00
|
|
|
func runHTTPRequest(port int) error {
|
2018-06-28 20:53:08 +00:00
|
|
|
url := fmt.Sprintf("http://localhost:%d/not-found", port)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reaching http server: %v", err)
|
|
|
|
}
|
|
|
|
if want := http.StatusNotFound; resp.StatusCode != want {
|
|
|
|
return fmt.Errorf("Wrong response code, got: %d, want: %d", resp.StatusCode, want)
|
|
|
|
}
|
2018-06-20 21:37:56 +00:00
|
|
|
|
2018-06-28 20:53:08 +00:00
|
|
|
url = fmt.Sprintf("http://localhost:%d/latin10k.txt", port)
|
|
|
|
resp, err = http.Get(url)
|
2018-06-20 21:37:56 +00:00
|
|
|
if err != nil {
|
2018-06-28 20:53:08 +00:00
|
|
|
return fmt.Errorf("Error reaching http server: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
2018-06-28 20:53:08 +00:00
|
|
|
if want := http.StatusOK; resp.StatusCode != want {
|
|
|
|
return fmt.Errorf("Wrong response code, got: %d, want: %d", resp.StatusCode, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading http response: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
2018-06-28 20:53:08 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// READALL is the last word in the file. Ensures everything was read.
|
|
|
|
if want := "READALL"; strings.HasSuffix(string(body), want) {
|
|
|
|
return fmt.Errorf("response doesn't contain %q, resp: %q", want, body)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-13 07:49:32 +00:00
|
|
|
func testHTTPServer(t *testing.T, port int) {
|
|
|
|
const requests = 10
|
|
|
|
ch := make(chan error, requests)
|
|
|
|
for i := 0; i < requests; i++ {
|
|
|
|
go func() {
|
|
|
|
start := time.Now()
|
|
|
|
err := runHTTPRequest(port)
|
|
|
|
log.Printf("Response time %v: %v", time.Since(start).String(), err)
|
|
|
|
ch <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < requests; i++ {
|
|
|
|
err := <-ch
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("testHTTPServer(%d) failed: %v", port, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 20:53:08 +00:00
|
|
|
func TestHttpd(t *testing.T) {
|
2020-04-23 18:32:08 +00:00
|
|
|
d := dockerutil.MakeDocker(t)
|
|
|
|
defer d.CleanUp()
|
2018-06-20 21:37:56 +00:00
|
|
|
|
|
|
|
// Start the container.
|
2020-04-23 18:32:08 +00:00
|
|
|
d.CopyFiles("/usr/local/apache2/htdocs", "test/image/latin10k.txt")
|
|
|
|
if err := d.Spawn(dockerutil.RunOpts{
|
|
|
|
Image: "basic/httpd",
|
|
|
|
Ports: []int{80},
|
|
|
|
}); err != nil {
|
2018-08-07 20:47:16 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find where port 80 is mapped to.
|
2018-07-11 16:36:20 +00:00
|
|
|
port, err := d.FindPort(80)
|
2018-06-20 21:37:56 +00:00
|
|
|
if err != nil {
|
2020-04-23 18:32:08 +00:00
|
|
|
t.Fatalf("FindPort(80) failed: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until it's up and running.
|
2019-05-13 07:49:32 +00:00
|
|
|
if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil {
|
|
|
|
t.Errorf("WaitForHTTP() timeout: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 07:49:32 +00:00
|
|
|
testHTTPServer(t, port)
|
2018-06-28 20:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNginx(t *testing.T) {
|
2020-04-23 18:32:08 +00:00
|
|
|
d := dockerutil.MakeDocker(t)
|
|
|
|
defer d.CleanUp()
|
2018-06-28 20:53:08 +00:00
|
|
|
|
|
|
|
// Start the container.
|
2020-04-23 18:32:08 +00:00
|
|
|
d.CopyFiles("/usr/share/nginx/html", "test/image/latin10k.txt")
|
|
|
|
if err := d.Spawn(dockerutil.RunOpts{
|
|
|
|
Image: "basic/nginx",
|
|
|
|
Ports: []int{80},
|
|
|
|
}); err != nil {
|
2018-08-07 20:47:16 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 20:53:08 +00:00
|
|
|
// Find where port 80 is mapped to.
|
2018-07-11 16:36:20 +00:00
|
|
|
port, err := d.FindPort(80)
|
2018-06-20 21:37:56 +00:00
|
|
|
if err != nil {
|
2020-04-23 18:32:08 +00:00
|
|
|
t.Fatalf("FindPort(80) failed: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
2018-06-28 20:53:08 +00:00
|
|
|
|
|
|
|
// Wait until it's up and running.
|
2019-05-13 07:49:32 +00:00
|
|
|
if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil {
|
|
|
|
t.Errorf("WaitForHTTP() timeout: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 07:49:32 +00:00
|
|
|
testHTTPServer(t, port)
|
2018-06-28 20:53:08 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 01:22:22 +00:00
|
|
|
func TestMysql(t *testing.T) {
|
2020-04-23 18:32:08 +00:00
|
|
|
server := dockerutil.MakeDocker(t)
|
|
|
|
defer server.CleanUp()
|
2018-06-28 20:53:08 +00:00
|
|
|
|
|
|
|
// Start the container.
|
2020-04-23 18:32:08 +00:00
|
|
|
if err := server.Spawn(dockerutil.RunOpts{
|
|
|
|
Image: "basic/mysql",
|
|
|
|
Env: []string{"MYSQL_ROOT_PASSWORD=foobar123"},
|
|
|
|
}); err != nil {
|
2018-08-07 20:47:16 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
2018-06-28 20:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until it's up and running.
|
2020-04-23 18:32:08 +00:00
|
|
|
if _, err := server.WaitForOutput("port: 3306 MySQL Community Server", 3*time.Minute); err != nil {
|
|
|
|
t.Fatalf("WaitForOutput() timeout: %v", err)
|
2018-06-28 20:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Generate the client and copy in the SQL payload.
|
|
|
|
client := dockerutil.MakeDocker(t)
|
|
|
|
defer client.CleanUp()
|
2018-06-20 21:37:56 +00:00
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Tell mysql client to connect to the server and execute the file in
|
|
|
|
// verbose mode to verify the output.
|
|
|
|
client.CopyFiles("/sql", "test/image/mysql.sql")
|
|
|
|
client.Link("mysql", server)
|
|
|
|
if _, err := client.Run(dockerutil.RunOpts{
|
|
|
|
Image: "basic/mysql",
|
|
|
|
}, "mysql", "-hmysql", "-uroot", "-pfoobar123", "-v", "-e", "source /sql/mysql.sql"); err != nil {
|
2018-08-07 20:47:16 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
2018-06-28 20:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure file executed to the end and shutdown mysql.
|
2020-04-23 18:32:08 +00:00
|
|
|
if _, err := server.WaitForOutput("mysqld: Shutdown complete", 30*time.Second); err != nil {
|
|
|
|
t.Fatalf("WaitForOutput() timeout: %v", err)
|
2018-06-20 21:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
func TestTomcat(t *testing.T) {
|
|
|
|
d := dockerutil.MakeDocker(t)
|
2018-08-28 18:29:53 +00:00
|
|
|
defer d.CleanUp()
|
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Start the server.
|
|
|
|
if err := d.Spawn(dockerutil.RunOpts{
|
|
|
|
Image: "basic/tomcat",
|
|
|
|
Ports: []int{8080},
|
|
|
|
}); err != nil {
|
2018-08-28 18:29:53 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find where port 8080 is mapped to.
|
|
|
|
port, err := d.FindPort(8080)
|
|
|
|
if err != nil {
|
2020-04-23 18:32:08 +00:00
|
|
|
t.Fatalf("FindPort(8080) failed: %v", err)
|
2018-08-28 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until it's up and running.
|
2019-05-13 07:49:32 +00:00
|
|
|
if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil {
|
2018-08-28 18:29:53 +00:00
|
|
|
t.Fatalf("WaitForHTTP() timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that content is being served.
|
|
|
|
url := fmt.Sprintf("http://localhost:%d", port)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reaching http server: %v", err)
|
|
|
|
}
|
|
|
|
if want := http.StatusOK; resp.StatusCode != want {
|
|
|
|
t.Errorf("Wrong response code, got: %d, want: %d", resp.StatusCode, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 22:51:36 +00:00
|
|
|
func TestRuby(t *testing.T) {
|
2020-04-23 18:32:08 +00:00
|
|
|
d := dockerutil.MakeDocker(t)
|
|
|
|
defer d.CleanUp()
|
2018-09-28 22:51:36 +00:00
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Execute the ruby workload.
|
|
|
|
d.CopyFiles("/src", "test/image/ruby.rb", "test/image/ruby.sh")
|
|
|
|
if err := d.Spawn(dockerutil.RunOpts{
|
|
|
|
Image: "basic/ruby",
|
|
|
|
Ports: []int{8080},
|
|
|
|
}, "/src/ruby.sh"); err != nil {
|
2018-09-28 22:51:36 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find where port 8080 is mapped to.
|
|
|
|
port, err := d.FindPort(8080)
|
|
|
|
if err != nil {
|
2020-04-23 18:32:08 +00:00
|
|
|
t.Fatalf("FindPort(8080) failed: %v", err)
|
2018-09-28 22:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until it's up and running, 'gem install' can take some time.
|
2018-10-03 00:27:09 +00:00
|
|
|
if err := testutil.WaitForHTTP(port, 1*time.Minute); err != nil {
|
2018-09-28 22:51:36 +00:00
|
|
|
t.Fatalf("WaitForHTTP() timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that content is being served.
|
|
|
|
url := fmt.Sprintf("http://localhost:%d", port)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error reaching http server: %v", err)
|
|
|
|
}
|
|
|
|
if want := http.StatusOK; resp.StatusCode != want {
|
|
|
|
t.Errorf("wrong response code, got: %d, want: %d", resp.StatusCode, want)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading body: %v", err)
|
|
|
|
}
|
|
|
|
if got, want := string(body), "Hello World"; !strings.Contains(got, want) {
|
|
|
|
t.Errorf("invalid body content, got: %q, want: %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:00:40 +00:00
|
|
|
func TestStdio(t *testing.T) {
|
2020-04-23 18:32:08 +00:00
|
|
|
d := dockerutil.MakeDocker(t)
|
|
|
|
defer d.CleanUp()
|
2018-10-04 18:00:40 +00:00
|
|
|
|
|
|
|
wantStdout := "hello stdout"
|
|
|
|
wantStderr := "bonjour stderr"
|
|
|
|
cmd := fmt.Sprintf("echo %q; echo %q 1>&2;", wantStdout, wantStderr)
|
2020-04-23 18:32:08 +00:00
|
|
|
if err := d.Spawn(dockerutil.RunOpts{
|
|
|
|
Image: "basic/alpine",
|
|
|
|
}, "/bin/sh", "-c", cmd); err != nil {
|
2018-10-04 18:00:40 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, want := range []string{wantStdout, wantStderr} {
|
|
|
|
if _, err := d.WaitForOutput(want, 5*time.Second); err != nil {
|
|
|
|
t.Fatalf("docker didn't get output %q : %v", want, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 03:43:18 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2019-09-04 05:01:34 +00:00
|
|
|
dockerutil.EnsureSupportedDockerVersion()
|
|
|
|
flag.Parse()
|
2018-06-20 21:37:56 +00:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|