2024-11-22 16:10:05 +00:00
|
|
|
package diagnostic_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2024-11-25 18:43:32 +00:00
|
|
|
"net"
|
2024-11-22 16:10:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-12-11 10:48:41 +00:00
|
|
|
"runtime"
|
2024-11-22 16:10:05 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-11-25 18:43:32 +00:00
|
|
|
"github.com/google/uuid"
|
2024-11-22 16:10:05 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-11-25 18:43:32 +00:00
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
2024-11-22 16:10:05 +00:00
|
|
|
"github.com/cloudflare/cloudflared/diagnostic"
|
2024-11-25 18:43:32 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelstate"
|
2024-11-22 16:10:05 +00:00
|
|
|
)
|
|
|
|
|
2024-12-11 10:48:41 +00:00
|
|
|
type SystemCollectorMock struct {
|
|
|
|
systemInfo *diagnostic.SystemInformation
|
|
|
|
err error
|
|
|
|
}
|
2024-11-22 16:10:05 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
systemInformationKey = "sikey"
|
|
|
|
errorKey = "errkey"
|
|
|
|
)
|
|
|
|
|
2024-11-25 18:43:32 +00:00
|
|
|
func newTrackerFromConns(t *testing.T, connections []tunnelstate.IndexedConnectionInfo) *tunnelstate.ConnTracker {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
log := zerolog.Nop()
|
|
|
|
tracker := tunnelstate.NewConnTracker(&log)
|
|
|
|
|
|
|
|
for _, conn := range connections {
|
|
|
|
tracker.OnTunnelEvent(connection.Event{
|
|
|
|
Index: conn.Index,
|
|
|
|
EventType: connection.Connected,
|
|
|
|
Protocol: conn.Protocol,
|
|
|
|
EdgeAddress: conn.EdgeAddress,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracker
|
|
|
|
}
|
2024-11-25 19:24:51 +00:00
|
|
|
|
2024-12-11 10:48:41 +00:00
|
|
|
func (collector *SystemCollectorMock) Collect(context.Context) (*diagnostic.SystemInformation, error) {
|
|
|
|
return collector.systemInfo, collector.err
|
2024-11-22 16:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemHandler(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
log := zerolog.Nop()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
systemInfo *diagnostic.SystemInformation
|
|
|
|
err error
|
|
|
|
statusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "happy path",
|
|
|
|
systemInfo: diagnostic.NewSystemInformation(
|
|
|
|
0, 0, 0, 0,
|
|
|
|
"string", "string", "string", "string",
|
2024-12-11 10:48:41 +00:00
|
|
|
"string", "string",
|
|
|
|
runtime.Version(), runtime.GOARCH, nil,
|
2024-11-22 16:10:05 +00:00
|
|
|
),
|
2024-12-11 10:48:41 +00:00
|
|
|
|
2024-11-22 16:10:05 +00:00
|
|
|
err: nil,
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "on error and no raw info", systemInfo: nil,
|
2024-12-11 10:48:41 +00:00
|
|
|
err: errors.New("an error"), statusCode: http.StatusOK,
|
2024-11-22 16:10:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tCase := range tests {
|
|
|
|
t.Run(tCase.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2024-12-11 10:48:41 +00:00
|
|
|
handler := diagnostic.NewDiagnosticHandler(&log, 0, &SystemCollectorMock{
|
|
|
|
systemInfo: tCase.systemInfo,
|
|
|
|
err: tCase.err,
|
|
|
|
}, uuid.New(), uuid.New(), nil, map[string]string{}, nil)
|
2024-11-22 16:10:05 +00:00
|
|
|
recorder := httptest.NewRecorder()
|
2024-12-11 10:48:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "/diag/system", nil)
|
2024-11-22 16:10:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
handler.SystemHandler(recorder, request)
|
|
|
|
|
|
|
|
assert.Equal(t, tCase.statusCode, recorder.Code)
|
|
|
|
if tCase.statusCode == http.StatusOK && tCase.systemInfo != nil {
|
2024-12-11 10:48:41 +00:00
|
|
|
var response diagnostic.SystemInformationResponse
|
2024-11-22 16:10:05 +00:00
|
|
|
decoder := json.NewDecoder(recorder.Body)
|
2024-12-11 10:48:41 +00:00
|
|
|
err := decoder.Decode(&response)
|
2024-11-22 16:10:05 +00:00
|
|
|
require.NoError(t, err)
|
2024-12-11 10:48:41 +00:00
|
|
|
assert.Equal(t, tCase.systemInfo, response.Info)
|
2024-11-22 16:10:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-11-25 18:43:32 +00:00
|
|
|
|
|
|
|
func TestTunnelStateHandler(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
log := zerolog.Nop()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tunnelID uuid.UUID
|
|
|
|
clientID uuid.UUID
|
|
|
|
connections []tunnelstate.IndexedConnectionInfo
|
2024-12-10 18:42:33 +00:00
|
|
|
icmpSources []string
|
2024-11-25 18:43:32 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "case1",
|
|
|
|
tunnelID: uuid.New(),
|
|
|
|
clientID: uuid.New(),
|
|
|
|
},
|
|
|
|
{
|
2024-12-10 18:42:33 +00:00
|
|
|
name: "case2",
|
|
|
|
tunnelID: uuid.New(),
|
|
|
|
clientID: uuid.New(),
|
|
|
|
icmpSources: []string{"172.17.0.3", "::1"},
|
2024-11-25 18:43:32 +00:00
|
|
|
connections: []tunnelstate.IndexedConnectionInfo{{
|
|
|
|
ConnectionInfo: tunnelstate.ConnectionInfo{
|
|
|
|
IsConnected: true,
|
|
|
|
Protocol: connection.QUIC,
|
|
|
|
EdgeAddress: net.IPv4(100, 100, 100, 100),
|
|
|
|
},
|
|
|
|
Index: 0,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tCase := range tests {
|
|
|
|
t.Run(tCase.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tracker := newTrackerFromConns(t, tCase.connections)
|
2024-12-10 18:42:33 +00:00
|
|
|
handler := diagnostic.NewDiagnosticHandler(
|
|
|
|
&log,
|
|
|
|
0,
|
|
|
|
nil,
|
|
|
|
tCase.tunnelID,
|
|
|
|
tCase.clientID,
|
|
|
|
tracker,
|
2024-12-11 09:29:20 +00:00
|
|
|
map[string]string{},
|
2024-12-10 18:42:33 +00:00
|
|
|
tCase.icmpSources,
|
|
|
|
)
|
2024-11-25 18:43:32 +00:00
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
handler.TunnelStateHandler(recorder, nil)
|
|
|
|
decoder := json.NewDecoder(recorder.Body)
|
|
|
|
|
2024-11-29 17:08:42 +00:00
|
|
|
var response diagnostic.TunnelState
|
2024-11-25 18:43:32 +00:00
|
|
|
err := decoder.Decode(&response)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, recorder.Code)
|
|
|
|
assert.Equal(t, tCase.tunnelID, response.TunnelID)
|
|
|
|
assert.Equal(t, tCase.clientID, response.ConnectorID)
|
|
|
|
assert.Equal(t, tCase.connections, response.Connections)
|
2024-12-10 18:42:33 +00:00
|
|
|
assert.Equal(t, tCase.icmpSources, response.ICMPSources)
|
2024-11-25 18:43:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-11-25 19:24:51 +00:00
|
|
|
|
|
|
|
func TestConfigurationHandler(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
log := zerolog.Nop()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
flags map[string]string
|
|
|
|
expected map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty cli",
|
|
|
|
flags: make(map[string]string),
|
|
|
|
expected: map[string]string{
|
|
|
|
"uid": "0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cli with flags",
|
|
|
|
flags: map[string]string{
|
2024-12-11 09:29:20 +00:00
|
|
|
"b": "a",
|
|
|
|
"c": "a",
|
|
|
|
"d": "a",
|
|
|
|
"uid": "0",
|
2024-11-25 19:24:51 +00:00
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"b": "a",
|
|
|
|
"c": "a",
|
|
|
|
"d": "a",
|
|
|
|
"uid": "0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tCase := range tests {
|
|
|
|
t.Run(tCase.name, func(t *testing.T) {
|
2024-12-11 09:29:20 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-11-25 19:24:51 +00:00
|
|
|
var response map[string]string
|
|
|
|
|
2024-12-11 09:29:20 +00:00
|
|
|
handler := diagnostic.NewDiagnosticHandler(&log, 0, nil, uuid.New(), uuid.New(), nil, tCase.flags, nil)
|
2024-11-25 19:24:51 +00:00
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
handler.ConfigurationHandler(recorder, nil)
|
|
|
|
decoder := json.NewDecoder(recorder.Body)
|
|
|
|
err := decoder.Decode(&response)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, ok := response["uid"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
delete(tCase.expected, "uid")
|
|
|
|
delete(response, "uid")
|
|
|
|
assert.Equal(t, http.StatusOK, recorder.Code)
|
|
|
|
assert.Equal(t, tCase.expected, response)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|