123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- package docker
- import (
- "bufio"
- "fmt"
- "github.com/dotcloud/docker/runconfig"
- "github.com/dotcloud/docker/utils"
- "io"
- "io/ioutil"
- "os"
- "path"
- "regexp"
- "sort"
- "strings"
- "testing"
- "time"
- )
- func TestCpuShares(t *testing.T) {
- _, err1 := os.Stat("/sys/fs/cgroup/cpuacct,cpu")
- _, err2 := os.Stat("/sys/fs/cgroup/cpu,cpuacct")
- if err1 == nil || err2 == nil {
- t.Skip("Fixme. Setting cpu cgroup shares doesn't work in dind on a Fedora host. The lxc utils are confused by the cpu,cpuacct mount.")
- }
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, _ := mkContainer(daemon, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
- defer daemon.Destroy(container)
- cStdin, err := container.StdinPipe()
- if err != nil {
- t.Fatal(err)
- }
- 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.IsRunning() {
- t.Errorf("Container should be running")
- }
- if err := container.Start(); err != nil {
- t.Fatalf("A running container should be able to be started")
- }
- // Try to avoid the timeout in destroy. Best effort, don't check error
- cStdin.Close()
- container.WaitTimeout(2 * time.Second)
- }
- func TestKillDifferentUser(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"cat"},
- OpenStdin: true,
- User: "daemon",
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.Destroy(container)
- // FIXME @shykes: this seems redundant, but is very old, I'm leaving it in case
- // there is a side effect I'm not seeing.
- // defer container.stdin.Close()
- if container.State.IsRunning() {
- t.Errorf("Container shouldn't be running")
- }
- if err := container.Start(); err != nil {
- t.Fatal(err)
- }
- setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
- for !container.State.IsRunning() {
- time.Sleep(10 * time.Millisecond)
- }
- })
- setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
- out, _ := container.StdoutPipe()
- in, _ := container.StdinPipe()
- if err := assertPipe("hello\n", "hello", out, in, 150); err != nil {
- t.Fatal(err)
- }
- })
- if err := container.Kill(); err != nil {
- t.Fatal(err)
- }
- if container.State.IsRunning() {
- t.Errorf("Container shouldn't be running")
- }
- container.Wait()
- if container.State.IsRunning() {
- t.Errorf("Container shouldn't be running")
- }
- // Try stopping twice
- if err := container.Kill(); err != nil {
- t.Fatal(err)
- }
- }
- func TestRestart(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"echo", "-n", "foobar"},
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.Destroy(container)
- 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))
- }
- }
- func TestRestartStdin(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"cat"},
- OpenStdin: true,
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.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)
- }
- 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 err := stdout.Close(); err != nil {
- t.Fatal(err)
- }
- if string(output) != "hello world" {
- 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)
- }
- 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)
- if err != nil {
- t.Fatal(err)
- }
- if err := stdout.Close(); err != nil {
- t.Fatal(err)
- }
- if string(output) != "hello world #2" {
- t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
- }
- }
- func TestStdin(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"cat"},
- OpenStdin: true,
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.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)
- }
- 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.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
- }
- }
- func TestTty(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"cat"},
- OpenStdin: true,
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.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)
- }
- 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.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
- }
- }
- func TestEntrypoint(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(
- &runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Entrypoint: []string{"/bin/echo"},
- Cmd: []string{"-n", "foobar"},
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.Destroy(container)
- output, err := container.Output()
- if err != nil {
- t.Fatal(err)
- }
- if string(output) != "foobar" {
- t.Error(string(output))
- }
- }
- func TestEntrypointNoCmd(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(
- &runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Entrypoint: []string{"/bin/echo", "foobar"},
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.Destroy(container)
- output, err := container.Output()
- if err != nil {
- t.Fatal(err)
- }
- if strings.Trim(string(output), "\r\n") != "foobar" {
- t.Error(string(output))
- }
- }
- func BenchmarkRunSequential(b *testing.B) {
- daemon := mkDaemon(b)
- defer nuke(daemon)
- for i := 0; i < b.N; i++ {
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"echo", "-n", "foo"},
- },
- "",
- )
- if err != nil {
- b.Fatal(err)
- }
- defer daemon.Destroy(container)
- output, err := container.Output()
- if err != nil {
- b.Fatal(err)
- }
- if string(output) != "foo" {
- b.Fatalf("Unexpected output: %s", output)
- }
- if err := daemon.Destroy(container); err != nil {
- b.Fatal(err)
- }
- }
- }
- func BenchmarkRunParallel(b *testing.B) {
- daemon := mkDaemon(b)
- defer nuke(daemon)
- 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) {
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"echo", "-n", "foo"},
- },
- "",
- )
- if err != nil {
- complete <- err
- return
- }
- defer daemon.Destroy(container)
- 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))
- // }
- if err := daemon.Destroy(container); err != nil {
- 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)
- }
- }
- func tempDir(t *testing.T) string {
- tmpDir, err := ioutil.TempDir("", "docker-test-container")
- if err != nil {
- t.Fatal(err)
- }
- return tmpDir
- }
- // Test for #1737
- func TestCopyVolumeUidGid(t *testing.T) {
- eng := NewTestEngine(t)
- r := mkDaemonFromEngine(eng, t)
- defer r.Nuke()
- // Add directory not owned by root
- container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
- defer r.Destroy(container1)
- if container1.State.IsRunning() {
- t.Errorf("Container shouldn't be running")
- }
- if err := container1.Run(); err != nil {
- t.Fatal(err)
- }
- if container1.State.IsRunning() {
- t.Errorf("Container shouldn't be running")
- }
- img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
- if err != nil {
- t.Error(err)
- }
- // Test that the uid and gid is copied from the image to the volume
- tmpDir1 := tempDir(t)
- defer os.RemoveAll(tmpDir1)
- stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
- if !strings.Contains(stdout1, "daemon daemon") {
- t.Fatal("Container failed to transfer uid and gid to volume")
- }
- }
- // Test for #1582
- func TestCopyVolumeContent(t *testing.T) {
- eng := NewTestEngine(t)
- r := mkDaemonFromEngine(eng, t)
- defer r.Nuke()
- // Put some content in a directory of a container and commit it
- container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
- defer r.Destroy(container1)
- if container1.State.IsRunning() {
- t.Errorf("Container shouldn't be running")
- }
- if err := container1.Run(); err != nil {
- t.Fatal(err)
- }
- if container1.State.IsRunning() {
- t.Errorf("Container shouldn't be running")
- }
- img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
- if err != nil {
- t.Error(err)
- }
- // Test that the content is copied from the image to the volume
- tmpDir1 := tempDir(t)
- defer os.RemoveAll(tmpDir1)
- stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
- if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
- t.Fatal("Container failed to transfer content to volume")
- }
- }
- func TestBindMounts(t *testing.T) {
- eng := NewTestEngine(t)
- r := mkDaemonFromEngine(eng, t)
- defer r.Nuke()
- tmpDir := tempDir(t)
- defer os.RemoveAll(tmpDir)
- writeFile(path.Join(tmpDir, "touch-me"), "", t)
- // Test reading from a read-only bind mount
- stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
- if !strings.Contains(stdout, "touch-me") {
- t.Fatal("Container failed to read from bind mount")
- }
- // test writing to bind mount
- runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
- readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
- // test mounting to an illegal destination directory
- if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
- t.Fatal("Container bind mounted illegal directory")
- }
- // test mount a file
- runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
- content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
- if content != "yotta" {
- t.Fatal("Container failed to write to bind mount file")
- }
- }
- // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
- func TestRestartWithVolumes(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(&runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"echo", "-n", "foobar"},
- Volumes: map[string]struct{}{"/test": {}},
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.Destroy(container)
- for key := range container.Config.Volumes {
- if key != "/test" {
- t.Fail()
- }
- }
- _, err = container.Output()
- if err != nil {
- t.Fatal(err)
- }
- expected := container.Volumes["/test"]
- if expected == "" {
- t.Fail()
- }
- // Run the container again to verify the volume path persists
- _, err = container.Output()
- if err != nil {
- t.Fatal(err)
- }
- actual := container.Volumes["/test"]
- if expected != actual {
- t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
- }
- }
- func TestContainerNetwork(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(
- &runconfig.Config{
- Image: GetTestImage(daemon).ID,
- // If I change this to ping 8.8.8.8 it fails. Any idea why? - timthelion
- Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.Destroy(container)
- if err := container.Run(); err != nil {
- t.Fatal(err)
- }
- if code := container.State.GetExitCode(); code != 0 {
- t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
- }
- }
- // Issue #4681
- func TestLoopbackFunctionsWhenNetworkingIsDissabled(t *testing.T) {
- daemon := mkDaemon(t)
- defer nuke(daemon)
- container, _, err := daemon.Create(
- &runconfig.Config{
- Image: GetTestImage(daemon).ID,
- Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
- NetworkDisabled: true,
- },
- "",
- )
- if err != nil {
- t.Fatal(err)
- }
- defer daemon.Destroy(container)
- if err := container.Run(); err != nil {
- t.Fatal(err)
- }
- if code := container.State.GetExitCode(); code != 0 {
- t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
- }
- }
- func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
- eng := NewTestEngine(t)
- daemon := mkDaemonFromEngine(eng, t)
- defer nuke(daemon)
- config, hc, _, err := runconfig.Parse([]string{"-n=false", GetTestImage(daemon).ID, "ip", "addr", "show", "up"}, nil)
- if err != nil {
- t.Fatal(err)
- }
- jobCreate := eng.Job("create")
- if err := jobCreate.ImportEnv(config); err != nil {
- t.Fatal(err)
- }
- var id string
- jobCreate.Stdout.AddString(&id)
- if err := jobCreate.Run(); err != nil {
- t.Fatal(err)
- }
- // FIXME: this hack can be removed once Wait is a job
- c := daemon.Get(id)
- if c == nil {
- t.Fatalf("Couldn't retrieve container %s from daemon", id)
- }
- stdout, err := c.StdoutPipe()
- if err != nil {
- t.Fatal(err)
- }
- jobStart := eng.Job("start", id)
- if err := jobStart.ImportEnv(hc); err != nil {
- t.Fatal(err)
- }
- if err := jobStart.Run(); err != nil {
- t.Fatal(err)
- }
- c.WaitTimeout(500 * time.Millisecond)
- c.Wait()
- output, err := ioutil.ReadAll(stdout)
- if err != nil {
- t.Fatal(err)
- }
- interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
- if len(interfaces) != 1 {
- t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
- }
- if !strings.HasSuffix(interfaces[0], ": lo") {
- t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
- }
- }
- func TestPrivilegedCanMknod(t *testing.T) {
- eng := NewTestEngine(t)
- daemon := mkDaemonFromEngine(eng, t)
- defer daemon.Nuke()
- if output, err := runContainer(eng, daemon, []string{"--privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
- t.Fatalf("Could not mknod into privileged container %s %v", output, err)
- }
- }
- func TestPrivilegedCanMount(t *testing.T) {
- eng := NewTestEngine(t)
- daemon := mkDaemonFromEngine(eng, t)
- defer daemon.Nuke()
- if output, _ := runContainer(eng, daemon, []string{"--privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
- t.Fatal("Could not mount into privileged container")
- }
- }
- func TestUnprivilegedCanMknod(t *testing.T) {
- eng := NewTestEngine(t)
- daemon := mkDaemonFromEngine(eng, t)
- defer daemon.Nuke()
- if output, _ := runContainer(eng, daemon, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
- t.Fatal("Couldn't mknod into secure container")
- }
- }
- func TestUnprivilegedCannotMount(t *testing.T) {
- eng := NewTestEngine(t)
- daemon := mkDaemonFromEngine(eng, t)
- defer daemon.Nuke()
- if output, _ := runContainer(eng, daemon, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
- t.Fatal("Could mount into secure container")
- }
- }
|