api_test.go 28 KB

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