@@ -3,12 +3,15 @@ package telemetry
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "runtime"
6
7
"testing"
7
8
"time"
8
9
9
10
"github.com/stretchr/testify/assert"
10
11
"github.com/stretchr/testify/require"
12
+ corev1 "k8s.io/api/core/v1"
11
13
"k8s.io/apimachinery/pkg/types"
14
+ "k8s.io/client-go/rest"
12
15
"k8s.io/utils/ptr"
13
16
"sigs.k8s.io/controller-runtime/pkg/client"
14
17
@@ -23,6 +26,8 @@ import (
23
26
mockClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client"
24
27
"github.com/mongodb/mongodb-kubernetes/pkg/util"
25
28
"github.com/mongodb/mongodb-kubernetes/pkg/util/architectures"
29
+
30
+ kubeclient "sigs.k8s.io/controller-runtime/pkg/client"
26
31
)
27
32
28
33
const (
@@ -1331,3 +1336,93 @@ func TestAddSearchEvents(t *testing.T) {
1331
1336
})
1332
1337
}
1333
1338
}
1339
+
1340
+ func TestCollectOperatorSnapshot (t * testing.T ) {
1341
+ tests := map [string ]struct {
1342
+ memberClusterMap map [string ]ConfigClient
1343
+ expectedProps OperatorUsageSnapshotProperties
1344
+ }{
1345
+ "single cluster" : {
1346
+ memberClusterMap : map [string ]ConfigClient {},
1347
+ expectedProps : OperatorUsageSnapshotProperties {
1348
+ OperatorID : testOperatorUUID ,
1349
+ OperatorType : MEKO ,
1350
+ OperatorArchitecture : runtime .GOARCH ,
1351
+ OperatorOS : runtime .GOOS ,
1352
+ },
1353
+ },
1354
+ "multi cluster" : {
1355
+ memberClusterMap : map [string ]ConfigClient {
1356
+ "cluster1" : & mockConfigClient {clusterUUID : "cluster-uuid-1" },
1357
+ "cluster2" : & mockConfigClient {clusterUUID : "cluster-uuid-2" },
1358
+ },
1359
+ expectedProps : OperatorUsageSnapshotProperties {
1360
+ OperatorID : testOperatorUUID ,
1361
+ OperatorType : MEKO ,
1362
+ OperatorArchitecture : runtime .GOARCH ,
1363
+ OperatorOS : runtime .GOOS ,
1364
+ },
1365
+ },
1366
+ }
1367
+
1368
+ for name , test := range tests {
1369
+ t .Run (name , func (t * testing.T ) {
1370
+ t .Parallel ()
1371
+
1372
+ kubeSystemNamespace := & corev1.Namespace {
1373
+ ObjectMeta : metav1.ObjectMeta {
1374
+ Name : "kube-system" ,
1375
+ UID : types .UID ("operator-cluster-uuid" ),
1376
+ },
1377
+ }
1378
+
1379
+ k8sClient := mock .NewEmptyFakeClientBuilder ().WithObjects (kubeSystemNamespace ).Build ()
1380
+ mgr := mockClient .NewManagerWithClient (k8sClient )
1381
+
1382
+ ctx := context .Background ()
1383
+
1384
+ events := collectOperatorSnapshot (ctx , test .memberClusterMap , mgr , testOperatorUUID , "" , "" )
1385
+
1386
+ require .Len (t , events , 1 , "expected exactly one operator event" )
1387
+ event := events [0 ]
1388
+
1389
+ assert .Equal (t , Operators , event .Source )
1390
+ require .NotNil (t , event .Timestamp , "event timestamp is nil" )
1391
+
1392
+ assert .Equal (t , runtime .GOARCH , event .Properties ["operatorArchitecture" ])
1393
+ assert .Equal (t , runtime .GOOS , event .Properties ["operatorOS" ])
1394
+ assert .Equal (t , testOperatorUUID , event .Properties ["operatorID" ])
1395
+ assert .Equal (t , string (MEKO ), event .Properties ["operatorType" ])
1396
+
1397
+ assert .Contains (t , event .Properties , "kubernetesClusterID" )
1398
+ assert .Contains (t , event .Properties , "kubernetesClusterIDs" )
1399
+ assert .Contains (t , event .Properties , "operatorVersion" )
1400
+ })
1401
+ }
1402
+ }
1403
+
1404
+ // mockConfigClient is a simple mock implementation of ConfigClient for testing
1405
+ type mockConfigClient struct {
1406
+ clusterUUID string
1407
+ }
1408
+
1409
+ func (m * mockConfigClient ) GetConfig () * rest.Config {
1410
+ return & rest.Config {}
1411
+ }
1412
+
1413
+ func (m * mockConfigClient ) GetAPIReader () kubeclient.Reader {
1414
+ uuid := m .clusterUUID
1415
+ if uuid == "" {
1416
+ uuid = "default-cluster-uuid"
1417
+ }
1418
+
1419
+ kubeSystemNamespace := & corev1.Namespace {
1420
+ ObjectMeta : metav1.ObjectMeta {
1421
+ Name : "kube-system" ,
1422
+ UID : types .UID (uuid ),
1423
+ },
1424
+ }
1425
+
1426
+ k8sClient := mock .NewEmptyFakeClientBuilder ().WithObjects (kubeSystemNamespace ).Build ()
1427
+ return k8sClient
1428
+ }
0 commit comments