api_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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/utils"
  9. "io"
  10. "net"
  11. "net/http"
  12. "net/http/httptest"
  13. "os"
  14. "path"
  15. "testing"
  16. "time"
  17. )
  18. func TestGetBoolParam(t *testing.T) {
  19. if ret, err := getBoolParam("true"); err != nil || !ret {
  20. t.Fatalf("true -> true, nil | got %t %s", ret, err)
  21. }
  22. if ret, err := getBoolParam("True"); err != nil || !ret {
  23. t.Fatalf("True -> true, nil | got %t %s", ret, err)
  24. }
  25. if ret, err := getBoolParam("1"); err != nil || !ret {
  26. t.Fatalf("1 -> true, nil | got %t %s", ret, err)
  27. }
  28. if ret, err := getBoolParam(""); err != nil || ret {
  29. t.Fatalf("\"\" -> false, nil | got %t %s", ret, err)
  30. }
  31. if ret, err := getBoolParam("false"); err != nil || ret {
  32. t.Fatalf("false -> false, nil | got %t %s", ret, err)
  33. }
  34. if ret, err := getBoolParam("0"); err != nil || ret {
  35. t.Fatalf("0 -> false, nil | got %t %s", ret, err)
  36. }
  37. if ret, err := getBoolParam("faux"); err == nil || ret {
  38. t.Fatalf("faux -> false, err | got %t %s", ret, err)
  39. }
  40. }
  41. func TestPostAuth(t *testing.T) {
  42. runtime, err := newTestRuntime()
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. defer nuke(runtime)
  47. srv := &Server{
  48. runtime: runtime,
  49. }
  50. r := httptest.NewRecorder()
  51. authConfig := &auth.AuthConfig{
  52. Username: "utest",
  53. Password: "utest",
  54. Email: "utest@yopmail.com",
  55. }
  56. authConfigJSON, err := json.Marshal(authConfig)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJSON))
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := postAuth(srv, APIVERSION, r, req, nil); err != nil {
  65. t.Fatal(err)
  66. }
  67. if r.Code != http.StatusOK && r.Code != 0 {
  68. t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
  69. }
  70. }
  71. func TestGetVersion(t *testing.T) {
  72. runtime, err := newTestRuntime()
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. defer nuke(runtime)
  77. srv := &Server{runtime: runtime}
  78. r := httptest.NewRecorder()
  79. if err := getVersion(srv, APIVERSION, r, nil, nil); err != nil {
  80. t.Fatal(err)
  81. }
  82. v := &APIVersion{}
  83. if err = json.Unmarshal(r.Body.Bytes(), v); err != nil {
  84. t.Fatal(err)
  85. }
  86. if v.Version != VERSION {
  87. t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
  88. }
  89. }
  90. func TestGetInfo(t *testing.T) {
  91. runtime, err := newTestRuntime()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. defer nuke(runtime)
  96. srv := &Server{runtime: runtime}
  97. r := httptest.NewRecorder()
  98. if err := getInfo(srv, APIVERSION, r, nil, nil); err != nil {
  99. t.Fatal(err)
  100. }
  101. infos := &APIInfo{}
  102. err = json.Unmarshal(r.Body.Bytes(), infos)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if infos.Images != 1 {
  107. t.Errorf("Excepted images: %d, %d found", 1, infos.Images)
  108. }
  109. }
  110. func TestGetImagesJSON(t *testing.T) {
  111. runtime, err := newTestRuntime()
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. defer nuke(runtime)
  116. srv := &Server{runtime: runtime}
  117. // all=0
  118. req, err := http.NewRequest("GET", "/images/json?all=0", nil)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. r := httptest.NewRecorder()
  123. if err := getImagesJSON(srv, APIVERSION, r, req, nil); err != nil {
  124. t.Fatal(err)
  125. }
  126. images := []APIImages{}
  127. if err := json.Unmarshal(r.Body.Bytes(), &images); err != nil {
  128. t.Fatal(err)
  129. }
  130. if len(images) != 1 {
  131. t.Errorf("Excepted 1 image, %d found", len(images))
  132. }
  133. if images[0].Repository != unitTestImageName {
  134. t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository)
  135. }
  136. r2 := httptest.NewRecorder()
  137. // all=1
  138. req2, err := http.NewRequest("GET", "/images/json?all=true", nil)
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. if err := getImagesJSON(srv, APIVERSION, r2, req2, nil); err != nil {
  143. t.Fatal(err)
  144. }
  145. images2 := []APIImages{}
  146. if err := json.Unmarshal(r2.Body.Bytes(), &images2); err != nil {
  147. t.Fatal(err)
  148. }
  149. if len(images2) != 1 {
  150. t.Errorf("Excepted 1 image, %d found", len(images2))
  151. }
  152. if images2[0].ID != GetTestImage(runtime).ID {
  153. t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ID, images2[0].ID)
  154. }
  155. r3 := httptest.NewRecorder()
  156. // filter=a
  157. req3, err := http.NewRequest("GET", "/images/json?filter=a", nil)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. if err := getImagesJSON(srv, APIVERSION, r3, req3, nil); err != nil {
  162. t.Fatal(err)
  163. }
  164. images3 := []APIImages{}
  165. if err := json.Unmarshal(r3.Body.Bytes(), &images3); err != nil {
  166. t.Fatal(err)
  167. }
  168. if len(images3) != 0 {
  169. t.Errorf("Excepted 1 image, %d found", len(images3))
  170. }
  171. r4 := httptest.NewRecorder()
  172. // all=foobar
  173. req4, err := http.NewRequest("GET", "/images/json?all=foobar", nil)
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. err = getImagesJSON(srv, APIVERSION, r4, req4, nil)
  178. if err == nil {
  179. t.Fatalf("Error expected, received none")
  180. }
  181. httpError(r4, err)
  182. if r4.Code != http.StatusBadRequest {
  183. t.Fatalf("%d Bad Request expected, received %d\n", http.StatusBadRequest, r4.Code)
  184. }
  185. }
  186. func TestGetImagesViz(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. r := httptest.NewRecorder()
  194. if err := getImagesViz(srv, APIVERSION, r, nil, nil); err != nil {
  195. t.Fatal(err)
  196. }
  197. if r.Code != http.StatusOK {
  198. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  199. }
  200. reader := bufio.NewReader(r.Body)
  201. line, err := reader.ReadString('\n')
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. if line != "digraph docker {\n" {
  206. t.Errorf("Excepted digraph docker {\n, %s found", line)
  207. }
  208. }
  209. func TestGetImagesSearch(t *testing.T) {
  210. runtime, err := newTestRuntime()
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. defer nuke(runtime)
  215. srv := &Server{
  216. runtime: runtime,
  217. }
  218. r := httptest.NewRecorder()
  219. req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. if err := getImagesSearch(srv, APIVERSION, r, req, nil); err != nil {
  224. t.Fatal(err)
  225. }
  226. results := []APISearch{}
  227. if err := json.Unmarshal(r.Body.Bytes(), &results); err != nil {
  228. t.Fatal(err)
  229. }
  230. if len(results) < 2 {
  231. t.Errorf("Excepted at least 2 lines, %d found", len(results))
  232. }
  233. }
  234. func TestGetImagesHistory(t *testing.T) {
  235. runtime, err := newTestRuntime()
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. defer nuke(runtime)
  240. srv := &Server{runtime: runtime}
  241. r := httptest.NewRecorder()
  242. if err := getImagesHistory(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
  243. t.Fatal(err)
  244. }
  245. history := []APIHistory{}
  246. if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil {
  247. t.Fatal(err)
  248. }
  249. if len(history) != 1 {
  250. t.Errorf("Excepted 1 line, %d found", len(history))
  251. }
  252. }
  253. func TestGetImagesByName(t *testing.T) {
  254. runtime, err := newTestRuntime()
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. defer nuke(runtime)
  259. srv := &Server{runtime: runtime}
  260. r := httptest.NewRecorder()
  261. if err := getImagesByName(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
  262. t.Fatal(err)
  263. }
  264. img := &Image{}
  265. if err := json.Unmarshal(r.Body.Bytes(), img); err != nil {
  266. t.Fatal(err)
  267. }
  268. if img.ID != GetTestImage(runtime).ID || img.Comment != "Imported from http://get.docker.io/images/busybox" {
  269. t.Errorf("Error inspecting image")
  270. }
  271. }
  272. func TestGetContainersJSON(t *testing.T) {
  273. runtime, err := newTestRuntime()
  274. if err != nil {
  275. t.Fatal(err)
  276. }
  277. defer nuke(runtime)
  278. srv := &Server{runtime: runtime}
  279. container, err := NewBuilder(runtime).Create(&Config{
  280. Image: GetTestImage(runtime).ID,
  281. Cmd: []string{"echo", "test"},
  282. })
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. defer runtime.Destroy(container)
  287. req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. r := httptest.NewRecorder()
  292. if err := getContainersJSON(srv, APIVERSION, r, req, nil); err != nil {
  293. t.Fatal(err)
  294. }
  295. containers := []APIContainers{}
  296. if err := json.Unmarshal(r.Body.Bytes(), &containers); err != nil {
  297. t.Fatal(err)
  298. }
  299. if len(containers) != 1 {
  300. t.Fatalf("Excepted %d container, %d found", 1, len(containers))
  301. }
  302. if containers[0].ID != container.ID {
  303. t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ID, containers[0].ID)
  304. }
  305. }
  306. func TestGetContainersExport(t *testing.T) {
  307. runtime, err := newTestRuntime()
  308. if err != nil {
  309. t.Fatal(err)
  310. }
  311. defer nuke(runtime)
  312. srv := &Server{runtime: runtime}
  313. builder := NewBuilder(runtime)
  314. // Create a container and remove a file
  315. container, err := builder.Create(
  316. &Config{
  317. Image: GetTestImage(runtime).ID,
  318. Cmd: []string{"touch", "/test"},
  319. },
  320. )
  321. if err != nil {
  322. t.Fatal(err)
  323. }
  324. defer runtime.Destroy(container)
  325. if err := container.Run(); err != nil {
  326. t.Fatal(err)
  327. }
  328. r := httptest.NewRecorder()
  329. if err = getContainersExport(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  330. t.Fatal(err)
  331. }
  332. if r.Code != http.StatusOK {
  333. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  334. }
  335. found := false
  336. for tarReader := tar.NewReader(r.Body); ; {
  337. h, err := tarReader.Next()
  338. if err != nil {
  339. if err == io.EOF {
  340. break
  341. }
  342. t.Fatal(err)
  343. }
  344. if h.Name == "./test" {
  345. found = true
  346. break
  347. }
  348. }
  349. if !found {
  350. t.Fatalf("The created test file has not been found in the exported image")
  351. }
  352. }
  353. func TestGetContainersChanges(t *testing.T) {
  354. runtime, err := newTestRuntime()
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. defer nuke(runtime)
  359. srv := &Server{runtime: runtime}
  360. builder := NewBuilder(runtime)
  361. // Create a container and remove a file
  362. container, err := builder.Create(
  363. &Config{
  364. Image: GetTestImage(runtime).ID,
  365. Cmd: []string{"/bin/rm", "/etc/passwd"},
  366. },
  367. )
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. defer runtime.Destroy(container)
  372. if err := container.Run(); err != nil {
  373. t.Fatal(err)
  374. }
  375. r := httptest.NewRecorder()
  376. if err := getContainersChanges(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  377. t.Fatal(err)
  378. }
  379. changes := []Change{}
  380. if err := json.Unmarshal(r.Body.Bytes(), &changes); err != nil {
  381. t.Fatal(err)
  382. }
  383. // Check the changelog
  384. success := false
  385. for _, elem := range changes {
  386. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  387. success = true
  388. }
  389. }
  390. if !success {
  391. t.Fatalf("/etc/passwd as been removed but is not present in the diff")
  392. }
  393. }
  394. func TestGetContainersTop(t *testing.T) {
  395. runtime, err := newTestRuntime()
  396. if err != nil {
  397. t.Fatal(err)
  398. }
  399. defer nuke(runtime)
  400. srv := &Server{runtime: runtime}
  401. builder := NewBuilder(runtime)
  402. container, err := builder.Create(
  403. &Config{
  404. Image: GetTestImage(runtime).ID,
  405. Cmd: []string{"/bin/sh", "-c", "sleep 2"},
  406. },
  407. )
  408. if err != nil {
  409. t.Fatal(err)
  410. }
  411. defer runtime.Destroy(container)
  412. hostConfig := &HostConfig{}
  413. if err := container.Start(hostConfig); err != nil {
  414. t.Fatal(err)
  415. }
  416. // Give some time to the process to start
  417. container.WaitTimeout(500 * time.Millisecond)
  418. if !container.State.Running {
  419. t.Errorf("Container should be running")
  420. }
  421. r := httptest.NewRecorder()
  422. if err := getContainersTop(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  423. t.Fatal(err)
  424. }
  425. procs := []APITop{}
  426. if err := json.Unmarshal(r.Body.Bytes(), &procs); err != nil {
  427. t.Fatal(err)
  428. }
  429. if len(procs) != 2 {
  430. t.Fatalf("Expected 2 processes, found %d.", len(procs))
  431. }
  432. if procs[0].Cmd != "sh" && procs[0].Cmd != "exe" {
  433. t.Fatalf("Expected `sleep` or `sh`, found %s.", procs[0].Cmd)
  434. }
  435. if procs[1].Cmd != "sh" && procs[1].Cmd != "exe" {
  436. t.Fatalf("Expected `sleep` or `sh`, found %s.", procs[1].Cmd)
  437. }
  438. }
  439. func TestGetContainersByName(t *testing.T) {
  440. runtime, err := newTestRuntime()
  441. if err != nil {
  442. t.Fatal(err)
  443. }
  444. defer nuke(runtime)
  445. srv := &Server{runtime: runtime}
  446. builder := NewBuilder(runtime)
  447. // Create a container and remove a file
  448. container, err := builder.Create(
  449. &Config{
  450. Image: GetTestImage(runtime).ID,
  451. Cmd: []string{"echo", "test"},
  452. },
  453. )
  454. if err != nil {
  455. t.Fatal(err)
  456. }
  457. defer runtime.Destroy(container)
  458. r := httptest.NewRecorder()
  459. if err := getContainersByName(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  460. t.Fatal(err)
  461. }
  462. outContainer := &Container{}
  463. if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
  464. t.Fatal(err)
  465. }
  466. if outContainer.ID != container.ID {
  467. t.Fatalf("Wrong containers retrieved. Expected %s, recieved %s", container.ID, outContainer.ID)
  468. }
  469. }
  470. func TestPostCommit(t *testing.T) {
  471. runtime, err := newTestRuntime()
  472. if err != nil {
  473. t.Fatal(err)
  474. }
  475. defer nuke(runtime)
  476. srv := &Server{runtime: runtime}
  477. builder := NewBuilder(runtime)
  478. // Create a container and remove a file
  479. container, err := builder.Create(
  480. &Config{
  481. Image: GetTestImage(runtime).ID,
  482. Cmd: []string{"touch", "/test"},
  483. },
  484. )
  485. if err != nil {
  486. t.Fatal(err)
  487. }
  488. defer runtime.Destroy(container)
  489. if err := container.Run(); err != nil {
  490. t.Fatal(err)
  491. }
  492. req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+container.ID, bytes.NewReader([]byte{}))
  493. if err != nil {
  494. t.Fatal(err)
  495. }
  496. r := httptest.NewRecorder()
  497. if err := postCommit(srv, APIVERSION, r, req, nil); err != nil {
  498. t.Fatal(err)
  499. }
  500. if r.Code != http.StatusCreated {
  501. t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  502. }
  503. apiID := &APIID{}
  504. if err := json.Unmarshal(r.Body.Bytes(), apiID); err != nil {
  505. t.Fatal(err)
  506. }
  507. if _, err := runtime.graph.Get(apiID.ID); err != nil {
  508. t.Fatalf("The image has not been commited")
  509. }
  510. }
  511. func TestPostImagesCreate(t *testing.T) {
  512. // FIXME: Use the staging in order to perform tests
  513. // runtime, err := newTestRuntime()
  514. // if err != nil {
  515. // t.Fatal(err)
  516. // }
  517. // defer nuke(runtime)
  518. // srv := &Server{runtime: runtime}
  519. // stdin, stdinPipe := io.Pipe()
  520. // stdout, stdoutPipe := io.Pipe()
  521. // c1 := make(chan struct{})
  522. // go func() {
  523. // defer close(c1)
  524. // r := &hijackTester{
  525. // ResponseRecorder: httptest.NewRecorder(),
  526. // in: stdin,
  527. // out: stdoutPipe,
  528. // }
  529. // req, err := http.NewRequest("POST", "/images/create?fromImage="+unitTestImageName, bytes.NewReader([]byte{}))
  530. // if err != nil {
  531. // t.Fatal(err)
  532. // }
  533. // body, err := postImagesCreate(srv, r, req, nil)
  534. // if err != nil {
  535. // t.Fatal(err)
  536. // }
  537. // if body != nil {
  538. // t.Fatalf("No body expected, received: %s\n", body)
  539. // }
  540. // }()
  541. // // Acknowledge hijack
  542. // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  543. // stdout.Read([]byte{})
  544. // stdout.Read(make([]byte, 4096))
  545. // })
  546. // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
  547. // reader := bufio.NewReader(stdout)
  548. // line, err := reader.ReadString('\n')
  549. // if err != nil {
  550. // t.Fatal(err)
  551. // }
  552. // if !strings.HasPrefix(line, "Pulling repository d from") {
  553. // t.Fatalf("Expected Pulling repository docker-ut from..., found %s", line)
  554. // }
  555. // })
  556. // // Close pipes (client disconnects)
  557. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  558. // t.Fatal(err)
  559. // }
  560. // // Wait for imagesCreate to finish, the client disconnected, therefore, Create finished his job
  561. // setTimeout(t, "Waiting for imagesCreate timed out", 10*time.Second, func() {
  562. // <-c1
  563. // })
  564. }
  565. func TestPostImagesInsert(t *testing.T) {
  566. // runtime, err := newTestRuntime()
  567. // if err != nil {
  568. // t.Fatal(err)
  569. // }
  570. // defer nuke(runtime)
  571. // srv := &Server{runtime: runtime}
  572. // stdin, stdinPipe := io.Pipe()
  573. // stdout, stdoutPipe := io.Pipe()
  574. // // Attach to it
  575. // c1 := make(chan struct{})
  576. // go func() {
  577. // defer close(c1)
  578. // r := &hijackTester{
  579. // ResponseRecorder: httptest.NewRecorder(),
  580. // in: stdin,
  581. // out: stdoutPipe,
  582. // }
  583. // 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{}))
  584. // if err != nil {
  585. // t.Fatal(err)
  586. // }
  587. // if err := postContainersCreate(srv, r, req, nil); err != nil {
  588. // t.Fatal(err)
  589. // }
  590. // }()
  591. // // Acknowledge hijack
  592. // setTimeout(t, "hijack acknowledge timed out", 5*time.Second, func() {
  593. // stdout.Read([]byte{})
  594. // stdout.Read(make([]byte, 4096))
  595. // })
  596. // id := ""
  597. // setTimeout(t, "Waiting for imagesInsert output", 10*time.Second, func() {
  598. // for {
  599. // reader := bufio.NewReader(stdout)
  600. // id, err = reader.ReadString('\n')
  601. // if err != nil {
  602. // t.Fatal(err)
  603. // }
  604. // }
  605. // })
  606. // // Close pipes (client disconnects)
  607. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  608. // t.Fatal(err)
  609. // }
  610. // // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  611. // setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  612. // <-c1
  613. // })
  614. // img, err := srv.runtime.repositories.LookupImage(id)
  615. // if err != nil {
  616. // t.Fatalf("New image %s expected but not found", id)
  617. // }
  618. // layer, err := img.layer()
  619. // if err != nil {
  620. // t.Fatal(err)
  621. // }
  622. // if _, err := os.Stat(path.Join(layer, "test")); err != nil {
  623. // t.Fatalf("The test file has not been found")
  624. // }
  625. // if err := srv.runtime.graph.Delete(img.ID); err != nil {
  626. // t.Fatal(err)
  627. // }
  628. }
  629. func TestPostImagesPush(t *testing.T) {
  630. //FIXME: Use staging in order to perform tests
  631. // runtime, err := newTestRuntime()
  632. // if err != nil {
  633. // t.Fatal(err)
  634. // }
  635. // defer nuke(runtime)
  636. // srv := &Server{runtime: runtime}
  637. // stdin, stdinPipe := io.Pipe()
  638. // stdout, stdoutPipe := io.Pipe()
  639. // c1 := make(chan struct{})
  640. // go func() {
  641. // r := &hijackTester{
  642. // ResponseRecorder: httptest.NewRecorder(),
  643. // in: stdin,
  644. // out: stdoutPipe,
  645. // }
  646. // req, err := http.NewRequest("POST", "/images/docker-ut/push", bytes.NewReader([]byte{}))
  647. // if err != nil {
  648. // t.Fatal(err)
  649. // }
  650. // body, err := postImagesPush(srv, r, req, map[string]string{"name": "docker-ut"})
  651. // close(c1)
  652. // if err != nil {
  653. // t.Fatal(err)
  654. // }
  655. // if body != nil {
  656. // t.Fatalf("No body expected, received: %s\n", body)
  657. // }
  658. // }()
  659. // // Acknowledge hijack
  660. // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  661. // stdout.Read([]byte{})
  662. // stdout.Read(make([]byte, 4096))
  663. // })
  664. // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
  665. // reader := bufio.NewReader(stdout)
  666. // line, err := reader.ReadString('\n')
  667. // if err != nil {
  668. // t.Fatal(err)
  669. // }
  670. // if !strings.HasPrefix(line, "Processing checksum") {
  671. // t.Fatalf("Processing checksum..., found %s", line)
  672. // }
  673. // })
  674. // // Close pipes (client disconnects)
  675. // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  676. // t.Fatal(err)
  677. // }
  678. // // Wait for imagesPush to finish, the client disconnected, therefore, Push finished his job
  679. // setTimeout(t, "Waiting for imagesPush timed out", 10*time.Second, func() {
  680. // <-c1
  681. // })
  682. }
  683. func TestPostImagesTag(t *testing.T) {
  684. // FIXME: Use staging in order to perform tests
  685. // runtime, err := newTestRuntime()
  686. // if err != nil {
  687. // t.Fatal(err)
  688. // }
  689. // defer nuke(runtime)
  690. // srv := &Server{runtime: runtime}
  691. // r := httptest.NewRecorder()
  692. // req, err := http.NewRequest("POST", "/images/docker-ut/tag?repo=testrepo&tag=testtag", bytes.NewReader([]byte{}))
  693. // if err != nil {
  694. // t.Fatal(err)
  695. // }
  696. // body, err := postImagesTag(srv, r, req, map[string]string{"name": "docker-ut"})
  697. // if err != nil {
  698. // t.Fatal(err)
  699. // }
  700. // if body != nil {
  701. // t.Fatalf("No body expected, received: %s\n", body)
  702. // }
  703. // if r.Code != http.StatusCreated {
  704. // t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  705. // }
  706. }
  707. func TestPostContainersCreate(t *testing.T) {
  708. runtime, err := newTestRuntime()
  709. if err != nil {
  710. t.Fatal(err)
  711. }
  712. defer nuke(runtime)
  713. srv := &Server{runtime: runtime}
  714. configJSON, err := json.Marshal(&Config{
  715. Image: GetTestImage(runtime).ID,
  716. Memory: 33554432,
  717. Cmd: []string{"touch", "/test"},
  718. })
  719. if err != nil {
  720. t.Fatal(err)
  721. }
  722. req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJSON))
  723. if err != nil {
  724. t.Fatal(err)
  725. }
  726. r := httptest.NewRecorder()
  727. if err := postContainersCreate(srv, APIVERSION, r, req, nil); err != nil {
  728. t.Fatal(err)
  729. }
  730. if r.Code != http.StatusCreated {
  731. t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
  732. }
  733. apiRun := &APIRun{}
  734. if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
  735. t.Fatal(err)
  736. }
  737. container := srv.runtime.Get(apiRun.ID)
  738. if container == nil {
  739. t.Fatalf("Container not created")
  740. }
  741. if err := container.Run(); err != nil {
  742. t.Fatal(err)
  743. }
  744. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err != nil {
  745. if os.IsNotExist(err) {
  746. utils.Debugf("Err: %s", err)
  747. t.Fatalf("The test file has not been created")
  748. }
  749. t.Fatal(err)
  750. }
  751. }
  752. func TestPostContainersKill(t *testing.T) {
  753. runtime, err := newTestRuntime()
  754. if err != nil {
  755. t.Fatal(err)
  756. }
  757. defer nuke(runtime)
  758. srv := &Server{runtime: runtime}
  759. container, err := NewBuilder(runtime).Create(
  760. &Config{
  761. Image: GetTestImage(runtime).ID,
  762. Cmd: []string{"/bin/cat"},
  763. OpenStdin: true,
  764. },
  765. )
  766. if err != nil {
  767. t.Fatal(err)
  768. }
  769. defer runtime.Destroy(container)
  770. hostConfig := &HostConfig{}
  771. if err := container.Start(hostConfig); err != nil {
  772. t.Fatal(err)
  773. }
  774. // Give some time to the process to start
  775. container.WaitTimeout(500 * time.Millisecond)
  776. if !container.State.Running {
  777. t.Errorf("Container should be running")
  778. }
  779. r := httptest.NewRecorder()
  780. if err := postContainersKill(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  781. t.Fatal(err)
  782. }
  783. if r.Code != http.StatusNoContent {
  784. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  785. }
  786. if container.State.Running {
  787. t.Fatalf("The container hasn't been killed")
  788. }
  789. }
  790. func TestPostContainersRestart(t *testing.T) {
  791. runtime, err := newTestRuntime()
  792. if err != nil {
  793. t.Fatal(err)
  794. }
  795. defer nuke(runtime)
  796. srv := &Server{runtime: runtime}
  797. container, err := NewBuilder(runtime).Create(
  798. &Config{
  799. Image: GetTestImage(runtime).ID,
  800. Cmd: []string{"/bin/cat"},
  801. OpenStdin: true,
  802. },
  803. )
  804. if err != nil {
  805. t.Fatal(err)
  806. }
  807. defer runtime.Destroy(container)
  808. hostConfig := &HostConfig{}
  809. if err := container.Start(hostConfig); err != nil {
  810. t.Fatal(err)
  811. }
  812. // Give some time to the process to start
  813. container.WaitTimeout(500 * time.Millisecond)
  814. if !container.State.Running {
  815. t.Errorf("Container should be running")
  816. }
  817. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/restart?t=1", bytes.NewReader([]byte{}))
  818. if err != nil {
  819. t.Fatal(err)
  820. }
  821. r := httptest.NewRecorder()
  822. if err := postContainersRestart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  823. t.Fatal(err)
  824. }
  825. if r.Code != http.StatusNoContent {
  826. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  827. }
  828. // Give some time to the process to restart
  829. container.WaitTimeout(500 * time.Millisecond)
  830. if !container.State.Running {
  831. t.Fatalf("Container should be running")
  832. }
  833. if err := container.Kill(); err != nil {
  834. t.Fatal(err)
  835. }
  836. }
  837. func TestPostContainersStart(t *testing.T) {
  838. runtime, err := newTestRuntime()
  839. if err != nil {
  840. t.Fatal(err)
  841. }
  842. defer nuke(runtime)
  843. srv := &Server{runtime: runtime}
  844. container, err := NewBuilder(runtime).Create(
  845. &Config{
  846. Image: GetTestImage(runtime).ID,
  847. Cmd: []string{"/bin/cat"},
  848. OpenStdin: true,
  849. },
  850. )
  851. if err != nil {
  852. t.Fatal(err)
  853. }
  854. defer runtime.Destroy(container)
  855. hostConfigJSON, err := json.Marshal(&HostConfig{})
  856. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/start", bytes.NewReader(hostConfigJSON))
  857. if err != nil {
  858. t.Fatal(err)
  859. }
  860. r := httptest.NewRecorder()
  861. if err := postContainersStart(srv, APIVERSION, r, req, 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, APIVERSION, r, req, 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. hostConfig := &HostConfig{}
  899. if err := container.Start(hostConfig); err != nil {
  900. t.Fatal(err)
  901. }
  902. // Give some time to the process to start
  903. container.WaitTimeout(500 * time.Millisecond)
  904. if !container.State.Running {
  905. t.Errorf("Container should be running")
  906. }
  907. // Note: as it is a POST request, it requires a body.
  908. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/stop?t=1", bytes.NewReader([]byte{}))
  909. if err != nil {
  910. t.Fatal(err)
  911. }
  912. r := httptest.NewRecorder()
  913. if err := postContainersStop(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  914. t.Fatal(err)
  915. }
  916. if r.Code != http.StatusNoContent {
  917. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  918. }
  919. if container.State.Running {
  920. t.Fatalf("The container hasn't been stopped")
  921. }
  922. }
  923. func TestPostContainersWait(t *testing.T) {
  924. runtime, err := newTestRuntime()
  925. if err != nil {
  926. t.Fatal(err)
  927. }
  928. defer nuke(runtime)
  929. srv := &Server{runtime: runtime}
  930. container, err := NewBuilder(runtime).Create(
  931. &Config{
  932. Image: GetTestImage(runtime).ID,
  933. Cmd: []string{"/bin/sleep", "1"},
  934. OpenStdin: true,
  935. },
  936. )
  937. if err != nil {
  938. t.Fatal(err)
  939. }
  940. defer runtime.Destroy(container)
  941. hostConfig := &HostConfig{}
  942. if err := container.Start(hostConfig); err != nil {
  943. t.Fatal(err)
  944. }
  945. setTimeout(t, "Wait timed out", 3*time.Second, func() {
  946. r := httptest.NewRecorder()
  947. if err := postContainersWait(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
  948. t.Fatal(err)
  949. }
  950. apiWait := &APIWait{}
  951. if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
  952. t.Fatal(err)
  953. }
  954. if apiWait.StatusCode != 0 {
  955. t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
  956. }
  957. })
  958. if container.State.Running {
  959. t.Fatalf("The container should be stopped after wait")
  960. }
  961. }
  962. func TestPostContainersAttach(t *testing.T) {
  963. runtime, err := newTestRuntime()
  964. if err != nil {
  965. t.Fatal(err)
  966. }
  967. defer nuke(runtime)
  968. srv := &Server{runtime: runtime}
  969. container, err := NewBuilder(runtime).Create(
  970. &Config{
  971. Image: GetTestImage(runtime).ID,
  972. Cmd: []string{"/bin/cat"},
  973. OpenStdin: true,
  974. },
  975. )
  976. if err != nil {
  977. t.Fatal(err)
  978. }
  979. defer runtime.Destroy(container)
  980. // Start the process
  981. hostConfig := &HostConfig{}
  982. if err := container.Start(hostConfig); err != nil {
  983. t.Fatal(err)
  984. }
  985. stdin, stdinPipe := io.Pipe()
  986. stdout, stdoutPipe := io.Pipe()
  987. // Attach to it
  988. c1 := make(chan struct{})
  989. go func() {
  990. defer close(c1)
  991. r := &hijackTester{
  992. ResponseRecorder: httptest.NewRecorder(),
  993. in: stdin,
  994. out: stdoutPipe,
  995. }
  996. req, err := http.NewRequest("POST", "/containers/"+container.ID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
  997. if err != nil {
  998. t.Fatal(err)
  999. }
  1000. if err := postContainersAttach(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  1001. t.Fatal(err)
  1002. }
  1003. }()
  1004. // Acknowledge hijack
  1005. setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
  1006. stdout.Read([]byte{})
  1007. stdout.Read(make([]byte, 4096))
  1008. })
  1009. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  1010. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  1011. t.Fatal(err)
  1012. }
  1013. })
  1014. // Close pipes (client disconnects)
  1015. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  1016. t.Fatal(err)
  1017. }
  1018. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  1019. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  1020. <-c1
  1021. })
  1022. // We closed stdin, expect /bin/cat to still be running
  1023. // Wait a little bit to make sure container.monitor() did his thing
  1024. err = container.WaitTimeout(500 * time.Millisecond)
  1025. if err == nil || !container.State.Running {
  1026. t.Fatalf("/bin/cat is not running after closing stdin")
  1027. }
  1028. // Try to avoid the timeoout in destroy. Best effort, don't check error
  1029. cStdin, _ := container.StdinPipe()
  1030. cStdin.Close()
  1031. container.Wait()
  1032. }
  1033. // FIXME: Test deleting running container
  1034. // FIXME: Test deleting container with volume
  1035. // FIXME: Test deleting volume in use by other container
  1036. func TestDeleteContainers(t *testing.T) {
  1037. runtime, err := newTestRuntime()
  1038. if err != nil {
  1039. t.Fatal(err)
  1040. }
  1041. defer nuke(runtime)
  1042. srv := &Server{runtime: runtime}
  1043. container, err := NewBuilder(runtime).Create(&Config{
  1044. Image: GetTestImage(runtime).ID,
  1045. Cmd: []string{"touch", "/test"},
  1046. })
  1047. if err != nil {
  1048. t.Fatal(err)
  1049. }
  1050. defer runtime.Destroy(container)
  1051. if err := container.Run(); err != nil {
  1052. t.Fatal(err)
  1053. }
  1054. req, err := http.NewRequest("DELETE", "/containers/"+container.ID, nil)
  1055. if err != nil {
  1056. t.Fatal(err)
  1057. }
  1058. r := httptest.NewRecorder()
  1059. if err := deleteContainers(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
  1060. t.Fatal(err)
  1061. }
  1062. if r.Code != http.StatusNoContent {
  1063. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  1064. }
  1065. if c := runtime.Get(container.ID); c != nil {
  1066. t.Fatalf("The container as not been deleted")
  1067. }
  1068. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  1069. t.Fatalf("The test file has not been deleted")
  1070. }
  1071. }
  1072. func TestOptionsRoute(t *testing.T) {
  1073. runtime, err := newTestRuntime()
  1074. if err != nil {
  1075. t.Fatal(err)
  1076. }
  1077. defer nuke(runtime)
  1078. srv := &Server{runtime: runtime, enableCors: true}
  1079. r := httptest.NewRecorder()
  1080. router, err := createRouter(srv, false)
  1081. if err != nil {
  1082. t.Fatal(err)
  1083. }
  1084. req, err := http.NewRequest("OPTIONS", "/", nil)
  1085. if err != nil {
  1086. t.Fatal(err)
  1087. }
  1088. router.ServeHTTP(r, req)
  1089. if r.Code != http.StatusOK {
  1090. t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
  1091. }
  1092. }
  1093. func TestGetEnabledCors(t *testing.T) {
  1094. runtime, err := newTestRuntime()
  1095. if err != nil {
  1096. t.Fatal(err)
  1097. }
  1098. defer nuke(runtime)
  1099. srv := &Server{runtime: runtime, enableCors: true}
  1100. r := httptest.NewRecorder()
  1101. router, err := createRouter(srv, false)
  1102. if err != nil {
  1103. t.Fatal(err)
  1104. }
  1105. req, err := http.NewRequest("GET", "/version", nil)
  1106. if err != nil {
  1107. t.Fatal(err)
  1108. }
  1109. router.ServeHTTP(r, req)
  1110. if r.Code != http.StatusOK {
  1111. t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
  1112. }
  1113. allowOrigin := r.Header().Get("Access-Control-Allow-Origin")
  1114. allowHeaders := r.Header().Get("Access-Control-Allow-Headers")
  1115. allowMethods := r.Header().Get("Access-Control-Allow-Methods")
  1116. if allowOrigin != "*" {
  1117. t.Errorf("Expected header Access-Control-Allow-Origin to be \"*\", %s found.", allowOrigin)
  1118. }
  1119. if allowHeaders != "Origin, X-Requested-With, Content-Type, Accept" {
  1120. t.Errorf("Expected header Access-Control-Allow-Headers to be \"Origin, X-Requested-With, Content-Type, Accept\", %s found.", allowHeaders)
  1121. }
  1122. if allowMethods != "GET, POST, DELETE, PUT, OPTIONS" {
  1123. t.Errorf("Expected hearder Access-Control-Allow-Methods to be \"GET, POST, DELETE, PUT, OPTIONS\", %s found.", allowMethods)
  1124. }
  1125. }
  1126. func TestDeleteImages(t *testing.T) {
  1127. runtime, err := newTestRuntime()
  1128. if err != nil {
  1129. t.Fatal(err)
  1130. }
  1131. defer nuke(runtime)
  1132. srv := &Server{runtime: runtime}
  1133. if err := srv.runtime.repositories.Set("test", "test", unitTestImageName, true); err != nil {
  1134. t.Fatal(err)
  1135. }
  1136. images, err := srv.Images(false, "")
  1137. if err != nil {
  1138. t.Fatal(err)
  1139. }
  1140. if len(images) != 2 {
  1141. t.Errorf("Excepted 2 images, %d found", len(images))
  1142. }
  1143. req, err := http.NewRequest("DELETE", "/images/test:test", nil)
  1144. if err != nil {
  1145. t.Fatal(err)
  1146. }
  1147. r := httptest.NewRecorder()
  1148. if err := deleteImages(srv, APIVERSION, r, req, map[string]string{"name": "test:test"}); err != nil {
  1149. t.Fatal(err)
  1150. }
  1151. if r.Code != http.StatusOK {
  1152. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  1153. }
  1154. var outs []APIRmi
  1155. if err := json.Unmarshal(r.Body.Bytes(), &outs); err != nil {
  1156. t.Fatal(err)
  1157. }
  1158. if len(outs) != 1 {
  1159. t.Fatalf("Expected %d event (untagged), got %d", 1, len(outs))
  1160. }
  1161. images, err = srv.Images(false, "")
  1162. if err != nil {
  1163. t.Fatal(err)
  1164. }
  1165. if len(images) != 1 {
  1166. t.Errorf("Excepted 1 image, %d found", len(images))
  1167. }
  1168. /* if c := runtime.Get(container.Id); c != nil {
  1169. t.Fatalf("The container as not been deleted")
  1170. }
  1171. if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
  1172. t.Fatalf("The test file has not been deleted")
  1173. } */
  1174. }
  1175. // Mocked types for tests
  1176. type NopConn struct {
  1177. io.ReadCloser
  1178. io.Writer
  1179. }
  1180. func (c *NopConn) LocalAddr() net.Addr { return nil }
  1181. func (c *NopConn) RemoteAddr() net.Addr { return nil }
  1182. func (c *NopConn) SetDeadline(t time.Time) error { return nil }
  1183. func (c *NopConn) SetReadDeadline(t time.Time) error { return nil }
  1184. func (c *NopConn) SetWriteDeadline(t time.Time) error { return nil }
  1185. type hijackTester struct {
  1186. *httptest.ResponseRecorder
  1187. in io.ReadCloser
  1188. out io.Writer
  1189. }
  1190. func (t *hijackTester) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  1191. bufrw := bufio.NewReadWriter(bufio.NewReader(t.in), bufio.NewWriter(t.out))
  1192. conn := &NopConn{
  1193. ReadCloser: t.in,
  1194. Writer: t.out,
  1195. }
  1196. return conn, bufrw, nil
  1197. }