mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Improve the containers unit tests (add error checking)
This commit is contained in:
parent
f483b214bb
commit
232dbb1864
1 changed files with 100 additions and 32 deletions
|
@ -91,17 +91,33 @@ func TestCommitRun(t *testing.T) {
|
|||
defer runtime.Destroy(container2)
|
||||
|
||||
stdout, err := container2.StdoutPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stderr, err := container2.StderrPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := container2.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
container2.Wait()
|
||||
output, err := ioutil.ReadAll(stdout)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output2, err := ioutil.ReadAll(stderr)
|
||||
stdout.Close()
|
||||
stderr.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stdout.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stderr.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(output) != "hello\n" {
|
||||
t.Fatalf("\nout: %s\nerr: %s\n", string(output), string(output2))
|
||||
t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", string(output), string(output2))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,11 +224,9 @@ func TestExitCode(t *testing.T) {
|
|||
defer nuke(runtime)
|
||||
|
||||
trueContainer, err := runtime.Create(&Config{
|
||||
|
||||
Image: GetTestImage(runtime).Id,
|
||||
Cmd: []string{"/bin/true", ""},
|
||||
},
|
||||
)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -220,12 +234,14 @@ func TestExitCode(t *testing.T) {
|
|||
if err := trueContainer.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if trueContainer.State.ExitCode != 0 {
|
||||
t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
|
||||
}
|
||||
|
||||
falseContainer, err := runtime.Create(&Config{
|
||||
Image: GetTestImage(runtime).Id,
|
||||
Cmd: []string{"/bin/false", ""},
|
||||
},
|
||||
)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -233,13 +249,8 @@ func TestExitCode(t *testing.T) {
|
|||
if err := falseContainer.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if trueContainer.State.ExitCode != 0 {
|
||||
t.Errorf("Unexpected exit code %v", trueContainer.State.ExitCode)
|
||||
}
|
||||
|
||||
if falseContainer.State.ExitCode != 1 {
|
||||
t.Errorf("Unexpected exit code %v", falseContainer.State.ExitCode)
|
||||
t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -295,32 +306,62 @@ func TestRestartStdin(t *testing.T) {
|
|||
defer runtime.Destroy(container)
|
||||
|
||||
stdin, err := container.StdinPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stdout, err := container.StdoutPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := container.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
io.WriteString(stdin, "hello world")
|
||||
stdin.Close()
|
||||
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stdin.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
container.Wait()
|
||||
output, err := ioutil.ReadAll(stdout)
|
||||
stdout.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stdout.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(output) != "hello world" {
|
||||
t.Fatal(string(output))
|
||||
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
||||
}
|
||||
|
||||
// Restart and try again
|
||||
stdin, err = container.StdinPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stdout, err = container.StdoutPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := container.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
io.WriteString(stdin, "hello world #2")
|
||||
stdin.Close()
|
||||
if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stdin.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
container.Wait()
|
||||
output, err = ioutil.ReadAll(stdout)
|
||||
stdout.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stdout.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(output) != "hello world #2" {
|
||||
t.Fatal(string(output))
|
||||
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -504,18 +545,31 @@ func TestStdin(t *testing.T) {
|
|||
defer runtime.Destroy(container)
|
||||
|
||||
stdin, err := container.StdinPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stdout, err := container.StdoutPipe()
|
||||
defer stdin.Close()
|
||||
defer stdout.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := container.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
io.WriteString(stdin, "hello world")
|
||||
stdin.Close()
|
||||
defer stdin.Close()
|
||||
defer stdout.Close()
|
||||
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stdin.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
container.Wait()
|
||||
output, err := ioutil.ReadAll(stdout)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(output) != "hello world" {
|
||||
t.Fatal(string(output))
|
||||
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -538,18 +592,31 @@ func TestTty(t *testing.T) {
|
|||
defer runtime.Destroy(container)
|
||||
|
||||
stdin, err := container.StdinPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stdout, err := container.StdoutPipe()
|
||||
defer stdin.Close()
|
||||
defer stdout.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := container.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
io.WriteString(stdin, "hello world")
|
||||
stdin.Close()
|
||||
defer stdin.Close()
|
||||
defer stdout.Close()
|
||||
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := stdin.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
container.Wait()
|
||||
output, err := ioutil.ReadAll(stdout)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(output) != "hello world" {
|
||||
t.Fatal(string(output))
|
||||
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -568,6 +635,7 @@ func TestEnv(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
defer runtime.Destroy(container)
|
||||
|
||||
stdout, err := container.StdoutPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
Loading…
Reference in a new issue