77package ess
88
99import (
10- "bytes"
1110 "context"
12- "encoding/json"
13- "fmt"
1411 "net/http"
1512 "testing"
1613 "time"
1714
1815 "github.com/gofrs/uuid/v5"
16+ "github.com/stretchr/testify/assert"
1917 "github.com/stretchr/testify/require"
2018 "github.com/stretchr/testify/suite"
2119
@@ -67,6 +65,7 @@ type MonitoringTextRunner struct {
6765 suite.Suite
6866 info * define.Info
6967 agentFixture * atesting.Fixture
68+ agentID string
7069
7170 ESHost string
7271
@@ -121,11 +120,12 @@ func (runner *MonitoringTextRunner) SetupSuite() {
121120 err = runner .agentFixture .WriteFileToWorkDir (ctx , defaultTextCfg , "elastic-agent.yml" )
122121 require .NoError (runner .T (), err )
123122
124- policyResp , _ , err := tools .InstallAgentWithPolicy (ctx , runner .T (), installOpts , runner .agentFixture , runner .info .KibanaClient , basePolicy )
123+ policyResp , agentID , err := tools .InstallAgentWithPolicy (ctx , runner .T (), installOpts , runner .agentFixture , runner .info .KibanaClient , basePolicy )
125124 require .NoError (runner .T (), err )
126125
127126 runner .policyID = policyResp .ID
128127 runner .policyName = basePolicy .Name
128+ runner .agentID = agentID
129129
130130 _ , err = tools .InstallPackageFromDefaultFile (ctx , runner .info .KibanaClient , "system" ,
131131 integration .PreinstalledPackages ["system" ], "testdata/system_integration_setup.json" , uuid .Must (uuid .NewV4 ()).String (), policyResp .ID )
@@ -149,10 +149,10 @@ func (runner *MonitoringTextRunner) TestMonitoringLiveness() {
149149 require .Equal (runner .T (), http .StatusOK , initResp .StatusCode )
150150
151151 // use the fleet override API to change the port that we're running on.
152- override := map [ string ] interface {} {
153- "name" : runner .policyName ,
154- "namespace" : "default" ,
155- "overrides" : map [string ]interface {}{
152+ overrideUpdateRequest := kibana. AgentPolicyUpdateRequest {
153+ Name : runner .policyName ,
154+ Namespace : "default" ,
155+ Overrides : map [string ]interface {}{
156156 "agent" : map [string ]interface {}{
157157 "monitoring" : map [string ]interface {}{
158158 "http" : map [string ]interface {}{
@@ -165,20 +165,23 @@ func (runner *MonitoringTextRunner) TestMonitoringLiveness() {
165165 },
166166 }
167167
168- raw , err := json . Marshal ( override )
168+ policyResponse , err := runner . info . KibanaClient . UpdatePolicy ( ctx , runner . policyID , overrideUpdateRequest )
169169 require .NoError (runner .T (), err )
170- reader := bytes .NewBuffer (raw )
171- overrideEndpoint := fmt .Sprintf ("/api/fleet/agent_policies/%s" , runner .policyID )
172- statusCode , overrideResp , err := runner .info .KibanaClient .Request ("PUT" , overrideEndpoint , nil , nil , reader )
173- require .NoError (runner .T (), err )
174- require .Equal (runner .T (), http .StatusOK , statusCode , "non-200 status code; got response: %s" , string (overrideResp ))
170+
171+ // verify the new policy revision was applied
172+ require .Eventually (
173+ runner .T (),
174+ tools .IsPolicyRevision (ctx , runner .T (), runner .info .KibanaClient , runner .agentID , policyResponse .Revision ),
175+ 5 * time .Minute , time .Second )
175176
176177 runner .AllComponentsHealthy (ctx )
177178
178179 updatedEndpoint := "http://localhost:6792/processes"
179180 // second stage: ensure the HTTP config has updated
180- req , err = http .NewRequestWithContext (ctx , "GET" , updatedEndpoint , nil )
181- require .NoError (runner .T (), err )
181+ require .EventuallyWithT (runner .T (), func (collect * assert.CollectT ) {
182+ req , err = http .NewRequestWithContext (ctx , "GET" , updatedEndpoint , nil )
183+ require .NoError (collect , err )
184+ }, time .Minute , time .Second )
182185
183186 initResp , err = client .Do (req )
184187 require .NoError (runner .T (), err )
0 commit comments