diff options
author | Fabricio Voznika <fvoznika@google.com> | 2018-09-28 15:51:36 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-09-28 15:52:33 -0700 |
commit | 49ff81a42b51a3fa2ee139e1e86179fa0c427a86 (patch) | |
tree | 35194b1301a589cf5ff830f84f8b7c984f88f0a5 | |
parent | 2496d9b4b6343154525f73e9583a4a60bebcfa30 (diff) |
Add ruby image tests
PiperOrigin-RevId: 215009066
Change-Id: I54ab920fa649cf4d0817f7cb8ea76f9126523330
-rw-r--r-- | runsc/test/image/BUILD | 2 | ||||
-rw-r--r-- | runsc/test/image/image_test.go | 49 | ||||
-rw-r--r-- | runsc/test/image/ruby.rb | 23 | ||||
-rw-r--r-- | runsc/test/image/ruby.sh | 20 | ||||
-rw-r--r-- | runsc/test/testutil/docker.go | 17 |
5 files changed, 106 insertions, 5 deletions
diff --git a/runsc/test/image/BUILD b/runsc/test/image/BUILD index 5854eec12..c41161d50 100644 --- a/runsc/test/image/BUILD +++ b/runsc/test/image/BUILD @@ -11,6 +11,8 @@ go_test( data = [ "latin10k.txt", "mysql.sql", + "ruby.rb", + "ruby.sh", ], embed = [":image"], tags = [ diff --git a/runsc/test/image/image_test.go b/runsc/test/image/image_test.go index 13fd8f1ee..5f90ca9d2 100644 --- a/runsc/test/image/image_test.go +++ b/runsc/test/image/image_test.go @@ -30,6 +30,7 @@ import ( "io/ioutil" "net/http" "os" + "path/filepath" "strings" "testing" "time" @@ -256,6 +257,54 @@ func TestTomcat(t *testing.T) { } } +func TestRuby(t *testing.T) { + if err := testutil.Pull("ruby"); err != nil { + t.Fatalf("docker pull failed: %v", err) + } + d := testutil.MakeDocker("ruby-test") + + dir, err := testutil.PrepareFiles("ruby.rb", "ruby.sh") + if err != nil { + t.Fatalf("PrepareFiles() failed: %v", err) + } + if err := os.Chmod(filepath.Join(dir, "ruby.sh"), 0333); err != nil { + t.Fatalf("os.Chmod(%q, 0333) failed: %v", dir, err) + } + + if _, err := d.Run("-p", "8080", "-v", testutil.MountArg(dir, "/src:ro"), "ruby", "/src/ruby.sh"); err != nil { + t.Fatalf("docker run failed: %v", err) + } + defer d.CleanUp() + + // Find where port 8080 is mapped to. + port, err := d.FindPort(8080) + if err != nil { + t.Fatalf("docker.FindPort(8080) failed: %v", err) + } + + // Wait until it's up and running, 'gem install' can take some time. + if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil { + 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) + } +} + func MainTest(m *testing.M) { testutil.EnsureSupportedDockerVersion() os.Exit(m.Run()) diff --git a/runsc/test/image/ruby.rb b/runsc/test/image/ruby.rb new file mode 100644 index 000000000..ae5de3419 --- /dev/null +++ b/runsc/test/image/ruby.rb @@ -0,0 +1,23 @@ +# Copyright 2018 Google Inc. +# +# 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. + +require 'sinatra' + +set :bind, "0.0.0.0" +set :port, 8080 + +get '/' do + 'Hello World' +end + diff --git a/runsc/test/image/ruby.sh b/runsc/test/image/ruby.sh new file mode 100644 index 000000000..54be2c931 --- /dev/null +++ b/runsc/test/image/ruby.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Copyright 2018 Google Inc. +# +# 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. + +set -e + +gem install sinatra +ruby /src/ruby.rb diff --git a/runsc/test/testutil/docker.go b/runsc/test/testutil/docker.go index c73bb0406..d0446df4e 100644 --- a/runsc/test/testutil/docker.go +++ b/runsc/test/testutil/docker.go @@ -162,6 +162,11 @@ func (d *Docker) Run(args ...string) (string, error) { return do(a...) } +// Logs calls 'docker logs'. +func (d *Docker) Logs() (string, error) { + return do("logs", d.Name) +} + // Exec calls 'docker exec' with the arguments provided. func (d *Docker) Exec(args ...string) (string, error) { a := []string{"exec", d.Name} @@ -193,12 +198,14 @@ func (d *Docker) Remove() error { return nil } -// CleanUp kills and deletes the container. -func (d *Docker) CleanUp() error { +// CleanUp kills and deletes the container (best effort). +func (d *Docker) CleanUp() { if _, err := do("kill", d.Name); err != nil { - return fmt.Errorf("error killing container %q: %v", d.Name, err) + log.Printf("error killing container %q: %v", d.Name, err) + } + if err := d.Remove(); err != nil { + log.Print(err) } - return d.Remove() } // FindPort returns the host port that is mapped to 'sandboxPort'. This calls @@ -223,7 +230,7 @@ func (d *Docker) WaitForOutput(pattern string, timeout time.Duration) (string, e var out string for exp := time.Now().Add(timeout); time.Now().Before(exp); { var err error - out, err = do("logs", d.Name) + out, err = d.Logs() if err != nil { return "", err } |