mocks_for_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package allregions
  2. import (
  3. "fmt"
  4. "math"
  5. "math/rand"
  6. "net"
  7. "reflect"
  8. "testing/quick"
  9. )
  10. var (
  11. v4Addrs = []*EdgeAddr{&addr0, &addr1, &addr2, &addr3}
  12. v6Addrs = []*EdgeAddr{&addr4, &addr5, &addr6, &addr7}
  13. addr0 = EdgeAddr{
  14. TCP: &net.TCPAddr{
  15. IP: net.ParseIP("123.4.5.0"),
  16. Port: 8000,
  17. Zone: "",
  18. },
  19. UDP: &net.UDPAddr{
  20. IP: net.ParseIP("123.4.5.0"),
  21. Port: 8000,
  22. Zone: "",
  23. },
  24. IPVersion: V4,
  25. }
  26. addr1 = EdgeAddr{
  27. TCP: &net.TCPAddr{
  28. IP: net.ParseIP("123.4.5.1"),
  29. Port: 8000,
  30. Zone: "",
  31. },
  32. UDP: &net.UDPAddr{
  33. IP: net.ParseIP("123.4.5.1"),
  34. Port: 8000,
  35. Zone: "",
  36. },
  37. IPVersion: V4,
  38. }
  39. addr2 = EdgeAddr{
  40. TCP: &net.TCPAddr{
  41. IP: net.ParseIP("123.4.5.2"),
  42. Port: 8000,
  43. Zone: "",
  44. },
  45. UDP: &net.UDPAddr{
  46. IP: net.ParseIP("123.4.5.2"),
  47. Port: 8000,
  48. Zone: "",
  49. },
  50. IPVersion: V4,
  51. }
  52. addr3 = EdgeAddr{
  53. TCP: &net.TCPAddr{
  54. IP: net.ParseIP("123.4.5.3"),
  55. Port: 8000,
  56. Zone: "",
  57. },
  58. UDP: &net.UDPAddr{
  59. IP: net.ParseIP("123.4.5.3"),
  60. Port: 8000,
  61. Zone: "",
  62. },
  63. IPVersion: V4,
  64. }
  65. addr4 = EdgeAddr{
  66. TCP: &net.TCPAddr{
  67. IP: net.ParseIP("2606:4700:a0::1"),
  68. Port: 8000,
  69. Zone: "",
  70. },
  71. UDP: &net.UDPAddr{
  72. IP: net.ParseIP("2606:4700:a0::1"),
  73. Port: 8000,
  74. Zone: "",
  75. },
  76. IPVersion: V6,
  77. }
  78. addr5 = EdgeAddr{
  79. TCP: &net.TCPAddr{
  80. IP: net.ParseIP("2606:4700:a0::2"),
  81. Port: 8000,
  82. Zone: "",
  83. },
  84. UDP: &net.UDPAddr{
  85. IP: net.ParseIP("2606:4700:a0::2"),
  86. Port: 8000,
  87. Zone: "",
  88. },
  89. IPVersion: V6,
  90. }
  91. addr6 = EdgeAddr{
  92. TCP: &net.TCPAddr{
  93. IP: net.ParseIP("2606:4700:a0::3"),
  94. Port: 8000,
  95. Zone: "",
  96. },
  97. UDP: &net.UDPAddr{
  98. IP: net.ParseIP("2606:4700:a0::3"),
  99. Port: 8000,
  100. Zone: "",
  101. },
  102. IPVersion: V6,
  103. }
  104. addr7 = EdgeAddr{
  105. TCP: &net.TCPAddr{
  106. IP: net.ParseIP("2606:4700:a0::4"),
  107. Port: 8000,
  108. Zone: "",
  109. },
  110. UDP: &net.UDPAddr{
  111. IP: net.ParseIP("2606:4700:a0::4"),
  112. Port: 8000,
  113. Zone: "",
  114. },
  115. IPVersion: V6,
  116. }
  117. )
  118. type mockAddrs struct {
  119. // a set of synthetic SRV records
  120. addrMap map[net.SRV][]*EdgeAddr
  121. // the total number of addresses, aggregated across addrMap.
  122. // For the convenience of test code that would otherwise have to compute
  123. // this by hand every time.
  124. numAddrs int
  125. }
  126. func newMockAddrs(port uint16, numRegions uint8, numAddrsPerRegion uint8) mockAddrs {
  127. addrMap := make(map[net.SRV][]*EdgeAddr)
  128. numAddrs := 0
  129. for r := uint8(0); r < numRegions; r++ {
  130. var (
  131. srv = net.SRV{Target: fmt.Sprintf("test-region-%v.example.com", r), Port: port}
  132. addrs []*EdgeAddr
  133. )
  134. for a := uint8(0); a < numAddrsPerRegion; a++ {
  135. tcpAddr := &net.TCPAddr{
  136. IP: net.ParseIP(fmt.Sprintf("10.0.%v.%v", r, a)),
  137. Port: int(port),
  138. }
  139. udpAddr := &net.UDPAddr{
  140. IP: net.ParseIP(fmt.Sprintf("10.0.%v.%v", r, a)),
  141. Port: int(port),
  142. }
  143. addrs = append(addrs, &EdgeAddr{tcpAddr, udpAddr, V4})
  144. }
  145. addrMap[srv] = addrs
  146. numAddrs += len(addrs)
  147. }
  148. return mockAddrs{addrMap: addrMap, numAddrs: numAddrs}
  149. }
  150. var _ quick.Generator = mockAddrs{}
  151. func (mockAddrs) Generate(rand *rand.Rand, size int) reflect.Value {
  152. port := uint16(rand.Intn(math.MaxUint16))
  153. numRegions := uint8(1 + rand.Intn(10))
  154. numAddrsPerRegion := uint8(1 + rand.Intn(32))
  155. result := newMockAddrs(port, numRegions, numAddrsPerRegion)
  156. return reflect.ValueOf(result)
  157. }
  158. // Returns a function compatible with net.LookupSRV that will return the SRV
  159. // records from mockAddrs.
  160. func mockNetLookupSRV(
  161. m mockAddrs,
  162. ) func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
  163. var addrs []*net.SRV
  164. for k := range m.addrMap {
  165. addr := k
  166. addrs = append(addrs, &addr)
  167. // We can't just do
  168. // addrs = append(addrs, &k)
  169. // `k` will be reused by subsequent loop iterations,
  170. // so all the copies of `&k` would point to the same location.
  171. }
  172. return func(_, _, _ string) (string, []*net.SRV, error) {
  173. return "", addrs, nil
  174. }
  175. }
  176. // Returns a function compatible with net.LookupIP that translates the SRV records
  177. // from mockAddrs into IP addresses, based on the TCP addresses in mockAddrs.
  178. func mockNetLookupIP(
  179. m mockAddrs,
  180. ) func(host string) ([]net.IP, error) {
  181. return func(host string) ([]net.IP, error) {
  182. for srv, addrs := range m.addrMap {
  183. if srv.Target != host {
  184. continue
  185. }
  186. result := make([]net.IP, len(addrs))
  187. for i, addr := range addrs {
  188. result[i] = addr.TCP.IP
  189. }
  190. return result, nil
  191. }
  192. return nil, fmt.Errorf("No IPs for %v", host)
  193. }
  194. }