@@ -220,7 +220,11 @@ func (s *AzureAssetsFetcherTestSuite) TestFetcher_Fetch() {
220
220
221
221
ecs , err := result .GetElasticCommonData ()
222
222
s .Require ().NoError (err )
223
- s .Empty (ecs )
223
+ if expected .Type == inventory .VirtualMachineAssetType {
224
+ s .Contains (ecs , "host.name" )
225
+ } else {
226
+ s .Empty (ecs )
227
+ }
224
228
})
225
229
}
226
230
}
@@ -266,6 +270,143 @@ func (s *AzureAssetsFetcherTestSuite) TestFetcher_Fetch_Errors() {
266
270
}, metadata )
267
271
}
268
272
273
+ func (s * AzureAssetsFetcherTestSuite ) TestFetcher_Fetch_VM_Hostname_Empty () {
274
+ mockAssetGroups := make (map [string ][]inventory.AzureAsset )
275
+ totalMockAssets := 0
276
+ var flatMockAssets []inventory.AzureAsset
277
+ for _ , assetGroup := range AzureAssetGroups {
278
+ var mockAssets []inventory.AzureAsset
279
+ propertyVariations := [](map [string ]any ){
280
+ map [string ]any {"definitelyNotOsProfile" : "nope" },
281
+ map [string ]any {"osProfile" : map [string ]any {"notComputerName" : "nope" }},
282
+ map [string ]any {"osProfile" : map [string ]any {"computerName" : 42 }},
283
+ }
284
+ for _ , properties := range propertyVariations {
285
+ mockAssets = append (mockAssets ,
286
+ inventory.AzureAsset {
287
+ Id : "id" ,
288
+ Name : "name" ,
289
+ Location : "location" ,
290
+ Properties : properties ,
291
+ ResourceGroup : "rg" ,
292
+ SubscriptionId : "subId" ,
293
+ TenantId : "tenantId" ,
294
+ Type : inventory .VirtualMachineAssetType ,
295
+ Sku : map [string ]any {"key" : "value" },
296
+ Identity : map [string ]any {"key" : "value" },
297
+ },
298
+ )
299
+ }
300
+ totalMockAssets += len (mockAssets )
301
+ mockAssetGroups [assetGroup ] = mockAssets
302
+ flatMockAssets = append (flatMockAssets , mockAssets ... )
303
+ }
304
+
305
+ mockProvider := azurelib .NewMockProviderAPI (s .T ())
306
+ mockProvider .EXPECT ().GetSubscriptions (mock .Anything , mock .Anything ).Return (
307
+ map [string ]governance.Subscription {
308
+ "subId" : {
309
+ FullyQualifiedID : "subId" ,
310
+ ShortID : "subId" ,
311
+ DisplayName : "subName" ,
312
+ ManagementGroup : governance.ManagementGroup {
313
+ FullyQualifiedID : "mgId" ,
314
+ DisplayName : "mgName" ,
315
+ },
316
+ },
317
+ }, nil ,
318
+ ).Twice ()
319
+ mockProvider .EXPECT ().
320
+ ListDiagnosticSettingsAssetTypes (mock .Anything , cycle.Metadata {}, []string {"subId" }).
321
+ Return (nil , nil ).
322
+ Once ()
323
+ mockProvider .EXPECT ().
324
+ ListAllAssetTypesByName (mock .Anything , mock .AnythingOfType ("string" ), mock .AnythingOfType ("[]string" )).
325
+ RunAndReturn (func (_ context.Context , assetGroup string , _ []string ) ([]inventory.AzureAsset , error ) {
326
+ return mockAssetGroups [assetGroup ], nil
327
+ })
328
+
329
+ results , err := s .fetch (mockProvider , totalMockAssets )
330
+ s .Require ().NoError (err )
331
+ for index , result := range results {
332
+ expected := flatMockAssets [index ]
333
+ s .Run (expected .Type , func () {
334
+ s .Equal (expected , result .GetData ())
335
+
336
+ ecs , err := result .GetElasticCommonData ()
337
+ s .Require ().NoError (err )
338
+ s .Contains (ecs , "host.name" )
339
+ s .NotContains (ecs , "host.hostname" , "ECS data should not contain host.hostname for incomplete properties" )
340
+ })
341
+ }
342
+ }
343
+
344
+ func (s * AzureAssetsFetcherTestSuite ) TestFetcher_Fetch_VM_Hostname_OK () {
345
+ mockAssetGroups := make (map [string ][]inventory.AzureAsset )
346
+ totalMockAssets := 0
347
+ var flatMockAssets []inventory.AzureAsset
348
+ for _ , assetGroup := range AzureAssetGroups {
349
+ var mockAssets []inventory.AzureAsset
350
+ mockAssets = append (mockAssets ,
351
+ inventory.AzureAsset {
352
+ Id : "id" ,
353
+ Name : "name" ,
354
+ Location : "location" ,
355
+ Properties : map [string ]any {"osProfile" : map [string ]any {"computerName" : "hostname" }},
356
+ ResourceGroup : "rg" ,
357
+ SubscriptionId : "subId" ,
358
+ TenantId : "tenantId" ,
359
+ Type : inventory .VirtualMachineAssetType ,
360
+ Sku : map [string ]any {"key" : "value" },
361
+ Identity : map [string ]any {"key" : "value" },
362
+ },
363
+ )
364
+ totalMockAssets += len (mockAssets )
365
+ mockAssetGroups [assetGroup ] = mockAssets
366
+ flatMockAssets = append (flatMockAssets , mockAssets ... )
367
+ }
368
+
369
+ mockProvider := azurelib .NewMockProviderAPI (s .T ())
370
+ mockProvider .EXPECT ().GetSubscriptions (mock .Anything , mock .Anything ).Return (
371
+ map [string ]governance.Subscription {
372
+ "subId" : {
373
+ FullyQualifiedID : "subId" ,
374
+ ShortID : "subId" ,
375
+ DisplayName : "subName" ,
376
+ ManagementGroup : governance.ManagementGroup {
377
+ FullyQualifiedID : "mgId" ,
378
+ DisplayName : "mgName" ,
379
+ },
380
+ },
381
+ }, nil ,
382
+ ).Twice ()
383
+ mockProvider .EXPECT ().
384
+ ListDiagnosticSettingsAssetTypes (mock .Anything , cycle.Metadata {}, []string {"subId" }).
385
+ Return (nil , nil ).
386
+ Once ()
387
+ mockProvider .EXPECT ().
388
+ ListAllAssetTypesByName (mock .Anything , mock .AnythingOfType ("string" ), mock .AnythingOfType ("[]string" )).
389
+ RunAndReturn (func (_ context.Context , assetGroup string , _ []string ) ([]inventory.AzureAsset , error ) {
390
+ return mockAssetGroups [assetGroup ], nil
391
+ })
392
+
393
+ results , err := s .fetch (mockProvider , totalMockAssets )
394
+ s .Require ().NoError (err )
395
+ for index , result := range results {
396
+ expected := flatMockAssets [index ]
397
+ s .Run (expected .Type , func () {
398
+ s .Equal (expected , result .GetData ())
399
+
400
+ ecs , err := result .GetElasticCommonData ()
401
+ s .Require ().NoError (err )
402
+ s .Contains (ecs , "host.name" )
403
+ s .Equal ("name" , ecs ["host.name" ])
404
+ s .Contains (ecs , "host.hostname" )
405
+ s .Equal ("hostname" , ecs ["host.hostname" ])
406
+ })
407
+ }
408
+ }
409
+
269
410
func (s * AzureAssetsFetcherTestSuite ) fetch (provider * azurelib.MockProviderAPI , expectedLength int ) ([]fetching.ResourceInfo , error ) {
270
411
fetcher := AzureAssetsFetcher {
271
412
log : testhelper .NewLogger (s .T ()),
0 commit comments