@@ -2,7 +2,9 @@ package telemetry
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"errors"
7
+ "reflect"
6
8
"runtime"
7
9
"testing"
8
10
"time"
@@ -21,6 +23,7 @@ import (
21
23
"github.com/mongodb/mongodb-kubernetes/api/v1/mdbmulti"
22
24
omv1 "github.com/mongodb/mongodb-kubernetes/api/v1/om"
23
25
searchv1 "github.com/mongodb/mongodb-kubernetes/api/v1/search"
26
+ userv1 "github.com/mongodb/mongodb-kubernetes/api/v1/user"
24
27
"github.com/mongodb/mongodb-kubernetes/controllers/operator/mock"
25
28
mcov1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
26
29
mockClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client"
@@ -1157,12 +1160,17 @@ func findEventWithDeploymentUID(events []Event, deploymentUID string) *Event {
1157
1160
type MockClient struct {
1158
1161
client.Client
1159
1162
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
1160
1164
}
1161
1165
1162
1166
func (m * MockClient ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
1163
1167
return m .MockList (ctx , list , opts ... )
1164
1168
}
1165
1169
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
+
1166
1174
func TestAddCommunityEvents (t * testing.T ) {
1167
1175
operatorUUID := "test-operator-uuid"
1168
1176
@@ -1261,55 +1269,67 @@ func TestAddCommunityEvents(t *testing.T) {
1261
1269
1262
1270
func TestAddSearchEvents (t * testing.T ) {
1263
1271
operatorUUID := "test-operator-uuid"
1264
-
1265
1272
now := time .Now ()
1266
1273
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
+
1267
1278
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
1271
1283
}{
1272
1284
{
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" }}}},
1289
1303
},
1290
1304
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 },
1307
1310
},
1308
1311
},
1309
1312
{
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 {},
1313
1333
},
1314
1334
}
1315
1335
@@ -1318,10 +1338,23 @@ func TestAddSearchEvents(t *testing.T) {
1318
1338
mc := & MockClient {
1319
1339
MockList : func (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
1320
1340
if l , ok := list .(* searchv1.MongoDBSearchList ); ok {
1321
- * l = tc .resources
1341
+ * l = searchv1. MongoDBSearchList { Items : tc .searchItems }
1322
1342
}
1323
1343
return nil
1324
1344
},
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
+ },
1325
1358
}
1326
1359
1327
1360
events := addSearchEvents (context .Background (), mc , operatorUUID , now )
0 commit comments