api_test.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. package docker
  2. import (
  3. "archive/tar"
  4. "bufio"
  5. "bytes"
  6. "encoding/json"
  7. "github.com/dotcloud/docker/auth"
  8. "github.com/dotcloud/docker/utils"
  9. "io"
  10. "net"
  11. "net/http"
  12. "net/http/httptest"
  13. "os"
  14. "path"
  15. "testing"
  16. "time"
  17. )
  18. func TestPostAuth(t *testing.T) {
  19. runtime, err := newTestRuntime()
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. defer nuke(runtime)
  24. srv := &Server{
  25. runtime: runtime,
  26. }
  27. r := httptest.NewRecorder()
  28. authConfig := &auth.AuthConfig{
  29. Username: "utest",
  30. Password: "utest",
  31. Email: "utest@yopmail.com",
  32. }
  33. authConfigJson, err := json.Marshal(authConfig)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if err := postAuth(srv, API_VERSION, r, req, nil); err != nil {
  42. t.Fatal(err)
  43. }
  44. if r.Code != http.StatusOK && r.Code != 0 {
  45. t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
  46. }
  47. }
  48. func TestGetVersion(t *testing.T) {
  49. runtime, err := newTestRuntime()
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. defer nuke(runtime)
  54. srv := &Server{runtime: runtime}
  55. r := httptest.NewRecorder()
  56. if err := getVersion(srv, API_VERSION, r, nil, nil); err != nil {
  57. t.Fatal(err)
  58. }
  59. v := &ApiVersion{}
  60. if err = json.Unmarshal(r.Body.Bytes(), v); err != nil {
  61. t.Fatal(err)
  62. }
  63. if v.Version != VERSION {
  64. t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
  65. }
  66. }
  67. func TestGetInfo(t *testing.T) {
  68. runtime, err := newTestRuntime()
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer nuke(runtime)
  73. srv := &Server{runtime: runtime}
  74. r := httptest.NewRecorder()
  75. if err := getInfo(srv, API_VERSION, r, nil, nil); err != nil {
  76. t.Fatal(err)
  77. }
  78. infos := &ApiInfo{}
  79. err = json.Unmarshal(r.Body.Bytes(), infos)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if infos.Images != 1 {
  84. t.Errorf("Excepted images: %d, %d found", 1, infos.Images)
  85. }
  86. }
  87. func TestGetImagesJson(t *testing.T) {
  88. runtime, err := newTestRuntime()
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. defer nuke(runtime)
  93. srv := &Server{runtime: runtime}
  94. // all=0
  95. req, err := http.NewRequest("GET", "/images/json?all=0", nil)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. r := httptest.NewRecorder()
  100. if err := getImagesJson(srv, API_VERSION, r, req, nil); err != nil {
  101. t.Fatal(err)
  102. }
  103. images := []ApiImages{}
  104. if err := json.Unmarshal(r.Body.Bytes(), &images); err != nil {
  105. t.Fatal(err)
  106. }
  107. if len(images) != 1 {
  108. t.Errorf("Excepted 1 image, %d found", len(images))
  109. }
  110. if images[0].Repository != unitTestImageName {
  111. t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository)
  112. }
  113. r2 := httptest.NewRecorder()
  114. // all=1
  115. req2, err := http.NewRequest("GET", "/images/json?all=true", nil)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. if err := getImagesJson(srv, API_VERSION, r2, req2, nil); err != nil {
  120. t.Fatal(err)
  121. }
  122. images2 := []ApiImages{}
  123. if err := json.Unmarshal(r2.Body.Bytes(), &images2); err != nil {
  124. t.Fatal(err)
  125. }
  126. if len(images2) != 1 {
  127. t.Errorf("Excepted 1 image, %d found", len(images2))
  128. }
  129. if images2[0].Id != GetTestImage(runtime).Id {
  130. t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).Id, images2[0].Id)
  131. }
  132. r3 := httptest.NewRecorder()
  133. // filter=a
  134. req3, err := http.NewRequest("GET", "/images/json?filter=a", nil)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. if err := getImagesJson(srv, API_VERSION, r3, req3, nil); err != nil {
  139. t.Fatal(err)
  140. }
  141. images3 := []ApiImages{}
  142. if err := json.Unmarshal(r3.Body.Bytes(), &images3); err != nil {
  143. t.Fatal(err)
  144. }
  145. if len(images3) != 0 {
  146. t.Errorf("Excepted 1 image, %d found", len(images3))
  147. }
  148. r4 := httptest.NewRecorder()
  149. // all=foobar
  150. req4, err := http.NewRequest("GET", "/images/json?all=foobar", nil)
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. err = getImagesJson(srv, API_VERSION, r4, req4, nil)
  155. if err == nil {
  156. t.Fatalf("Error expected, received none")
  157. }
  158. httpError(r4, err)
  159. if r4.Code != http.StatusBadRequest {
  160. t.Fatalf("%d Bad Request expected, received %d\n", http.StatusBadRequest, r4.Code)
  161. }
  162. }
  163. func TestGetImagesViz(t *testing.T) {
  164. runtime, err := newTestRuntime()
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. defer nuke(runtime)
  169. srv := &Server{runtime: runtime}
  170. r := httptest.NewRecorder()
  171. if err := getImagesViz(srv, API_VERSION, r, nil, nil); err != nil {
  172. t.Fatal(err)
  173. }
  174. if r.Code != http.StatusOK {
  175. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  176. }
  177. reader := bufio.NewReader(r.Body)
  178. line, err := reader.ReadString('\n')
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. if line != "digraph docker {\n" {
  183. t.Errorf("Excepted digraph docker {\n, %s found", line)
  184. }
  185. }
  186. func TestGetImagesSearch(t *testing.T) {
  187. runtime, err := newTestRuntime()
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. defer nuke(runtime)
  192. srv := &Server{
  193. runtime: runtime,
  194. }
  195. r := httptest.NewRecorder()
  196. req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. if err := getImagesSearch(srv, API_VERSION, r, req, nil); err != nil {
  201. t.Fatal(err)
  202. }
  203. results := []ApiSearch{}
  204. if err := json.Unmarshal(r.Body.Bytes(), &results); err != nil {
  205. t.Fatal(err)
  206. }
  207. if len(results) < 2 {
  208. t.Errorf("Excepted at least 2 lines, %d found", len(results))
  209. }
  210. }
  211. func TestGetImagesHistory(t *testing.T) {
  212. runtime, err := newTestRuntime()
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. defer nuke(runtime)
  217. srv := &Server{runtime: runtime}
  218. r := httptest.NewRecorder()
  219. if err := getImagesHistory(srv, API_VERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
  220. t.Fatal(err)
  221. }
  222. history := []ApiHistory{}
  223. if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil {
  224. t.Fatal(err)
  225. }
  226. if len(history) != 1 {
  227. t.Errorf("Excepted 1 line, %d found", len(history))
  228. }
  229. }
  230. func TestGetImagesByName(t *testing.T) {
  231. runtime, err := newTestRuntime()
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. defer nuke(runtime)
  236. srv := &Server{runtime: runtime}
  237. r := httptest.NewRecorder()
  238. if err := getImagesByName(srv, API_VERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
  239. t.Fatal(err)
  240. }
  241. img := &Image{}
  242. if err := json.Unmarshal(r.Body.Bytes(), img); err != nil {
  243. t.Fatal(err)
  244. }
  245. if img.Id != GetTestImage(runtime).Id || img.Comment != "Imported from http://get.docker.io/images/busybox" {
  246. t.Errorf("Error inspecting image")
  247. }
  248. }
  249. func TestGetContainersJson(t *testing.T) {
  250. runtime, err := newTestRuntime()
  251. if err != nil {
  252. t.Fatal(err)
  253. }
  254. defer nuke(runtime)
  255. srv := &Server{runtime: runtime}
  256. container, err := NewBuilder(runtime).Create(&Config{
  257. Image: GetTestImage(runtime).Id,
  258. Cmd: []string{"echo", "test"},
  259. })
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. defer runtime.Destroy(container)
  264. req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
  265. if err != nil {
  266. t.Fatal(err)
  267. }
  268. r := httptest.NewRecorder()
  269. if err := getContainersJson(srv, API_VERSION, r, req, nil); err != nil {
  270. t.Fatal(err)
  271. }
  272. containers := []ApiContainers{}
  273. if err := json.Unmarshal(r.Body.Bytes(), &containers); err != nil {
  274. t.Fatal(err)
  275. }
  276. if len(containers) != 1 {
  277. t.Fatalf("Excepted %d container, %d found", 1, len(containers))
  278. }
  279. if containers[0].Id != container.Id {
  280. t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.Id, containers[0].Id)
  281. }
  282. }
  283. func TestGetContainersExport(t *testing.T) {
  284. runtime, err := newTestRuntime()
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. defer nuke(runtime)
  289. srv := &Server{runtime: runtime}
  290. builder := NewBuilder(runtime)
  291. // Create a container and remove a file
  292. container, err := builder.Create(
  293. &Config{
  294. Image: GetTestImage(runtime).Id,
  295. Cmd: []string{"touch", "/test"},
  296. },
  297. )
  298. if err != nil {
  299. t.Fatal(err)
  300. }
  301. defer runtime.Destroy(container)
  302. if err := container.Run(); err != nil {
  303. t.Fatal(err)
  304. }
  305. r := httptest.NewRecorder()
  306. if err = getContainersExport(srv, API_VERSION, r, nil, map[string]string{"name": container.Id}); err != nil {
  307. t.Fatal(err)
  308. }
  309. if r.Code != http.StatusOK {
  310. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  311. }
  312. found := false
  313. for tarReader := tar.NewReader(r.Body); ; {
  314. h, err := tarReader.Next()
  315. if err != nil {
  316. if err == io.EOF {
  317. break
  318. }
  319. t.Fatal(err)
  320. }
  321. if h.Name == "./test" {
  322. found = true
  323. break
  324. }
  325. }
  326. if !found {
  327. t.Fatalf("The created test file has not been found in the exported image")
  328. }
  329. }
  330. func TestGetContainersChanges(t *testing.T) {
  331. runtime, err := newTestRuntime()
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. defer nuke(runtime)
  336. srv := &Server{runtime: runtime}
  337. builder := NewBuilder(runtime)
  338. // Create a container and remove a file
  339. container, err := builder.Create(
  340. &Config{
  341. Image: GetTestImage(runtime).Id,
  342. Cmd: []string{"/bin/rm", "/etc/passwd"},
  343. },
  344. )
  345. if err != nil {
  346. t.Fatal(err)
  347. }
  348. defer runtime.Destroy(container)
  349. if err := container.Run(); err != nil {
  350. t.Fatal(err)
  351. }
  352. r := httptest.NewRecorder()
  353. if err := getContainersChanges(srv, API_VERSION, r, nil, map[string]string{"name": container.Id}); err != nil {
  354. t.Fatal(err)
  355. }
  356. changes := []Change{}
  357. if err := json.Unmarshal(r.Body.Bytes(), &changes); err != nil {
  358. t.Fatal(err)
  359. }
  360. // Check the changelog
  361. success := false
  362. for _, elem := range changes {
  363. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  364. success = true
  365. }
  366. }
  367. if !success {
  368. t.Fatalf("/etc/passwd as been removed but is not present in the diff")
  369. }
  370. }
  371. func TestGetContainersByName(t *testing.T) {
  372. runtime, err := newTestRuntime()
  373. if err != nil {
  374. t.Fatal(err)
  375. }
  376. defer nuke(runtime)
  377. srv := &Server{runtime: runtime}
  378. builder := NewBuilder(runtime)
  379. // Create a container and remove a file
  380. container, err := builder.Create(
  381. &Config{
  382. Image: GetTestImage(runtime).Id,
  383. Cmd: []string{"echo", "test"},
  384. },
  385. )
  386. if err != nil {
  387. t.Fatal(err)
  388. }
  389. defer runtime.Destroy(container)
  390. r := httptest.NewRecorder()
  391. if err := getContainersByName(srv, API_VERSION, r, nil, map[string]string{"name": container.Id}); err != nil {
  392. t.Fatal(err)
  393. }
  394. outContainer := &Container{}
  395. if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
  396. t.Fatal(err)
  397. }
  398. if outContainer.Id != container.Id {
  399. t.Fatalf("Wrong containers retrieved. Expected %s, recieved %s", container.Id, outContainer.Id)
  400. }
  401. }
  402. func TestPostCommit(t *testing.T) {
  403. runtime, err := newTestRuntime()
  404. if err != nil {
  405. t.Fatal(err)
  406. }
  407. defer nuke(runtime)
  408. srv := &Server{runtime: runtime}
  409. builder := NewBuilder(runtime)
  410. // Create a container and remove a file
  411. container, err := builder.Create(
  412. &Config{
  413. Image: GetTestImage(runtime).Id,
  414. Cmd: []string{"touch", "/test"},
  415. },
  416. )
  417. if err != nil {
  418. t.Fatal(err)
  419. }
  420. defer runtime.Destroy(container)
  421. if err := container.Run(); err != nil {
  422. t.Fatal(err)
  423. }
  424. req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+container.Id, bytes.NewReader([]byte{}))
  425. if err != nil {
  426. t.Fatal(err)
  427. }
  428. r := httptest.NewRecorder()
  429. if err := postCommit(srv, API_VERSION, r, req, nil); err != nil {
  430. t.Fatal(err)
  431. }
  432. if r.Code != http.StatusCreated {
  433. t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  434. }
  435. apiId := &ApiId{}
  436. if err := json.Unmarshal(r.Body.Bytes(), apiId); err != nil {
  437. t.Fatal(err)
  438. }
  439. if _, err := runtime.graph.Get(apiId.Id); err != nil {
  440. t.Fatalf("The image has not been commited")
  441. }
  442. }
  443. func TestPostImagesCreate(t *testing.T) {
  444. // FIXME: Use the staging in order to perform tests
  445. // runtime, err := newTestRuntime()
  446. // if err != nil {
  447. // t.Fatal(err)
  448. // }
  449. // defer nuke(runtime)
  450. // srv := &Server{runtime: runtime}
  451. // stdin, stdinPipe := io.Pipe()
  452. // stdout, stdoutPipe := io.Pipe()
  453. // c1 := make(chan struct{})
  454. // go func() {
  455. // defer close(c1)
  456. // r := &hijackTester{
  457. // ResponseRecorder: httptest.NewRecorder(),
  458. // in: stdin,
  459. // out: stdoutPipe,
  460. // }
  461. // req, err := http.NewRequest("POST", "/images/create?fromImage="+unitTestImageName, bytes.NewReader([]byte{}))
  462. // if err != nil {
  463. // t.Fatal(err)
  464. // }
  465. // body, err := postImagesCreate(srv, r, req, nil)
  466. // if err != nil {
  467. // t.Fatal(err)
  468. // }
  469. // if body != nil {
  470. // t.Fatalf("No body expected, received: %s\n", body)
  471. // }
  472. // }()
  473. // // Acknowledge hijack
  474. // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  475. // stdout.Read([]byte{})
  476. // stdout.Read(make([]byte, 4096))
  477. // })
  478. // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
  479. // reader := bufio.NewReader(stdout)
  480. // line, err := reader.ReadString('\n')
  481. // if err != nil {
  482. // t.Fatal(err)
  483. // }
  484. // if !strings.HasPrefix(line, "Pulling repository d from") {
  485. // t.Fatalf("Expected Pulling repository docker-ut from..., found %s", line)
  486. // }
  487. // })
  488. // // Close pipes (client disconnects)
  489. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  490. // t.Fatal(err)
  491. // }
  492. // // Wait for imagesCreate to finish, the client disconnected, therefore, Create finished his job
  493. // setTimeout(t, "Waiting for imagesCreate timed out", 10*time.Second, func() {
  494. // <-c1
  495. // })
  496. }
  497. func TestPostImagesInsert(t *testing.T) {
  498. // runtime, err := newTestRuntime()
  499. // if err != nil {
  500. // t.Fatal(err)
  501. // }
  502. // defer nuke(runtime)
  503. // srv := &Server{runtime: runtime}
  504. // stdin, stdinPipe := io.Pipe()
  505. // stdout, stdoutPipe := io.Pipe()
  506. // // Attach to it
  507. // c1 := make(chan struct{})
  508. // go func() {
  509. // defer close(c1)
  510. // r := &hijackTester{
  511. // ResponseRecorder: httptest.NewRecorder(),
  512. // in: stdin,
  513. // out: stdoutPipe,
  514. // }
  515. // req, err := http.NewRequest("POST", "/images/"+unitTestImageName+"/insert?path=%2Ftest&url=https%3A%2F%2Fraw.github.com%2Fdotcloud%2Fdocker%2Fmaster%2FREADME.md", bytes.NewReader([]byte{}))
  516. // if err != nil {
  517. // t.Fatal(err)
  518. // }
  519. // if err := postContainersCreate(srv, r, req, nil); err != nil {
  520. // t.Fatal(err)
  521. // }
  522. // }()
  523. // // Acknowledge hijack
  524. // setTimeout(t, "hijack acknowledge timed out", 5*time.Second, func() {
  525. // stdout.Read([]byte{})
  526. // stdout.Read(make([]byte, 4096))
  527. // })
  528. // id := ""
  529. // setTimeout(t, "Waiting for imagesInsert output", 10*time.Second, func() {
  530. // for {
  531. // reader := bufio.NewReader(stdout)
  532. // id, err = reader.ReadString('\n')
  533. // if err != nil {
  534. // t.Fatal(err)
  535. // }
  536. // }
  537. // })
  538. // // Close pipes (client disconnects)
  539. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  540. // t.Fatal(err)
  541. // }
  542. // // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  543. // setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  544. // <-c1
  545. // })
  546. // img, err := srv.runtime.repositories.LookupImage(id)
  547. // if err != nil {
  548. // t.Fatalf("New image %s expected but not found", id)
  549. // }
  550. // layer, err := img.layer()
  551. // if err != nil {
  552. // t.Fatal(err)
  553. // }
  554. // if _, err := os.Stat(path.Join(layer, "test")); err != nil {
  555. // t.Fatalf("The test file has not been found")
  556. // }
  557. // if err := srv.runtime.graph.Delete(img.Id); err != nil {
  558. // t.Fatal(err)
  559. // }
  560. }
  561. func TestPostImagesPush(t *testing.T) {
  562. //FIXME: Use staging in order to perform tests
  563. // runtime, err := newTestRuntime()
  564. // if err != nil {
  565. // t.Fatal(err)
  566. // }
  567. // defer nuke(runtime)
  568. // srv := &Server{runtime: runtime}
  569. // stdin, stdinPipe := io.Pipe()
  570. // stdout, stdoutPipe := io.Pipe()
  571. // c1 := make(chan struct{})
  572. // go func() {
  573. // r := &hijackTester{
  574. // ResponseRecorder: httptest.NewRecorder(),
  575. // in: stdin,
  576. // out: stdoutPipe,
  577. // }
  578. // req, err := http.NewRequest("POST", "/images/docker-ut/push", bytes.NewReader([]byte{}))
  579. // if err != nil {
  580. // t.Fatal(err)
  581. // }
  582. // body, err := postImagesPush(srv, r, req, map[string]string{"name": "docker-ut"})
  583. // close(c1)
  584. // if err != nil {
  585. // t.Fatal(err)
  586. // }
  587. // if body != nil {
  588. // t.Fatalf("No body expected, received: %s\n", body)
  589. // }
  590. // }()
  591. // // Acknowledge hijack
  592. // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  593. // stdout.Read([]byte{})
  594. // stdout.Read(make([]byte, 4096))
  595. // })
  596. // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
  597. // reader := bufio.NewReader(stdout)
  598. // line, err := reader.ReadString('\n')
  599. // if err != nil {
  600. // t.Fatal(err)
  601. // }
  602. // if !strings.HasPrefix(line, "Processing checksum") {
  603. // t.Fatalf("Processing checksum..., found %s", line)
  604. // }
  605. // })
  606. // // Close pipes (client disconnects)
  607. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  608. // t.Fatal(err)
  609. // }
  610. // // Wait for imagesPush to finish, the client disconnected, therefore, Push finished his job
  611. // setTimeout(t, "Waiting for imagesPush timed out", 10*time.Second, func() {
  612. // <-c1
  613. // })
  614. }
  615. func TestPostImagesTag(t *testing.T) {
  616. // FIXME: Use staging in order to perform tests
  617. // runtime, err := newTestRuntime()
  618. // if err != nil {
  619. // t.Fatal(err)
  620. // }
  621. // defer nuke(runtime)
  622. // srv := &Server{runtime: runtime}
  623. // r := httptest.NewRecorder()
  624. // req, err := http.NewRequest("POST", "/images/docker-ut/tag?repo=testrepo&tag=testtag", bytes.NewReader([]byte{}))
  625. // if err != nil {
  626. // t.Fatal(err)
  627. // }
  628. // body, err := postImagesTag(srv, r, req, map[string]string{"name": "docker-ut"})
  629. // if err != nil {
  630. // t.Fatal(err)
  631. // }
  632. // if body != nil {
  633. // t.Fatalf("No body expected, received: %s\n", body)
  634. // }
  635. // if r.Code != http.StatusCreated {
  636. // t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  637. // }
  638. }
  639. func TestPostContainersCreate(t *testing.T) {
  640. runtime, err := newTestRuntime()
  641. if err != nil {
  642. t.Fatal(err)
  643. }
  644. defer nuke(runtime)
  645. srv := &Server{runtime: runtime}
  646. configJson, err := json.Marshal(&Config{
  647. Image: GetTestImage(runtime).Id,
  648. Memory: 33554432,
  649. Cmd: []string{"touch", "/test"},
  650. })
  651. if err != nil {
  652. t.Fatal(err)
  653. }
  654. req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJson))
  655. if err != nil {
  656. t.Fatal(err)
  657. }
  658. r := httptest.NewRecorder()
  659. if err := postContainersCreate(srv, API_VERSION, r, req, nil); err != nil {
  660. t.Fatal(err)
  661. }
  662. if r.Code != http.StatusCreated {
  663. t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  664. }
  665. apiRun := &ApiRun{}
  666. if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
  667. t.Fatal(err)
  668. }
  669. container := srv.runtime.Get(apiRun.Id)
  670. if container == nil {
  671. t.Fatalf("Container not created")
  672. }
  673. if err := container.Run(); err != nil {
  674. t.Fatal(err)
  675. }
  676. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err != nil {
  677. if os.IsNotExist(err) {
  678. utils.Debugf("Err: %s", err)
  679. t.Fatalf("The test file has not been created")
  680. }
  681. t.Fatal(err)
  682. }
  683. }
  684. func TestPostContainersKill(t *testing.T) {
  685. runtime, err := newTestRuntime()
  686. if err != nil {
  687. t.Fatal(err)
  688. }
  689. defer nuke(runtime)
  690. srv := &Server{runtime: runtime}
  691. container, err := NewBuilder(runtime).Create(
  692. &Config{
  693. Image: GetTestImage(runtime).Id,
  694. Cmd: []string{"/bin/cat"},
  695. OpenStdin: true,
  696. },
  697. )
  698. if err != nil {
  699. t.Fatal(err)
  700. }
  701. defer runtime.Destroy(container)
  702. if err := container.Start(); err != nil {
  703. t.Fatal(err)
  704. }
  705. // Give some time to the process to start
  706. container.WaitTimeout(500 * time.Millisecond)
  707. if !container.State.Running {
  708. t.Errorf("Container should be running")
  709. }
  710. r := httptest.NewRecorder()
  711. if err := postContainersKill(srv, API_VERSION, r, nil, map[string]string{"name": container.Id}); err != nil {
  712. t.Fatal(err)
  713. }
  714. if r.Code != http.StatusNoContent {
  715. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  716. }
  717. if container.State.Running {
  718. t.Fatalf("The container hasn't been killed")
  719. }
  720. }
  721. func TestPostContainersRestart(t *testing.T) {
  722. runtime, err := newTestRuntime()
  723. if err != nil {
  724. t.Fatal(err)
  725. }
  726. defer nuke(runtime)
  727. srv := &Server{runtime: runtime}
  728. container, err := NewBuilder(runtime).Create(
  729. &Config{
  730. Image: GetTestImage(runtime).Id,
  731. Cmd: []string{"/bin/cat"},
  732. OpenStdin: true,
  733. },
  734. )
  735. if err != nil {
  736. t.Fatal(err)
  737. }
  738. defer runtime.Destroy(container)
  739. if err := container.Start(); err != nil {
  740. t.Fatal(err)
  741. }
  742. // Give some time to the process to start
  743. container.WaitTimeout(500 * time.Millisecond)
  744. if !container.State.Running {
  745. t.Errorf("Container should be running")
  746. }
  747. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/restart?t=1", bytes.NewReader([]byte{}))
  748. if err != nil {
  749. t.Fatal(err)
  750. }
  751. r := httptest.NewRecorder()
  752. if err := postContainersRestart(srv, API_VERSION, r, req, map[string]string{"name": container.Id}); err != nil {
  753. t.Fatal(err)
  754. }
  755. if r.Code != http.StatusNoContent {
  756. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  757. }
  758. // Give some time to the process to restart
  759. container.WaitTimeout(500 * time.Millisecond)
  760. if !container.State.Running {
  761. t.Fatalf("Container should be running")
  762. }
  763. if err := container.Kill(); err != nil {
  764. t.Fatal(err)
  765. }
  766. }
  767. func TestPostContainersStart(t *testing.T) {
  768. runtime, err := newTestRuntime()
  769. if err != nil {
  770. t.Fatal(err)
  771. }
  772. defer nuke(runtime)
  773. srv := &Server{runtime: runtime}
  774. container, err := NewBuilder(runtime).Create(
  775. &Config{
  776. Image: GetTestImage(runtime).Id,
  777. Cmd: []string{"/bin/cat"},
  778. OpenStdin: true,
  779. },
  780. )
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. defer runtime.Destroy(container)
  785. r := httptest.NewRecorder()
  786. if err := postContainersStart(srv, API_VERSION, r, nil, map[string]string{"name": container.Id}); err != nil {
  787. t.Fatal(err)
  788. }
  789. if r.Code != http.StatusNoContent {
  790. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  791. }
  792. // Give some time to the process to start
  793. container.WaitTimeout(500 * time.Millisecond)
  794. if !container.State.Running {
  795. t.Errorf("Container should be running")
  796. }
  797. r = httptest.NewRecorder()
  798. if err = postContainersStart(srv, API_VERSION, r, nil, map[string]string{"name": container.Id}); err == nil {
  799. t.Fatalf("A running containter should be able to be started")
  800. }
  801. if err := container.Kill(); err != nil {
  802. t.Fatal(err)
  803. }
  804. }
  805. func TestPostContainersStop(t *testing.T) {
  806. runtime, err := newTestRuntime()
  807. if err != nil {
  808. t.Fatal(err)
  809. }
  810. defer nuke(runtime)
  811. srv := &Server{runtime: runtime}
  812. container, err := NewBuilder(runtime).Create(
  813. &Config{
  814. Image: GetTestImage(runtime).Id,
  815. Cmd: []string{"/bin/cat"},
  816. OpenStdin: true,
  817. },
  818. )
  819. if err != nil {
  820. t.Fatal(err)
  821. }
  822. defer runtime.Destroy(container)
  823. if err := container.Start(); err != nil {
  824. t.Fatal(err)
  825. }
  826. // Give some time to the process to start
  827. container.WaitTimeout(500 * time.Millisecond)
  828. if !container.State.Running {
  829. t.Errorf("Container should be running")
  830. }
  831. // Note: as it is a POST request, it requires a body.
  832. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/stop?t=1", bytes.NewReader([]byte{}))
  833. if err != nil {
  834. t.Fatal(err)
  835. }
  836. r := httptest.NewRecorder()
  837. if err := postContainersStop(srv, API_VERSION, r, req, map[string]string{"name": container.Id}); err != nil {
  838. t.Fatal(err)
  839. }
  840. if r.Code != http.StatusNoContent {
  841. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  842. }
  843. if container.State.Running {
  844. t.Fatalf("The container hasn't been stopped")
  845. }
  846. }
  847. func TestPostContainersWait(t *testing.T) {
  848. runtime, err := newTestRuntime()
  849. if err != nil {
  850. t.Fatal(err)
  851. }
  852. defer nuke(runtime)
  853. srv := &Server{runtime: runtime}
  854. container, err := NewBuilder(runtime).Create(
  855. &Config{
  856. Image: GetTestImage(runtime).Id,
  857. Cmd: []string{"/bin/sleep", "1"},
  858. OpenStdin: true,
  859. },
  860. )
  861. if err != nil {
  862. t.Fatal(err)
  863. }
  864. defer runtime.Destroy(container)
  865. if err := container.Start(); err != nil {
  866. t.Fatal(err)
  867. }
  868. setTimeout(t, "Wait timed out", 3*time.Second, func() {
  869. r := httptest.NewRecorder()
  870. if err := postContainersWait(srv, API_VERSION, r, nil, map[string]string{"name": container.Id}); err != nil {
  871. t.Fatal(err)
  872. }
  873. apiWait := &ApiWait{}
  874. if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
  875. t.Fatal(err)
  876. }
  877. if apiWait.StatusCode != 0 {
  878. t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
  879. }
  880. })
  881. if container.State.Running {
  882. t.Fatalf("The container should be stopped after wait")
  883. }
  884. }
  885. func TestPostContainersAttach(t *testing.T) {
  886. runtime, err := newTestRuntime()
  887. if err != nil {
  888. t.Fatal(err)
  889. }
  890. defer nuke(runtime)
  891. srv := &Server{runtime: runtime}
  892. container, err := NewBuilder(runtime).Create(
  893. &Config{
  894. Image: GetTestImage(runtime).Id,
  895. Cmd: []string{"/bin/cat"},
  896. OpenStdin: true,
  897. },
  898. )
  899. if err != nil {
  900. t.Fatal(err)
  901. }
  902. defer runtime.Destroy(container)
  903. // Start the process
  904. if err := container.Start(); err != nil {
  905. t.Fatal(err)
  906. }
  907. stdin, stdinPipe := io.Pipe()
  908. stdout, stdoutPipe := io.Pipe()
  909. // Attach to it
  910. c1 := make(chan struct{})
  911. go func() {
  912. defer close(c1)
  913. r := &hijackTester{
  914. ResponseRecorder: httptest.NewRecorder(),
  915. in: stdin,
  916. out: stdoutPipe,
  917. }
  918. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
  919. if err != nil {
  920. t.Fatal(err)
  921. }
  922. if err := postContainersAttach(srv, API_VERSION, r, req, map[string]string{"name": container.Id}); err != nil {
  923. t.Fatal(err)
  924. }
  925. }()
  926. // Acknowledge hijack
  927. setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  928. stdout.Read([]byte{})
  929. stdout.Read(make([]byte, 4096))
  930. })
  931. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  932. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  933. t.Fatal(err)
  934. }
  935. })
  936. // Close pipes (client disconnects)
  937. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  938. t.Fatal(err)
  939. }
  940. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  941. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  942. <-c1
  943. })
  944. // We closed stdin, expect /bin/cat to still be running
  945. // Wait a little bit to make sure container.monitor() did his thing
  946. err = container.WaitTimeout(500 * time.Millisecond)
  947. if err == nil || !container.State.Running {
  948. t.Fatalf("/bin/cat is not running after closing stdin")
  949. }
  950. // Try to avoid the timeoout in destroy. Best effort, don't check error
  951. cStdin, _ := container.StdinPipe()
  952. cStdin.Close()
  953. container.Wait()
  954. }
  955. // FIXME: Test deleting running container
  956. // FIXME: Test deleting container with volume
  957. // FIXME: Test deleting volume in use by other container
  958. func TestDeleteContainers(t *testing.T) {
  959. runtime, err := newTestRuntime()
  960. if err != nil {
  961. t.Fatal(err)
  962. }
  963. defer nuke(runtime)
  964. srv := &Server{runtime: runtime}
  965. container, err := NewBuilder(runtime).Create(&Config{
  966. Image: GetTestImage(runtime).Id,
  967. Cmd: []string{"touch", "/test"},
  968. })
  969. if err != nil {
  970. t.Fatal(err)
  971. }
  972. defer runtime.Destroy(container)
  973. if err := container.Run(); err != nil {
  974. t.Fatal(err)
  975. }
  976. req, err := http.NewRequest("DELETE", "/containers/"+container.Id, nil)
  977. if err != nil {
  978. t.Fatal(err)
  979. }
  980. r := httptest.NewRecorder()
  981. if err := deleteContainers(srv, API_VERSION, r, req, map[string]string{"name": container.Id}); err != nil {
  982. t.Fatal(err)
  983. }
  984. if r.Code != http.StatusNoContent {
  985. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  986. }
  987. if c := runtime.Get(container.Id); c != nil {
  988. t.Fatalf("The container as not been deleted")
  989. }
  990. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  991. t.Fatalf("The test file has not been deleted")
  992. }
  993. }
  994. func TestDeleteImages(t *testing.T) {
  995. //FIXME: Implement this test
  996. t.Log("Test not implemented")
  997. }
  998. // Mocked types for tests
  999. type NopConn struct {
  1000. io.ReadCloser
  1001. io.Writer
  1002. }
  1003. func (c *NopConn) LocalAddr() net.Addr { return nil }
  1004. func (c *NopConn) RemoteAddr() net.Addr { return nil }
  1005. func (c *NopConn) SetDeadline(t time.Time) error { return nil }
  1006. func (c *NopConn) SetReadDeadline(t time.Time) error { return nil }
  1007. func (c *NopConn) SetWriteDeadline(t time.Time) error { return nil }
  1008. type hijackTester struct {
  1009. *httptest.ResponseRecorder
  1010. in io.ReadCloser
  1011. out io.Writer
  1012. }
  1013. func (t *hijackTester) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  1014. bufrw := bufio.NewReadWriter(bufio.NewReader(t.in), bufio.NewWriter(t.out))
  1015. conn := &NopConn{
  1016. ReadCloser: t.in,
  1017. Writer: t.out,
  1018. }
  1019. return conn, bufrw, nil
  1020. }