api_test.go 29 KB

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