2020-02-06 00:55:26 +00:00
|
|
|
package allregions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"testing/quick"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockAddrs struct {
|
|
|
|
// a set of synthetic SRV records
|
2021-08-06 13:31:22 +00:00
|
|
|
addrMap map[net.SRV][]*EdgeAddr
|
2020-02-06 00:55:26 +00:00
|
|
|
// the total number of addresses, aggregated across addrMap.
|
|
|
|
// For the convenience of test code that would otherwise have to compute
|
|
|
|
// this by hand every time.
|
|
|
|
numAddrs int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockAddrs(port uint16, numRegions uint8, numAddrsPerRegion uint8) mockAddrs {
|
2021-08-06 13:31:22 +00:00
|
|
|
addrMap := make(map[net.SRV][]*EdgeAddr)
|
2020-02-06 00:55:26 +00:00
|
|
|
numAddrs := 0
|
|
|
|
|
|
|
|
for r := uint8(0); r < numRegions; r++ {
|
|
|
|
var (
|
|
|
|
srv = net.SRV{Target: fmt.Sprintf("test-region-%v.example.com", r), Port: port}
|
2021-08-06 13:31:22 +00:00
|
|
|
addrs []*EdgeAddr
|
2020-02-06 00:55:26 +00:00
|
|
|
)
|
|
|
|
for a := uint8(0); a < numAddrsPerRegion; a++ {
|
2021-08-06 13:31:22 +00:00
|
|
|
tcpAddr := &net.TCPAddr{
|
2020-02-06 00:55:26 +00:00
|
|
|
IP: net.ParseIP(fmt.Sprintf("10.0.%v.%v", r, a)),
|
|
|
|
Port: int(port),
|
2021-08-06 13:31:22 +00:00
|
|
|
}
|
|
|
|
udpAddr := &net.UDPAddr{
|
|
|
|
IP: net.ParseIP(fmt.Sprintf("10.0.%v.%v", r, a)),
|
|
|
|
Port: int(port),
|
|
|
|
}
|
2022-06-02 17:57:37 +00:00
|
|
|
addrs = append(addrs, &EdgeAddr{tcpAddr, udpAddr, V4})
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
addrMap[srv] = addrs
|
|
|
|
numAddrs += len(addrs)
|
|
|
|
}
|
|
|
|
return mockAddrs{addrMap: addrMap, numAddrs: numAddrs}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ quick.Generator = mockAddrs{}
|
|
|
|
|
|
|
|
func (mockAddrs) Generate(rand *rand.Rand, size int) reflect.Value {
|
|
|
|
port := uint16(rand.Intn(math.MaxUint16))
|
|
|
|
numRegions := uint8(1 + rand.Intn(10))
|
|
|
|
numAddrsPerRegion := uint8(1 + rand.Intn(32))
|
|
|
|
result := newMockAddrs(port, numRegions, numAddrsPerRegion)
|
|
|
|
return reflect.ValueOf(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a function compatible with net.LookupSRV that will return the SRV
|
|
|
|
// records from mockAddrs.
|
|
|
|
func mockNetLookupSRV(
|
|
|
|
m mockAddrs,
|
|
|
|
) func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
|
|
|
|
var addrs []*net.SRV
|
|
|
|
for k := range m.addrMap {
|
|
|
|
addr := k
|
|
|
|
addrs = append(addrs, &addr)
|
|
|
|
// We can't just do
|
|
|
|
// addrs = append(addrs, &k)
|
|
|
|
// `k` will be reused by subsequent loop iterations,
|
|
|
|
// so all the copies of `&k` would point to the same location.
|
|
|
|
}
|
|
|
|
return func(_, _, _ string) (string, []*net.SRV, error) {
|
|
|
|
return "", addrs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a function compatible with net.LookupIP that translates the SRV records
|
|
|
|
// from mockAddrs into IP addresses, based on the TCP addresses in mockAddrs.
|
|
|
|
func mockNetLookupIP(
|
|
|
|
m mockAddrs,
|
|
|
|
) func(host string) ([]net.IP, error) {
|
|
|
|
return func(host string) ([]net.IP, error) {
|
2021-08-06 13:31:22 +00:00
|
|
|
for srv, addrs := range m.addrMap {
|
2020-02-06 00:55:26 +00:00
|
|
|
if srv.Target != host {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-06 13:31:22 +00:00
|
|
|
result := make([]net.IP, len(addrs))
|
|
|
|
for i, addr := range addrs {
|
|
|
|
result[i] = addr.TCP.IP
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("No IPs for %v", host)
|
|
|
|
}
|
|
|
|
}
|