api_test.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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 TestPostBuild(t *testing.T) {
  479. runtime, err := newTestRuntime()
  480. if err != nil {
  481. t.Fatal(err)
  482. }
  483. defer nuke(runtime)
  484. srv := &Server{runtime: runtime}
  485. stdin, stdinPipe := io.Pipe()
  486. stdout, stdoutPipe := io.Pipe()
  487. c1 := make(chan struct{})
  488. go func() {
  489. defer close(c1)
  490. r := &hijackTester{
  491. ResponseRecorder: httptest.NewRecorder(),
  492. in: stdin,
  493. out: stdoutPipe,
  494. }
  495. if err := postBuild(srv, r, nil, nil); err != nil {
  496. t.Fatal(err)
  497. }
  498. }()
  499. // Acknowledge hijack
  500. setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  501. stdout.Read([]byte{})
  502. stdout.Read(make([]byte, 4096))
  503. })
  504. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  505. if err := assertPipe("from docker-ut\n", "FROM docker-ut", stdout, stdinPipe, 15); err != nil {
  506. t.Fatal(err)
  507. }
  508. })
  509. // Close pipes (client disconnects)
  510. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  511. t.Fatal(err)
  512. }
  513. // Wait for build to finish, the client disconnected, therefore, Build finished his job
  514. setTimeout(t, "Waiting for CmdBuild timed out", 2*time.Second, func() {
  515. <-c1
  516. })
  517. }
  518. func TestPostImagesCreate(t *testing.T) {
  519. // FIXME: Use the staging in order to perform tests
  520. // runtime, err := newTestRuntime()
  521. // if err != nil {
  522. // t.Fatal(err)
  523. // }
  524. // defer nuke(runtime)
  525. // srv := &Server{runtime: runtime}
  526. // stdin, stdinPipe := io.Pipe()
  527. // stdout, stdoutPipe := io.Pipe()
  528. // c1 := make(chan struct{})
  529. // go func() {
  530. // defer close(c1)
  531. // r := &hijackTester{
  532. // ResponseRecorder: httptest.NewRecorder(),
  533. // in: stdin,
  534. // out: stdoutPipe,
  535. // }
  536. // req, err := http.NewRequest("POST", "/images/create?fromImage="+unitTestImageName, bytes.NewReader([]byte{}))
  537. // if err != nil {
  538. // t.Fatal(err)
  539. // }
  540. // body, err := postImagesCreate(srv, r, req, nil)
  541. // if err != nil {
  542. // t.Fatal(err)
  543. // }
  544. // if body != nil {
  545. // t.Fatalf("No body expected, received: %s\n", body)
  546. // }
  547. // }()
  548. // // Acknowledge hijack
  549. // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  550. // stdout.Read([]byte{})
  551. // stdout.Read(make([]byte, 4096))
  552. // })
  553. // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
  554. // reader := bufio.NewReader(stdout)
  555. // line, err := reader.ReadString('\n')
  556. // if err != nil {
  557. // t.Fatal(err)
  558. // }
  559. // if !strings.HasPrefix(line, "Pulling repository d from") {
  560. // t.Fatalf("Expected Pulling repository docker-ut from..., found %s", line)
  561. // }
  562. // })
  563. // // Close pipes (client disconnects)
  564. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  565. // t.Fatal(err)
  566. // }
  567. // // Wait for imagesCreate to finish, the client disconnected, therefore, Create finished his job
  568. // setTimeout(t, "Waiting for imagesCreate timed out", 10*time.Second, func() {
  569. // <-c1
  570. // })
  571. }
  572. // func TestPostImagesInsert(t *testing.T) {
  573. // //FIXME: Implement this test (or remove this endpoint)
  574. // t.Log("Test not implemented")
  575. // }
  576. func TestPostImagesPush(t *testing.T) {
  577. //FIXME: Use staging in order to perform tests
  578. // runtime, err := newTestRuntime()
  579. // if err != nil {
  580. // t.Fatal(err)
  581. // }
  582. // defer nuke(runtime)
  583. // srv := &Server{runtime: runtime}
  584. // stdin, stdinPipe := io.Pipe()
  585. // stdout, stdoutPipe := io.Pipe()
  586. // c1 := make(chan struct{})
  587. // go func() {
  588. // r := &hijackTester{
  589. // ResponseRecorder: httptest.NewRecorder(),
  590. // in: stdin,
  591. // out: stdoutPipe,
  592. // }
  593. // req, err := http.NewRequest("POST", "/images/docker-ut/push", bytes.NewReader([]byte{}))
  594. // if err != nil {
  595. // t.Fatal(err)
  596. // }
  597. // body, err := postImagesPush(srv, r, req, map[string]string{"name": "docker-ut"})
  598. // close(c1)
  599. // if err != nil {
  600. // t.Fatal(err)
  601. // }
  602. // if body != nil {
  603. // t.Fatalf("No body expected, received: %s\n", body)
  604. // }
  605. // }()
  606. // // Acknowledge hijack
  607. // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  608. // stdout.Read([]byte{})
  609. // stdout.Read(make([]byte, 4096))
  610. // })
  611. // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
  612. // reader := bufio.NewReader(stdout)
  613. // line, err := reader.ReadString('\n')
  614. // if err != nil {
  615. // t.Fatal(err)
  616. // }
  617. // if !strings.HasPrefix(line, "Processing checksum") {
  618. // t.Fatalf("Processing checksum..., found %s", line)
  619. // }
  620. // })
  621. // // Close pipes (client disconnects)
  622. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  623. // t.Fatal(err)
  624. // }
  625. // // Wait for imagesPush to finish, the client disconnected, therefore, Push finished his job
  626. // setTimeout(t, "Waiting for imagesPush timed out", 10*time.Second, func() {
  627. // <-c1
  628. // })
  629. }
  630. func TestPostImagesTag(t *testing.T) {
  631. // FIXME: Use staging in order to perform tests
  632. // runtime, err := newTestRuntime()
  633. // if err != nil {
  634. // t.Fatal(err)
  635. // }
  636. // defer nuke(runtime)
  637. // srv := &Server{runtime: runtime}
  638. // r := httptest.NewRecorder()
  639. // req, err := http.NewRequest("POST", "/images/docker-ut/tag?repo=testrepo&tag=testtag", bytes.NewReader([]byte{}))
  640. // if err != nil {
  641. // t.Fatal(err)
  642. // }
  643. // body, err := postImagesTag(srv, r, req, map[string]string{"name": "docker-ut"})
  644. // if err != nil {
  645. // t.Fatal(err)
  646. // }
  647. // if body != nil {
  648. // t.Fatalf("No body expected, received: %s\n", body)
  649. // }
  650. // if r.Code != http.StatusCreated {
  651. // t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  652. // }
  653. }
  654. func TestPostContainersCreate(t *testing.T) {
  655. runtime, err := newTestRuntime()
  656. if err != nil {
  657. t.Fatal(err)
  658. }
  659. defer nuke(runtime)
  660. srv := &Server{runtime: runtime}
  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. r := httptest.NewRecorder()
  674. if err := postContainersCreate(srv, r, req, nil); 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(r.Body.Bytes(), 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. utils.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. if err := postContainersKill(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
  727. t.Fatal(err)
  728. }
  729. if r.Code != http.StatusNoContent {
  730. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  731. }
  732. if container.State.Running {
  733. t.Fatalf("The container hasn't been killed")
  734. }
  735. }
  736. func TestPostContainersRestart(t *testing.T) {
  737. runtime, err := newTestRuntime()
  738. if err != nil {
  739. t.Fatal(err)
  740. }
  741. defer nuke(runtime)
  742. srv := &Server{runtime: runtime}
  743. container, err := NewBuilder(runtime).Create(
  744. &Config{
  745. Image: GetTestImage(runtime).Id,
  746. Cmd: []string{"/bin/cat"},
  747. OpenStdin: true,
  748. },
  749. )
  750. if err != nil {
  751. t.Fatal(err)
  752. }
  753. defer runtime.Destroy(container)
  754. if err := container.Start(); err != nil {
  755. t.Fatal(err)
  756. }
  757. // Give some time to the process to start
  758. container.WaitTimeout(500 * time.Millisecond)
  759. if !container.State.Running {
  760. t.Errorf("Container should be running")
  761. }
  762. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/restart?t=1", bytes.NewReader([]byte{}))
  763. if err != nil {
  764. t.Fatal(err)
  765. }
  766. r := httptest.NewRecorder()
  767. if err := postContainersRestart(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  768. t.Fatal(err)
  769. }
  770. if r.Code != http.StatusNoContent {
  771. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  772. }
  773. // Give some time to the process to restart
  774. container.WaitTimeout(500 * time.Millisecond)
  775. if !container.State.Running {
  776. t.Fatalf("Container should be running")
  777. }
  778. if err := container.Kill(); err != nil {
  779. t.Fatal(err)
  780. }
  781. }
  782. func TestPostContainersStart(t *testing.T) {
  783. runtime, err := newTestRuntime()
  784. if err != nil {
  785. t.Fatal(err)
  786. }
  787. defer nuke(runtime)
  788. srv := &Server{runtime: runtime}
  789. container, err := NewBuilder(runtime).Create(
  790. &Config{
  791. Image: GetTestImage(runtime).Id,
  792. Cmd: []string{"/bin/cat"},
  793. OpenStdin: true,
  794. },
  795. )
  796. if err != nil {
  797. t.Fatal(err)
  798. }
  799. defer runtime.Destroy(container)
  800. r := httptest.NewRecorder()
  801. if err := postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
  802. t.Fatal(err)
  803. }
  804. if r.Code != http.StatusNoContent {
  805. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  806. }
  807. // Give some time to the process to start
  808. container.WaitTimeout(500 * time.Millisecond)
  809. if !container.State.Running {
  810. t.Errorf("Container should be running")
  811. }
  812. r = httptest.NewRecorder()
  813. if err = postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err == nil {
  814. t.Fatalf("A running containter should be able to be started")
  815. }
  816. if err := container.Kill(); err != nil {
  817. t.Fatal(err)
  818. }
  819. }
  820. func TestPostContainersStop(t *testing.T) {
  821. runtime, err := newTestRuntime()
  822. if err != nil {
  823. t.Fatal(err)
  824. }
  825. defer nuke(runtime)
  826. srv := &Server{runtime: runtime}
  827. container, err := NewBuilder(runtime).Create(
  828. &Config{
  829. Image: GetTestImage(runtime).Id,
  830. Cmd: []string{"/bin/cat"},
  831. OpenStdin: true,
  832. },
  833. )
  834. if err != nil {
  835. t.Fatal(err)
  836. }
  837. defer runtime.Destroy(container)
  838. if err := container.Start(); err != nil {
  839. t.Fatal(err)
  840. }
  841. // Give some time to the process to start
  842. container.WaitTimeout(500 * time.Millisecond)
  843. if !container.State.Running {
  844. t.Errorf("Container should be running")
  845. }
  846. // Note: as it is a POST request, it requires a body.
  847. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/stop?t=1", bytes.NewReader([]byte{}))
  848. if err != nil {
  849. t.Fatal(err)
  850. }
  851. r := httptest.NewRecorder()
  852. if err := postContainersStop(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  853. t.Fatal(err)
  854. }
  855. if r.Code != http.StatusNoContent {
  856. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  857. }
  858. if container.State.Running {
  859. t.Fatalf("The container hasn't been stopped")
  860. }
  861. }
  862. func TestPostContainersWait(t *testing.T) {
  863. runtime, err := newTestRuntime()
  864. if err != nil {
  865. t.Fatal(err)
  866. }
  867. defer nuke(runtime)
  868. srv := &Server{runtime: runtime}
  869. container, err := NewBuilder(runtime).Create(
  870. &Config{
  871. Image: GetTestImage(runtime).Id,
  872. Cmd: []string{"/bin/sleep", "1"},
  873. OpenStdin: true,
  874. },
  875. )
  876. if err != nil {
  877. t.Fatal(err)
  878. }
  879. defer runtime.Destroy(container)
  880. if err := container.Start(); err != nil {
  881. t.Fatal(err)
  882. }
  883. setTimeout(t, "Wait timed out", 3*time.Second, func() {
  884. r := httptest.NewRecorder()
  885. if err := postContainersWait(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
  886. t.Fatal(err)
  887. }
  888. apiWait := &ApiWait{}
  889. if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
  890. t.Fatal(err)
  891. }
  892. if apiWait.StatusCode != 0 {
  893. t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
  894. }
  895. })
  896. if container.State.Running {
  897. t.Fatalf("The container should be stopped after wait")
  898. }
  899. }
  900. func TestPostContainersAttach(t *testing.T) {
  901. runtime, err := newTestRuntime()
  902. if err != nil {
  903. t.Fatal(err)
  904. }
  905. defer nuke(runtime)
  906. srv := &Server{runtime: runtime}
  907. container, err := NewBuilder(runtime).Create(
  908. &Config{
  909. Image: GetTestImage(runtime).Id,
  910. Cmd: []string{"/bin/cat"},
  911. OpenStdin: true,
  912. },
  913. )
  914. if err != nil {
  915. t.Fatal(err)
  916. }
  917. defer runtime.Destroy(container)
  918. // Start the process
  919. if err := container.Start(); err != nil {
  920. t.Fatal(err)
  921. }
  922. stdin, stdinPipe := io.Pipe()
  923. stdout, stdoutPipe := io.Pipe()
  924. // Attach to it
  925. c1 := make(chan struct{})
  926. go func() {
  927. defer close(c1)
  928. r := &hijackTester{
  929. ResponseRecorder: httptest.NewRecorder(),
  930. in: stdin,
  931. out: stdoutPipe,
  932. }
  933. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
  934. if err != nil {
  935. t.Fatal(err)
  936. }
  937. if err := postContainersAttach(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  938. t.Fatal(err)
  939. }
  940. }()
  941. // Acknowledge hijack
  942. setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  943. stdout.Read([]byte{})
  944. stdout.Read(make([]byte, 4096))
  945. })
  946. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  947. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  948. t.Fatal(err)
  949. }
  950. })
  951. // Close pipes (client disconnects)
  952. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  953. t.Fatal(err)
  954. }
  955. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  956. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  957. <-c1
  958. })
  959. // We closed stdin, expect /bin/cat to still be running
  960. // Wait a little bit to make sure container.monitor() did his thing
  961. err = container.WaitTimeout(500 * time.Millisecond)
  962. if err == nil || !container.State.Running {
  963. t.Fatalf("/bin/cat is not running after closing stdin")
  964. }
  965. // Try to avoid the timeoout in destroy. Best effort, don't check error
  966. cStdin, _ := container.StdinPipe()
  967. cStdin.Close()
  968. container.Wait()
  969. }
  970. // FIXME: Test deleting running container
  971. // FIXME: Test deleting container with volume
  972. // FIXME: Test deleting volume in use by other container
  973. func TestDeleteContainers(t *testing.T) {
  974. runtime, err := newTestRuntime()
  975. if err != nil {
  976. t.Fatal(err)
  977. }
  978. defer nuke(runtime)
  979. srv := &Server{runtime: runtime}
  980. container, err := NewBuilder(runtime).Create(&Config{
  981. Image: GetTestImage(runtime).Id,
  982. Cmd: []string{"touch", "/test"},
  983. })
  984. if err != nil {
  985. t.Fatal(err)
  986. }
  987. defer runtime.Destroy(container)
  988. if err := container.Run(); err != nil {
  989. t.Fatal(err)
  990. }
  991. req, err := http.NewRequest("DELETE", "/containers/"+container.Id, nil)
  992. if err != nil {
  993. t.Fatal(err)
  994. }
  995. r := httptest.NewRecorder()
  996. if err := deleteContainers(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  997. t.Fatal(err)
  998. }
  999. if r.Code != http.StatusNoContent {
  1000. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  1001. }
  1002. if c := runtime.Get(container.Id); c != nil {
  1003. t.Fatalf("The container as not been deleted")
  1004. }
  1005. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  1006. t.Fatalf("The test file has not been deleted")
  1007. }
  1008. }
  1009. func TestDeleteImages(t *testing.T) {
  1010. runtime, err := newTestRuntime()
  1011. if err != nil {
  1012. t.Fatal(err)
  1013. }
  1014. defer nuke(runtime)
  1015. srv := &Server{runtime: runtime}
  1016. if err := srv.runtime.repositories.Set("test", "test", unitTestImageName, true); err != nil {
  1017. t.Fatal(err)
  1018. }
  1019. images, err := srv.Images(false, "")
  1020. if err != nil {
  1021. t.Fatal(err)
  1022. }
  1023. if len(images) != 2 {
  1024. t.Errorf("Excepted 2 images, %d found", len(images))
  1025. }
  1026. r := httptest.NewRecorder()
  1027. if err := deleteImages(srv, r, nil, map[string]string{"name": "test:test"}); err != nil {
  1028. t.Fatal(err)
  1029. }
  1030. if r.Code != http.StatusNoContent {
  1031. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  1032. }
  1033. images, err = srv.Images(false, "")
  1034. if err != nil {
  1035. t.Fatal(err)
  1036. }
  1037. if len(images) != 1 {
  1038. t.Errorf("Excepted 1 image, %d found", len(images))
  1039. }
  1040. /* if c := runtime.Get(container.Id); c != nil {
  1041. t.Fatalf("The container as not been deleted")
  1042. }
  1043. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  1044. t.Fatalf("The test file has not been deleted")
  1045. } */
  1046. }
  1047. // Mocked types for tests
  1048. type NopConn struct {
  1049. io.ReadCloser
  1050. io.Writer
  1051. }
  1052. func (c *NopConn) LocalAddr() net.Addr { return nil }
  1053. func (c *NopConn) RemoteAddr() net.Addr { return nil }
  1054. func (c *NopConn) SetDeadline(t time.Time) error { return nil }
  1055. func (c *NopConn) SetReadDeadline(t time.Time) error { return nil }
  1056. func (c *NopConn) SetWriteDeadline(t time.Time) error { return nil }
  1057. type hijackTester struct {
  1058. *httptest.ResponseRecorder
  1059. in io.ReadCloser
  1060. out io.Writer
  1061. }
  1062. func (t *hijackTester) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  1063. bufrw := bufio.NewReadWriter(bufio.NewReader(t.in), bufio.NewWriter(t.out))
  1064. conn := &NopConn{
  1065. ReadCloser: t.in,
  1066. Writer: t.out,
  1067. }
  1068. return conn, bufrw, nil
  1069. }