a27b4b8c |
package docker
import ( |
4e5ae883 |
"bufio" |
a2d7dd1a |
"fmt" |
ca40989e |
"io"
"io/ioutil" |
4e5ae883 |
"math/rand"
"os" |
a8ae0aaf |
"regexp" |
fb350e0c |
"sort" |
6de3e8a2 |
"strings" |
a27b4b8c |
"testing" |
a2d7dd1a |
"time" |
a27b4b8c |
)
|
fd224ee5 |
func TestIDFormat(t *testing.T) { |
a8ae0aaf |
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) |
ff95f2b0 |
container1, err := NewBuilder(runtime).Create( |
a8ae0aaf |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ebb2491 |
Cmd: []string{"/bin/sh", "-c", "echo hello world"}, |
a8ae0aaf |
},
)
if err != nil {
t.Fatal(err)
} |
fd224ee5 |
match, err := regexp.Match("^[0-9a-f]{64}$", []byte(container1.ID)) |
a8ae0aaf |
if err != nil {
t.Fatal(err)
}
if !match { |
fd224ee5 |
t.Fatalf("Invalid container ID: %s", container1.ID) |
a8ae0aaf |
}
}
|
0ebdca5e |
func TestMultipleAttachRestart(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create( |
0ebdca5e |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
0ebdca5e |
Cmd: []string{"/bin/sh", "-c",
"i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
// Simulate 3 client attaching to the container and stop/restart
stdout1, err := container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
stdout2, err := container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
stdout3, err := container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
if err := container.Start(); err != nil {
t.Fatal(err)
}
l1, err := bufio.NewReader(stdout1).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if strings.Trim(l1, " \r\n") != "hello" {
t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
}
l2, err := bufio.NewReader(stdout2).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if strings.Trim(l2, " \r\n") != "hello" {
t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
}
l3, err := bufio.NewReader(stdout3).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if strings.Trim(l3, " \r\n") != "hello" {
t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
}
|
90602ab6 |
if err := container.Stop(10); err != nil { |
0ebdca5e |
t.Fatal(err)
}
stdout1, err = container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
stdout2, err = container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
stdout3, err = container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
if err := container.Start(); err != nil {
t.Fatal(err)
} |
c45beabc |
setTimeout(t, "Timeout reading from the process", 3*time.Second, func() { |
0ebdca5e |
l1, err = bufio.NewReader(stdout1).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if strings.Trim(l1, " \r\n") != "hello" {
t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
}
l2, err = bufio.NewReader(stdout2).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if strings.Trim(l2, " \r\n") != "hello" {
t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
}
l3, err = bufio.NewReader(stdout3).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if strings.Trim(l3, " \r\n") != "hello" {
t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
} |
c45beabc |
})
container.Wait() |
0ebdca5e |
}
|
97535e5a |
func TestDiff(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
|
ff95f2b0 |
builder := NewBuilder(runtime)
|
97535e5a |
// Create a container and remove a file |
ff95f2b0 |
container1, err := builder.Create( |
97535e5a |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
97535e5a |
Cmd: []string{"/bin/rm", "/etc/passwd"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container1)
if err := container1.Run(); err != nil {
t.Fatal(err)
}
// Check the changelog
c, err := container1.Changes()
if err != nil {
t.Fatal(err)
}
success := false
for _, elem := range c {
if elem.Path == "/etc/passwd" && elem.Kind == 2 {
success = true
}
}
if !success {
t.Fatalf("/etc/passwd as been removed but is not present in the diff")
}
// Commit the container
rwTar, err := container1.ExportRw()
if err != nil {
t.Error(err)
} |
724e2d6b |
img, err := runtime.graph.Create(rwTar, container1, "unit test commited image - diff", "", nil) |
97535e5a |
if err != nil {
t.Error(err)
}
// Create a new container from the commited image |
ff95f2b0 |
container2, err := builder.Create( |
97535e5a |
&Config{ |
fd224ee5 |
Image: img.ID, |
97535e5a |
Cmd: []string{"cat", "/etc/passwd"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container2)
if err := container2.Run(); err != nil {
t.Fatal(err)
}
// Check the changelog
c, err = container2.Changes()
if err != nil {
t.Fatal(err)
}
for _, elem := range c {
if elem.Path == "/etc/passwd" {
t.Fatalf("/etc/passwd should not be present in the diff after commit.")
}
} |
71b1657e |
// Create a new containere
container3, err := builder.Create(
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
71b1657e |
Cmd: []string{"rm", "/bin/httpd"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container3)
if err := container3.Run(); err != nil {
t.Fatal(err)
}
// Check the changelog
c, err = container3.Changes()
if err != nil {
t.Fatal(err)
}
success = false
for _, elem := range c {
if elem.Path == "/bin/httpd" && elem.Kind == 2 {
success = true
}
}
if !success {
t.Fatalf("/bin/httpd should be present in the diff after commit.")
} |
97535e5a |
}
|
30d327d3 |
func TestCommitAutoRun(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) |
6c168a89 |
builder := NewBuilder(runtime)
container1, err := builder.Create( |
30d327d3 |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
30d327d3 |
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container1)
if container1.State.Running {
t.Errorf("Container shouldn't be running")
}
if err := container1.Run(); err != nil {
t.Fatal(err)
}
if container1.State.Running {
t.Errorf("Container shouldn't be running")
}
rwTar, err := container1.ExportRw()
if err != nil {
t.Error(err)
}
img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", &Config{Cmd: []string{"cat", "/world"}})
if err != nil {
t.Error(err)
}
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world |
6c168a89 |
container2, err := builder.Create( |
30d327d3 |
&Config{ |
fd224ee5 |
Image: img.ID, |
30d327d3 |
},
)
if err != nil {
t.Fatal(err)
}
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)
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("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
}
}
|
f2dc079c |
func TestCommitRun(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
builder := NewBuilder(runtime)
container1, err := builder.Create( |
a27b4b8c |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ebb2491 |
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"}, |
a27b4b8c |
},
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container1) |
a27b4b8c |
|
f2dc079c |
if container1.State.Running { |
a27b4b8c |
t.Errorf("Container shouldn't be running")
} |
f2dc079c |
if err := container1.Run(); err != nil { |
a27b4b8c |
t.Fatal(err)
} |
f2dc079c |
if container1.State.Running { |
a27b4b8c |
t.Errorf("Container shouldn't be running")
} |
f2dc079c |
|
7c57a4cf |
rwTar, err := container1.ExportRw() |
f2dc079c |
if err != nil {
t.Error(err)
} |
724e2d6b |
img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", nil) |
f2dc079c |
if err != nil {
t.Error(err)
}
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
ff95f2b0 |
container2, err := builder.Create( |
f2dc079c |
&Config{ |
fd224ee5 |
Image: img.ID, |
6ebb2491 |
Cmd: []string{"cat", "/world"}, |
45a8bba1 |
},
) |
f2dc079c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container2) |
f2dc079c |
stdout, err := container2.StdoutPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
f2dc079c |
stderr, err := container2.StderrPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
f2dc079c |
if err := container2.Start(); err != nil {
t.Fatal(err)
}
container2.Wait()
output, err := ioutil.ReadAll(stdout) |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
f2dc079c |
output2, err := ioutil.ReadAll(stderr) |
232dbb18 |
if err != nil {
t.Fatal(err)
}
if err := stdout.Close(); err != nil {
t.Fatal(err)
}
if err := stderr.Close(); err != nil {
t.Fatal(err)
} |
f2dc079c |
if string(output) != "hello\n" { |
15b30881 |
t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2) |
a27b4b8c |
} |
d949e280 |
}
func TestStart(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create( |
d949e280 |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
d949e280 |
Memory: 33554432, |
efd9becb |
CpuShares: 1000, |
d949e280 |
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
|
2a303dab |
cStdin, err := container.StdinPipe()
if err != nil {
t.Fatal(err)
}
|
d949e280 |
if err := container.Start(); err != nil {
t.Fatal(err)
}
// Give some time to the process to start
container.WaitTimeout(500 * time.Millisecond)
if !container.State.Running {
t.Errorf("Container should be running")
}
if err := container.Start(); err == nil {
t.Fatalf("A running containter should be able to be started")
} |
91b1f9ee |
// Try to avoid the timeoout in destroy. Best effort, don't check error
cStdin.Close() |
20085794 |
container.WaitTimeout(2 * time.Second) |
a27b4b8c |
}
func TestRun(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create( |
a27b4b8c |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ebb2491 |
Cmd: []string{"ls", "-al"}, |
a27b4b8c |
},
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
a27b4b8c |
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
if err := container.Run(); err != nil {
t.Fatal(err)
}
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
}
func TestOutput(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create( |
031f91df |
&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"echo", "-n", "foobar"}, |
031f91df |
}, |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
a27b4b8c |
output, err := container.Output()
if err != nil {
t.Fatal(err)
}
if string(output) != "foobar" {
t.Error(string(output))
}
}
|
bb22cd49 |
func TestKillDifferentUser(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
bb22cd49 |
Cmd: []string{"tail", "-f", "/etc/resolv.conf"},
User: "daemon",
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
if err := container.Start(); err != nil {
t.Fatal(err)
}
// Give some time to lxc to spawn the process (setuid might take some time)
container.WaitTimeout(500 * time.Millisecond)
if !container.State.Running {
t.Errorf("Container should be running")
}
if err := container.Kill(); err != nil {
t.Fatal(err)
}
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
container.Wait()
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
// Try stopping twice
if err := container.Kill(); err != nil {
t.Fatal(err)
}
}
|
24dac228 |
func TestKill(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"cat", "/dev/zero"}, |
031f91df |
}, |
a27b4b8c |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
a27b4b8c |
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
if err := container.Start(); err != nil {
t.Fatal(err)
} |
bb22cd49 |
// Give some time to lxc to spawn the process
container.WaitTimeout(500 * time.Millisecond)
|
a27b4b8c |
if !container.State.Running {
t.Errorf("Container should be running")
} |
24dac228 |
if err := container.Kill(); err != nil { |
a27b4b8c |
t.Fatal(err)
}
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
container.Wait()
if container.State.Running {
t.Errorf("Container shouldn't be running")
}
// Try stopping twice |
24dac228 |
if err := container.Kill(); err != nil { |
a27b4b8c |
t.Fatal(err)
}
}
func TestExitCode(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
a27b4b8c |
|
ff95f2b0 |
builder := NewBuilder(runtime)
trueContainer, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"/bin/true", ""}, |
232dbb18 |
}) |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(trueContainer) |
a27b4b8c |
if err := trueContainer.Run(); err != nil {
t.Fatal(err)
} |
232dbb18 |
if trueContainer.State.ExitCode != 0 {
t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
} |
a27b4b8c |
|
ff95f2b0 |
falseContainer, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"/bin/false", ""}, |
232dbb18 |
}) |
a27b4b8c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(falseContainer) |
a27b4b8c |
if err := falseContainer.Run(); err != nil {
t.Fatal(err)
}
if falseContainer.State.ExitCode != 1 { |
232dbb18 |
t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode) |
a27b4b8c |
}
} |
f958bdba |
|
f2c2d953 |
func TestRestart(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
f2c2d953 |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"echo", "-n", "foobar"}, |
031f91df |
}, |
f2c2d953 |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
f2c2d953 |
output, err := container.Output()
if err != nil {
t.Fatal(err)
}
if string(output) != "foobar" {
t.Error(string(output))
}
// Run the container again and check the output
output, err = container.Output()
if err != nil {
t.Fatal(err)
}
if string(output) != "foobar" {
t.Error(string(output))
}
}
|
0da9ccc1 |
func TestRestartStdin(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
0da9ccc1 |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"cat"}, |
031f91df |
OpenStdin: true,
}, |
0da9ccc1 |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
0da9ccc1 |
stdin, err := container.StdinPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
0da9ccc1 |
stdout, err := container.StdoutPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
0da9ccc1 |
if err := container.Start(); err != nil {
t.Fatal(err)
} |
232dbb18 |
if _, err := io.WriteString(stdin, "hello world"); err != nil {
t.Fatal(err)
}
if err := stdin.Close(); err != nil {
t.Fatal(err)
} |
0da9ccc1 |
container.Wait()
output, err := ioutil.ReadAll(stdout) |
232dbb18 |
if err != nil {
t.Fatal(err)
}
if err := stdout.Close(); err != nil {
t.Fatal(err)
} |
0da9ccc1 |
if string(output) != "hello world" { |
232dbb18 |
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output)) |
0da9ccc1 |
}
// Restart and try again
stdin, err = container.StdinPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
0da9ccc1 |
stdout, err = container.StdoutPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
0da9ccc1 |
if err := container.Start(); err != nil {
t.Fatal(err)
} |
232dbb18 |
if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
t.Fatal(err)
}
if err := stdin.Close(); err != nil {
t.Fatal(err)
} |
0da9ccc1 |
container.Wait()
output, err = ioutil.ReadAll(stdout) |
232dbb18 |
if err != nil {
t.Fatal(err)
}
if err := stdout.Close(); err != nil {
t.Fatal(err)
} |
0da9ccc1 |
if string(output) != "hello world #2" { |
232dbb18 |
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output)) |
0da9ccc1 |
}
}
|
6de3e8a2 |
func TestUser(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
6de3e8a2 |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
6de3e8a2 |
|
ff95f2b0 |
builder := NewBuilder(runtime)
|
6de3e8a2 |
// Default user must be root |
ff95f2b0 |
container, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"id"}, |
031f91df |
}, |
6de3e8a2 |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
6de3e8a2 |
output, err := container.Output()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
t.Error(string(output))
}
// Set a username |
ff95f2b0 |
container, err = builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"id"}, |
031f91df |
User: "root",
}, |
6de3e8a2 |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
6de3e8a2 |
output, err = container.Output() |
ac36c986 |
if err != nil || container.State.ExitCode != 0 { |
6de3e8a2 |
t.Fatal(err)
}
if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
t.Error(string(output))
}
// Set a UID |
ff95f2b0 |
container, err = builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"id"}, |
031f91df |
User: "0",
}, |
6de3e8a2 |
) |
ac36c986 |
if err != nil || container.State.ExitCode != 0 { |
6de3e8a2 |
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
6de3e8a2 |
output, err = container.Output() |
ac36c986 |
if err != nil || container.State.ExitCode != 0 { |
6de3e8a2 |
t.Fatal(err)
}
if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
t.Error(string(output))
}
// Set a different user by uid |
ff95f2b0 |
container, err = builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"id"}, |
031f91df |
User: "1",
}, |
6de3e8a2 |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
6de3e8a2 |
output, err = container.Output() |
2ebf3464 |
if err != nil { |
6de3e8a2 |
t.Fatal(err) |
2ebf3464 |
} else if container.State.ExitCode != 0 {
t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output) |
6de3e8a2 |
}
if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
t.Error(string(output))
}
// Set a different user by username |
ff95f2b0 |
container, err = builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"id"}, |
031f91df |
User: "daemon",
}, |
6de3e8a2 |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
6de3e8a2 |
output, err = container.Output() |
ac36c986 |
if err != nil || container.State.ExitCode != 0 { |
6de3e8a2 |
t.Fatal(err)
}
if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
t.Error(string(output))
}
}
|
f958bdba |
func TestMultipleContainers(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
f958bdba |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
f958bdba |
|
ff95f2b0 |
builder := NewBuilder(runtime)
container1, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"cat", "/dev/zero"}, |
031f91df |
}, |
f958bdba |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container1) |
f958bdba |
|
ff95f2b0 |
container2, err := builder.Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"cat", "/dev/zero"}, |
031f91df |
}, |
f958bdba |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container2) |
f958bdba |
// Start both containers
if err := container1.Start(); err != nil {
t.Fatal(err)
}
if err := container2.Start(); err != nil {
t.Fatal(err)
}
|
bb22cd49 |
// Make sure they are running before trying to kill them
container1.WaitTimeout(250 * time.Millisecond)
container2.WaitTimeout(250 * time.Millisecond)
|
f958bdba |
// If we are here, both containers should be running
if !container1.State.Running {
t.Fatal("Container not running")
}
if !container2.State.Running {
t.Fatal("Container not running")
}
// Kill them
if err := container1.Kill(); err != nil {
t.Fatal(err)
}
if err := container2.Kill(); err != nil {
t.Fatal(err)
}
} |
a2d7dd1a |
|
ca40989e |
func TestStdin(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
ca40989e |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"cat"}, |
031f91df |
OpenStdin: true,
}, |
ca40989e |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
ca40989e |
stdin, err := container.StdinPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
ca40989e |
stdout, err := container.StdoutPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
}
if err := container.Start(); err != nil {
t.Fatal(err)
} |
ca40989e |
defer stdin.Close()
defer stdout.Close() |
232dbb18 |
if _, err := io.WriteString(stdin, "hello world"); err != nil {
t.Fatal(err)
}
if err := stdin.Close(); err != nil { |
ca40989e |
t.Fatal(err)
}
container.Wait()
output, err := ioutil.ReadAll(stdout) |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
ca40989e |
if string(output) != "hello world" { |
232dbb18 |
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output)) |
ca40989e |
}
}
func TestTty(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
ca40989e |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"cat"}, |
031f91df |
OpenStdin: true,
}, |
ca40989e |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
ca40989e |
stdin, err := container.StdinPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
ca40989e |
stdout, err := container.StdoutPipe() |
232dbb18 |
if err != nil {
t.Fatal(err)
}
if err := container.Start(); err != nil {
t.Fatal(err)
} |
ca40989e |
defer stdin.Close()
defer stdout.Close() |
232dbb18 |
if _, err := io.WriteString(stdin, "hello world"); err != nil {
t.Fatal(err)
}
if err := stdin.Close(); err != nil { |
ca40989e |
t.Fatal(err)
}
container.Wait()
output, err := ioutil.ReadAll(stdout) |
232dbb18 |
if err != nil {
t.Fatal(err)
} |
ca40989e |
if string(output) != "hello world" { |
232dbb18 |
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output)) |
ca40989e |
}
}
|
fb350e0c |
func TestEnv(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
fb350e0c |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"/usr/bin/env"}, |
031f91df |
}, |
fb350e0c |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
232dbb18 |
|
fb350e0c |
stdout, err := container.StdoutPipe()
if err != nil {
t.Fatal(err)
}
defer stdout.Close()
if err := container.Start(); err != nil {
t.Fatal(err)
}
container.Wait()
output, err := ioutil.ReadAll(stdout)
if err != nil {
t.Fatal(err)
}
actualEnv := strings.Split(string(output), "\n")
if actualEnv[len(actualEnv)-1] == "" {
actualEnv = actualEnv[:len(actualEnv)-1]
}
sort.Strings(actualEnv)
goodEnv := []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"HOME=/",
}
sort.Strings(goodEnv)
if len(goodEnv) != len(actualEnv) {
t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
}
for i := range goodEnv {
if actualEnv[i] != goodEnv[i] {
t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
}
}
}
|
4e5ae883 |
func grepFile(t *testing.T, path string, pattern string) {
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
r := bufio.NewReader(f)
var (
line string
)
err = nil
for err == nil {
line, err = r.ReadString('\n')
if strings.Contains(line, pattern) == true {
return
}
}
t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
}
func TestLXCConfig(t *testing.T) { |
b8547f31 |
runtime, err := newTestRuntime() |
4e5ae883 |
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
94896183 |
// Memory is allocated randomly for testing |
4e5ae883 |
rand.Seed(time.Now().UTC().UnixNano()) |
94896183 |
memMin := 33554432
memMax := 536870912
mem := memMin + rand.Intn(memMax-memMin) |
efd9becb |
// CPU shares as well
cpuMin := 100
cpuMax := 10000
cpu := cpuMin + rand.Intn(cpuMax-cpuMin) |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"/bin/true"}, |
031f91df |
|
efd9becb |
Hostname: "foobar",
Memory: int64(mem),
CpuShares: int64(cpu), |
031f91df |
}, |
4e5ae883 |
)
if err != nil {
t.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
4e5ae883 |
container.generateLXCConfig() |
7c57a4cf |
grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
grepFile(t, container.lxcConfigPath(), |
94896183 |
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem)) |
7c57a4cf |
grepFile(t, container.lxcConfigPath(), |
94896183 |
fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2)) |
4e5ae883 |
}
|
a2d7dd1a |
func BenchmarkRunSequencial(b *testing.B) { |
b8547f31 |
runtime, err := newTestRuntime() |
a2d7dd1a |
if err != nil {
b.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
a2d7dd1a |
for i := 0; i < b.N; i++ { |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"echo", "-n", "foo"}, |
031f91df |
}, |
a2d7dd1a |
)
if err != nil {
b.Fatal(err)
} |
b8547f31 |
defer runtime.Destroy(container) |
a2d7dd1a |
output, err := container.Output()
if err != nil {
b.Fatal(err)
}
if string(output) != "foo" { |
15b30881 |
b.Fatalf("Unexpected output: %s", output) |
a2d7dd1a |
} |
b8547f31 |
if err := runtime.Destroy(container); err != nil { |
a2d7dd1a |
b.Fatal(err)
}
}
}
func BenchmarkRunParallel(b *testing.B) { |
b8547f31 |
runtime, err := newTestRuntime() |
a2d7dd1a |
if err != nil {
b.Fatal(err)
} |
b8547f31 |
defer nuke(runtime) |
a2d7dd1a |
var tasks []chan error
for i := 0; i < b.N; i++ {
complete := make(chan error)
tasks = append(tasks, complete)
go func(i int, complete chan error) { |
ff95f2b0 |
container, err := NewBuilder(runtime).Create(&Config{ |
fd224ee5 |
Image: GetTestImage(runtime).ID, |
6ce64e84 |
Cmd: []string{"echo", "-n", "foo"}, |
031f91df |
}, |
a2d7dd1a |
)
if err != nil {
complete <- err
return
} |
b8547f31 |
defer runtime.Destroy(container) |
a2d7dd1a |
if err := container.Start(); err != nil {
complete <- err
return
}
if err := container.WaitTimeout(15 * time.Second); err != nil {
complete <- err
return
}
// if string(output) != "foo" {
// complete <- fmt.Errorf("Unexecpted output: %v", string(output))
// } |
b8547f31 |
if err := runtime.Destroy(container); err != nil { |
a2d7dd1a |
complete <- err
return
}
complete <- nil
}(i, complete)
}
var errors []error
for _, task := range tasks {
err := <-task
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
b.Fatal(errors)
}
} |