This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
mocks_test.go
121 lines (100 loc) · 2.83 KB
/
mocks_test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package emissary
import (
"context"
"encoding/json"
"testing"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
xds "github.com/envoyproxy/go-control-plane/envoy/api/v2"
consul "github.com/segmentio/consul-go"
"github.com/segmentio/errors-go"
"google.golang.org/grpc/metadata"
)
type serviceAddr string
func (serviceAddr) Network() string { return "" }
func (a serviceAddr) String() string { return string(a) }
type service struct {
Address string
Port int
}
type mockEndpointServer struct {
t *testing.T
typeUrl string
resourceNames []string
lastResponse *xds.DiscoveryResponse
responseHandler func(t *testing.T, response *xds.DiscoveryResponse)
}
func (m *mockEndpointServer) Send(response *xds.DiscoveryResponse) error {
m.lastResponse = response
if m.responseHandler != nil {
m.responseHandler(m.t, response)
}
return nil
}
func (m *mockEndpointServer) Recv() (*xds.DiscoveryRequest, error) {
if m.lastResponse == nil {
return &xds.DiscoveryRequest{TypeUrl: m.typeUrl, ResourceNames: m.resourceNames}, nil
}
return &xds.DiscoveryRequest{
TypeUrl: m.typeUrl,
ResourceNames: m.resourceNames,
VersionInfo: m.lastResponse.VersionInfo,
ResponseNonce: m.lastResponse.Nonce,
}, errors.New("leaving now")
}
func (m *mockEndpointServer) SetHeader(metadata.MD) error {
panic("implement me")
}
func (m *mockEndpointServer) SendHeader(metadata.MD) error {
panic("implement me")
}
func (m *mockEndpointServer) SetTrailer(metadata.MD) {
panic("implement me")
}
func (m *mockEndpointServer) Context() context.Context {
panic("implement me")
}
func (me *mockEndpointServer) SendMsg(m interface{}) error {
panic("implement me")
}
func (me *mockEndpointServer) RecvMsg(m interface{}) error {
panic("implement me")
}
func newServerClient(handler func(http.ResponseWriter, *http.Request)) (server *httptest.Server, client *consul.Client) {
server = httptest.NewServer(http.HandlerFunc(handler))
client = &consul.Client{
Address: server.URL,
UserAgent: "test",
Datacenter: "dc1",
}
return
}
func newServer(t *testing.T, services [][]struct{ Service service }) (server *httptest.Server, client *consul.Client) {
index := 0
return newServerClient(func(res http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
t.Error("bad method:", req.Method)
}
if req.URL.Path != "/v1/health/service/1234" {
t.Error("bad URL path:", req.URL.Path)
}
foundQuery := req.URL.Query()
expectQuery := url.Values{
"passing": {""},
"stale": {""},
"dc": {"dc1"},
"tag": {"A", "B", "C"},
"node-meta": {"answer:42"},
}
if !reflect.DeepEqual(foundQuery, expectQuery) {
t.Error("bad URL query:")
t.Logf("expected: %#v", expectQuery)
t.Logf("found: %#v", foundQuery)
}
json.NewEncoder(res).Encode(services[index])
index += 1
return
})
}