server_unit_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/docker/docker/api"
  10. "github.com/docker/docker/engine"
  11. "github.com/docker/docker/pkg/version"
  12. )
  13. func TestHttpError(t *testing.T) {
  14. r := httptest.NewRecorder()
  15. httpError(r, fmt.Errorf("No such method"))
  16. if r.Code != http.StatusNotFound {
  17. t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
  18. }
  19. r = httptest.NewRecorder()
  20. httpError(r, fmt.Errorf("This accound hasn't been activated"))
  21. if r.Code != http.StatusForbidden {
  22. t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
  23. }
  24. r = httptest.NewRecorder()
  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 assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
  125. if recorder.HeaderMap.Get("Content-Type") != contentType {
  126. t.Fatalf("%#v\n", recorder)
  127. }
  128. }