server_unit_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. httpError(r, fmt.Errorf("This accound hasn't been activated"))
  20. if r.Code != http.StatusForbidden {
  21. t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
  22. }
  23. httpError(r, fmt.Errorf("Some error"))
  24. if r.Code != http.StatusInternalServerError {
  25. t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
  26. }
  27. }
  28. func TestGetContainersByName(t *testing.T) {
  29. eng := engine.New()
  30. name := "container_name"
  31. var called bool
  32. eng.Register("container_inspect", func(job *engine.Job) error {
  33. called = true
  34. if job.Args[0] != name {
  35. t.Errorf("name != '%s': %#v", name, job.Args[0])
  36. }
  37. if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
  38. t.Errorf("dirty env variable not set")
  39. } else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
  40. t.Errorf("dirty env variable set when it shouldn't")
  41. }
  42. v := &engine.Env{}
  43. v.SetBool("dirty", true)
  44. if _, err := v.WriteTo(job.Stdout); err != nil {
  45. return err
  46. }
  47. return nil
  48. })
  49. r := serveRequest("GET", "/containers/"+name+"/json", nil, eng, t)
  50. if !called {
  51. t.Fatal("handler was not called")
  52. }
  53. assertContentType(r, "application/json", t)
  54. var stdoutJson interface{}
  55. if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
  56. t.Fatalf("%#v", err)
  57. }
  58. if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
  59. t.Fatalf("%#v", stdoutJson)
  60. }
  61. }
  62. func TestGetImagesByName(t *testing.T) {
  63. eng := engine.New()
  64. name := "image_name"
  65. var called bool
  66. eng.Register("image_inspect", func(job *engine.Job) error {
  67. called = true
  68. if job.Args[0] != name {
  69. t.Fatalf("name != '%s': %#v", name, job.Args[0])
  70. }
  71. if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
  72. t.Fatal("dirty env variable not set")
  73. } else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
  74. t.Fatal("dirty env variable set when it shouldn't")
  75. }
  76. v := &engine.Env{}
  77. v.SetBool("dirty", true)
  78. if _, err := v.WriteTo(job.Stdout); err != nil {
  79. return err
  80. }
  81. return nil
  82. })
  83. r := serveRequest("GET", "/images/"+name+"/json", nil, eng, t)
  84. if !called {
  85. t.Fatal("handler was not called")
  86. }
  87. if r.HeaderMap.Get("Content-Type") != "application/json" {
  88. t.Fatalf("%#v\n", r)
  89. }
  90. var stdoutJson interface{}
  91. if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
  92. t.Fatalf("%#v", err)
  93. }
  94. if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
  95. t.Fatalf("%#v", stdoutJson)
  96. }
  97. }
  98. func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  99. return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
  100. }
  101. func serveRequestUsingVersion(method, target string, version version.Version, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  102. r := httptest.NewRecorder()
  103. req, err := http.NewRequest(method, target, body)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. ServeRequest(eng, version, r, req)
  108. return r
  109. }
  110. func readEnv(src io.Reader, t *testing.T) *engine.Env {
  111. out := engine.NewOutput()
  112. v, err := out.AddEnv()
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if _, err := io.Copy(out, src); err != nil {
  117. t.Fatal(err)
  118. }
  119. out.Close()
  120. return v
  121. }
  122. func assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
  123. if recorder.HeaderMap.Get("Content-Type") != contentType {
  124. t.Fatalf("%#v\n", recorder)
  125. }
  126. }