|
| 1 | +package supervisor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/rs/zerolog" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + |
| 10 | + "github.com/cloudflare/cloudflared/connection" |
| 11 | + "github.com/cloudflare/cloudflared/edgediscovery" |
| 12 | + "github.com/cloudflare/cloudflared/edgediscovery/allregions" |
| 13 | + "github.com/cloudflare/cloudflared/signal" |
| 14 | +) |
| 15 | + |
| 16 | +type mockProtocolSelector struct { |
| 17 | + protocols []connection.Protocol |
| 18 | + index int |
| 19 | +} |
| 20 | + |
| 21 | +func (m *mockProtocolSelector) Current() connection.Protocol { |
| 22 | + return m.protocols[m.index] |
| 23 | +} |
| 24 | + |
| 25 | +func (m *mockProtocolSelector) Fallback() (connection.Protocol, bool) { |
| 26 | + m.index++ |
| 27 | + if m.index == len(m.protocols) { |
| 28 | + return m.protocols[len(m.protocols)-1], false |
| 29 | + } |
| 30 | + |
| 31 | + return m.protocols[m.index], true |
| 32 | +} |
| 33 | + |
| 34 | +type mockEdgeTunnelServer struct { |
| 35 | + config *TunnelConfig |
| 36 | +} |
| 37 | + |
| 38 | +func (m *mockEdgeTunnelServer) Serve(ctx context.Context, connIndex uint8, protocolFallback *protocolFallback, connectedSignal *signal.Signal) error { |
| 39 | + // This is to mock the first connection falling back because of connectivity issues. |
| 40 | + protocolFallback.protocol, _ = m.config.ProtocolSelector.Fallback() |
| 41 | + connectedSignal.Notify() |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// Test to check if initialize sets all the different connections to the same protocol should the first |
| 46 | +// tunnel fall back. |
| 47 | +func Test_Initialize_Same_Protocol(t *testing.T) { |
| 48 | + edgeIPs, err := edgediscovery.ResolveEdge(&zerolog.Logger{}, "us", allregions.Auto) |
| 49 | + assert.Nil(t, err) |
| 50 | + s := Supervisor{ |
| 51 | + edgeIPs: edgeIPs, |
| 52 | + config: &TunnelConfig{ |
| 53 | + ProtocolSelector: &mockProtocolSelector{protocols: []connection.Protocol{connection.QUIC, connection.HTTP2, connection.H2mux}}, |
| 54 | + }, |
| 55 | + tunnelsProtocolFallback: make(map[int]*protocolFallback), |
| 56 | + edgeTunnelServer: &mockEdgeTunnelServer{ |
| 57 | + config: &TunnelConfig{ |
| 58 | + ProtocolSelector: &mockProtocolSelector{protocols: []connection.Protocol{connection.QUIC, connection.HTTP2, connection.H2mux}}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + ctx := context.Background() |
| 64 | + connectedSignal := signal.New(make(chan struct{})) |
| 65 | + s.initialize(ctx, connectedSignal) |
| 66 | + |
| 67 | + // Make sure we fell back to http2 as the mock Serve is wont to do. |
| 68 | + assert.Equal(t, s.tunnelsProtocolFallback[0].protocol, connection.HTTP2) |
| 69 | + |
| 70 | + // Ensure all the protocols we set to try are the same as what the first tunnel has fallen back to. |
| 71 | + for _, protocolFallback := range s.tunnelsProtocolFallback { |
| 72 | + assert.Equal(t, protocolFallback.protocol, s.tunnelsProtocolFallback[0].protocol) |
| 73 | + } |
| 74 | +} |
0 commit comments