1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

refactor integration test to use checkers

Signed-off-by: Shijiang Wei <mountkin@gmail.com>
This commit is contained in:
Shijiang Wei 2015-10-11 15:33:24 +08:00
parent 9a13c2dc79
commit a1d4b7dd0d
2 changed files with 32 additions and 91 deletions

View file

@ -22,9 +22,8 @@ func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
endGroup.Add(3) endGroup.Add(3)
startGroup.Add(3) startGroup.Add(3)
if err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done"); err != nil { err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
c.Fatal(err) c.Assert(err, check.IsNil)
}
startDone := make(chan struct{}) startDone := make(chan struct{})
endDone := make(chan struct{}) endDone := make(chan struct{})
@ -84,7 +83,6 @@ func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
case <-time.After(attachWait): case <-time.After(attachWait):
c.Fatalf("Attaches did not finish properly") c.Fatalf("Attaches did not finish properly")
} }
} }
func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) { func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) {
@ -94,13 +92,6 @@ func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) {
id := strings.TrimSpace(out) id := strings.TrimSpace(out)
c.Assert(waitRun(id), check.IsNil) c.Assert(waitRun(id), check.IsNil)
defer func() {
cmd := exec.Command(dockerBinary, "kill", id)
if out, _, err := runCommandWithOutput(cmd); err != nil {
c.Fatalf("failed to kill container: %v (%v)", out, err)
}
}()
done := make(chan error) done := make(chan error)
go func() { go func() {
defer close(done) defer close(done)
@ -141,37 +132,21 @@ func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
} }
defer stdin.Close() defer stdin.Close()
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { c.Assert(err, check.IsNil)
c.Fatal(err)
}
defer stdout.Close() defer stdout.Close()
if err := cmd.Start(); err != nil { c.Assert(cmd.Start(), check.IsNil)
c.Fatal(err)
}
defer cmd.Process.Kill() defer cmd.Process.Kill()
if _, err := stdin.Write([]byte("hello\n")); err != nil { _, err = stdin.Write([]byte("hello\n"))
c.Fatal(err) c.Assert(err, check.IsNil)
}
out, err = bufio.NewReader(stdout).ReadString('\n') out, err = bufio.NewReader(stdout).ReadString('\n')
if err != nil { c.Assert(err, check.IsNil)
c.Fatal(err) c.Assert(strings.TrimSpace(out), check.Equals, "hello")
}
if strings.TrimSpace(out) != "hello" {
c.Fatalf("expected 'hello', got %q", out)
}
if err := stdin.Close(); err != nil { c.Assert(stdin.Close(), check.IsNil)
c.Fatal(err)
}
// Expect container to still be running after stdin is closed // Expect container to still be running after stdin is closed
running, err := inspectField(id, "State.Running") running, err := inspectField(id, "State.Running")
if err != nil { c.Assert(err, check.IsNil)
c.Fatal(err) c.Assert(running, check.Equals, "true")
}
if running != "true" {
c.Fatal("expected container to still be running")
}
} }

View file

