@@ -2,7 +2,9 @@ package telemetry
22
33import (
44 "context"
5+ "encoding/json"
56 "errors"
7+ "reflect"
68 "runtime"
79 "testing"
810 "time"
@@ -21,6 +23,7 @@ import (
2123 "github.com/mongodb/mongodb-kubernetes/api/v1/mdbmulti"
2224 omv1 "github.com/mongodb/mongodb-kubernetes/api/v1/om"
2325 searchv1 "github.com/mongodb/mongodb-kubernetes/api/v1/search"
26+ userv1 "github.com/mongodb/mongodb-kubernetes/api/v1/user"
2427 "github.com/mongodb/mongodb-kubernetes/controllers/operator/mock"
2528 mcov1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
2629 mockClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client"
@@ -1157,12 +1160,17 @@ func findEventWithDeploymentUID(events []Event, deploymentUID string) *Event {
11571160type MockClient struct {
11581161 client.Client
11591162 MockList func (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error
1163+ MockGet func (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error
11601164}
11611165
11621166func (m * MockClient ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
11631167 return m .MockList (ctx , list , opts ... )
11641168}
11651169
1170+ func (m * MockClient ) Get (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
1171+ return m .MockGet (ctx , key , obj , opts ... )
1172+ }
1173+
11661174func TestAddCommunityEvents (t * testing.T ) {
11671175 operatorUUID := "test-operator-uuid"
11681176
@@ -1261,55 +1269,67 @@ func TestAddCommunityEvents(t *testing.T) {
12611269
12621270func TestAddSearchEvents (t * testing.T ) {
12631271 operatorUUID := "test-operator-uuid"
1264-
12651272 now := time .Now ()
12661273
1274+ mdbStatic := & mdbv1.MongoDB {ObjectMeta : metav1.ObjectMeta {Namespace : "default" , Name : "mdb-static" , Annotations : map [string ]string {architectures .ArchitectureAnnotation : string (architectures .Static )}}}
1275+ mdbNonStatic := & mdbv1.MongoDB {ObjectMeta : metav1.ObjectMeta {Namespace : "default" , Name : "mdb-nonstatic" , Annotations : map [string ]string {architectures .ArchitectureAnnotation : string (architectures .NonStatic )}}}
1276+ community := & mcov1.MongoDBCommunity {ObjectMeta : metav1.ObjectMeta {Namespace : "default" , Name : "community-db" }}
1277+
12671278 testCases := []struct {
1268- name string
1269- resources searchv1.MongoDBSearchList
1270- events []DeploymentUsageSnapshotProperties
1279+ name string
1280+ searchItems []searchv1.MongoDBSearch
1281+ events []DeploymentUsageSnapshotProperties
1282+ sources map [reflect.Type ][]client.Object
12711283 }{
12721284 {
1273- name : "With resources" ,
1274- resources : searchv1.MongoDBSearchList {
1275- Items : []searchv1.MongoDBSearch {
1276- {
1277- ObjectMeta : metav1.ObjectMeta {
1278- UID : types .UID ("search-1" ),
1279- Name : "test-search-1" ,
1280- },
1281- },
1282- {
1283- ObjectMeta : metav1.ObjectMeta {
1284- UID : types .UID ("search-2" ),
1285- Name : "test-search-2" ,
1286- },
1287- },
1288- },
1285+ name : "External source" ,
1286+ searchItems : []searchv1.MongoDBSearch {
1287+ {ObjectMeta : metav1.ObjectMeta {UID : types .UID ("search-external" ), Name : "search-external" , Namespace : "default" }, Spec : searchv1.MongoDBSearchSpec {Source : & searchv1.MongoDBSource {ExternalMongoDBSource : & searchv1.ExternalMongoDBSource {}}}},
1288+ },
1289+ events : []DeploymentUsageSnapshotProperties {{
1290+ DeploymentUID : "search-external" ,
1291+ OperatorID : operatorUUID ,
1292+ Architecture : "external" ,
1293+ IsMultiCluster : false ,
1294+ Type : "Search" ,
1295+ IsRunningEnterpriseImage : false ,
1296+ }},
1297+ },
1298+ {
1299+ name : "Enterprise static and non-static" ,
1300+ searchItems : []searchv1.MongoDBSearch {
1301+ {ObjectMeta : metav1.ObjectMeta {UID : types .UID ("search-static" ), Name : "search-static" , Namespace : "default" }, Spec : searchv1.MongoDBSearchSpec {Source : & searchv1.MongoDBSource {MongoDBResourceRef : & userv1.MongoDBResourceRef {Name : "mdb-static" }}}},
1302+ {ObjectMeta : metav1.ObjectMeta {UID : types .UID ("search-nonstatic" ), Name : "search-nonstatic" , Namespace : "default" }, Spec : searchv1.MongoDBSearchSpec {Source : & searchv1.MongoDBSource {MongoDBResourceRef : & userv1.MongoDBResourceRef {Name : "mdb-nonstatic" }}}},
12891303 },
12901304 events : []DeploymentUsageSnapshotProperties {
1291- {
1292- DeploymentUID : "search-1" ,
1293- OperatorID : operatorUUID ,
1294- Architecture : string (architectures .Static ),
1295- IsMultiCluster : false ,
1296- Type : "Search" ,
1297- IsRunningEnterpriseImage : false ,
1298- },
1299- {
1300- DeploymentUID : "search-2" ,
1301- OperatorID : operatorUUID ,
1302- Architecture : string (architectures .Static ),
1303- IsMultiCluster : false ,
1304- Type : "Search" ,
1305- IsRunningEnterpriseImage : false ,
1306- },
1305+ {DeploymentUID : "search-static" , OperatorID : operatorUUID , Architecture : string (architectures .Static ), IsMultiCluster : false , Type : "Search" , IsRunningEnterpriseImage : true },
1306+ {DeploymentUID : "search-nonstatic" , OperatorID : operatorUUID , Architecture : string (architectures .NonStatic ), IsMultiCluster : false , Type : "Search" , IsRunningEnterpriseImage : true },
1307+ },
1308+ sources : map [reflect.Type ][]client.Object {
1309+ reflect .TypeOf (& mdbv1.MongoDB {}): {mdbStatic , mdbNonStatic },
13071310 },
13081311 },
13091312 {
1310- name : "With no resources" ,
1311- resources : searchv1.MongoDBSearchList {},
1312- events : []DeploymentUsageSnapshotProperties {},
1313+ name : "Community source" ,
1314+ searchItems : []searchv1.MongoDBSearch {
1315+ {ObjectMeta : metav1.ObjectMeta {UID : types .UID ("search-community" ), Name : "search-community" , Namespace : "default" }, Spec : searchv1.MongoDBSearchSpec {Source : & searchv1.MongoDBSource {MongoDBResourceRef : & userv1.MongoDBResourceRef {Name : "community-db" }}}},
1316+ },
1317+ events : []DeploymentUsageSnapshotProperties {{DeploymentUID : "search-community" , OperatorID : operatorUUID , Architecture : "static" , IsMultiCluster : false , Type : "Search" , IsRunningEnterpriseImage : false }},
1318+ sources : map [reflect.Type ][]client.Object {
1319+ reflect .TypeOf (& mcov1.MongoDBCommunity {}): {community },
1320+ },
1321+ },
1322+ {
1323+ name : "Missing underlying resource (skipped)" ,
1324+ searchItems : []searchv1.MongoDBSearch {
1325+ {ObjectMeta : metav1.ObjectMeta {UID : types .UID ("search-missing" ), Name : "search-missing" , Namespace : "default" }, Spec : searchv1.MongoDBSearchSpec {Source : & searchv1.MongoDBSource {MongoDBResourceRef : & userv1.MongoDBResourceRef {Name : "does-not-exist" }}}},
1326+ },
1327+ events : []DeploymentUsageSnapshotProperties {},
1328+ },
1329+ {
1330+ name : "No search resources" ,
1331+ searchItems : []searchv1.MongoDBSearch {},
1332+ events : []DeploymentUsageSnapshotProperties {},
13131333 },
13141334 }
13151335
@@ -1318,10 +1338,23 @@ func TestAddSearchEvents(t *testing.T) {
13181338 mc := & MockClient {
13191339 MockList : func (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
13201340 if l , ok := list .(* searchv1.MongoDBSearchList ); ok {
1321- * l = tc .resources
1341+ * l = searchv1. MongoDBSearchList { Items : tc .searchItems }
13221342 }
13231343 return nil
13241344 },
1345+ MockGet : func (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
1346+ objects := tc .sources [reflect .TypeOf (obj )]
1347+ for _ , o := range objects {
1348+ if o .GetName () == key .Name && o .GetNamespace () == key .Namespace {
1349+ // copy the arranged object into the obj pointer like controller-runtime's pkg/client/fake does
1350+ bytes , _ := json .Marshal (o )
1351+ json .Unmarshal (bytes , obj )
1352+ return nil
1353+ }
1354+ }
1355+
1356+ return errors .New ("not found" )
1357+ },
13251358 }
13261359
13271360 events := addSearchEvents (context .Background (), mc , operatorUUID , now )
0 commit comments