api_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. package docker
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/dotcloud/docker/auth"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. )
  10. func TestAuth(t *testing.T) {
  11. runtime, err := newTestRuntime()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. defer nuke(runtime)
  16. srv := &Server{runtime: runtime}
  17. r := httptest.NewRecorder()
  18. authConfig := &auth.AuthConfig{
  19. Username: "utest",
  20. Password: "utest",
  21. Email: "utest@yopmail.com",
  22. }
  23. authConfigJson, err := json.Marshal(authConfig)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. body, err := postAuth(srv, r, req, nil)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if body == nil {
  36. t.Fatalf("No body received\n")
  37. }
  38. if r.Code != http.StatusOK {
  39. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  40. }
  41. authConfig = &auth.AuthConfig{}
  42. req, err = http.NewRequest("GET", "/auth", nil)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. body, err = getAuth(srv, nil, req, nil)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. err = json.Unmarshal(body, authConfig)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if authConfig.Username != "utest" {
  55. t.Errorf("Expected username to be utest, %s found", authConfig.Username)
  56. }
  57. }
  58. func TestVersion(t *testing.T) {
  59. runtime, err := newTestRuntime()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. defer nuke(runtime)
  64. srv := &Server{runtime: runtime}
  65. body, err := getVersion(srv, nil, nil, nil)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. v := &ApiVersion{}
  70. err = json.Unmarshal(body, v)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if v.Version != VERSION {
  75. t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
  76. }
  77. }
  78. func TestContainersExport(t *testing.T) {
  79. //FIXME: Implement this test
  80. }
  81. func TestGetImages(t *testing.T) {
  82. runtime, err := newTestRuntime()
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. defer nuke(runtime)
  87. srv := &Server{runtime: runtime}
  88. // FIXME: Do more tests with filter
  89. req, err := http.NewRequest("GET", "/images?quiet=0&all=0", nil)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. body, err := getImages(srv, nil, req, nil)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. images := []ApiImages{}
  98. err = json.Unmarshal(body, &images)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if len(images) != 1 {
  103. t.Errorf("Excepted 1 image, %d found", len(images))
  104. }
  105. if images[0].Repository != "docker-ut" {
  106. t.Errorf("Excepted image docker-ut, %s found", images[0].Repository)
  107. }
  108. }
  109. func TestInfo(t *testing.T) {
  110. runtime, err := newTestRuntime()
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. defer nuke(runtime)
  115. srv := &Server{runtime: runtime}
  116. body, err := getInfo(srv, nil, nil, nil)
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. infos := &ApiInfo{}
  121. err = json.Unmarshal(body, infos)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. if infos.Version != VERSION {
  126. t.Errorf("Excepted version %s, %s found", VERSION, infos.Version)
  127. }
  128. }
  129. func TestHistory(t *testing.T) {
  130. runtime, err := newTestRuntime()
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. defer nuke(runtime)
  135. srv := &Server{runtime: runtime}
  136. body, err := getImagesHistory(srv, nil, nil, map[string]string{"name": unitTestImageName})
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. history := []ApiHistory{}
  141. err = json.Unmarshal(body, &history)
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. if len(history) != 1 {
  146. t.Errorf("Excepted 1 line, %d found", len(history))
  147. }
  148. }
  149. func TestImagesSearch(t *testing.T) {
  150. runtime, err := newTestRuntime()
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. defer nuke(runtime)
  155. srv := &Server{runtime: runtime}
  156. req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. body, err := getImagesSearch(srv, nil, req, nil)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. results := []ApiSearch{}
  165. err = json.Unmarshal(body, &results)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. if len(results) < 2 {
  170. t.Errorf("Excepted at least 2 lines, %d found", len(results))
  171. }
  172. }
  173. func TestGetImage(t *testing.T) {
  174. runtime, err := newTestRuntime()
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. defer nuke(runtime)
  179. srv := &Server{runtime: runtime}
  180. body, err := getImagesByName(srv, nil, nil, map[string]string{"name": unitTestImageName})
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. img := &Image{}
  185. err = json.Unmarshal(body, img)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. if img.Comment != "Imported from http://get.docker.io/images/busybox" {
  190. t.Errorf("Error inspecting image")
  191. }
  192. }
  193. func TestCreateListStartStopRestartKillWaitDelete(t *testing.T) {
  194. runtime, err := newTestRuntime()
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. defer nuke(runtime)
  199. srv := &Server{runtime: runtime}
  200. containers := testListContainers(t, srv, -1)
  201. for _, container := range containers {
  202. testDeleteContainer(t, srv, container.Id)
  203. }
  204. testCreateContainer(t, srv)
  205. id := testListContainers(t, srv, 1)[0].Id
  206. testContainerStart(t, srv, id)
  207. testContainerStop(t, srv, id)
  208. testContainerRestart(t, srv, id)
  209. testContainerKill(t, srv, id)
  210. testContainerWait(t, srv, id)
  211. testDeleteContainer(t, srv, id)
  212. testListContainers(t, srv, 0)
  213. }
  214. func testCreateContainer(t *testing.T, srv *Server) {
  215. r := httptest.NewRecorder()
  216. config, _, err := ParseRun([]string{unitTestImageName, "touch test"}, nil)
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. configJson, err := json.Marshal(config)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. req, err := http.NewRequest("POST", "/containers", bytes.NewReader(configJson))
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. body, err := postContainersCreate(srv, r, req, nil)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. if body == nil {
  233. t.Fatalf("Body expected, received: nil\n")
  234. }
  235. if r.Code != http.StatusCreated {
  236. t.Fatalf("%d Created expected, received %d\n", http.StatusNoContent, r.Code)
  237. }
  238. }
  239. func testListContainers(t *testing.T, srv *Server, expected int) []ApiContainers {
  240. r := httptest.NewRecorder()
  241. req, err := http.NewRequest("GET", "/containers?quiet=1&all=1", nil)
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. body, err := getContainersPs(srv, r, req, nil)
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. var outs []ApiContainers
  250. err = json.Unmarshal(body, &outs)
  251. if err != nil {
  252. t.Fatal(err)
  253. }
  254. if expected >= 0 && len(outs) != expected {
  255. t.Errorf("Excepted %d container, %d found", expected, len(outs))
  256. }
  257. return outs
  258. }
  259. func testContainerStart(t *testing.T, srv *Server, id string) {
  260. r := httptest.NewRecorder()
  261. req, err := http.NewRequest("POST", "/containers/"+id+"/start", nil)
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. body, err := postContainersStart(srv, r, req, nil)
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. if body != nil {
  270. t.Fatalf("No body expected, received: %s\n", body)
  271. }
  272. if r.Code != http.StatusNoContent {
  273. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  274. }
  275. }
  276. func testContainerRestart(t *testing.T, srv *Server, id string) {
  277. r := httptest.NewRecorder()
  278. req, err := http.NewRequest("POST", "/containers/"+id+"/restart?t=1", nil)
  279. if err != nil {
  280. t.Fatal(err)
  281. }
  282. body, err := postContainersRestart(srv, r, req, nil)
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. if body != nil {
  287. t.Fatalf("No body expected, received: %s\n", body)
  288. }
  289. if r.Code != http.StatusNoContent {
  290. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  291. }
  292. }
  293. func testContainerStop(t *testing.T, srv *Server, id string) {
  294. r := httptest.NewRecorder()
  295. req, err := http.NewRequest("POST", "/containers/"+id+"/stop?t=1", nil)
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. body, err := postContainersStop(srv, r, req, nil)
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. if body != nil {
  304. t.Fatalf("No body expected, received: %s\n", body)
  305. }
  306. if r.Code != http.StatusNoContent {
  307. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  308. }
  309. }
  310. func testContainerKill(t *testing.T, srv *Server, id string) {
  311. r := httptest.NewRecorder()
  312. req, err := http.NewRequest("POST", "/containers/"+id+"/kill", nil)
  313. if err != nil {
  314. t.Fatal(err)
  315. }
  316. body, err := postContainersKill(srv, r, req, nil)
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. if body != nil {
  321. t.Fatalf("No body expected, received: %s\n", body)
  322. }
  323. if r.Code != http.StatusNoContent {
  324. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  325. }
  326. }
  327. func testContainerWait(t *testing.T, srv *Server, id string) {
  328. r := httptest.NewRecorder()
  329. req, err := http.NewRequest("POST", "/containers/"+id+"/wait", nil)
  330. req.Header.Set("Content-Type", "plain/text")
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. body, err := postContainersWait(srv, r, req, nil)
  335. if err != nil {
  336. t.Fatal(err)
  337. }
  338. if body == nil {
  339. t.Fatalf("Body expected, received: nil\n")
  340. }
  341. if r.Code != http.StatusOK {
  342. t.Fatalf("%d OK expected, received %d\n", http.StatusNoContent, r.Code)
  343. }
  344. }
  345. func testDeleteContainer(t *testing.T, srv *Server, id string) {
  346. r := httptest.NewRecorder()
  347. req, err := http.NewRequest("DELETE", "/containers/"+id, nil)
  348. if err != nil {
  349. t.Fatal(err)
  350. }
  351. body, err := deleteContainers(srv, r, req, nil)
  352. if err != nil {
  353. t.Fatal(err)
  354. }
  355. if body != nil {
  356. t.Fatalf("No body expected, received: %s\n", body)
  357. }
  358. if r.Code != http.StatusNoContent {
  359. t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
  360. }
  361. }
  362. func testContainerChanges(t *testing.T, srv *Server, id string) {
  363. r := httptest.NewRecorder()
  364. req, err := http.NewRequest("GET", "/containers/"+id+"/changes", nil)
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. body, err := getContainersChanges(srv, r, req, nil)
  369. if err != nil {
  370. t.Fatal(err)
  371. }
  372. if body == nil {
  373. t.Fatalf("Body expected, received: nil\n")
  374. }
  375. if r.Code != http.StatusOK {
  376. t.Fatalf("%d OK expected, received %d\n", http.StatusNoContent, r.Code)
  377. }
  378. }