/
local_agent.go
58 lines (43 loc) · 1.5 KB
/
local_agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package integration
import (
"fmt"
"net"
"github.com/cloudfoundry/pcap-release/src/pcap"
. "github.com/onsi/ginkgo/v2" //nolint:revive,stylecheck // this is the common way to import ginkgo and gomega
"go.uber.org/zap"
)
var TestNodeIndex = 0
// localNodeListener gives each mock gorouter a separate local IP address, so gorouters can be distinguished based on their IP address in tests.
func localNodeListener(port int) net.Listener {
// exclude .0 and .255 as they may have special meaning.
node := (TestNodeIndex % 254) + 1 //nolint:gomnd // default IP range
subnet := TestNodeIndex / 254 //nolint:gomnd // default IP range
TestNodeIndex++
address := fmt.Sprintf("127.0.%d.%d:%d", subnet, node, port)
listener, err := net.Listen("tcp", address)
if err != nil {
GinkgoWriter.Printf("Fatal: Could not bind to address %s: %v", address, err)
}
return listener
}
func NewLocalResolver(manualEndpoints []pcap.AgentEndpoint) LocalResolver {
return LocalResolver{manualEndpoints: manualEndpoints}
}
type LocalResolver struct {
manualEndpoints []pcap.AgentEndpoint
}
func (l LocalResolver) Healthy() bool {
return true
}
func (l LocalResolver) Name() string {
return "local"
}
func (l LocalResolver) CanResolve(request *pcap.EndpointRequest) bool {
return request.GetRequest() != nil
}
func (l LocalResolver) Resolve(_ *pcap.EndpointRequest, _ *zap.Logger) ([]pcap.AgentEndpoint, error) {
if l.manualEndpoints != nil {
return l.manualEndpoints, nil
}
return nil, fmt.Errorf("no endpoints configured")
}