2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-01-29 18:16:45 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-01-18 19:13:39 -05:00
|
|
|
"testing"
|
2013-01-28 14:51:51 -05:00
|
|
|
"time"
|
2014-06-06 07:30:04 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2013-02-13 22:05:57 -05:00
|
|
|
func TestRestartStdin(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(t)
|
|
|
|
defer nuke(daemon)
|
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-03-23 15:39:09 -04:00
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-02-13 22:05:57 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-17 17:43:01 -04:00
|
|
|
defer daemon.Destroy(container)
|
2013-02-13 22:05:57 -05:00
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
stdin := container.StdinPipe()
|
|
|
|
stdout := container.StdoutPipe()
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-02-13 22:05:57 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-29 10:44:58 -04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-02-13 22:05:57 -05:00
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdout.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-02-13 22:05:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restart and try again
|
2014-12-04 16:12:29 -05:00
|
|
|
stdin = container.StdinPipe()
|
|
|
|
stdout = container.StdoutPipe()
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-02-13 22:05:57 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-29 10:44:58 -04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-02-13 22:05:57 -05:00
|
|
|
output, err = ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdout.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
if string(output) != "hello world #2" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
|
2013-02-13 22:05:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 18:16:45 -05:00
|
|
|
func TestStdin(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(t)
|
|
|
|
defer nuke(daemon)
|
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-03-23 15:39:09 -04:00
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-01-29 18:16:45 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-17 17:43:01 -04:00
|
|
|
defer daemon.Destroy(container)
|
2013-01-29 18:16:45 -05:00
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
stdin := container.StdinPipe()
|
|
|
|
stdout := container.StdoutPipe()
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
2013-03-29 10:44:58 -04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
2013-01-29 18:16:45 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-01-29 18:16:45 -05:00
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-01-29 18:16:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTty(t *testing.T) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(t)
|
|
|
|
defer nuke(daemon)
|
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-03-23 15:39:09 -04:00
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-01-29 18:16:45 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-17 17:43:01 -04:00
|
|
|
defer daemon.Destroy(container)
|
2013-01-29 18:16:45 -05:00
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
stdin := container.StdinPipe()
|
|
|
|
stdout := container.StdoutPipe()
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
2013-03-29 10:44:58 -04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
2013-01-29 18:16:45 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-01-29 18:16:45 -05:00
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-01-29 18:16:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 11:34:49 -04:00
|
|
|
func BenchmarkRunSequential(b *testing.B) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(b)
|
|
|
|
defer nuke(daemon)
|
2013-01-28 14:51:51 -05:00
|
|
|
for i := 0; i < b.N; i++ {
|
2014-04-17 17:43:01 -04:00
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-03-23 15:39:09 -04:00
|
|
|
Cmd: []string{"echo", "-n", "foo"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-01-28 14:51:51 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2014-04-17 17:43:01 -04:00
|
|
|
defer daemon.Destroy(container)
|
2013-01-28 14:51:51 -05:00
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "foo" {
|
2013-03-30 03:22:56 -04:00
|
|
|
b.Fatalf("Unexpected output: %s", output)
|
2013-01-28 14:51:51 -05:00
|
|
|
}
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.Destroy(container); err != nil {
|
2013-01-28 14:51:51 -05:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRunParallel(b *testing.B) {
|
2014-04-17 17:43:01 -04:00
|
|
|
daemon := mkDaemon(b)
|
|
|
|
defer nuke(daemon)
|
2013-01-28 14:51:51 -05:00
|
|
|
|
|
|
|
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) {
|
2014-04-17 17:43:01 -04:00
|
|
|
container, _, err := daemon.Create(&runconfig.Config{
|
|
|
|
Image: GetTestImage(daemon).ID,
|
2013-03-23 15:39:09 -04:00
|
|
|
Cmd: []string{"echo", "-n", "foo"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2014-09-25 17:23:59 -04:00
|
|
|
&runconfig.HostConfig{},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-01-28 14:51:51 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
complete <- err
|
|
|
|
return
|
|
|
|
}
|
2014-04-17 17:43:01 -04:00
|
|
|
defer daemon.Destroy(container)
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-01-28 14:51:51 -05:00
|
|
|
complete <- err
|
|
|
|
return
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
if _, err := container.WaitStop(15 * time.Second); err != nil {
|
2013-01-28 14:51:51 -05:00
|
|
|
complete <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if string(output) != "foo" {
|
|
|
|
// complete <- fmt.Errorf("Unexecpted output: %v", string(output))
|
|
|
|
// }
|
2014-04-17 17:43:01 -04:00
|
|
|
if err := daemon.Destroy(container); err != nil {
|
2013-01-28 14:51:51 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|