server_unit_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 TestGetContainersByName(t *testing.T) {
  31. eng := engine.New()
  32. name := "container_name"
  33. var called bool
  34. eng.Register("container_inspect", func(job *engine.Job) error {
  35. called = true
  36. if job.Args[0] != name {
  37. t.Errorf("name != '%s': %#v", name, job.Args[0])
  38. }
  39. if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
  40. t.Errorf("dirty env variable not set")
  41. } else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
  42. t.Errorf("dirty env variable set when it shouldn't")
  43. }
  44. v := &engine.Env{}
  45. v.SetBool("dirty", true)
  46. if _, err := v.WriteTo(job.Stdout); err != nil {
  47. return err
  48. }
  49. return nil
  50. })
  51. r := serveRequest("GET", "/containers/"+name+"/json", nil, eng, t)
  52. if !called {
  53. t.Fatal("handler was not called")
  54. }
  55. assertContentType(r, "application/json", t)
  56. var stdoutJson interface{}
  57. if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
  58. t.Fatalf("%#v", err)
  59. }
  60. if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
  61. t.Fatalf("%#v", stdoutJson)
  62. }
  63. }
  64. func TestGetImagesByName(t *testing.T) {
  65. eng := engine.New()
  66. name := "image_name"
  67. var called bool
  68. eng.Register("image_inspect", func(job *engine.Job) error {
  69. called = true
  70. if job.Args[0] != name {
  71. t.Fatalf("name != '%s': %#v", name, job.Args[0])
  72. }
  73. if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
  74. t.Fatal("dirty env variable not set")
  75. } else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
  76. t.Fatal("dirty env variable set when it shouldn't")
  77. }
  78. v := &engine.Env{}
  79. v.SetBool("dirty", true)
  80. if _, err := v.WriteTo(job.Stdout); err != nil {
  81. return err
  82. }
  83. return nil
  84. })
  85. r := serveRequest("GET", "/images/"+name+"/json", nil, eng, t)
  86. if !called {
  87. t.Fatal("handler was not called")
  88. }
  89. if r.HeaderMap.Get("Content-Type") != "application/json" {
  90. t.Fatalf("%#v\n", r)
  91. }
  92. var stdoutJson interface{}
  93. if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
  94. t.Fatalf("%#v", err)
  95. }
  96. if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
  97. t.Fatalf("%#v", stdoutJson)
  98. }
  99. }
  100. func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  101. return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
  102. }
  103. func serveRequestUsingVersion(method, target string, version version.Version, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  104. r := httptest.NewRecorder()
  105. req, err := http.NewRequest(method, target, body)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. ServeRequest(eng, version, r, req)
  110. return r
  111. }
  112. func readEnv(src io.Reader, t *testing.T) *engine.Env {
  113. out := engine.NewOutput()
  114. v, err := out.AddEnv()
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if _, err := io.Copy(out, src); err != nil {
  119. t.Fatal(err)
  120. }
  121. out.Close()
  122. return v
  123. }
  124. func toJson(data interface{}, t *testing.T) io.Reader {
  125. var buf bytes.Buffer
  126. if err := json.NewEncoder(&buf).Encode(data); err != nil {
  127. t.Fatal(err)
  128. }
  129. return &buf
  130. }
  131. func assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
  132. if recorder.HeaderMap.Get("Content-Type") != contentType {
  133. t.Fatalf("%#v\n", recorder)
  134. }
  135. }
  136. // XXX: Duplicated from integration/utils_test.go, but maybe that's OK as that
  137. // should die as soon as we converted all integration tests?
  138. // assertHttpNotError expect the given response to not have an error.
  139. // Otherwise the it causes the test to fail.
  140. func assertHttpNotError(r *httptest.ResponseRecorder, t *testing.T) {
  141. // Non-error http status are [200, 400)
  142. if r.Code < http.StatusOK || r.Code >= http.StatusBadRequest {
  143. t.Fatal(fmt.Errorf("Unexpected http error: %v", r.Code))
  144. }
  145. }
  146. func createEnvFromGetImagesJSONStruct(data getImagesJSONStruct) types.Image {
  147. return types.Image{
  148. RepoTags: data.RepoTags,
  149. ID: data.Id,
  150. Created: int(data.Created),
  151. Size: int(data.Size),
  152. VirtualSize: int(data.VirtualSize),
  153. }
  154. }
  155. type getImagesJSONStruct struct {
  156. RepoTags []string
  157. Id string
  158. Created int64
  159. Size int64
  160. VirtualSize int64
  161. }
  162. var sampleImage getImagesJSONStruct = getImagesJSONStruct{
  163. RepoTags: []string{"test-name:test-tag"},
  164. Id: "ID",
  165. Created: 999,
  166. Size: 777,
  167. VirtualSize: 666,
  168. }