api_test.go 28 KB

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