api_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 init() {
  11. // // Make it our Store root
  12. // runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
  13. // if err != nil {
  14. // panic(err)
  15. // }
  16. // // Create the "Server"
  17. // srv := &Server{
  18. // runtime: runtime,
  19. // }
  20. // go ListenAndServe("0.0.0.0:4243", srv, false)
  21. // }
  22. func TestAuth(t *testing.T) {
  23. runtime, err := newTestRuntime()
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. defer nuke(runtime)
  28. srv := &Server{runtime: runtime}
  29. r := httptest.NewRecorder()
  30. authConfig := &auth.AuthConfig{
  31. Username: "utest",
  32. Password: "utest",
  33. Email: "utest@yopmail.com",
  34. }
  35. authConfigJson, err := json.Marshal(authConfig)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. body, err := postAuth(srv, r, req)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. if body == nil {
  48. t.Fatalf("No body received\n")
  49. }
  50. if r.Code != http.StatusOK {
  51. t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
  52. }
  53. authConfig = &auth.AuthConfig{}
  54. req, err = http.NewRequest("GET", "/auth", nil)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. body, err = getAuth(srv, nil, req)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. err = json.Unmarshal(body, authConfig)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if authConfig.Username != "utest" {
  67. t.Errorf("Expected username to be utest, %s found", authConfig.Username)
  68. }
  69. }
  70. func TestVersion(t *testing.T) {
  71. runtime, err := newTestRuntime()
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. defer nuke(runtime)
  76. srv := &Server{runtime: runtime}
  77. body, err := getVersion(srv, nil, nil)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. v := &ApiVersion{}
  82. err = json.Unmarshal(body, v)
  83. if 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 TestImages(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. // FIXME: Do more tests with filter
  98. req, err := http.NewRequest("GET", "/images?quiet=0&all=0", nil)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. body, err := getImages(srv, nil, req)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. images := []ApiImages{}
  107. err = json.Unmarshal(body, &images)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. if len(images) != 1 {
  112. t.Errorf("Excepted 1 image, %d found", len(images))
  113. }
  114. if images[0].Repository != "docker-ut" {
  115. t.Errorf("Excepted image docker-ut, %s found", images[0].Repository)
  116. }
  117. }
  118. func TestInfo(t *testing.T) {
  119. runtime, err := newTestRuntime()
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. defer nuke(runtime)
  124. srv := &Server{runtime: runtime}
  125. body, err := getInfo(srv, nil, nil)
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. infos := &ApiInfo{}
  130. err = json.Unmarshal(body, infos)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if infos.Version != VERSION {
  135. t.Errorf("Excepted version %s, %s found", VERSION, infos.Version)
  136. }
  137. }
  138. // func TestHistory(t *testing.T) {
  139. // runtime, err := newTestRuntime()
  140. // if err != nil {
  141. // t.Fatal(err)
  142. // }
  143. // defer nuke(runtime)
  144. // srv := &Server{runtime: runtime}
  145. // req, err := http.NewRequest("GET", "/images/"+unitTestImageName+"/history", nil)
  146. // if err != nil {
  147. // t.Fatal(err)
  148. // }
  149. // router := mux.NewRouter()
  150. // router.Path("/images/{name:.*}/history")
  151. // vars := mux.Vars(req)
  152. // router.
  153. // vars["name"] = unitTestImageName
  154. // body, err := getImagesHistory(srv, nil, req)
  155. // if err != nil {
  156. // t.Fatal(err)
  157. // }
  158. // var outs []ApiHistory
  159. // err = json.Unmarshal(body, &outs)
  160. // if err != nil {
  161. // t.Fatal(err)
  162. // }
  163. // if len(outs) != 1 {
  164. // t.Errorf("Excepted 1 line, %d found", len(outs))
  165. // }
  166. // }
  167. // func TestImagesSearch(t *testing.T) {
  168. // body, _, err := call("GET", "/images/search?term=redis", nil)
  169. // if err != nil {
  170. // t.Fatal(err)
  171. // }
  172. // var outs []ApiSearch
  173. // err = json.Unmarshal(body, &outs)
  174. // if err != nil {
  175. // t.Fatal(err)
  176. // }
  177. // if len(outs) < 2 {
  178. // t.Errorf("Excepted at least 2 lines, %d found", len(outs))
  179. // }
  180. // }
  181. // func TestGetImage(t *testing.T) {
  182. // obj, _, err := call("GET", "/images/"+unitTestImageName+"/json", nil)
  183. // if err != nil {
  184. // t.Fatal(err)
  185. // }
  186. // var out Image
  187. // err = json.Unmarshal(obj, &out)
  188. // if err != nil {
  189. // t.Fatal(err)
  190. // }
  191. // if out.Comment != "Imported from http://get.docker.io/images/busybox" {
  192. // t.Errorf("Error inspecting image")
  193. // }
  194. // }
  195. // func TestCreateListStartStopRestartKillWaitDelete(t *testing.T) {
  196. // containers := testListContainers(t, -1)
  197. // for _, container := range containers {
  198. // testDeleteContainer(t, container.Id)
  199. // }
  200. // testCreateContainer(t)
  201. // id := testListContainers(t, 1)[0].Id
  202. // testContainerStart(t, id)
  203. // testContainerStop(t, id)
  204. // testContainerRestart(t, id)
  205. // testContainerKill(t, id)
  206. // testContainerWait(t, id)
  207. // testDeleteContainer(t, id)
  208. // testListContainers(t, 0)
  209. // }
  210. // func testCreateContainer(t *testing.T) {
  211. // config, _, err := ParseRun([]string{unitTestImageName, "touch test"}, nil)
  212. // if err != nil {
  213. // t.Fatal(err)
  214. // }
  215. // _, _, err = call("POST", "/containers", *config)
  216. // if err != nil {
  217. // t.Fatal(err)
  218. // }
  219. // }
  220. // func testListContainers(t *testing.T, expected int) []ApiContainers {
  221. // body, _, err := call("GET", "/containers?quiet=1&all=1", nil)
  222. // if err != nil {
  223. // t.Fatal(err)
  224. // }
  225. // var outs []ApiContainers
  226. // err = json.Unmarshal(body, &outs)
  227. // if err != nil {
  228. // t.Fatal(err)
  229. // }
  230. // if expected >= 0 && len(outs) != expected {
  231. // t.Errorf("Excepted %d container, %d found", expected, len(outs))
  232. // }
  233. // return outs
  234. // }
  235. // func testContainerStart(t *testing.T, id string) {
  236. // _, _, err := call("POST", "/containers/"+id+"/start", nil)
  237. // if err != nil {
  238. // t.Fatal(err)
  239. // }
  240. // }
  241. // func testContainerRestart(t *testing.T, id string) {
  242. // _, _, err := call("POST", "/containers/"+id+"/restart?t=1", nil)
  243. // if err != nil {
  244. // t.Fatal(err)
  245. // }
  246. // }
  247. // func testContainerStop(t *testing.T, id string) {
  248. // _, _, err := call("POST", "/containers/"+id+"/stop?t=1", nil)
  249. // if err != nil {
  250. // t.Fatal(err)
  251. // }
  252. // }
  253. // func testContainerKill(t *testing.T, id string) {
  254. // _, _, err := call("POST", "/containers/"+id+"/kill", nil)
  255. // if err != nil {
  256. // t.Fatal(err)
  257. // }
  258. // }
  259. // func testContainerWait(t *testing.T, id string) {
  260. // _, _, err := call("POST", "/containers/"+id+"/wait", nil)
  261. // if err != nil {
  262. // t.Fatal(err)
  263. // }
  264. // }
  265. // func testDeleteContainer(t *testing.T, id string) {
  266. // _, _, err := call("DELETE", "/containers/"+id, nil)
  267. // if err != nil {
  268. // t.Fatal(err)
  269. // }
  270. // }
  271. // func testContainerChanges(t *testing.T, id string) {
  272. // _, _, err := call("GET", "/containers/"+id+"/changes", nil)
  273. // if err != nil {
  274. // t.Fatal(err)
  275. // }
  276. // }