Skip to content

Commit bb99cb7

Browse files
authored
chore: move FakeCoordinator to tailnettest (#11992)
Moves FakeCoordinator to tailnettest since it's reused in testing multiple packages in this stack of PRs.
1 parent 646ac94 commit bb99cb7

File tree

2 files changed

+79
-83
lines changed

2 files changed

+79
-83
lines changed

tailnet/service_test.go

+6-83
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
package tailnet_test
22

33
import (
4-
"context"
54
"io"
65
"net"
7-
"net/http"
86
"sync/atomic"
97
"testing"
108
"time"
119

10+
"github.com/google/uuid"
11+
"github.com/stretchr/testify/require"
1212
"golang.org/x/xerrors"
1313
"tailscale.com/tailcfg"
1414

15-
"github.com/google/uuid"
16-
1715
"cdr.dev/slog"
1816
"cdr.dev/slog/sloggers/slogtest"
17+
"github.com/coder/coder/v2/tailnet"
1918
"github.com/coder/coder/v2/tailnet/proto"
19+
"github.com/coder/coder/v2/tailnet/tailnettest"
2020
"github.com/coder/coder/v2/testutil"
21-
22-
"github.com/stretchr/testify/require"
23-
24-
"github.com/coder/coder/v2/tailnet"
2521
)
2622

2723
func TestClientService_ServeClient_V2(t *testing.T) {
2824
t.Parallel()
29-
fCoord := NewFakeCoordinator()
25+
fCoord := tailnettest.NewFakeCoordinator()
3026
var coord tailnet.Coordinator = fCoord
3127
coordPtr := atomic.Pointer[tailnet.Coordinator]{}
3228
coordPtr.Store(&coord)
@@ -107,7 +103,7 @@ func TestClientService_ServeClient_V2(t *testing.T) {
107103

108104
func TestClientService_ServeClient_V1(t *testing.T) {
109105
t.Parallel()
110-
fCoord := NewFakeCoordinator()
106+
fCoord := tailnettest.NewFakeCoordinator()
111107
var coord tailnet.Coordinator = fCoord
112108
coordPtr := atomic.Pointer[tailnet.Coordinator]{}
113109
coordPtr.Store(&coord)
@@ -144,76 +140,3 @@ func TestClientService_ServeClient_V1(t *testing.T) {
144140
err = testutil.RequireRecvCtx(ctx, t, errCh)
145141
require.ErrorIs(t, err, expectedError)
146142
}
147-
148-
type FakeCoordinator struct {
149-
CoordinateCalls chan *FakeCoordinate
150-
ServeClientCalls chan *FakeServeClient
151-
}
152-
153-
func (*FakeCoordinator) ServeHTTPDebug(http.ResponseWriter, *http.Request) {
154-
panic("unimplemented")
155-
}
156-
157-
func (*FakeCoordinator) Node(uuid.UUID) *tailnet.Node {
158-
panic("unimplemented")
159-
}
160-
161-
func (f *FakeCoordinator) ServeClient(conn net.Conn, id uuid.UUID, agent uuid.UUID) error {
162-
errCh := make(chan error)
163-
f.ServeClientCalls <- &FakeServeClient{
164-
Conn: conn,
165-
ID: id,
166-
Agent: agent,
167-
ErrCh: errCh,
168-
}
169-
return <-errCh
170-
}
171-
172-
func (*FakeCoordinator) ServeAgent(net.Conn, uuid.UUID, string) error {
173-
panic("unimplemented")
174-
}
175-
176-
func (*FakeCoordinator) Close() error {
177-
panic("unimplemented")
178-
}
179-
180-
func (*FakeCoordinator) ServeMultiAgent(uuid.UUID) tailnet.MultiAgentConn {
181-
panic("unimplemented")
182-
}
183-
184-
func (f *FakeCoordinator) Coordinate(ctx context.Context, id uuid.UUID, name string, a tailnet.TunnelAuth) (chan<- *proto.CoordinateRequest, <-chan *proto.CoordinateResponse) {
185-
reqs := make(chan *proto.CoordinateRequest, 100)
186-
resps := make(chan *proto.CoordinateResponse, 100)
187-
f.CoordinateCalls <- &FakeCoordinate{
188-
Ctx: ctx,
189-
ID: id,
190-
Name: name,
191-
Auth: a,
192-
Reqs: reqs,
193-
Resps: resps,
194-
}
195-
return reqs, resps
196-
}
197-
198-
func NewFakeCoordinator() *FakeCoordinator {
199-
return &FakeCoordinator{
200-
CoordinateCalls: make(chan *FakeCoordinate, 100),
201-
ServeClientCalls: make(chan *FakeServeClient, 100),
202-
}
203-
}
204-
205-
type FakeCoordinate struct {
206-
Ctx context.Context
207-
ID uuid.UUID
208-
Name string
209-
Auth tailnet.TunnelAuth
210-
Reqs chan *proto.CoordinateRequest
211-
Resps chan *proto.CoordinateResponse
212-
}
213-
214-
type FakeServeClient struct {
215-
Conn net.Conn
216-
ID uuid.UUID
217-
Agent uuid.UUID
218-
ErrCh chan error
219-
}

tailnet/tailnettest/tailnettest.go

+73
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,76 @@ func (m *TestMultiAgent) RequireEventuallyClosed(ctx context.Context) {
249249
}
250250
}
251251
}
252+
253+
type FakeCoordinator struct {
254+
CoordinateCalls chan *FakeCoordinate
255+
ServeClientCalls chan *FakeServeClient
256+
}
257+
258+
func (*FakeCoordinator) ServeHTTPDebug(http.ResponseWriter, *http.Request) {
259+
panic("unimplemented")
260+
}
261+
262+
func (*FakeCoordinator) Node(uuid.UUID) *tailnet.Node {
263+
panic("unimplemented")
264+
}
265+
266+
func (f *FakeCoordinator) ServeClient(conn net.Conn, id uuid.UUID, agent uuid.UUID) error {
267+
errCh := make(chan error)
268+
f.ServeClientCalls <- &FakeServeClient{
269+
Conn: conn,
270+
ID: id,
271+
Agent: agent,
272+
ErrCh: errCh,
273+
}
274+
return <-errCh
275+
}
276+
277+
func (*FakeCoordinator) ServeAgent(net.Conn, uuid.UUID, string) error {
278+
panic("unimplemented")
279+
}
280+
281+
func (*FakeCoordinator) Close() error {
282+
panic("unimplemented")
283+
}
284+
285+
func (*FakeCoordinator) ServeMultiAgent(uuid.UUID) tailnet.MultiAgentConn {
286+
panic("unimplemented")
287+
}
288+
289+
func (f *FakeCoordinator) Coordinate(ctx context.Context, id uuid.UUID, name string, a tailnet.TunnelAuth) (chan<- *proto.CoordinateRequest, <-chan *proto.CoordinateResponse) {
290+
reqs := make(chan *proto.CoordinateRequest, 100)
291+
resps := make(chan *proto.CoordinateResponse, 100)
292+
f.CoordinateCalls <- &FakeCoordinate{
293+
Ctx: ctx,
294+
ID: id,
295+
Name: name,
296+
Auth: a,
297+
Reqs: reqs,
298+
Resps: resps,
299+
}
300+
return reqs, resps
301+
}
302+
303+
func NewFakeCoordinator() *FakeCoordinator {
304+
return &FakeCoordinator{
305+
CoordinateCalls: make(chan *FakeCoordinate, 100),
306+
ServeClientCalls: make(chan *FakeServeClient, 100),
307+
}
308+
}
309+
310+
type FakeCoordinate struct {
311+
Ctx context.Context
312+
ID uuid.UUID
313+
Name string
314+
Auth tailnet.TunnelAuth
315+
Reqs chan *proto.CoordinateRequest
316+
Resps chan *proto.CoordinateResponse
317+
}
318+
319+
type FakeServeClient struct {
320+
Conn net.Conn
321+
ID uuid.UUID
322+
Agent uuid.UUID
323+
ErrCh chan error
324+
}

0 commit comments

Comments
 (0)