2020-11-30 20:05:37 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2021-01-14 22:33:36 +00:00
|
|
|
|
2022-06-08 06:09:26 +00:00
|
|
|
"github.com/google/uuid"
|
2021-01-14 22:33:36 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
2022-06-08 06:09:26 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelstate"
|
2020-11-30 20:05:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadyServer_makeResponse(t *testing.T) {
|
|
|
|
type fields struct {
|
2022-08-11 12:31:35 +00:00
|
|
|
isConnected map[uint8]tunnelstate.ConnectionInfo
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
wantOK bool
|
2021-11-08 15:43:36 +00:00
|
|
|
wantReadyConnections uint
|
2020-11-30 20:05:37 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "One connection online => HTTP 200",
|
|
|
|
fields: fields{
|
2022-08-11 12:31:35 +00:00
|
|
|
isConnected: map[uint8]tunnelstate.ConnectionInfo{
|
|
|
|
0: {IsConnected: false},
|
|
|
|
1: {IsConnected: false},
|
|
|
|
2: {IsConnected: true},
|
|
|
|
3: {IsConnected: false},
|
2020-11-30 20:05:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantOK: true,
|
|
|
|
wantReadyConnections: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No connections online => no HTTP 200",
|
|
|
|
fields: fields{
|
2022-08-11 12:31:35 +00:00
|
|
|
isConnected: map[uint8]tunnelstate.ConnectionInfo{
|
|
|
|
0: {IsConnected: false},
|
|
|
|
1: {IsConnected: false},
|
|
|
|
2: {IsConnected: false},
|
|
|
|
3: {IsConnected: false},
|
2020-11-30 20:05:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReadyConnections: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rs := &ReadyServer{
|
2021-11-08 15:43:36 +00:00
|
|
|
tracker: tunnelstate.MockedConnTracker(tt.fields.isConnected),
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
|
|
|
gotStatusCode, gotReadyConnections := rs.makeResponse()
|
|
|
|
if tt.wantOK && gotStatusCode != http.StatusOK {
|
|
|
|
t.Errorf("ReadyServer.makeResponse() gotStatusCode = %v, want ok = %v", gotStatusCode, tt.wantOK)
|
|
|
|
}
|
|
|
|
if gotReadyConnections != tt.wantReadyConnections {
|
|
|
|
t.Errorf("ReadyServer.makeResponse() gotReadyConnections = %v, want %v", gotReadyConnections, tt.wantReadyConnections)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
func TestReadinessEventHandling(t *testing.T) {
|
|
|
|
nopLogger := zerolog.Nop()
|
2022-06-08 06:09:26 +00:00
|
|
|
rs := NewReadyServer(&nopLogger, uuid.Nil)
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
// start not ok
|
|
|
|
code, ready := rs.makeResponse()
|
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
|
|
|
assert.Zero(t, ready)
|
|
|
|
|
|
|
|
// one connected => ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Connected,
|
|
|
|
})
|
|
|
|
code, ready = rs.makeResponse()
|
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
|
|
|
assert.EqualValues(t, 1, ready)
|
|
|
|
|
|
|
|
// another connected => still ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 2,
|
|
|
|
EventType: connection.Connected,
|
|
|
|
})
|
|
|
|
code, ready = rs.makeResponse()
|
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
2021-01-19 12:20:11 +00:00
|
|
|
assert.EqualValues(t, 2, ready)
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
// one reconnecting => still ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 2,
|
|
|
|
EventType: connection.Reconnecting,
|
|
|
|
})
|
|
|
|
code, ready = rs.makeResponse()
|
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
|
|
|
assert.EqualValues(t, 1, ready)
|
|
|
|
|
2021-01-19 12:20:11 +00:00
|
|
|
// Regression test for TUN-3777
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.RegisteringTunnel,
|
|
|
|
})
|
|
|
|
code, ready = rs.makeResponse()
|
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
|
|
|
assert.Zero(t, ready)
|
|
|
|
|
2021-02-04 21:09:17 +00:00
|
|
|
// other connected then unregistered => not ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Connected,
|
|
|
|
})
|
|
|
|
code, ready = rs.makeResponse()
|
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
|
|
|
assert.EqualValues(t, 1, ready)
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Unregistering,
|
|
|
|
})
|
|
|
|
code, ready = rs.makeResponse()
|
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
|
|
|
assert.Zero(t, ready)
|
|
|
|
|
2021-01-14 22:33:36 +00:00
|
|
|
// other disconnected => not ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Disconnected,
|
|
|
|
})
|
|
|
|
code, ready = rs.makeResponse()
|
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
|
|
|
assert.Zero(t, ready)
|
|
|
|
}
|