api_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. // runtime, err := newTestRuntime()
  574. // if err != nil {
  575. // t.Fatal(err)
  576. // }
  577. // defer nuke(runtime)
  578. // srv := &Server{runtime: runtime}
  579. // stdin, stdinPipe := io.Pipe()
  580. // stdout, stdoutPipe := io.Pipe()
  581. // // Attach to it
  582. // c1 := make(chan struct{})
  583. // go func() {
  584. // defer close(c1)
  585. // r := &hijackTester{
  586. // ResponseRecorder: httptest.NewRecorder(),
  587. // in: stdin,
  588. // out: stdoutPipe,
  589. // }
  590. // req, err := http.NewRequest("POST", "/images/"+unitTestImageName+"/insert?path=%2Ftest&url=https%3A%2F%2Fraw.github.com%2Fdotcloud%2Fdocker%2Fmaster%2FREADME.md", bytes.NewReader([]byte{}))
  591. // if err != nil {
  592. // t.Fatal(err)
  593. // }
  594. // if err := postContainersCreate(srv, r, req, nil); err != nil {
  595. // t.Fatal(err)
  596. // }
  597. // }()
  598. // // Acknowledge hijack
  599. // setTimeout(t, "hijack acknowledge timed out", 5*time.Second, func() {
  600. // stdout.Read([]byte{})
  601. // stdout.Read(make([]byte, 4096))
  602. // })
  603. // id := ""
  604. // setTimeout(t, "Waiting for imagesInsert output", 10*time.Second, func() {
  605. // for {
  606. // reader := bufio.NewReader(stdout)
  607. // id, err = reader.ReadString('\n')
  608. // if err != nil {
  609. // t.Fatal(err)
  610. // }
  611. // }
  612. // })
  613. // // Close pipes (client disconnects)
  614. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  615. // t.Fatal(err)
  616. // }
  617. // // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  618. // setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  619. // <-c1
  620. // })
  621. // img, err := srv.runtime.repositories.LookupImage(id)
  622. // if err != nil {
  623. // t.Fatalf("New image %s expected but not found", id)
  624. // }
  625. // layer, err := img.layer()
  626. // if err != nil {
  627. // t.Fatal(err)
  628. // }
  629. // if _, err := os.Stat(path.Join(layer, "test")); err != nil {
  630. // t.Fatalf("The test file has not been found")
  631. // }
  632. // if err := srv.runtime.graph.Delete(img.Id); err != nil {
  633. // t.Fatal(err)
  634. // }
  635. }
  636. func TestPostImagesPush(t *testing.T) {
  637. //FIXME: Use staging in order to perform tests
  638. // runtime, err := newTestRuntime()
  639. // if err != nil {
  640. // t.Fatal(err)
  641. // }
  642. // defer nuke(runtime)
  643. // srv := &Server{runtime: runtime}
  644. // stdin, stdinPipe := io.Pipe()
  645. // stdout, stdoutPipe := io.Pipe()
  646. // c1 := make(chan struct{})
  647. // go func() {
  648. // r := &hijackTester{
  649. // ResponseRecorder: httptest.NewRecorder(),
  650. // in: stdin,
  651. // out: stdoutPipe,
  652. // }
  653. // req, err := http.NewRequest("POST", "/images/docker-ut/push", bytes.NewReader([]byte{}))
  654. // if err != nil {
  655. // t.Fatal(err)
  656. // }
  657. // body, err := postImagesPush(srv, r, req, map[string]string{"name": "docker-ut"})
  658. // close(c1)
  659. // if err != nil {
  660. // t.Fatal(err)
  661. // }
  662. // if body != nil {
  663. // t.Fatalf("No body expected, received: %s\n", body)
  664. // }
  665. // }()
  666. // // Acknowledge hijack
  667. // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  668. // stdout.Read([]byte{})
  669. // stdout.Read(make([]byte, 4096))
  670. // })
  671. // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
  672. // reader := bufio.NewReader(stdout)
  673. // line, err := reader.ReadString('\n')
  674. // if err != nil {
  675. // t.Fatal(err)
  676. // }
  677. // if !strings.HasPrefix(line, "Processing checksum") {
  678. // t.Fatalf("Processing checksum..., found %s", line)
  679. // }
  680. // })
  681. // // Close pipes (client disconnects)
  682. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  683. // t.Fatal(err)
  684. // }
  685. // // Wait for imagesPush to finish, the client disconnected, therefore, Push finished his job
  686. // setTimeout(t, "Waiting for imagesPush timed out", 10*time.Second, func() {
  687. // <-c1
  688. // })
  689. }
  690. func TestPostImagesTag(t *testing.T) {
  691. // FIXME: Use staging in order to perform tests
  692. // runtime, err := newTestRuntime()
  693. // if err != nil {
  694. // t.Fatal(err)
  695. // }
  696. // defer nuke(runtime)
  697. // srv := &Server{runtime: runtime}
  698. // r := httptest.NewRecorder()
  699. // req, err := http.NewRequest("POST", "/images/docker-ut/tag?repo=testrepo&tag=testtag", bytes.NewReader([]byte{}))
  700. // if err != nil {
  701. // t.Fatal(err)
  702. // }
  703. // body, err := postImagesTag(srv, r, req, map[string]string{"name": "docker-ut"})
  704. // if err != nil {
  705. // t.Fatal(err)
  706. // }
  707. // if body != nil {
  708. // t.Fatalf("No body expected, received: %s\n", body)
  709. // }
  710. // if r.Code != http.StatusCreated {
  711. // t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  712. // }
  713. }
  714. func TestPostContainersCreate(t *testing.T) {
  715. runtime, err := newTestRuntime()
  716. if err != nil {
  717. t.Fatal(err)
  718. }
  719. defer nuke(runtime)
  720. srv := &Server{runtime: runtime}
  721. configJson, err := json.Marshal(&Config{
  722. Image: GetTestImage(runtime).Id,
  723. Memory: 33554432,
  724. Cmd: []string{"touch", "/test"},
  725. })
  726. if err != nil {
  727. t.Fatal(err)
  728. }
  729. req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJson))
  730. if err != nil {
  731. t.Fatal(err)
  732. }
  733. r := httptest.NewRecorder()
  734. if err := postContainersCreate(srv, r, req, nil); err != nil {
  735. t.Fatal(err)
  736. }
  737. if r.Code != http.StatusCreated {
  738. t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  739. }
  740. apiRun := &ApiRun{}
  741. if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
  742. t.Fatal(err)
  743. }
  744. container := srv.runtime.Get(apiRun.Id)
  745. if container == nil {
  746. t.Fatalf("Container not created")
  747. }
  748. if err := container.Run(); err != nil {
  749. t.Fatal(err)
  750. }
  751. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err != nil {
  752. if os.IsNotExist(err) {
  753. utils.Debugf("Err: %s", err)
  754. t.Fatalf("The test file has not been created")
  755. }
  756. t.Fatal(err)
  757. }
  758. }
  759. func TestPostContainersKill(t *testing.T) {
  760. runtime, err := newTestRuntime()
  761. if err != nil {
  762. t.Fatal(err)
  763. }
  764. defer nuke(runtime)
  765. srv := &Server{runtime: runtime}
  766. container, err := NewBuilder(runtime).Create(
  767. &Config{
  768. Image: GetTestImage(runtime).Id,
  769. Cmd: []string{"/bin/cat"},
  770. OpenStdin: true,
  771. },
  772. )
  773. if err != nil {
  774. t.Fatal(err)
  775. }
  776. defer runtime.Destroy(container)
  777. if err := container.Start(); err != nil {
  778. t.Fatal(err)
  779. }
  780. // Give some time to the process to start
  781. container.WaitTimeout(500 * time.Millisecond)
  782. if !container.State.Running {
  783. t.Errorf("Container should be running")
  784. }
  785. r := httptest.NewRecorder()
  786. if err := postContainersKill(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
  787. t.Fatal(err)
  788. }
  789. if r.Code != http.StatusNoContent {
  790. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  791. }
  792. if container.State.Running {
  793. t.Fatalf("The container hasn't been killed")
  794. }
  795. }
  796. func TestPostContainersRestart(t *testing.T) {
  797. runtime, err := newTestRuntime()
  798. if err != nil {
  799. t.Fatal(err)
  800. }
  801. defer nuke(runtime)
  802. srv := &Server{runtime: runtime}
  803. container, err := NewBuilder(runtime).Create(
  804. &Config{
  805. Image: GetTestImage(runtime).Id,
  806. Cmd: []string{"/bin/cat"},
  807. OpenStdin: true,
  808. },
  809. )
  810. if err != nil {
  811. t.Fatal(err)
  812. }
  813. defer runtime.Destroy(container)
  814. if err := container.Start(); err != nil {
  815. t.Fatal(err)
  816. }
  817. // Give some time to the process to start
  818. container.WaitTimeout(500 * time.Millisecond)
  819. if !container.State.Running {
  820. t.Errorf("Container should be running")
  821. }
  822. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/restart?t=1", bytes.NewReader([]byte{}))
  823. if err != nil {
  824. t.Fatal(err)
  825. }
  826. r := httptest.NewRecorder()
  827. if err := postContainersRestart(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  828. t.Fatal(err)
  829. }
  830. if r.Code != http.StatusNoContent {
  831. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  832. }
  833. // Give some time to the process to restart
  834. container.WaitTimeout(500 * time.Millisecond)
  835. if !container.State.Running {
  836. t.Fatalf("Container should be running")
  837. }
  838. if err := container.Kill(); err != nil {
  839. t.Fatal(err)
  840. }
  841. }
  842. func TestPostContainersStart(t *testing.T) {
  843. runtime, err := newTestRuntime()
  844. if err != nil {
  845. t.Fatal(err)
  846. }
  847. defer nuke(runtime)
  848. srv := &Server{runtime: runtime}
  849. container, err := NewBuilder(runtime).Create(
  850. &Config{
  851. Image: GetTestImage(runtime).Id,
  852. Cmd: []string{"/bin/cat"},
  853. OpenStdin: true,
  854. },
  855. )
  856. if err != nil {
  857. t.Fatal(err)
  858. }
  859. defer runtime.Destroy(container)
  860. r := httptest.NewRecorder()
  861. if err := postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
  862. t.Fatal(err)
  863. }
  864. if r.Code != http.StatusNoContent {
  865. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  866. }
  867. // Give some time to the process to start
  868. container.WaitTimeout(500 * time.Millisecond)
  869. if !container.State.Running {
  870. t.Errorf("Container should be running")
  871. }
  872. r = httptest.NewRecorder()
  873. if err = postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err == nil {
  874. t.Fatalf("A running containter should be able to be started")
  875. }
  876. if err := container.Kill(); err != nil {
  877. t.Fatal(err)
  878. }
  879. }
  880. func TestPostContainersStop(t *testing.T) {
  881. runtime, err := newTestRuntime()
  882. if err != nil {
  883. t.Fatal(err)
  884. }
  885. defer nuke(runtime)
  886. srv := &Server{runtime: runtime}
  887. container, err := NewBuilder(runtime).Create(
  888. &Config{
  889. Image: GetTestImage(runtime).Id,
  890. Cmd: []string{"/bin/cat"},
  891. OpenStdin: true,
  892. },
  893. )
  894. if err != nil {
  895. t.Fatal(err)
  896. }
  897. defer runtime.Destroy(container)
  898. if err := container.Start(); err != nil {
  899. t.Fatal(err)
  900. }
  901. // Give some time to the process to start
  902. container.WaitTimeout(500 * time.Millisecond)
  903. if !container.State.Running {
  904. t.Errorf("Container should be running")
  905. }
  906. // Note: as it is a POST request, it requires a body.
  907. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/stop?t=1", bytes.NewReader([]byte{}))
  908. if err != nil {
  909. t.Fatal(err)
  910. }
  911. r := httptest.NewRecorder()
  912. if err := postContainersStop(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  913. t.Fatal(err)
  914. }
  915. if r.Code != http.StatusNoContent {
  916. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  917. }
  918. if container.State.Running {
  919. t.Fatalf("The container hasn't been stopped")
  920. }
  921. }
  922. func TestPostContainersWait(t *testing.T) {
  923. runtime, err := newTestRuntime()
  924. if err != nil {
  925. t.Fatal(err)
  926. }
  927. defer nuke(runtime)
  928. srv := &Server{runtime: runtime}
  929. container, err := NewBuilder(runtime).Create(
  930. &Config{
  931. Image: GetTestImage(runtime).Id,
  932. Cmd: []string{"/bin/sleep", "1"},
  933. OpenStdin: true,
  934. },
  935. )
  936. if err != nil {
  937. t.Fatal(err)
  938. }
  939. defer runtime.Destroy(container)
  940. if err := container.Start(); err != nil {
  941. t.Fatal(err)
  942. }
  943. setTimeout(t, "Wait timed out", 3*time.Second, func() {
  944. r := httptest.NewRecorder()
  945. if err := postContainersWait(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
  946. t.Fatal(err)
  947. }
  948. apiWait := &ApiWait{}
  949. if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
  950. t.Fatal(err)
  951. }
  952. if apiWait.StatusCode != 0 {
  953. t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
  954. }
  955. })
  956. if container.State.Running {
  957. t.Fatalf("The container should be stopped after wait")
  958. }
  959. }
  960. func TestPostContainersAttach(t *testing.T) {
  961. runtime, err := newTestRuntime()
  962. if err != nil {
  963. t.Fatal(err)
  964. }
  965. defer nuke(runtime)
  966. srv := &Server{runtime: runtime}
  967. container, err := NewBuilder(runtime).Create(
  968. &Config{
  969. Image: GetTestImage(runtime).Id,
  970. Cmd: []string{"/bin/cat"},
  971. OpenStdin: true,
  972. },
  973. )
  974. if err != nil {
  975. t.Fatal(err)
  976. }
  977. defer runtime.Destroy(container)
  978. // Start the process
  979. if err := container.Start(); err != nil {
  980. t.Fatal(err)
  981. }
  982. stdin, stdinPipe := io.Pipe()
  983. stdout, stdoutPipe := io.Pipe()
  984. // Attach to it
  985. c1 := make(chan struct{})
  986. go func() {
  987. defer close(c1)
  988. r := &hijackTester{
  989. ResponseRecorder: httptest.NewRecorder(),
  990. in: stdin,
  991. out: stdoutPipe,
  992. }
  993. req, err := http.NewRequest("POST", "/containers/"+container.Id+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
  994. if err != nil {
  995. t.Fatal(err)
  996. }
  997. if err := postContainersAttach(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  998. t.Fatal(err)
  999. }
  1000. }()
  1001. // Acknowledge hijack
  1002. setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  1003. stdout.Read([]byte{})
  1004. stdout.Read(make([]byte, 4096))
  1005. })
  1006. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  1007. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  1008. t.Fatal(err)
  1009. }
  1010. })
  1011. // Close pipes (client disconnects)
  1012. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  1013. t.Fatal(err)
  1014. }
  1015. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  1016. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  1017. <-c1
  1018. })
  1019. // We closed stdin, expect /bin/cat to still be running
  1020. // Wait a little bit to make sure container.monitor() did his thing
  1021. err = container.WaitTimeout(500 * time.Millisecond)
  1022. if err == nil || !container.State.Running {
  1023. t.Fatalf("/bin/cat is not running after closing stdin")
  1024. }
  1025. // Try to avoid the timeoout in destroy. Best effort, don't check error
  1026. cStdin, _ := container.StdinPipe()
  1027. cStdin.Close()
  1028. container.Wait()
  1029. }
  1030. // FIXME: Test deleting running container
  1031. // FIXME: Test deleting container with volume
  1032. // FIXME: Test deleting volume in use by other container
  1033. func TestDeleteContainers(t *testing.T) {
  1034. runtime, err := newTestRuntime()
  1035. if err != nil {
  1036. t.Fatal(err)
  1037. }
  1038. defer nuke(runtime)
  1039. srv := &Server{runtime: runtime}
  1040. container, err := NewBuilder(runtime).Create(&Config{
  1041. Image: GetTestImage(runtime).Id,
  1042. Cmd: []string{"touch", "/test"},
  1043. })
  1044. if err != nil {
  1045. t.Fatal(err)
  1046. }
  1047. defer runtime.Destroy(container)
  1048. if err := container.Run(); err != nil {
  1049. t.Fatal(err)
  1050. }
  1051. req, err := http.NewRequest("DELETE", "/containers/"+container.Id, nil)
  1052. if err != nil {
  1053. t.Fatal(err)
  1054. }
  1055. r := httptest.NewRecorder()
  1056. if err := deleteContainers(srv, r, req, map[string]string{"name": container.Id}); err != nil {
  1057. t.Fatal(err)
  1058. }
  1059. if r.Code != http.StatusNoContent {
  1060. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  1061. }
  1062. if c := runtime.Get(container.Id); c != nil {
  1063. t.Fatalf("The container as not been deleted")
  1064. }
  1065. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  1066. t.Fatalf("The test file has not been deleted")
  1067. }
  1068. }
  1069. func TestDeleteImages(t *testing.T) {
  1070. //FIXME: Implement this test
  1071. t.Log("Test not implemented")
  1072. }
  1073. // Mocked types for tests
  1074. type NopConn struct {
  1075. io.ReadCloser
  1076. io.Writer
  1077. }
  1078. func (c *NopConn) LocalAddr() net.Addr { return nil }
  1079. func (c *NopConn) RemoteAddr() net.Addr { return nil }
  1080. func (c *NopConn) SetDeadline(t time.Time) error { return nil }
  1081. func (c *NopConn) SetReadDeadline(t time.Time) error { return nil }
  1082. func (c *NopConn) SetWriteDeadline(t time.Time) error { return nil }
  1083. type hijackTester struct {
  1084. *httptest.ResponseRecorder
  1085. in io.ReadCloser
  1086. out io.Writer
  1087. }
  1088. func (t *hijackTester) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  1089. bufrw := bufio.NewReadWriter(bufio.NewReader(t.in), bufio.NewWriter(t.out))
  1090. conn := &NopConn{
  1091. ReadCloser: t.in,
  1092. Writer: t.out,
  1093. }
  1094. return conn, bufrw, nil
  1095. }