@ -57,21 +57,15 @@ type Daemon struct {
// The daemon will not automatically start. // The daemon will not automatically start.
func NewDaemon(c *check.C) *Daemon { func NewDaemon(c *check.C) *Daemon {
dest := os.Getenv("DEST") dest := os.Getenv("DEST")
if dest == "" { c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
c.Fatal("Please set the DEST environment variable")
}
id := fmt.Sprintf("d%d", time.Now().UnixNano()%100000000) id := fmt.Sprintf("d%d", time.Now().UnixNano()%100000000)
dir := filepath.Join(dest, id) dir := filepath.Join(dest, id)
daemonFolder, err := filepath.Abs(dir) daemonFolder, err := filepath.Abs(dir)
if err != nil { c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
c.Fatalf("Could not make %q an absolute path: %v", dir, err)
}
daemonRoot := filepath.Join(daemonFolder, "root") daemonRoot := filepath.Join(daemonFolder, "root")
if err := os.MkdirAll(daemonRoot, 0755); err != nil { c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
c.Fatalf("Could not create daemon root %q: %v", dir, err)
}
userlandProxy := true userlandProxy := true
if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" { if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
@ -96,9 +90,7 @@ func NewDaemon(c *check.C) *Daemon {
// You can specify additional daemon flags. // You can specify additional daemon flags.
func (d *Daemon) Start(arg ...string) error { func (d *Daemon) Start(arg ...string) error {
dockerBinary, err := exec.LookPath(dockerBinary) dockerBinary, err := exec.LookPath(dockerBinary)
if err != nil { d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
d.c.Fatalf("[%s] could not find docker binary in $PATH: %v", d.id, err)
}
args := append(d.GlobalFlags, args := append(d.GlobalFlags,
d.Command, d.Command,
@ -136,9 +128,7 @@ func (d *Daemon) Start(arg ...string) error {
d.cmd = exec.Command(dockerBinary, args...) d.cmd = exec.Command(dockerBinary, args...)
d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil { d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
d.c.Fatalf("[%s] Could not create %s/docker.log: %v", d.id, d.folder, err)
}
d.cmd.Stdout = d.logFile d.cmd.Stdout = d.logFile
d.cmd.Stderr = d.logFile d.cmd.Stderr = d.logFile
@ -187,9 +177,7 @@ func (d *Daemon) Start(arg ...string) error {
defer client.Close() defer client.Close()
req, err := http.NewRequest("GET", "/_ping", nil) req, err := http.NewRequest("GET", "/_ping", nil)
if err != nil { d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not create new request", d.id))
d.c.Fatalf("[%s] could not create new request: %v", d.id, err)
}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
@ -718,13 +706,7 @@ func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...strin
} }
func findContainerIP(c *check.C, id string, vargs ...string) string { func findContainerIP(c *check.C, id string, vargs ...string) string {
args := append(vargs, "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", id) out, _ := dockerCmd(c, "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", id)
cmd := exec.Command(dockerBinary, args...)
out, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out)
}
return strings.Trim(out, " \r\n'") return strings.Trim(out, " \r\n'")
} }
@ -1276,30 +1258,23 @@ func newFakeGit(name string, files map[string]string, enforceLocalServer bool) (
// Write `content` to the file at path `dst`, creating it if necessary, // Write `content` to the file at path `dst`, creating it if necessary,
// as well as any missing directories. // as well as any missing directories.
// The file is truncated if it already exists. // The file is truncated if it already exists.
// Call c.Fatal() at the first error. // Fail the test when error occures.
func writeFile(dst, content string, c *check.C) { func writeFile(dst, content string, c *check.C) {
// Create subdirectories if necessary // Create subdirectories if necessary
if err := os.MkdirAll(path.Dir(dst), 0700); err != nil { c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil)
c.Fatal(err)
}
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700) f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
if err != nil { c.Assert(err, check.IsNil)
c.Fatal(err)
}
defer f.Close() defer f.Close()
// Write content (truncate if it exists) // Write content (truncate if it exists)
if _, err := io.Copy(f, strings.NewReader(content)); err != nil { _, err = io.Copy(f, strings.NewReader(content))
c.Fatal(err) c.Assert(err, check.IsNil)
}
} }
// Return the contents of file at path `src`. // Return the contents of file at path `src`.
// Call c.Fatal() at the first error (including if the file doesn't exist) // Fail the test when error occures.
func readFile(src string, c *check.C) (content string) { func readFile(src string, c *check.C) (content string) {
data, err := ioutil.ReadFile(src) data, err := ioutil.ReadFile(src)
if err != nil { c.Assert(err, check.IsNil)
c.Fatal(err)
}
return string(data) return string(data)
} }
@ -1351,30 +1326,25 @@ func daemonTime(c *check.C) time.Time {
} }
status, body, err := sockRequest("GET", "/info", nil) status, body, err := sockRequest("GET", "/info", nil)
c.Assert(status, check.Equals, http.StatusOK)
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)
type infoJSON struct { type infoJSON struct {
SystemTime string SystemTime string
} }
var info infoJSON var info infoJSON
if err = json.Unmarshal(body, &info); err != nil { err = json.Unmarshal(body, &info)
c.Fatalf("unable to unmarshal /info response: %v", err) c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
}
dt, err := time.Parse(time.RFC3339Nano, info.SystemTime) dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
if err != nil { c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
c.Fatal(err)
}
return dt return dt
} }
func setupRegistry(c *check.C) *testRegistryV2 { func setupRegistry(c *check.C) *testRegistryV2 {
testRequires(c, RegistryHosting) testRequires(c, RegistryHosting)
reg, err := newTestRegistryV2(c) reg, err := newTestRegistryV2(c)
if err != nil { c.Assert(err, check.IsNil)
c.Fatal(err)
}
// Wait for registry to be ready to serve requests. // Wait for registry to be ready to serve requests.
for i := 0; i != 5; i++ { for i := 0; i != 5; i++ {
@ -1384,18 +1354,14 @@ func setupRegistry(c *check.C) *testRegistryV2 {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
if err != nil { c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available"))
c.Fatal("Timeout waiting for test registry to become available")
}
return reg return reg
} }
func setupNotary(c *check.C) *testNotary { func setupNotary(c *check.C) *testNotary {
testRequires(c, NotaryHosting) testRequires(c, NotaryHosting)
ts, err := newTestNotary(c) ts, err := newTestNotary(c)
if err != nil { c.Assert(err, check.IsNil)
c.Fatal(err)
}
return ts return ts
} }