Add ruby image tests

PiperOrigin-RevId: 215009066
Change-Id: I54ab920fa649cf4d0817f7cb8ea76f9126523330
This commit is contained in:
Fabricio Voznika
2018-09-28 15:52:33 -07:00
committed by Shentubot
parent 2496d9b4b6
commit 49ff81a42b
5 changed files with 106 additions and 5 deletions
+2
View File
@@ -11,6 +11,8 @@ go_test(
data = [
"latin10k.txt",
"mysql.sql",
"ruby.rb",
"ruby.sh",
],
embed = [":image"],
tags = [
+49
View File
@@ -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())
+23
View File
@@ -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
+20
View File
@@ -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
+12 -5
View File
@@ -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
}