2021-09-20 21:55:21 +03:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-10-04 21:43:46 +03:00
|
|
|
"github.com/Azure/azure-container-networking/cni/log"
|
2021-09-20 21:55:21 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-10-04 21:43:46 +03:00
|
|
|
"go.uber.org/zap"
|
2021-09-20 21:55:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const telemetryConfig = "azure-vnet-telemetry.config"
|
|
|
|
|
|
|
|
func createTBServer(t *testing.T) (*TelemetryBuffer, func()) {
|
2023-10-04 21:43:46 +03:00
|
|
|
tbServer := NewTelemetryBuffer(nil)
|
2021-09-20 21:55:21 +03:00
|
|
|
err := tbServer.StartServer()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return tbServer, func() {
|
|
|
|
tbServer.Close()
|
|
|
|
err := tbServer.Cleanup(FdName)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartServer(t *testing.T) {
|
|
|
|
_, closeTBServer := createTBServer(t)
|
|
|
|
defer closeTBServer()
|
|
|
|
|
2023-10-04 21:43:46 +03:00
|
|
|
secondTBServer := NewTelemetryBuffer(nil)
|
2021-09-20 21:55:21 +03:00
|
|
|
err := secondTBServer.StartServer()
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnect(t *testing.T) {
|
|
|
|
_, closeTBServer := createTBServer(t)
|
|
|
|
defer closeTBServer()
|
|
|
|
|
2023-10-04 21:43:46 +03:00
|
|
|
logger := log.TelemetryLogger.With(zap.String("component", "cni-telemetry"))
|
|
|
|
tbClient := NewTelemetryBuffer(logger)
|
2021-09-20 21:55:21 +03:00
|
|
|
err := tbClient.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tbClient.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerConnClose(t *testing.T) {
|
|
|
|
tbServer, closeTBServer := createTBServer(t)
|
|
|
|
defer closeTBServer()
|
|
|
|
|
2023-10-04 21:43:46 +03:00
|
|
|
tbClient := NewTelemetryBuffer(nil)
|
2021-09-20 21:55:21 +03:00
|
|
|
err := tbClient.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tbClient.Close()
|
|
|
|
|
|
|
|
tbServer.Close()
|
|
|
|
|
|
|
|
b := []byte("testdata")
|
|
|
|
_, err = tbClient.Write(b)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientConnClose(t *testing.T) {
|
|
|
|
_, closeTBServer := createTBServer(t)
|
|
|
|
defer closeTBServer()
|
|
|
|
|
2023-10-04 21:43:46 +03:00
|
|
|
tbClient := NewTelemetryBuffer(nil)
|
2021-09-20 21:55:21 +03:00
|
|
|
err := tbClient.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
tbClient.Close()
|
|
|
|
}
|
|
|
|
|
2024-06-21 21:42:27 +03:00
|
|
|
func TestCloseOnWriteError(t *testing.T) {
|
|
|
|
tbServer, closeTBServer := createTBServer(t)
|
|
|
|
defer closeTBServer()
|
|
|
|
|
|
|
|
tbClient := NewTelemetryBuffer(nil)
|
|
|
|
err := tbClient.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tbClient.Close()
|
|
|
|
|
|
|
|
data := []byte("{\"good\":1}")
|
|
|
|
_, err = tbClient.Write(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// need to wait for connection to populate in server
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
tbServer.mutex.Lock()
|
|
|
|
conns := tbServer.connections
|
|
|
|
tbServer.mutex.Unlock()
|
|
|
|
require.Len(t, conns, 1)
|
|
|
|
|
|
|
|
// the connection should be automatically closed on failure
|
|
|
|
badData := []byte("} malformed json }}}")
|
|
|
|
_, err = tbClient.Write(badData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
tbServer.mutex.Lock()
|
|
|
|
conns = tbServer.connections
|
|
|
|
tbServer.mutex.Unlock()
|
|
|
|
require.Empty(t, conns)
|
|
|
|
}
|
|
|
|
|
2021-09-20 21:55:21 +03:00
|
|
|
func TestWrite(t *testing.T) {
|
|
|
|
_, closeTBServer := createTBServer(t)
|
|
|
|
defer closeTBServer()
|
|
|
|
|
2023-10-04 21:43:46 +03:00
|
|
|
tbClient := NewTelemetryBuffer(nil)
|
2021-09-20 21:55:21 +03:00
|
|
|
err := tbClient.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tbClient.Close()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
data []byte
|
|
|
|
want int
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "write",
|
2024-06-21 21:42:27 +03:00
|
|
|
data: []byte("{\"testdata\":1}"),
|
|
|
|
want: len("{\"testdata\":1}") + 1, // +1 due to Delimiter('\n)
|
2021-09-20 21:55:21 +03:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "write zero data",
|
|
|
|
data: []byte(""),
|
|
|
|
want: 1, // +1 due to Delimiter('\n)
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := tbClient.Write(tt.data)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2021-11-09 19:19:44 +03:00
|
|
|
require.Equal(t, tt.want, got, "Expected:%d but got:%d", tt.want, got)
|
2021-09-20 21:55:21 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadConfigFile(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fileName string
|
|
|
|
want TelemetryConfig
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "read existing file",
|
|
|
|
fileName: telemetryConfig,
|
|
|
|
want: TelemetryConfig{
|
|
|
|
ReportToHostIntervalInSeconds: time.Duration(30),
|
|
|
|
RefreshTimeoutInSecs: 15,
|
|
|
|
BatchIntervalInSecs: 15,
|
|
|
|
BatchSizeInBytes: 16384,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "read non-existing file",
|
|
|
|
fileName: "non-existing-file",
|
|
|
|
want: TelemetryConfig{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := ReadConfigFile(tt.fileName)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartTelemetryService(t *testing.T) {
|
2023-10-13 22:08:58 +03:00
|
|
|
tb := NewTelemetryBuffer(nil)
|
|
|
|
err := tb.StartTelemetryService("", nil)
|
2021-09-20 21:55:21 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|