github.com/xuyutom/docker@v1.6.0/integration/container_test.go (about) 1 package docker 2 3 import ( 4 "io" 5 "io/ioutil" 6 "testing" 7 "time" 8 9 "github.com/docker/docker/runconfig" 10 ) 11 12 func TestRestartStdin(t *testing.T) { 13 daemon := mkDaemon(t) 14 defer nuke(daemon) 15 container, _, err := daemon.Create(&runconfig.Config{ 16 Image: GetTestImage(daemon).ID, 17 Cmd: []string{"cat"}, 18 19 OpenStdin: true, 20 }, 21 &runconfig.HostConfig{}, 22 "", 23 ) 24 if err != nil { 25 t.Fatal(err) 26 } 27 defer daemon.Rm(container) 28 29 stdin := container.StdinPipe() 30 stdout := container.StdoutPipe() 31 if err := container.Start(); err != nil { 32 t.Fatal(err) 33 } 34 if _, err := io.WriteString(stdin, "hello world"); err != nil { 35 t.Fatal(err) 36 } 37 if err := stdin.Close(); err != nil { 38 t.Fatal(err) 39 } 40 container.WaitStop(-1 * time.Second) 41 output, err := ioutil.ReadAll(stdout) 42 if err != nil { 43 t.Fatal(err) 44 } 45 if err := stdout.Close(); err != nil { 46 t.Fatal(err) 47 } 48 if string(output) != "hello world" { 49 t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output)) 50 } 51 52 // Restart and try again 53 stdin = container.StdinPipe() 54 stdout = container.StdoutPipe() 55 if err := container.Start(); err != nil { 56 t.Fatal(err) 57 } 58 if _, err := io.WriteString(stdin, "hello world #2"); err != nil { 59 t.Fatal(err) 60 } 61 if err := stdin.Close(); err != nil { 62 t.Fatal(err) 63 } 64 container.WaitStop(-1 * time.Second) 65 output, err = ioutil.ReadAll(stdout) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if err := stdout.Close(); err != nil { 70 t.Fatal(err) 71 } 72 if string(output) != "hello world #2" { 73 t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output)) 74 } 75 } 76 77 func TestStdin(t *testing.T) { 78 daemon := mkDaemon(t) 79 defer nuke(daemon) 80 container, _, err := daemon.Create(&runconfig.Config{ 81 Image: GetTestImage(daemon).ID, 82 Cmd: []string{"cat"}, 83 84 OpenStdin: true, 85 }, 86 &runconfig.HostConfig{}, 87 "", 88 ) 89 if err != nil { 90 t.Fatal(err) 91 } 92 defer daemon.Rm(container) 93 94 stdin := container.StdinPipe() 95 stdout := container.StdoutPipe() 96 if err := container.Start(); err != nil { 97 t.Fatal(err) 98 } 99 defer stdin.Close() 100 defer stdout.Close() 101 if _, err := io.WriteString(stdin, "hello world"); err != nil { 102 t.Fatal(err) 103 } 104 if err := stdin.Close(); err != nil { 105 t.Fatal(err) 106 } 107 container.WaitStop(-1 * time.Second) 108 output, err := ioutil.ReadAll(stdout) 109 if err != nil { 110 t.Fatal(err) 111 } 112 if string(output) != "hello world" { 113 t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output)) 114 } 115 } 116 117 func TestTty(t *testing.T) { 118 daemon := mkDaemon(t) 119 defer nuke(daemon) 120 container, _, err := daemon.Create(&runconfig.Config{ 121 Image: GetTestImage(daemon).ID, 122 Cmd: []string{"cat"}, 123 124 OpenStdin: true, 125 }, 126 &runconfig.HostConfig{}, 127 "", 128 ) 129 if err != nil { 130 t.Fatal(err) 131 } 132 defer daemon.Rm(container) 133 134 stdin := container.StdinPipe() 135 stdout := container.StdoutPipe() 136 if err := container.Start(); err != nil { 137 t.Fatal(err) 138 } 139 defer stdin.Close() 140 defer stdout.Close() 141 if _, err := io.WriteString(stdin, "hello world"); err != nil { 142 t.Fatal(err) 143 } 144 if err := stdin.Close(); err != nil { 145 t.Fatal(err) 146 } 147 container.WaitStop(-1 * time.Second) 148 output, err := ioutil.ReadAll(stdout) 149 if err != nil { 150 t.Fatal(err) 151 } 152 if string(output) != "hello world" { 153 t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output)) 154 } 155 } 156 157 func BenchmarkRunSequential(b *testing.B) { 158 daemon := mkDaemon(b) 159 defer nuke(daemon) 160 for i := 0; i < b.N; i++ { 161 container, _, err := daemon.Create(&runconfig.Config{ 162 Image: GetTestImage(daemon).ID, 163 Cmd: []string{"echo", "-n", "foo"}, 164 }, 165 &runconfig.HostConfig{}, 166 "", 167 ) 168 if err != nil { 169 b.Fatal(err) 170 } 171 defer daemon.Rm(container) 172 output, err := container.Output() 173 if err != nil { 174 b.Fatal(err) 175 } 176 if string(output) != "foo" { 177 b.Fatalf("Unexpected output: %s", output) 178 } 179 if err := daemon.Rm(container); err != nil { 180 b.Fatal(err) 181 } 182 } 183 } 184 185 func BenchmarkRunParallel(b *testing.B) { 186 daemon := mkDaemon(b) 187 defer nuke(daemon) 188 189 var tasks []chan error 190 191 for i := 0; i < b.N; i++ { 192 complete := make(chan error) 193 tasks = append(tasks, complete) 194 go func(i int, complete chan error) { 195 container, _, err := daemon.Create(&runconfig.Config{ 196 Image: GetTestImage(daemon).ID, 197 Cmd: []string{"echo", "-n", "foo"}, 198 }, 199 &runconfig.HostConfig{}, 200 "", 201 ) 202 if err != nil { 203 complete <- err 204 return 205 } 206 defer daemon.Rm(container) 207 if err := container.Start(); err != nil { 208 complete <- err 209 return 210 } 211 if _, err := container.WaitStop(15 * time.Second); err != nil { 212 complete <- err 213 return 214 } 215 // if string(output) != "foo" { 216 // complete <- fmt.Errorf("Unexecpted output: %v", string(output)) 217 // } 218 if err := daemon.Rm(container); err != nil { 219 complete <- err 220 return 221 } 222 complete <- nil 223 }(i, complete) 224 } 225 var errors []error 226 for _, task := range tasks { 227 err := <-task 228 if err != nil { 229 errors = append(errors, err) 230 } 231 } 232 if len(errors) > 0 { 233 b.Fatal(errors) 234 } 235 }