graceful_shutdown_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package master
  14. import (
  15. "io"
  16. "io/ioutil"
  17. "net/http"
  18. "sync"
  19. "testing"
  20. "time"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. "k8s.io/client-go/rest"
  23. kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
  24. "k8s.io/kubernetes/test/integration/framework"
  25. )
  26. func TestGracefulShutdown(t *testing.T) {
  27. server := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, framework.SharedEtcd())
  28. tearDownOnce := sync.Once{}
  29. defer tearDownOnce.Do(server.TearDownFn)
  30. transport, err := rest.TransportFor(server.ClientConfig)
  31. if err != nil {
  32. t.Fatalf("unexpected error: %v", err)
  33. }
  34. client := http.Client{Transport: transport}
  35. req, body, err := newBlockingRequest("POST", server.ClientConfig.Host+"/api/v1/namespaces")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. respErrCh := backgroundRoundtrip(transport, req)
  40. t.Logf("server should be blocking request for data in body")
  41. time.Sleep(time.Millisecond * 500)
  42. select {
  43. case respErr := <-respErrCh:
  44. if respErr.err != nil {
  45. t.Fatalf("unexpected error: %v", err)
  46. }
  47. bs, err := ioutil.ReadAll(respErr.resp.Body)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. t.Fatalf("unexpected server answer: %d, body: %s", respErr.resp.StatusCode, string(bs))
  52. default:
  53. }
  54. t.Logf("server should answer")
  55. resp, err := client.Get(server.ClientConfig.Host + "/")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. resp.Body.Close()
  60. t.Logf("shutting down server")
  61. tearDownOnce.Do(server.TearDownFn)
  62. t.Logf("server should fail new requests")
  63. if err := wait.Poll(time.Millisecond*100, wait.ForeverTestTimeout, func() (done bool, err error) {
  64. resp, err := client.Get(server.ClientConfig.Host + "/")
  65. if err != nil {
  66. return true, nil
  67. }
  68. resp.Body.Close()
  69. return false, nil
  70. }); err != nil {
  71. t.Fatalf("server did not shutdown")
  72. }
  73. t.Logf("server should answer pending request")
  74. time.Sleep(time.Millisecond * 500)
  75. if _, err := body.Write([]byte("garbage")); err != nil {
  76. t.Fatal(err)
  77. }
  78. body.Close()
  79. respErr := <-respErrCh
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. defer respErr.resp.Body.Close()
  84. bs, err := ioutil.ReadAll(respErr.resp.Body)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. t.Logf("response: code %d, body: %s", respErr.resp.StatusCode, string(bs))
  89. }
  90. type responseErrorPair struct {
  91. resp *http.Response
  92. err error
  93. }
  94. func backgroundRoundtrip(transport http.RoundTripper, req *http.Request) <-chan responseErrorPair {
  95. ch := make(chan responseErrorPair)
  96. go func() {
  97. resp, err := transport.RoundTrip(req)
  98. ch <- responseErrorPair{resp, err}
  99. }()
  100. return ch
  101. }
  102. func newBlockingRequest(method, url string) (*http.Request, io.WriteCloser, error) {
  103. bodyReader, bodyWriter := io.Pipe()
  104. req, err := http.NewRequest(method, url, bodyReader)
  105. return req, bodyWriter, err
  106. }