userspace_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tcpproxy
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net"
  19. "net/http"
  20. "net/http/httptest"
  21. "net/url"
  22. "testing"
  23. )
  24. func TestUserspaceProxy(t *testing.T) {
  25. l, err := net.Listen("tcp", "127.0.0.1:0")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer l.Close()
  30. want := "hello proxy"
  31. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  32. fmt.Fprint(w, want)
  33. }))
  34. defer ts.Close()
  35. u, err := url.Parse(ts.URL)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. var port uint16
  40. fmt.Sscanf(u.Port(), "%d", &port)
  41. p := TCPProxy{
  42. Listener: l,
  43. Endpoints: []*net.SRV{{Target: u.Hostname(), Port: port}},
  44. }
  45. go p.Run()
  46. defer p.Stop()
  47. u.Host = l.Addr().String()
  48. res, err := http.Get(u.String())
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. got, gerr := ioutil.ReadAll(res.Body)
  53. res.Body.Close()
  54. if gerr != nil {
  55. t.Fatal(gerr)
  56. }
  57. if string(got) != want {
  58. t.Errorf("got = %s, want %s", got, want)
  59. }
  60. }
  61. func TestUserspaceProxyPriority(t *testing.T) {
  62. l, err := net.Listen("tcp", "127.0.0.1:0")
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer l.Close()
  67. backends := []struct {
  68. Payload string
  69. Priority uint16
  70. }{
  71. {"hello proxy 1", 1},
  72. {"hello proxy 2", 2},
  73. {"hello proxy 3", 3},
  74. }
  75. var eps []*net.SRV
  76. var front *url.URL
  77. for _, b := range backends {
  78. backend := b
  79. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  80. fmt.Fprint(w, backend.Payload)
  81. }))
  82. defer ts.Close()
  83. front, err = url.Parse(ts.URL)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. var port uint16
  88. fmt.Sscanf(front.Port(), "%d", &port)
  89. ep := &net.SRV{Target: front.Hostname(), Port: port, Priority: backend.Priority}
  90. eps = append(eps, ep)
  91. }
  92. p := TCPProxy{
  93. Listener: l,
  94. Endpoints: eps,
  95. }
  96. go p.Run()
  97. defer p.Stop()
  98. front.Host = l.Addr().String()
  99. res, err := http.Get(front.String())
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. got, gerr := ioutil.ReadAll(res.Body)
  104. res.Body.Close()
  105. if gerr != nil {
  106. t.Fatal(gerr)
  107. }
  108. want := "hello proxy 1"
  109. if string(got) != want {
  110. t.Errorf("got = %s, want %s", got, want)
  111. }
  112. }