Add fabric session packet transport
This commit is contained in:
@@ -123,6 +123,32 @@ type memoryPacketTransport struct {
|
||||
recv [][]byte
|
||||
}
|
||||
|
||||
type captureFabricSessionSender struct {
|
||||
err error
|
||||
frames []fabricproto.Frame
|
||||
}
|
||||
|
||||
func (s *captureFabricSessionSender) Send(_ context.Context, frame fabricproto.Frame) error {
|
||||
if s.err != nil {
|
||||
return s.err
|
||||
}
|
||||
s.frames = append(s.frames, frame)
|
||||
return nil
|
||||
}
|
||||
|
||||
type memoryFabricSessionReceiver struct {
|
||||
frames chan fabricproto.Frame
|
||||
errors chan error
|
||||
}
|
||||
|
||||
func (r memoryFabricSessionReceiver) Frames() <-chan fabricproto.Frame {
|
||||
return r.frames
|
||||
}
|
||||
|
||||
func (r memoryFabricSessionReceiver) Errors() <-chan error {
|
||||
return r.errors
|
||||
}
|
||||
|
||||
func (t *memoryPacketTransport) SendGatewayPacketBatch(_ context.Context, packets [][]byte) error {
|
||||
if t.sendErr != nil {
|
||||
return t.sendErr
|
||||
@@ -172,6 +198,110 @@ func TestFabricPacketTransportSendsVPNPacketBatchEnvelope(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFabricSessionPacketTransportSendsDataFrame(t *testing.T) {
|
||||
sender := &captureFabricSessionSender{}
|
||||
transport := &FabricSessionPacketTransport{
|
||||
Sender: sender,
|
||||
StreamID: 700,
|
||||
VPNConnectionID: "vpn-1",
|
||||
SendDirection: FabricDirectionClientToGateway,
|
||||
TrafficClass: FabricTrafficClassInteractive,
|
||||
}
|
||||
|
||||
if err := transport.SendGatewayPacketBatch(context.Background(), [][]byte{[]byte("packet")}); err != nil {
|
||||
t.Fatalf("send fabric session packet batch: %v", err)
|
||||
}
|
||||
if len(sender.frames) != 1 {
|
||||
t.Fatalf("sent frames = %d, want 1", len(sender.frames))
|
||||
}
|
||||
frame := sender.frames[0]
|
||||
if frame.Type != fabricproto.FrameData || frame.StreamID != 700 || frame.Sequence != 1 || frame.TrafficClass != fabricproto.TrafficClassInteractive {
|
||||
t.Fatalf("frame = %+v", frame)
|
||||
}
|
||||
payload, err := DecodeFabricVPNPacketDataFrame(frame)
|
||||
if err != nil {
|
||||
t.Fatalf("decode sent frame: %v", err)
|
||||
}
|
||||
if payload.VPNConnectionID != "vpn-1" || payload.Direction != FabricDirectionClientToGateway || len(payload.Packets) != 1 || string(payload.Packets[0]) != "packet" {
|
||||
t.Fatalf("payload = %+v", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFabricSessionPacketTransportRunFrameIngressDeliversInbox(t *testing.T) {
|
||||
inbox := NewFabricPacketInbox(4)
|
||||
receiver := memoryFabricSessionReceiver{
|
||||
frames: make(chan fabricproto.Frame, 2),
|
||||
errors: make(chan error, 1),
|
||||
}
|
||||
transport := &FabricSessionPacketTransport{
|
||||
Receiver: receiver,
|
||||
Inbox: inbox,
|
||||
StreamID: 701,
|
||||
VPNConnectionID: "vpn-1",
|
||||
}
|
||||
frame, err := NewFabricVPNPacketDataFrame(FabricVPNPacketFrameInput{
|
||||
StreamID: 701,
|
||||
Sequence: 1,
|
||||
VPNConnectionID: "vpn-1",
|
||||
Direction: FabricDirectionClientToGateway,
|
||||
Packets: [][]byte{[]byte("packet")},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("new fabric vpn frame: %v", err)
|
||||
}
|
||||
receiver.frames <- fabricproto.Frame{Type: fabricproto.FrameAck, StreamID: 701, Sequence: 1}
|
||||
receiver.frames <- frame
|
||||
close(receiver.frames)
|
||||
|
||||
if err := transport.RunFrameIngress(context.Background()); err != nil {
|
||||
t.Fatalf("run frame ingress: %v", err)
|
||||
}
|
||||
packets, err := transport.ReceiveGatewayPacketBatch(context.Background(), time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("receive gateway packet: %v", err)
|
||||
}
|
||||
if len(packets) != 1 || string(packets[0]) != "packet" {
|
||||
t.Fatalf("packets = %#v", packets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFabricSessionPacketTransportIngressIgnoresOtherStreams(t *testing.T) {
|
||||
inbox := NewFabricPacketInbox(4)
|
||||
receiver := memoryFabricSessionReceiver{
|
||||
frames: make(chan fabricproto.Frame, 1),
|
||||
errors: make(chan error, 1),
|
||||
}
|
||||
transport := &FabricSessionPacketTransport{
|
||||
Receiver: receiver,
|
||||
Inbox: inbox,
|
||||
StreamID: 701,
|
||||
VPNConnectionID: "vpn-1",
|
||||
}
|
||||
frame, err := NewFabricVPNPacketDataFrame(FabricVPNPacketFrameInput{
|
||||
StreamID: 702,
|
||||
Sequence: 1,
|
||||
VPNConnectionID: "vpn-1",
|
||||
Direction: FabricDirectionClientToGateway,
|
||||
Packets: [][]byte{[]byte("packet")},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("new fabric vpn frame: %v", err)
|
||||
}
|
||||
receiver.frames <- frame
|
||||
close(receiver.frames)
|
||||
|
||||
if err := transport.RunFrameIngress(context.Background()); err != nil {
|
||||
t.Fatalf("run frame ingress: %v", err)
|
||||
}
|
||||
packets, err := transport.ReceiveGatewayPacketBatch(context.Background(), 10*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatalf("receive gateway packet: %v", err)
|
||||
}
|
||||
if len(packets) != 0 {
|
||||
t.Fatalf("packets = %#v, want none", packets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFabricPacketInboxReceivesMatchingDirection(t *testing.T) {
|
||||
inbox := NewFabricPacketInbox(4)
|
||||
envelope, err := mesh.NewProductionVPNPacketBatchEnvelope(mesh.ProductionVPNPacketEnvelopeInput{
|
||||
|
||||
Reference in New Issue
Block a user