api_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. package docker
  2. import (
  3. "archive/tar"
  4. "bufio"
  5. "bytes"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/dotcloud/docker/utils"
  9. "io"
  10. "net"
  11. "net/http"
  12. "net/http/httptest"
  13. "os"
  14. "path"
  15. "strings"
  16. "testing"
  17. "time"
  18. )
  19. func TestGetBoolParam(t *testing.T) {
  20. if ret, err := getBoolParam("true"); err != nil || !ret {
  21. t.Fatalf("true -> true, nil | got %t %s", ret, err)
  22. }
  23. if ret, err := getBoolParam("True"); err != nil || !ret {
  24. t.Fatalf("True -> true, nil | got %t %s", ret, err)
  25. }
  26. if ret, err := getBoolParam("1"); err != nil || !ret {
  27. t.Fatalf("1 -> true, nil | got %t %s", ret, err)
  28. }
  29. if ret, err := getBoolParam(""); err != nil || ret {
  30. t.Fatalf("\"\" -> false, nil | got %t %s", ret, err)
  31. }
  32. if ret, err := getBoolParam("false"); err != nil || ret {
  33. t.Fatalf("false -> false, nil | got %t %s", ret, err)
  34. }
  35. if ret, err := getBoolParam("0"); err != nil || ret {
  36. t.Fatalf("0 -> false, nil | got %t %s", ret, err)
  37. }
  38. if ret, err := getBoolParam("faux"); err == nil || ret {
  39. t.Fatalf("faux -> false, err | got %t %s", ret, err)
  40. }
  41. }
  42. func TesthttpError(t *testing.T) {
  43. r := httptest.NewRecorder()
  44. httpError(r, fmt.Errorf("No such method"))
  45. if r.Code != http.StatusNotFound {
  46. t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
  47. }
  48. httpError(r, fmt.Errorf("This accound hasn't been activated"))
  49. if r.Code != http.StatusForbidden {
  50. t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
  51. }
  52. httpError(r, fmt.Errorf("Some error"))
  53. if r.Code != http.StatusInternalServerError {
  54. t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
  55. }
  56. }
  57. func TestGetVersion(t *testing.T) {
  58. var err error
  59. runtime := mkRuntime(t)
  60. defer nuke(runtime)
  61. srv := &Server{runtime: runtime}
  62. r := httptest.NewRecorder()
  63. if err := getVersion(srv, APIVERSION, r, nil, nil); err != nil {
  64. t.Fatal(err)
  65. }
  66. v := &APIVersion{}
  67. if err = json.Unmarshal(r.Body.Bytes(), v); err != nil {
  68. t.Fatal(err)
  69. }
  70. if v.Version != VERSION {
  71. t.Errorf("Expected version %s, %s found", VERSION, v.Version)
  72. }
  73. }
  74. func TestGetInfo(t *testing.T) {
  75. runtime := mkRuntime(t)
  76. defer nuke(runtime)
  77. srv := &Server{runtime: runtime}
  78. initialImages, err := srv.runtime.graph.Map()
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. r := httptest.NewRecorder()
  83. if err := getInfo(srv, APIVERSION, r, nil, nil); err != nil {
  84. t.Fatal(err)
  85. }
  86. infos := &APIInfo{}
  87. err = json.Unmarshal(r.Body.Bytes(), infos)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if infos.Images != len(initialImages) {
  92. t.Errorf("Expected images: %d, %d found", len(initialImages), infos.Images)
  93. }
  94. }
  95. func TestGetEvents(t *testing.T) {
  96. runtime := mkRuntime(t)
  97. defer nuke(runtime)
  98. srv := &Server{
  99. runtime: runtime,
  100. events: make([]utils.JSONMessage, 0, 64),
  101. listeners: make(map[string]chan utils.JSONMessage),
  102. }
  103. srv.LogEvent("fakeaction", "fakeid", "fakeimage")
  104. srv.LogEvent("fakeaction2", "fakeid", "fakeimage")
  105. req, err := http.NewRequest("GET", "/events?since=1", nil)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. r := httptest.NewRecorder()
  110. setTimeout(t, "", 500*time.Millisecond, func() {
  111. if err := getEvents(srv, APIVERSION, r, req, nil); err != nil {
  112. t.Fatal(err)
  113. }
  114. })
  115. dec := json.NewDecoder(r.Body)
  116. for i := 0; i < 2; i++ {
  117. var jm utils.JSONMessage
  118. if err := dec.Decode(&jm); err == io.EOF {
  119. break
  120. } else if err != nil {
  121. t.Fatal(err)
  122. }
  123. if jm != srv.events[i] {
  124. t.Fatalf("Event received it different than expected")
  125. }
  126. }
  127. }
  128. func TestGetImagesJSON(t *testing.T) {
  129. runtime := mkRuntime(t)
  130. defer nuke(runtime)
  131. srv := &Server{runtime: runtime}
  132. // all=0
  133. initialImages, err := srv.Images(false, "")
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. req, err := http.NewRequest("GET", "/images/json?all=0", nil)
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. r := httptest.NewRecorder()
  142. if err := getImagesJSON(srv, APIVERSION, r, req, nil); err != nil {
  143. t.Fatal(err)
  144. }
  145. images := []APIImages{}
  146. if err := json.Unmarshal(r.Body.Bytes(), &images); err != nil {
  147. t.Fatal(err)
  148. }
  149. if len(images) != len(initialImages) {
  150. t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
  151. }
  152. found := false
  153. for _, img := range images {
  154. if img.Repository == unitTestImageName {
  155. found = true
  156. break
  157. }
  158. }
  159. if !found {
  160. t.Errorf("Expected image %s, %+v found", unitTestImageName, images)
  161. }
  162. r2 := httptest.NewRecorder()
  163. // all=1
  164. initialImages, err = srv.Images(true, "")
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. req2, err := http.NewRequest("GET", "/images/json?all=true", nil)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. if err := getImagesJSON(srv, APIVERSION, r2, req2, nil); err != nil {
  173. t.Fatal(err)
  174. }
  175. images2 := []APIImages{}
  176. if err := json.Unmarshal(r2.Body.Bytes(), &images2); err != nil {
  177. t.Fatal(err)
  178. }
  179. if len(images2) != len(initialImages) {
  180. t.Errorf("Expected %d image, %d found", len(initialImages), len(images2))
  181. }
  182. found = false
  183. for _, img := range images2 {
  184. if img.ID == GetTestImage(runtime).ID {
  185. found = true
  186. break
  187. }
  188. }
  189. if !found {
  190. t.Errorf("Retrieved image Id differs, expected %s, received %+v", GetTestImage(runtime).ID, images2)
  191. }
  192. r3 := httptest.NewRecorder()
  193. // filter=a
  194. req3, err := http.NewRequest("GET", "/images/json?filter=aaaaaaaaaa", nil)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. if err := getImagesJSON(srv, APIVERSION, r3, req3, nil); err != nil {
  199. t.Fatal(err)
  200. }
  201. images3 := []APIImages{}
  202. if err := json.Unmarshal(r3.Body.Bytes(), &images3); err != nil {
  203. t.Fatal(err)
  204. }
  205. if len(images3) != 0 {
  206. t.Errorf("Expected 0 image, %d found", len(images3))
  207. }
  208. r4 := httptest.NewRecorder()
  209. // all=foobar
  210. req4, err := http.NewRequest("GET", "/images/json?all=foobar", nil)
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. err = getImagesJSON(srv, APIVERSION, r4, req4, nil)
  215. if err == nil {
  216. t.Fatalf("Error expected, received none")
  217. }
  218. if !strings.HasPrefix(err.Error(), "Bad parameter") {
  219. t.Fatalf("Error should starts with \"Bad parameter\"")
  220. }
  221. http.Error(r4, err.Error(), http.StatusBadRequest)
  222. if r4.Code != http.StatusBadRequest {
  223. t.Fatalf("%d Bad Request expected, received %d\n", http.StatusBadRequest, r4.Code)
  224. }
  225. }
  226. func TestGetImagesViz(t *testing.T) {
  227. runtime := mkRuntime(t)
  228. defer nuke(runtime)
  229. srv := &Server{runtime: runtime}
  230. r := httptest.NewRecorder()
  231. if err := getImagesViz(srv, APIVERSION, r, nil, nil); err != nil {
  232. t.Fatal(err)
  233. }
  234. if r.Code != http.StatusOK {
  235. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  236. }
  237. reader := bufio.NewReader(r.Body)
  238. line, err := reader.ReadString('\n')
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. if line != "digraph docker {\n" {
  243. t.Errorf("Expected digraph docker {\n, %s found", line)
  244. }
  245. }
  246. func TestGetImagesHistory(t *testing.T) {
  247. runtime := mkRuntime(t)
  248. defer nuke(runtime)
  249. srv := &Server{runtime: runtime}
  250. r := httptest.NewRecorder()
  251. if err := getImagesHistory(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
  252. t.Fatal(err)
  253. }
  254. history := []APIHistory{}
  255. if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil {
  256. t.Fatal(err)
  257. }
  258. if len(history) != 1 {
  259. t.Errorf("Expected 1 line, %d found", len(history))
  260. }
  261. }
  262. func TestGetImagesByName(t *testing.T) {
  263. runtime := mkRuntime(t)
  264. defer nuke(runtime)
  265. srv := &Server{runtime: runtime}
  266. r := httptest.NewRecorder()
  267. if err := getImagesByName(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
  268. t.Fatal(err)
  269. }
  270. img := &Image{}
  271. if err := json.Unmarshal(r.Body.Bytes(), img); err != nil {
  272. t.Fatal(err)
  273. }
  274. if img.ID != unitTestImageID {
  275. t.Errorf("Error inspecting image")
  276. }
  277. }
  278. func TestGetContainersJSON(t *testing.T) {
  279. runtime := mkRuntime(t)
  280. defer nuke(runtime)
  281. srv := &Server{runtime: runtime}
  282. container, err := runtime.Create(&Config{
  283. Image: GetTestImage(runtime).ID,
  284. Cmd: []string{"echo", "test"},
  285. })
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. defer runtime.Destroy(container)
  290. req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. r := httptest.NewRecorder()
  295. setTimeout(t, "getContainerJSON timed out", 5*time.Second, func() {
  296. if err := getContainersJSON(srv, APIVERSION, r, req, nil); err != nil {
  297. t.Fatal(err)
  298. }
  299. })
  300. containers := []APIContainers{}
  301. if err := json.Unmarshal(r.Body.Bytes(), &containers); err != nil {
  302. t.Fatal(err)
  303. }
  304. if len(containers) != 1 {
  305. t.Fatalf("Expected %d container, %d found", 1, len(containers))
  306. }
  307. if containers[0].ID != container.ID {
  308. t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ID, containers[0].ID)
  309. }
  310. }
  311. func TestGetContainersExport(t *testing.T) {
  312. runtime := mkRuntime(t)
  313. defer nuke(runtime)
  314. srv := &Server{runtime: runtime}
  315. // Create a container and remove a file
  316. container, err := runtime.Create(
  317. &Config{
  318. Image: GetTestImage(runtime).ID,
  319. Cmd: []string{"touch", "/test"},
  320. },
  321. )
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. defer runtime.Destroy(container)
  326. if err := container.Run(); err != nil {
  327. t.Fatal(err)
  328. }
  329. r := httptest.NewRecorder()
  330. if err := getContainersExport(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  331. t.Fatal(err)
  332. }
  333. if r.Code != http.StatusOK {
  334. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  335. }
  336. found := false
  337. for tarReader := tar.NewReader(r.Body); ; {
  338. h, err := tarReader.Next()
  339. if err != nil {
  340. if err == io.EOF {
  341. break
  342. }
  343. t.Fatal(err)
  344. }
  345. if h.Name == "./test" {
  346. found = true
  347. break
  348. }
  349. }
  350. if !found {
  351. t.Fatalf("The created test file has not been found in the exported image")
  352. }
  353. }
  354. func TestGetContainersChanges(t *testing.T) {
  355. runtime := mkRuntime(t)
  356. defer nuke(runtime)
  357. srv := &Server{runtime: runtime}
  358. // Create a container and remove a file
  359. container, err := runtime.Create(
  360. &Config{
  361. Image: GetTestImage(runtime).ID,
  362. Cmd: []string{"/bin/rm", "/etc/passwd"},
  363. },
  364. )
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. defer runtime.Destroy(container)
  369. if err := container.Run(); err != nil {
  370. t.Fatal(err)
  371. }
  372. r := httptest.NewRecorder()
  373. if err := getContainersChanges(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  374. t.Fatal(err)
  375. }
  376. changes := []Change{}
  377. if err := json.Unmarshal(r.Body.Bytes(), &changes); err != nil {
  378. t.Fatal(err)
  379. }
  380. // Check the changelog
  381. success := false
  382. for _, elem := range changes {
  383. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  384. success = true
  385. }
  386. }
  387. if !success {
  388. t.Fatalf("/etc/passwd as been removed but is not present in the diff")
  389. }
  390. }
  391. func TestGetContainersTop(t *testing.T) {
  392. t.Skip("Fixme. Skipping test for now. Reported error when testing using dind: 'api_test.go:527: Expected 2 processes, found 0.'")
  393. runtime := mkRuntime(t)
  394. defer nuke(runtime)
  395. srv := &Server{runtime: runtime}
  396. container, err := runtime.Create(
  397. &Config{
  398. Image: GetTestImage(runtime).ID,
  399. Cmd: []string{"/bin/sh", "-c", "cat"},
  400. OpenStdin: true,
  401. },
  402. )
  403. if err != nil {
  404. t.Fatal(err)
  405. }
  406. defer runtime.Destroy(container)
  407. defer func() {
  408. // Make sure the process dies before destroying runtime
  409. container.stdin.Close()
  410. container.WaitTimeout(2 * time.Second)
  411. }()
  412. hostConfig := &HostConfig{}
  413. if err := container.Start(hostConfig); err != nil {
  414. t.Fatal(err)
  415. }
  416. setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() {
  417. for {
  418. if container.State.Running {
  419. break
  420. }
  421. time.Sleep(10 * time.Millisecond)
  422. }
  423. })
  424. if !container.State.Running {
  425. t.Fatalf("Container should be running")
  426. }
  427. // Make sure sh spawn up cat
  428. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  429. in, _ := container.StdinPipe()
  430. out, _ := container.StdoutPipe()
  431. if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
  432. t.Fatal(err)
  433. }
  434. })
  435. r := httptest.NewRecorder()
  436. req, err := http.NewRequest("GET", "/"+container.ID+"/top?ps_args=u", bytes.NewReader([]byte{}))
  437. if err != nil {
  438. t.Fatal(err)
  439. }
  440. if err := getContainersTop(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  441. t.Fatal(err)
  442. }
  443. procs := APITop{}
  444. if err := json.Unmarshal(r.Body.Bytes(), &procs); err != nil {
  445. t.Fatal(err)
  446. }
  447. if len(procs.Titles) != 11 {
  448. t.Fatalf("Expected 11 titles, found %d.", len(procs.Titles))
  449. }
  450. if procs.Titles[0] != "USER" || procs.Titles[10] != "COMMAND" {
  451. t.Fatalf("Expected Titles[0] to be USER and Titles[10] to be COMMAND, found %s and %s.", procs.Titles[0], procs.Titles[10])
  452. }
  453. if len(procs.Processes) != 2 {
  454. t.Fatalf("Expected 2 processes, found %d.", len(procs.Processes))
  455. }
  456. if procs.Processes[0][10] != "/bin/sh" && procs.Processes[0][10] != "cat" {
  457. t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[0][10])
  458. }
  459. if procs.Processes[1][10] != "/bin/sh" && procs.Processes[1][10] != "cat" {
  460. t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[1][10])
  461. }
  462. }
  463. func TestGetContainersByName(t *testing.T) {
  464. runtime := mkRuntime(t)
  465. defer nuke(runtime)
  466. srv := &Server{runtime: runtime}
  467. // Create a container and remove a file
  468. container, err := runtime.Create(
  469. &Config{
  470. Image: GetTestImage(runtime).ID,
  471. Cmd: []string{"echo", "test"},
  472. },
  473. )
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. defer runtime.Destroy(container)
  478. r := httptest.NewRecorder()
  479. if err := getContainersByName(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  480. t.Fatal(err)
  481. }
  482. outContainer := &Container{}
  483. if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
  484. t.Fatal(err)
  485. }
  486. if outContainer.ID != container.ID {
  487. t.Fatalf("Wrong containers retrieved. Expected %s, received %s", container.ID, outContainer.ID)
  488. }
  489. }
  490. func TestPostCommit(t *testing.T) {
  491. runtime := mkRuntime(t)
  492. defer nuke(runtime)
  493. srv := &Server{runtime: runtime}
  494. // Create a container and remove a file
  495. container, err := runtime.Create(
  496. &Config{
  497. Image: GetTestImage(runtime).ID,
  498. Cmd: []string{"touch", "/test"},
  499. },
  500. )
  501. if err != nil {
  502. t.Fatal(err)
  503. }
  504. defer runtime.Destroy(container)
  505. if err := container.Run(); err != nil {
  506. t.Fatal(err)
  507. }
  508. req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+container.ID, bytes.NewReader([]byte{}))
  509. if err != nil {
  510. t.Fatal(err)
  511. }
  512. r := httptest.NewRecorder()
  513. if err := postCommit(srv, APIVERSION, r, req, nil); err != nil {
  514. t.Fatal(err)
  515. }
  516. if r.Code != http.StatusCreated {
  517. t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  518. }
  519. apiID := &APIID{}
  520. if err := json.Unmarshal(r.Body.Bytes(), apiID); err != nil {
  521. t.Fatal(err)
  522. }
  523. if _, err := runtime.graph.Get(apiID.ID); err != nil {
  524. t.Fatalf("The image has not been commited")
  525. }
  526. }
  527. func TestPostContainersCreate(t *testing.T) {
  528. runtime := mkRuntime(t)
  529. defer nuke(runtime)
  530. srv := &Server{runtime: runtime}
  531. configJSON, err := json.Marshal(&Config{
  532. Image: GetTestImage(runtime).ID,
  533. Memory: 33554432,
  534. Cmd: []string{"touch", "/test"},
  535. })
  536. if err != nil {
  537. t.Fatal(err)
  538. }
  539. req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJSON))
  540. if err != nil {
  541. t.Fatal(err)
  542. }
  543. r := httptest.NewRecorder()
  544. if err := postContainersCreate(srv, APIVERSION, r, req, nil); err != nil {
  545. t.Fatal(err)
  546. }
  547. if r.Code != http.StatusCreated {
  548. t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  549. }
  550. apiRun := &APIRun{}
  551. if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
  552. t.Fatal(err)
  553. }
  554. container := srv.runtime.Get(apiRun.ID)
  555. if container == nil {
  556. t.Fatalf("Container not created")
  557. }
  558. if err := container.Run(); err != nil {
  559. t.Fatal(err)
  560. }
  561. if err := container.EnsureMounted(); err != nil {
  562. t.Fatalf("Unable to mount container: %s", err)
  563. }
  564. if _, err := os.Stat(path.Join(container.RootfsPath(), "test")); err != nil {
  565. if os.IsNotExist(err) {
  566. utils.Debugf("Err: %s", err)
  567. t.Fatalf("The test file has not been created")
  568. }
  569. t.Fatal(err)
  570. }
  571. if err := container.Unmount(); err != nil {
  572. t.Fatalf("Unable to unmount container: %s", err)
  573. }
  574. }
  575. func TestPostContainersKill(t *testing.T) {
  576. runtime := mkRuntime(t)
  577. defer nuke(runtime)
  578. srv := &Server{runtime: runtime}
  579. container, err := runtime.Create(
  580. &Config{
  581. Image: GetTestImage(runtime).ID,
  582. Cmd: []string{"/bin/cat"},
  583. OpenStdin: true,
  584. },
  585. )
  586. if err != nil {
  587. t.Fatal(err)
  588. }
  589. defer runtime.Destroy(container)
  590. hostConfig := &HostConfig{}
  591. if err := container.Start(hostConfig); err != nil {
  592. t.Fatal(err)
  593. }
  594. // Give some time to the process to start
  595. container.WaitTimeout(500 * time.Millisecond)
  596. if !container.State.Running {
  597. t.Errorf("Container should be running")
  598. }
  599. r := httptest.NewRecorder()
  600. if err := postContainersKill(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  601. t.Fatal(err)
  602. }
  603. if r.Code != http.StatusNoContent {
  604. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  605. }
  606. if container.State.Running {
  607. t.Fatalf("The container hasn't been killed")
  608. }
  609. }
  610. func TestPostContainersRestart(t *testing.T) {
  611. runtime := mkRuntime(t)
  612. defer nuke(runtime)
  613. srv := &Server{runtime: runtime}
  614. container, err := runtime.Create(
  615. &Config{
  616. Image: GetTestImage(runtime).ID,
  617. Cmd: []string{"/bin/cat"},
  618. OpenStdin: true,
  619. },
  620. )
  621. if err != nil {
  622. t.Fatal(err)
  623. }
  624. defer runtime.Destroy(container)
  625. hostConfig := &HostConfig{}
  626. if err := container.Start(hostConfig); err != nil {
  627. t.Fatal(err)
  628. }
  629. // Give some time to the process to start
  630. container.WaitTimeout(500 * time.Millisecond)
  631. if !container.State.Running {
  632. t.Errorf("Container should be running")
  633. }
  634. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/restart?t=1", bytes.NewReader([]byte{}))
  635. if err != nil {
  636. t.Fatal(err)
  637. }
  638. r := httptest.NewRecorder()
  639. if err := postContainersRestart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  640. t.Fatal(err)
  641. }
  642. if r.Code != http.StatusNoContent {
  643. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  644. }
  645. // Give some time to the process to restart
  646. container.WaitTimeout(500 * time.Millisecond)
  647. if !container.State.Running {
  648. t.Fatalf("Container should be running")
  649. }
  650. if err := container.Kill(); err != nil {
  651. t.Fatal(err)
  652. }
  653. }
  654. func TestPostContainersStart(t *testing.T) {
  655. runtime := mkRuntime(t)
  656. defer nuke(runtime)
  657. srv := &Server{runtime: runtime}
  658. container, err := runtime.Create(
  659. &Config{
  660. Image: GetTestImage(runtime).ID,
  661. Cmd: []string{"/bin/cat"},
  662. OpenStdin: true,
  663. },
  664. )
  665. if err != nil {
  666. t.Fatal(err)
  667. }
  668. defer runtime.Destroy(container)
  669. hostConfigJSON, err := json.Marshal(&HostConfig{})
  670. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/start", bytes.NewReader(hostConfigJSON))
  671. if err != nil {
  672. t.Fatal(err)
  673. }
  674. req.Header.Set("Content-Type", "application/json")
  675. r := httptest.NewRecorder()
  676. if err := postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  677. t.Fatal(err)
  678. }
  679. if r.Code != http.StatusNoContent {
  680. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  681. }
  682. // Give some time to the process to start
  683. container.WaitTimeout(500 * time.Millisecond)
  684. if !container.State.Running {
  685. t.Errorf("Container should be running")
  686. }
  687. r = httptest.NewRecorder()
  688. if err = postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err == nil {
  689. t.Fatalf("A running container should be able to be started")
  690. }
  691. if err := container.Kill(); err != nil {
  692. t.Fatal(err)
  693. }
  694. }
  695. func TestPostContainersStop(t *testing.T) {
  696. runtime := mkRuntime(t)
  697. defer nuke(runtime)
  698. srv := &Server{runtime: runtime}
  699. container, err := runtime.Create(
  700. &Config{
  701. Image: GetTestImage(runtime).ID,
  702. Cmd: []string{"/bin/cat"},
  703. OpenStdin: true,
  704. },
  705. )
  706. if err != nil {
  707. t.Fatal(err)
  708. }
  709. defer runtime.Destroy(container)
  710. hostConfig := &HostConfig{}
  711. if err := container.Start(hostConfig); err != nil {
  712. t.Fatal(err)
  713. }
  714. // Give some time to the process to start
  715. container.WaitTimeout(500 * time.Millisecond)
  716. if !container.State.Running {
  717. t.Errorf("Container should be running")
  718. }
  719. // Note: as it is a POST request, it requires a body.
  720. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/stop?t=1", bytes.NewReader([]byte{}))
  721. if err != nil {
  722. t.Fatal(err)
  723. }
  724. r := httptest.NewRecorder()
  725. if err := postContainersStop(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  726. t.Fatal(err)
  727. }
  728. if r.Code != http.StatusNoContent {
  729. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  730. }
  731. if container.State.Running {
  732. t.Fatalf("The container hasn't been stopped")
  733. }
  734. }
  735. func TestPostContainersWait(t *testing.T) {
  736. runtime := mkRuntime(t)
  737. defer nuke(runtime)
  738. srv := &Server{runtime: runtime}
  739. container, err := runtime.Create(
  740. &Config{
  741. Image: GetTestImage(runtime).ID,
  742. Cmd: []string{"/bin/sleep", "1"},
  743. OpenStdin: true,
  744. },
  745. )
  746. if err != nil {
  747. t.Fatal(err)
  748. }
  749. defer runtime.Destroy(container)
  750. hostConfig := &HostConfig{}
  751. if err := container.Start(hostConfig); err != nil {
  752. t.Fatal(err)
  753. }
  754. setTimeout(t, "Wait timed out", 3*time.Second, func() {
  755. r := httptest.NewRecorder()
  756. if err := postContainersWait(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  757. t.Fatal(err)
  758. }
  759. apiWait := &APIWait{}
  760. if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
  761. t.Fatal(err)
  762. }
  763. if apiWait.StatusCode != 0 {
  764. t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
  765. }
  766. })
  767. if container.State.Running {
  768. t.Fatalf("The container should be stopped after wait")
  769. }
  770. }
  771. func TestPostContainersAttach(t *testing.T) {
  772. runtime := mkRuntime(t)
  773. defer nuke(runtime)
  774. srv := &Server{runtime: runtime}
  775. container, err := runtime.Create(
  776. &Config{
  777. Image: GetTestImage(runtime).ID,
  778. Cmd: []string{"/bin/cat"},
  779. OpenStdin: true,
  780. },
  781. )
  782. if err != nil {
  783. t.Fatal(err)
  784. }
  785. defer runtime.Destroy(container)
  786. // Start the process
  787. hostConfig := &HostConfig{}
  788. if err := container.Start(hostConfig); err != nil {
  789. t.Fatal(err)
  790. }
  791. stdin, stdinPipe := io.Pipe()
  792. stdout, stdoutPipe := io.Pipe()
  793. // Try to avoid the timeout in destroy. Best effort, don't check error
  794. defer func() {
  795. closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
  796. container.Kill()
  797. }()
  798. // Attach to it
  799. c1 := make(chan struct{})
  800. go func() {
  801. defer close(c1)
  802. r := &hijackTester{
  803. ResponseRecorder: httptest.NewRecorder(),
  804. in: stdin,
  805. out: stdoutPipe,
  806. }
  807. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
  808. if err != nil {
  809. t.Fatal(err)
  810. }
  811. if err := postContainersAttach(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  812. t.Fatal(err)
  813. }
  814. }()
  815. // Acknowledge hijack
  816. setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  817. stdout.Read([]byte{})
  818. stdout.Read(make([]byte, 4096))
  819. })
  820. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  821. if err := assertPipe("hello\n", string([]byte{1, 0, 0, 0, 0, 0, 0, 6})+"hello", stdout, stdinPipe, 15); err != nil {
  822. t.Fatal(err)
  823. }
  824. })
  825. // Close pipes (client disconnects)
  826. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  827. t.Fatal(err)
  828. }
  829. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  830. setTimeout(t, "Waiting for CmdAttach timed out", 10*time.Second, func() {
  831. <-c1
  832. })
  833. // We closed stdin, expect /bin/cat to still be running
  834. // Wait a little bit to make sure container.monitor() did his thing
  835. err = container.WaitTimeout(500 * time.Millisecond)
  836. if err == nil || !container.State.Running {
  837. t.Fatalf("/bin/cat is not running after closing stdin")
  838. }
  839. // Try to avoid the timeout in destroy. Best effort, don't check error
  840. cStdin, _ := container.StdinPipe()
  841. cStdin.Close()
  842. container.Wait()
  843. }
  844. func TestPostContainersAttachStderr(t *testing.T) {
  845. runtime := mkRuntime(t)
  846. defer nuke(runtime)
  847. srv := &Server{runtime: runtime}
  848. container, err := runtime.Create(
  849. &Config{
  850. Image: GetTestImage(runtime).ID,
  851. Cmd: []string{"/bin/sh", "-c", "/bin/cat >&2"},
  852. OpenStdin: true,
  853. },
  854. )
  855. if err != nil {
  856. t.Fatal(err)
  857. }
  858. defer runtime.Destroy(container)
  859. // Start the process
  860. hostConfig := &HostConfig{}
  861. if err := container.Start(hostConfig); err != nil {
  862. t.Fatal(err)
  863. }
  864. stdin, stdinPipe := io.Pipe()
  865. stdout, stdoutPipe := io.Pipe()
  866. // Try to avoid the timeout in destroy. Best effort, don't check error
  867. defer func() {
  868. closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
  869. container.Kill()
  870. }()
  871. // Attach to it
  872. c1 := make(chan struct{})
  873. go func() {
  874. defer close(c1)
  875. r := &hijackTester{
  876. ResponseRecorder: httptest.NewRecorder(),
  877. in: stdin,
  878. out: stdoutPipe,
  879. }
  880. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
  881. if err != nil {
  882. t.Fatal(err)
  883. }
  884. if err := postContainersAttach(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  885. t.Fatal(err)
  886. }
  887. }()
  888. // Acknowledge hijack
  889. setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  890. stdout.Read([]byte{})
  891. stdout.Read(make([]byte, 4096))
  892. })
  893. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  894. if err := assertPipe("hello\n", string([]byte{2, 0, 0, 0, 0, 0, 0, 6})+"hello", stdout, stdinPipe, 15); err != nil {
  895. t.Fatal(err)
  896. }
  897. })
  898. // Close pipes (client disconnects)
  899. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  900. t.Fatal(err)
  901. }
  902. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  903. setTimeout(t, "Waiting for CmdAttach timed out", 10*time.Second, func() {
  904. <-c1
  905. })
  906. // We closed stdin, expect /bin/cat to still be running
  907. // Wait a little bit to make sure container.monitor() did his thing
  908. err = container.WaitTimeout(500 * time.Millisecond)
  909. if err == nil || !container.State.Running {
  910. t.Fatalf("/bin/cat is not running after closing stdin")
  911. }
  912. // Try to avoid the timeout in destroy. Best effort, don't check error
  913. cStdin, _ := container.StdinPipe()
  914. cStdin.Close()
  915. container.Wait()
  916. }
  917. // FIXME: Test deleting running container
  918. // FIXME: Test deleting container with volume
  919. // FIXME: Test deleting volume in use by other container
  920. func TestDeleteContainers(t *testing.T) {
  921. runtime := mkRuntime(t)
  922. defer nuke(runtime)
  923. srv := &Server{runtime: runtime}
  924. container, err := runtime.Create(&Config{
  925. Image: GetTestImage(runtime).ID,
  926. Cmd: []string{"touch", "/test"},
  927. })
  928. if err != nil {
  929. t.Fatal(err)
  930. }
  931. defer runtime.Destroy(container)
  932. if err := container.Run(); err != nil {
  933. t.Fatal(err)
  934. }
  935. req, err := http.NewRequest("DELETE", "/containers/"+container.ID, nil)
  936. if err != nil {
  937. t.Fatal(err)
  938. }
  939. r := httptest.NewRecorder()
  940. if err := deleteContainers(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  941. t.Fatal(err)
  942. }
  943. if r.Code != http.StatusNoContent {
  944. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  945. }
  946. if c := runtime.Get(container.ID); c != nil {
  947. t.Fatalf("The container as not been deleted")
  948. }
  949. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  950. t.Fatalf("The test file has not been deleted")
  951. }
  952. }
  953. func TestOptionsRoute(t *testing.T) {
  954. runtime := mkRuntime(t)
  955. defer nuke(runtime)
  956. srv := &Server{runtime: runtime, enableCors: true}
  957. r := httptest.NewRecorder()
  958. router, err := createRouter(srv, false)
  959. if err != nil {
  960. t.Fatal(err)
  961. }
  962. req, err := http.NewRequest("OPTIONS", "/", nil)
  963. if err != nil {
  964. t.Fatal(err)
  965. }
  966. router.ServeHTTP(r, req)
  967. if r.Code != http.StatusOK {
  968. t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
  969. }
  970. }
  971. func TestGetEnabledCors(t *testing.T) {
  972. runtime := mkRuntime(t)
  973. defer nuke(runtime)
  974. srv := &Server{runtime: runtime, enableCors: true}
  975. r := httptest.NewRecorder()
  976. router, err := createRouter(srv, false)
  977. if err != nil {
  978. t.Fatal(err)
  979. }
  980. req, err := http.NewRequest("GET", "/version", nil)
  981. if err != nil {
  982. t.Fatal(err)
  983. }
  984. router.ServeHTTP(r, req)
  985. if r.Code != http.StatusOK {
  986. t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
  987. }
  988. allowOrigin := r.Header().Get("Access-Control-Allow-Origin")
  989. allowHeaders := r.Header().Get("Access-Control-Allow-Headers")
  990. allowMethods := r.Header().Get("Access-Control-Allow-Methods")
  991. if allowOrigin != "*" {
  992. t.Errorf("Expected header Access-Control-Allow-Origin to be \"*\", %s found.", allowOrigin)
  993. }
  994. if allowHeaders != "Origin, X-Requested-With, Content-Type, Accept" {
  995. t.Errorf("Expected header Access-Control-Allow-Headers to be \"Origin, X-Requested-With, Content-Type, Accept\", %s found.", allowHeaders)
  996. }
  997. if allowMethods != "GET, POST, DELETE, PUT, OPTIONS" {
  998. t.Errorf("Expected hearder Access-Control-Allow-Methods to be \"GET, POST, DELETE, PUT, OPTIONS\", %s found.", allowMethods)
  999. }
  1000. }
  1001. func TestDeleteImages(t *testing.T) {
  1002. runtime := mkRuntime(t)
  1003. defer nuke(runtime)
  1004. srv := &Server{runtime: runtime}
  1005. initialImages, err := srv.Images(false, "")
  1006. if err != nil {
  1007. t.Fatal(err)
  1008. }
  1009. if err := srv.runtime.repositories.Set("test", "test", unitTestImageName, true); err != nil {
  1010. t.Fatal(err)
  1011. }
  1012. images, err := srv.Images(false, "")
  1013. if err != nil {
  1014. t.Fatal(err)
  1015. }
  1016. if len(images) != len(initialImages)+1 {
  1017. t.Errorf("Expected %d images, %d found", len(initialImages)+1, len(images))
  1018. }
  1019. req, err := http.NewRequest("DELETE", "/images/"+unitTestImageID, nil)
  1020. if err != nil {
  1021. t.Fatal(err)
  1022. }
  1023. r := httptest.NewRecorder()
  1024. if err := deleteImages(srv, APIVERSION, r, req, map[string]string{"name": unitTestImageID}); err == nil {
  1025. t.Fatalf("Expected conflict error, got none")
  1026. }
  1027. req2, err := http.NewRequest("DELETE", "/images/test:test", nil)
  1028. if err != nil {
  1029. t.Fatal(err)
  1030. }
  1031. r2 := httptest.NewRecorder()
  1032. if err := deleteImages(srv, APIVERSION, r2, req2, map[string]string{"name": "test:test"}); err != nil {
  1033. t.Fatal(err)
  1034. }
  1035. if r2.Code != http.StatusOK {
  1036. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  1037. }
  1038. var outs []APIRmi
  1039. if err := json.Unmarshal(r2.Body.Bytes(), &outs); err != nil {
  1040. t.Fatal(err)
  1041. }
  1042. if len(outs) != 1 {
  1043. t.Fatalf("Expected %d event (untagged), got %d", 1, len(outs))
  1044. }
  1045. images, err = srv.Images(false, "")
  1046. if err != nil {
  1047. t.Fatal(err)
  1048. }
  1049. if len(images) != len(initialImages) {
  1050. t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
  1051. }
  1052. /* if c := runtime.Get(container.Id); c != nil {
  1053. t.Fatalf("The container as not been deleted")
  1054. }
  1055. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  1056. t.Fatalf("The test file has not been deleted")
  1057. } */
  1058. }
  1059. func TestJsonContentType(t *testing.T) {
  1060. if !matchesContentType("application/json", "application/json") {
  1061. t.Fail()
  1062. }
  1063. if !matchesContentType("application/json; charset=utf-8", "application/json") {
  1064. t.Fail()
  1065. }
  1066. if matchesContentType("dockerapplication/json", "application/json") {
  1067. t.Fail()
  1068. }
  1069. }
  1070. func TestPostContainersCopy(t *testing.T) {
  1071. runtime := mkRuntime(t)
  1072. defer nuke(runtime)
  1073. srv := &Server{runtime: runtime}
  1074. // Create a container and remove a file
  1075. container, err := runtime.Create(
  1076. &Config{
  1077. Image: GetTestImage(runtime).ID,
  1078. Cmd: []string{"touch", "/test.txt"},
  1079. },
  1080. )
  1081. if err != nil {
  1082. t.Fatal(err)
  1083. }
  1084. defer runtime.Destroy(container)
  1085. if err := container.Run(); err != nil {
  1086. t.Fatal(err)
  1087. }
  1088. r := httptest.NewRecorder()
  1089. copyData := APICopy{HostPath: ".", Resource: "/test.txt"}
  1090. jsonData, err := json.Marshal(copyData)
  1091. if err != nil {
  1092. t.Fatal(err)
  1093. }
  1094. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/copy", bytes.NewReader(jsonData))
  1095. if err != nil {
  1096. t.Fatal(err)
  1097. }
  1098. req.Header.Add("Content-Type", "application/json")
  1099. if err = postContainersCopy(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  1100. t.Fatal(err)
  1101. }
  1102. if r.Code != http.StatusOK {
  1103. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  1104. }
  1105. found := false
  1106. for tarReader := tar.NewReader(r.Body); ; {
  1107. h, err := tarReader.Next()
  1108. if err != nil {
  1109. if err == io.EOF {
  1110. break
  1111. }
  1112. t.Fatal(err)
  1113. }
  1114. if h.Name == "test.txt" {
  1115. found = true
  1116. break
  1117. }
  1118. }
  1119. if !found {
  1120. t.Fatalf("The created test file has not been found in the copied output")
  1121. }
  1122. }
  1123. // Mocked types for tests
  1124. type NopConn struct {
  1125. io.ReadCloser
  1126. io.Writer
  1127. }
  1128. func (c *NopConn) LocalAddr() net.Addr { return nil }
  1129. func (c *NopConn) RemoteAddr() net.Addr { return nil }
  1130. func (c *NopConn) SetDeadline(t time.Time) error { return nil }
  1131. func (c *NopConn) SetReadDeadline(t time.Time) error { return nil }
  1132. func (c *NopConn) SetWriteDeadline(t time.Time) error { return nil }
  1133. type hijackTester struct {
  1134. *httptest.ResponseRecorder
  1135. in io.ReadCloser
  1136. out io.Writer
  1137. }
  1138. func (t *hijackTester) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  1139. bufrw := bufio.NewReadWriter(bufio.NewReader(t.in), bufio.NewWriter(t.out))
  1140. conn := &NopConn{
  1141. ReadCloser: t.in,
  1142. Writer: t.out,
  1143. }
  1144. return conn, bufrw, nil
  1145. }