@@ -509,7 +509,7 @@ func newFakePlugin(ctx customContext, pluginVersion int) externalPlugin {
509
509
Context : ctx ,
510
510
},
511
511
pluginInstance : & PluginV1Instance {
512
- Labels : map [string ]string {"role" : "fileserver" , "environment" : "development" },
512
+ Labels : map [string ]string {"role" : "fileserver" , "environment" : "development" , "agent_role" : "overwrite agent role" },
513
513
plugin : & Plugin {
514
514
Name : "new-plugin" ,
515
515
ProtocolVersion : pluginVersion ,
@@ -573,7 +573,7 @@ func newFakePluginWithEnvVars(pluginVersion int) externalPlugin {
573
573
Context : ctx ,
574
574
},
575
575
pluginInstance : & PluginV1Instance {
576
- Labels : map [string ]string {"role" : "fileserver" , "environment" : "development" },
576
+ Labels : map [string ]string {"role" : "fileserver" , "environment" : "development" , "agent_role" : "overwrite agent role" },
577
577
plugin : & Plugin {
578
578
Name : "new-plugin" ,
579
579
ProtocolVersion : pluginVersion ,
@@ -676,22 +676,36 @@ func (rs *RunnerSuite) TestPluginHandleOutputV1(c *C) {
676
676
c .Assert (err , IsNil )
677
677
678
678
c .Assert (rd , NotNil )
679
- c .Assert (len (rd .Data ), Equals , 4 )
680
- c .Assert (rd .Data [0 ].SortKey (), Equals , "first" )
681
-
682
- invData := rd .Data [3 ].(protocol.InventoryData )
683
- c .Assert (invData ["id" ], Equals , "integrationUser" )
684
- c .Assert (invData ["value" ], Equals , "test" )
685
679
686
680
c .Assert (event , NotNil )
687
681
c .Assert (event ["event_type" ], Equals , "LoadBalancerSample" )
688
682
c .Assert (event ["id" ], Equals , "first" )
689
683
c .Assert (event ["value" ], Equals , "random" )
690
684
c .Assert (event ["integrationUser" ], Equals , "test" )
691
685
692
- for _ , labelKey := range labelKeys {
693
- if rd .Data [1 ].SortKey () != labelKey && rd .Data [2 ].SortKey () != labelKey {
694
- c .Errorf ("There isn't label '%s'' in the inventory" , labelKey )
686
+ expectedLabelValues := map [string ]string {
687
+ "first" : "fake" ,
688
+ "labels/my_group" : "test group" ,
689
+ "labels/role" : "fileserver" ,
690
+ "labels/environment" : "development" ,
691
+ "labels/agent_role" : "overwrite agent role" ,
692
+ "integrationUser" : "test" ,
693
+ "integrationName" : "test" ,
694
+ "integrationVersion" : "1.0.0" ,
695
+ }
696
+
697
+ c .Assert (len (rd .Data ), Equals , 8 )
698
+
699
+ for _ , item := range rd .Data {
700
+ if invData , ok := item .(protocol.InventoryData ); ok {
701
+ id , _ := invData ["id" ].(string )
702
+ value , _ := invData ["value" ].(string )
703
+
704
+ if expectedValue , exists := expectedLabelValues [id ]; exists {
705
+ c .Assert (value , Equals , expectedValue )
706
+ } else {
707
+ c .Fatalf ("Expected label: %v not found in Inventory" , id )
708
+ }
695
709
}
696
710
}
697
711
}
@@ -726,6 +740,7 @@ func (rs *RunnerSuite) TestPluginHandleOutputEventsV1(c *C) {
726
740
// labels from pluginInstance
727
741
c .Assert (event ["label.environment" ], Equals , "development" )
728
742
c .Assert (event ["label.role" ], Equals , "fileserver" )
743
+ c .Assert (event ["label.agent_role" ], Equals , "overwrite agent role" )
729
744
730
745
// labels from databind
731
746
c .Assert (event ["label.expected" ], Equals , "extra label" )
@@ -831,7 +846,7 @@ func (rs *RunnerSuite) TestEventsPluginRunV1(c *C) {
831
846
c .Assert (err , IsNil )
832
847
833
848
c .Assert (rd , NotNil )
834
- c .Assert (len (rd .Data ), Equals , 3 )
849
+ c .Assert (len (rd .Data ), Equals , 7 )
835
850
c .Assert (rd .Data [0 ].SortKey (), Equals , "first" )
836
851
837
852
c .Assert (event , NotNil )
@@ -1030,7 +1045,7 @@ func (rs *RunnerSuite) TestHandleOutputV1(c *C) {
1030
1045
c .Assert (err , IsNil )
1031
1046
1032
1047
// labels are added as inventory
1033
- c .Assert (len (rd .Data )- len (labelKeys ), Equals , 3 )
1048
+ c .Assert (len (rd .Data )- len (labelKeys ), Equals , 7 )
1034
1049
1035
1050
firstData := rd .Data [0 ]
1036
1051
inv := firstData .(protocol.InventoryData )
@@ -1545,12 +1560,16 @@ func TestParsePayloadV3(t *testing.T) {
1545
1560
type fakeEmitter struct {
1546
1561
lastEventData map [string ]interface {}
1547
1562
lastEntityKey string
1563
+ inventory types.PluginInventoryDataset
1548
1564
}
1549
1565
1550
1566
func (f * fakeEmitter ) EmitInventoryWithPluginId (data types.PluginInventoryDataset , entityKey string , pluginId ids.PluginID ) {
1567
+ f .inventory = data
1551
1568
}
1552
1569
1553
- func (f * fakeEmitter ) EmitInventory (data types.PluginInventoryDataset , entity entity.Entity ) {}
1570
+ func (f * fakeEmitter ) EmitInventory (data types.PluginInventoryDataset , entity entity.Entity ) {
1571
+ f .inventory = data
1572
+ }
1554
1573
1555
1574
func (f * fakeEmitter ) EmitEvent (eventData map [string ]interface {}, entityKey entity.Key ) {
1556
1575
f .lastEventData = eventData
@@ -1586,6 +1605,29 @@ func TestEmitPayloadV2NoDisplayNameNoEntityName(t *testing.T) {
1586
1605
assert .EqualValues (t , "Motorbike" , emitter .lastEventData ["entityName" ])
1587
1606
assert .EqualValues (t , "motorbike:street_hawk" , emitter .lastEventData ["entityKey" ])
1588
1607
1608
+ expectedLabelValues := map [string ]string {
1609
+ "integrationUser" : "testuser" ,
1610
+ "integrationName" : "test/test" ,
1611
+ "integrationVersion" : "x.y.z" ,
1612
+ "reportingAgent" : "my-agent-id" ,
1613
+ "motor" : "" , // we are type converting to string so motor does not have a value
1614
+ }
1615
+
1616
+ assert .EqualValues (t , len (emitter .inventory ), 5 )
1617
+
1618
+ for _ , item := range emitter .inventory {
1619
+ if invData , ok := item .(protocol.InventoryData ); ok {
1620
+ id , _ := invData ["id" ].(string )
1621
+ value , _ := invData ["value" ].(string )
1622
+
1623
+ if expectedValue , exists := expectedLabelValues [id ]; exists {
1624
+ assert .EqualValues (t , value , expectedValue )
1625
+ } else {
1626
+ assert .Fail (t , "Expected label not found in Inventory" , "Label: %s not found in inventory" , id )
1627
+ }
1628
+ }
1629
+ }
1630
+
1589
1631
// Local entity, no displayName, no entityName
1590
1632
assert .NoError (t , EmitDataSet (ctx , & emitter , "test/test" , "x.y.z" , "testuser" , rd .DataSets [2 ], extraAnnotations , labels , entityRewrite , version ))
1591
1633
_ , ok := emitter .lastEventData ["displayName" ]
@@ -1594,6 +1636,27 @@ func TestEmitPayloadV2NoDisplayNameNoEntityName(t *testing.T) {
1594
1636
assert .False (t , ok )
1595
1637
// but entityKey is the agent key
1596
1638
assert .EqualValues (t , "my-agent-id" , emitter .lastEventData ["entityKey" ])
1639
+
1640
+ // Check inventory data
1641
+ assert .EqualValues (t , len (emitter .inventory ), 5 )
1642
+
1643
+ for _ , item := range emitter .inventory {
1644
+ if invData , ok := item .(protocol.InventoryData ); ok {
1645
+ id , _ := invData ["id" ].(string )
1646
+ value , _ := invData ["value" ].(string )
1647
+
1648
+ if expectedValue , exists := expectedLabelValues [id ]; exists {
1649
+ assert .EqualValues (t , value , expectedValue )
1650
+ } else {
1651
+ assert .Fail (t , "Expected label not found in Inventory" , "Label: %s not found in inventory" , id )
1652
+ }
1653
+ }
1654
+ }
1655
+ }
1656
+
1657
+ func createMockConfigWithDataMap (attrs map [string ]interface {}) * config.Config {
1658
+ customAttrs := config .CustomAttributeMap (attrs )
1659
+ return & config.Config {CustomAttributes : customAttrs } //nolint:all
1597
1660
}
1598
1661
1599
1662
func TestEmitDataSet_OnAddHostnameDecoratesWithHostname (t * testing.T ) {
@@ -1626,6 +1689,7 @@ func TestEmitDataSet_OnAddHostnameDecoratesWithHostname(t *testing.T) {
1626
1689
ctx .On ("EntityKey" ).Return (agentIdentifier )
1627
1690
ctx .On ("HostnameResolver" ).Return (newFixedHostnameResolver (hn , "short" ))
1628
1691
ctx .On ("IDLookup" ).Return (newFixedIDLookup ())
1692
+ ctx .On ("Config" ).Return (createMockConfigWithDataMap (make (map [string ]interface {})))
1629
1693
1630
1694
em := & fakeEmitter {}
1631
1695
extraAnnotations := map [string ]string {}
@@ -1677,6 +1741,7 @@ func TestEmitDataSet_EntityNameLocalhostIsNotReplacedWithHostnameV2(t *testing.T
1677
1741
ctx .On ("EntityKey" ).Return (agID )
1678
1742
ctx .On ("HostnameResolver" ).Return (newFixedHostnameResolver ("foo.bar" , "short" ))
1679
1743
ctx .On ("IDLookup" ).Return (newFixedIDLookup ())
1744
+ ctx .On ("Config" ).Return (createMockConfigWithDataMap (make (map [string ]interface {})))
1680
1745
1681
1746
em := & fakeEmitter {}
1682
1747
extraAnnotations := map [string ]string {}
@@ -1724,6 +1789,7 @@ func TestEmitDataSet_EntityNameLocalhostIsReplacedWithHostnameV3(t *testing.T) {
1724
1789
ctx .On ("EntityKey" ).Return (agID )
1725
1790
ctx .On ("HostnameResolver" ).Return (newFixedHostnameResolver ("foo.bar" , "short" ))
1726
1791
ctx .On ("IDLookup" ).Return (newFixedIDLookup ())
1792
+ ctx .On ("Config" ).Return (createMockConfigWithDataMap (make (map [string ]interface {})))
1727
1793
1728
1794
em := & fakeEmitter {}
1729
1795
extraAnnotations := map [string ]string {}
@@ -1772,6 +1838,7 @@ func TestEmitDataSet_MetricHostnameIsReplacedIfLocalhostV3(t *testing.T) {
1772
1838
ctx .On ("EntityKey" ).Return (agID )
1773
1839
ctx .On ("HostnameResolver" ).Return (newFixedHostnameResolver ("foo.bar" , "short" ))
1774
1840
ctx .On ("IDLookup" ).Return (newFixedIDLookup ())
1841
+ ctx .On ("Config" ).Return (createMockConfigWithDataMap (make (map [string ]interface {})))
1775
1842
1776
1843
em := & fakeEmitter {}
1777
1844
extraAnnotations := map [string ]string {}
@@ -1821,6 +1888,7 @@ func TestEmitDataSet_ReportingFieldsAreReplacedIfLocalhostV3(t *testing.T) {
1821
1888
ctx .On ("EntityKey" ).Return (agID )
1822
1889
ctx .On ("HostnameResolver" ).Return (newFixedHostnameResolver ("foo.bar" , "short" ))
1823
1890
ctx .On ("IDLookup" ).Return (newFixedIDLookup ())
1891
+ ctx .On ("Config" ).Return (createMockConfigWithDataMap (make (map [string ]interface {})))
1824
1892
1825
1893
em := & fakeEmitter {}
1826
1894
extraAnnotations := map [string ]string {}
@@ -1869,6 +1937,7 @@ func TestEmitDataSet_LogsEntityViolationsOncePerEntity(t *testing.T) {
1869
1937
ctx .On ("EntityKey" ).Return (agID )
1870
1938
ctx .On ("HostnameResolver" ).Return (newFixedHostnameResolver ("foo.bar" , "short" ))
1871
1939
ctx .On ("IDLookup" ).Return (newFixedIDLookup ())
1940
+ ctx .On ("Config" ).Return (createMockConfigWithDataMap (make (map [string ]interface {})))
1872
1941
1873
1942
em := & fakeEmitter {}
1874
1943
extraAnnotations := map [string ]string {}
@@ -1906,6 +1975,7 @@ func TestEmitDataSet_DoNotOverrideExistingMetrics(t *testing.T) {
1906
1975
ctx .On ("EntityKey" ).Return ("agent-id" )
1907
1976
ctx .On ("HostnameResolver" ).Return (newFixedHostnameResolver ("long" , "short" ))
1908
1977
ctx .On ("IDLookup" ).Return (newFixedIDLookup ())
1978
+ ctx .On ("Config" ).Return (createMockConfigWithDataMap (make (map [string ]interface {})))
1909
1979
em := & fakeEmitter {}
1910
1980
extraAnnotations := map [string ]string {
1911
1981
"cluster_name" : "K8sDiscoveredCluster" ,
@@ -2207,8 +2277,7 @@ func TestLogFields(t *testing.T) {
2207
2277
"TEMP_DIR" : "a/path" ,
2208
2278
})
2209
2279
assert .Equal (t , fields ["labels" ], map [string ]string {
2210
- "role" : "fileserver" ,
2211
- "environment" : "development" ,
2280
+ "agent_role" : "overwrite agent role" , "environment" : "development" , "role" : "fileserver" ,
2212
2281
})
2213
2282
2214
2283
if runtime .GOOS == "windows" {
0 commit comments