server_unit_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "github.com/docker/docker/api"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/engine"
  13. "github.com/docker/docker/pkg/version"
  14. )
  15. func TesthttpError(t *testing.T) {
  16. r := httptest.NewRecorder()
  17. httpError(r, fmt.Errorf("No such method"))
  18. if r.Code != http.StatusNotFound {
  19. t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
  20. }
  21. httpError(r, fmt.Errorf("This accound hasn't been activated"))
  22. if r.Code != http.StatusForbidden {
  23. t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
  24. }
  25. httpError(r, fmt.Errorf("Some error"))
  26. if r.Code != http.StatusInternalServerError {
  27. t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
  28. }
  29. }
  30. func TestGetInfo(t *testing.T) {
  31. eng := engine.New()
  32. var called bool
  33. eng.Register("info", func(job *engine.Job) error {
  34. called = true
  35. v := &engine.Env{}
  36. v.SetInt("Containers", 1)
  37. v.SetInt("Images", 42000)
  38. if _, err := v.WriteTo(job.Stdout); err != nil {
  39. return err
  40. }
  41. return nil
  42. })
  43. r := serveRequest("GET", "/info", nil, eng, t)
  44. if !called {
  45. t.Fatalf("handler was not called")
  46. }
  47. v := readEnv(r.Body, t)
  48. if v.GetInt("Images") != 42000 {
  49. t.Fatalf("%#v\n", v)
  50. }
  51. if v.GetInt("Containers") != 1 {
  52. t.Fatalf("%#v\n", v)
  53. }
  54. assertContentType(r, "application/json", t)
  55. }
  56. func TestGetContainersByName(t *testing.T) {
  57. eng := engine.New()
  58. name := "container_name"
  59. var called bool
  60. eng.Register("container_inspect", func(job *engine.Job) error {
  61. called = true
  62. if job.Args[0] != name {
  63. t.Errorf("name != '%s': %#v", name, job.Args[0])
  64. }
  65. if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
  66. t.Errorf("dirty env variable not set")
  67. } else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
  68. t.Errorf("dirty env variable set when it shouldn't")
  69. }
  70. v := &engine.Env{}
  71. v.SetBool("dirty", true)
  72. if _, err := v.WriteTo(job.Stdout); err != nil {
  73. return err
  74. }
  75. return nil
  76. })
  77. r := serveRequest("GET", "/containers/"+name+"/json", nil, eng, t)
  78. if !called {
  79. t.Fatal("handler was not called")
  80. }
  81. assertContentType(r, "application/json", t)
  82. var stdoutJson interface{}
  83. if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
  84. t.Fatalf("%#v", err)
  85. }
  86. if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
  87. t.Fatalf("%#v", stdoutJson)
  88. }
  89. }
  90. func TestGetImagesByName(t *testing.T) {
  91. eng := engine.New()
  92. name := "image_name"
  93. var called bool
  94. eng.Register("image_inspect", func(job *engine.Job) error {
  95. called = true
  96. if job.Args[0] != name {
  97. t.Fatalf("name != '%s': %#v", name, job.Args[0])
  98. }
  99. if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
  100. t.Fatal("dirty env variable not set")
  101. } else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
  102. t.Fatal("dirty env variable set when it shouldn't")
  103. }
  104. v := &engine.Env{}
  105. v.SetBool("dirty", true)
  106. if _, err := v.WriteTo(job.Stdout); err != nil {
  107. return err
  108. }
  109. return nil
  110. })
  111. r := serveRequest("GET", "/images/"+name+"/json", nil, eng, t)
  112. if !called {
  113. t.Fatal("handler was not called")
  114. }
  115. if r.HeaderMap.Get("Content-Type") != "application/json" {
  116. t.Fatalf("%#v\n", r)
  117. }
  118. var stdoutJson interface{}
  119. if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
  120. t.Fatalf("%#v", err)
  121. }
  122. if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
  123. t.Fatalf("%#v", stdoutJson)
  124. }
  125. }
  126. func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  127. return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
  128. }
  129. func serveRequestUsingVersion(method, target string, version version.Version, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  130. r := httptest.NewRecorder()
  131. req, err := http.NewRequest(method, target, body)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. ServeRequest(eng, version, r, req)
  136. return r
  137. }
  138. func readEnv(src io.Reader, t *testing.T) *engine.Env {
  139. out := engine.NewOutput()
  140. v, err := out.AddEnv()
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if _, err := io.Copy(out, src); err != nil {
  145. t.Fatal(err)
  146. }
  147. out.Close()
  148. return v
  149. }
  150. func toJson(data interface{}, t *testing.T) io.Reader {
  151. var buf bytes.Buffer
  152. if err := json.NewEncoder(&buf).Encode(data); err != nil {
  153. t.Fatal(err)
  154. }
  155. return &buf
  156. }
  157. func assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
  158. if recorder.HeaderMap.Get("Content-Type") != contentType {
  159. t.Fatalf("%#v\n", recorder)
  160. }
  161. }
  162. // XXX: Duplicated from integration/utils_test.go, but maybe that's OK as that
  163. // should die as soon as we converted all integration tests?
  164. // assertHttpNotError expect the given response to not have an error.
  165. // Otherwise the it causes the test to fail.
  166. func assertHttpNotError(r *httptest.ResponseRecorder, t *testing.T) {
  167. // Non-error http status are [200, 400)
  168. if r.Code < http.StatusOK || r.Code >= http.StatusBadRequest {
  169. t.Fatal(fmt.Errorf("Unexpected http error: %v", r.Code))
  170. }
  171. }
  172. func createEnvFromGetImagesJSONStruct(data getImagesJSONStruct) types.Image {
  173. return types.Image{
  174. RepoTags: data.RepoTags,
  175. ID: data.Id,
  176. Created: int(data.Created),
  177. Size: int(data.Size),
  178. VirtualSize: int(data.VirtualSize),
  179. }
  180. }
  181. type getImagesJSONStruct struct {
  182. RepoTags []string
  183. Id string
  184. Created int64
  185. Size int64
  186. VirtualSize int64
  187. }
  188. var sampleImage getImagesJSONStruct = getImagesJSONStruct{
  189. RepoTags: []string{"test-name:test-tag"},
  190. Id: "ID",
  191. Created: 999,
  192. Size: 777,
  193. VirtualSize: 666,
  194. }