api_test.go 28 KB

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