2024-11-22 15:23:46 +00:00
|
|
|
package metrics_test
|
2020-11-30 20:05:37 +00:00
|
|
|
|
|
|
|
import (
|
2024-11-22 15:23:46 +00:00
|
|
|
"encoding/json"
|
2020-11-30 20:05:37 +00:00
|
|
|
"net/http"
|
2024-11-22 15:23:46 +00:00
|
|
|
"net/http/httptest"
|
2020-11-30 20:05:37 +00:00
|
|
|
"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"
|
2024-11-22 15:23:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
2024-11-22 15:23:46 +00:00
|
|
|
"github.com/cloudflare/cloudflared/metrics"
|
2022-06-08 06:09:26 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelstate"
|
2020-11-30 20:05:37 +00:00
|
|
|
)
|
|
|
|
|
2024-11-22 15:23:46 +00:00
|
|
|
func mockRequest(t *testing.T, readyServer *metrics.ReadyServer) (int, uint) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var readyreadyConnections struct {
|
|
|
|
Status int `json:"status"`
|
|
|
|
ReadyConnections uint `json:"readyConnections"`
|
|
|
|
ConnectorID uuid.UUID `json:"connectorId"`
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
2024-11-22 15:23:46 +00:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
readyServer.ServeHTTP(rec, nil)
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(rec.Body)
|
|
|
|
err := decoder.Decode(&readyreadyConnections)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return rec.Code, readyreadyConnections.ReadyConnections
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
func TestReadinessEventHandling(t *testing.T) {
|
|
|
|
nopLogger := zerolog.Nop()
|
2024-11-22 15:23:46 +00:00
|
|
|
tracker := tunnelstate.NewConnTracker(&nopLogger)
|
|
|
|
rs := metrics.NewReadyServer(uuid.Nil, tracker)
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
// start not ok
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections := mockRequest(t, rs)
|
2021-01-14 22:33:36 +00:00
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.Zero(t, readyConnections)
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
// one connected => ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Connected,
|
|
|
|
})
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections = mockRequest(t, rs)
|
2021-01-14 22:33:36 +00:00
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.EqualValues(t, 1, readyConnections)
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
// another connected => still ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 2,
|
|
|
|
EventType: connection.Connected,
|
|
|
|
})
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections = mockRequest(t, rs)
|
2021-01-14 22:33:36 +00:00
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.EqualValues(t, 2, readyConnections)
|
2021-01-14 22:33:36 +00:00
|
|
|
|
|
|
|
// one reconnecting => still ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 2,
|
|
|
|
EventType: connection.Reconnecting,
|
|
|
|
})
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections = mockRequest(t, rs)
|
2021-01-14 22:33:36 +00:00
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.EqualValues(t, 1, readyConnections)
|
2021-01-14 22:33:36 +00:00
|
|
|
|
2021-01-19 12:20:11 +00:00
|
|
|
// Regression test for TUN-3777
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.RegisteringTunnel,
|
|
|
|
})
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections = mockRequest(t, rs)
|
2021-01-19 12:20:11 +00:00
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.Zero(t, readyConnections)
|
2021-01-19 12:20:11 +00:00
|
|
|
|
2021-02-04 21:09:17 +00:00
|
|
|
// other connected then unregistered => not ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Connected,
|
|
|
|
})
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections = mockRequest(t, rs)
|
2021-02-04 21:09:17 +00:00
|
|
|
assert.EqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.EqualValues(t, 1, readyConnections)
|
2021-02-04 21:09:17 +00:00
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Unregistering,
|
|
|
|
})
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections = mockRequest(t, rs)
|
2021-02-04 21:09:17 +00:00
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.Zero(t, readyConnections)
|
2021-02-04 21:09:17 +00:00
|
|
|
|
2021-01-14 22:33:36 +00:00
|
|
|
// other disconnected => not ok
|
|
|
|
rs.OnTunnelEvent(connection.Event{
|
|
|
|
Index: 1,
|
|
|
|
EventType: connection.Disconnected,
|
|
|
|
})
|
2024-11-22 15:23:46 +00:00
|
|
|
code, readyConnections = mockRequest(t, rs)
|
2021-01-14 22:33:36 +00:00
|
|
|
assert.NotEqualValues(t, http.StatusOK, code)
|
2024-11-22 15:23:46 +00:00
|
|
|
assert.Zero(t, readyConnections)
|
2021-01-14 22:33:36 +00:00
|
|
|
}
|