api_test.go 32 KB

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