@@ -1673,3 +1673,112 @@ func Test_SmokeTest_AuditUnenroll(t *testing.T) {
16731673 cancel ()
16741674 srv .waitExit () //nolint:errcheck // test case
16751675}
1676+
1677+ func TestCheckinOTelColPolicy (t * testing.T ) {
1678+ ctx , cancel := context .WithCancel (t .Context ())
1679+ defer cancel ()
1680+
1681+ idSuffix := uuid .Must (uuid .NewV4 ()).String ()
1682+ componentID := func (id string ) string {
1683+ return fmt .Sprintf ("%s/%s" , id , idSuffix )
1684+ }
1685+ policyData := model.PolicyData {
1686+ Outputs : map [string ]map [string ]interface {}{
1687+ "default" : {
1688+ "type" : "elasticsearch" ,
1689+ },
1690+ },
1691+ OutputPermissions : json .RawMessage (`{"default": {}}` ),
1692+ Inputs : []map [string ]any {},
1693+ Receivers : map [string ]any {
1694+ componentID ("somereceiver" ): map [string ]any {},
1695+ },
1696+ Processors : map [string ]any {
1697+ componentID ("someprocessor" ): map [string ]any {},
1698+ },
1699+ Connectors : map [string ]any {
1700+ componentID ("forward" ): map [string ]any {},
1701+ },
1702+ Exporters : map [string ]any {
1703+ componentID ("someexporter" ): map [string ]any {},
1704+ },
1705+ Service : & model.Service {
1706+ Pipelines : map [string ]* model.PipelinesItem {
1707+ componentID ("metrics" ): & model.PipelinesItem {
1708+ Receivers : []string {componentID ("somereceiver" )},
1709+ Processors : []string {componentID ("someprocessor" )},
1710+ Exporters : []string {componentID ("forward" )},
1711+ },
1712+ "metrics" : & model.PipelinesItem {
1713+ Receivers : []string {componentID ("forward" )},
1714+ Exporters : []string {componentID ("someexporter" )},
1715+ },
1716+ },
1717+ },
1718+ }
1719+
1720+ // Start test server
1721+ srv , err := startTestServer (t , ctx , policyData )
1722+ require .NoError (t , err )
1723+ ctx = testlog .SetLogger (t ).WithContext (ctx )
1724+
1725+ cli := cleanhttp .DefaultClient ()
1726+ // enroll an agent
1727+ t .Log ("Enroll an agent" )
1728+ req , err := http .NewRequestWithContext (ctx , "POST" , srv .buildURL ("" , "enroll" ), strings .NewReader (enrollBody ))
1729+ require .NoError (t , err )
1730+ req .Header .Set ("Authorization" , "ApiKey " + srv .enrollKey )
1731+ req .Header .Set ("User-Agent" , "elastic agent " + serverVersion )
1732+ req .Header .Set ("Content-Type" , "application/json" )
1733+ res , err := cli .Do (req )
1734+ require .NoError (t , err )
1735+
1736+ require .Equal (t , http .StatusOK , res .StatusCode )
1737+ t .Log ("Agent enrollment successful" )
1738+ p , _ := io .ReadAll (res .Body )
1739+ res .Body .Close ()
1740+ var obj map [string ]interface {}
1741+ err = json .Unmarshal (p , & obj )
1742+ require .NoError (t , err )
1743+
1744+ item := obj ["item" ]
1745+ mm , ok := item .(map [string ]interface {})
1746+ require .True (t , ok , "expected attribute item to be an object" )
1747+ agentID , ok := mm ["id" ].(string )
1748+ require .True (t , ok , "expected attribute id to be a string" )
1749+
1750+ apiKey , ok := mm ["access_api_key" ].(string )
1751+ require .True (t , ok , "expected attribute apiKey to be a string" )
1752+
1753+ // checkin
1754+ t .Logf ("Fake a checkin for agent %s" , agentID )
1755+ req , err = http .NewRequestWithContext (ctx , "POST" , srv .buildURL (agentID , "checkin" ), strings .NewReader (checkinBody ))
1756+ require .NoError (t , err )
1757+ req .Header .Set ("Authorization" , "ApiKey " + apiKey )
1758+ req .Header .Set ("User-Agent" , "elastic agent " + serverVersion )
1759+ req .Header .Set ("Content-Type" , "application/json" )
1760+ res , err = cli .Do (req )
1761+ require .NoError (t , err )
1762+ defer res .Body .Close ()
1763+
1764+ require .Equal (t , http .StatusOK , res .StatusCode )
1765+ t .Log ("Checkin successful, verify body" )
1766+ p , err = io .ReadAll (res .Body )
1767+ require .NoError (t , err )
1768+
1769+ err = json .Unmarshal (p , & obj )
1770+ require .NoError (t , err )
1771+
1772+ actionsRaw , ok := obj ["actions" ]
1773+ require .True (t , ok , "expected actions is missing" )
1774+ actions , ok := actionsRaw .([]interface {})
1775+ require .True (t , ok , "expected actions to be an array" )
1776+ require .Greater (t , len (actions ), 0 , "expected at least 1 action" )
1777+ action , ok := actions [0 ].(map [string ]interface {})
1778+ require .True (t , ok , "expected action to be an object" )
1779+ _ , ok = action ["id" ].(string )
1780+ require .True (t , ok , "expected action id to be string" )
1781+ aAgentID , ok := action ["agent_id" ].(string )
1782+ require .True (t , ok , "expected action agent_id to be string" )
1783+ require .Equal (t , agentID , aAgentID )
1784+ }
0 commit comments