api_test.go 28 KB

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