diff --git a/pkg/testing/fixture.go b/pkg/testing/fixture.go index 794d82c67a..95e5cc3fb4 100644 --- a/pkg/testing/fixture.go +++ b/pkg/testing/fixture.go @@ -714,24 +714,100 @@ func (e *ExecErr) Unwrap() error { return e.err } -// ExecStatus executes the status subcommand on the prepared Elastic Agent binary. -// It returns the parsed output and the error from the execution. Keep in mind -// the agent exits with status 1 if it's unhealthy, but it still outputs the -// status successfully. An empty AgentStatusOutput and non nil error -// means the output could not be parsed. -// As long as we get some output, we don't return any error. -// It should work with any 8.6+ agent -func (f *Fixture) ExecStatus(ctx context.Context, opts ...process.CmdOption) (AgentStatusOutput, error) { - out, err := f.Exec(ctx, []string{"status", "--output", "json"}, opts...) - status := AgentStatusOutput{} - if uerr := json.Unmarshal(out, &status); uerr != nil { - return AgentStatusOutput{}, - fmt.Errorf("could not unmarshal agent status output: %w:\n%s", errors.Join(uerr, err), out) - } else if status.IsZero() { - return status, fmt.Errorf("agent status output is empty: %w", err) +type statusOpts struct { + noRetry bool + retryTimeout time.Duration + retryInterval time.Duration + + cmdOptions []process.CmdOption +} + +type statusOpt func(*statusOpts) + +// WithNoRetry disables the retry logic in ExecStatus function call. +func WithNoRetry() func(opt *statusOpts) { + return func(opt *statusOpts) { + opt.noRetry = true } +} - return status, nil +// WithRetryTimeout adjusts the retry timeout from the default value of one minute. +func WithRetryTimeout(duration time.Duration) func(opt *statusOpts) { + return func(opt *statusOpts) { + opt.retryTimeout = duration + } +} + +// WithRetryInterval adjusts the retry interval from the default value of one second. +func WithRetryInterval(duration time.Duration) func(opt *statusOpts) { + return func(opt *statusOpts) { + opt.retryInterval = duration + } +} + +// WithCmdOptions adjusts the options of the command when status is called. +func WithCmdOptions(cmdOptions ...process.CmdOption) func(opt *statusOpts) { + return func(opt *statusOpts) { + opt.cmdOptions = append(opt.cmdOptions, cmdOptions...) + } +} + +// ExecStatus executes `elastic-agent status --output=json`. +// +// Returns the parsed output and the error from the execution. Keep in mind the agent exits with status 1 if it's +// unhealthy, but it still outputs the status successfully. This call does require that the Elastic Agent is running +// and communication over the control protocol is working. +// +// By default, retry logic is applied. Use WithNoRetry to disable this behavior. WithRetryTimeout and WithRetryInterval +// can be used to adjust the retry logic timing. The default retry timeout is one minute and the default retry +// interval is one second. +// +// An empty AgentStatusOutput and non nil error means the output could not be parsed. As long as we get some output, +// we don't return any error. It should work with any 8.6+ agent +func (f *Fixture) ExecStatus(ctx context.Context, opts ...statusOpt) (AgentStatusOutput, error) { + var opt statusOpts + opt.retryTimeout = 1 * time.Minute + opt.retryInterval = 1 * time.Second + for _, o := range opts { + o(&opt) + } + + if opt.noRetry { + out, err := f.Exec(ctx, []string{"status", "--output", "json"}, opt.cmdOptions...) + status := AgentStatusOutput{} + if uerr := json.Unmarshal(out, &status); uerr != nil { + return AgentStatusOutput{}, + fmt.Errorf("could not unmarshal agent status output: %w:\n%s", errors.Join(uerr, err), out) + } else if status.IsZero() { + return status, fmt.Errorf("agent status output is empty: %w", err) + } + } + + ctx, cancel := context.WithTimeout(ctx, opt.retryTimeout) + defer cancel() + + var lastErr error + for { + if ctx.Err() != nil { + if errors.Is(ctx.Err(), context.DeadlineExceeded) && lastErr != nil { + // return the last observed error + return AgentStatusOutput{}, fmt.Errorf("agent status returned an error: %w", lastErr) + } + return AgentStatusOutput{}, fmt.Errorf("agent status failed: %w", ctx.Err()) + } + out, err := f.Exec(ctx, []string{"status", "--output", "json"}, opt.cmdOptions...) + status := AgentStatusOutput{} + if uerr := json.Unmarshal(out, &status); uerr != nil { + // unmarshal error means that json was not outputted due to a communication error + lastErr = fmt.Errorf("could not unmarshal agent status output: %w:\n%s", errors.Join(uerr, err), out) + } else if status.IsZero() { + // still not correct try again for a successful status + lastErr = fmt.Errorf("agent status output is empty: %w", err) + } else { + return status, nil + } + sleepFor(ctx, opt.retryInterval) + } } // ExecInspect executes to inspect subcommand on the prepared Elastic Agent binary. @@ -799,12 +875,21 @@ func (f *Fixture) ExecDiagnostics(ctx context.Context, cmd ...string) (string, e return files[0], err } +// AgentID returns the ID of the installed Elastic Agent. +func (f *Fixture) AgentID(ctx context.Context, opts ...statusOpt) (string, error) { + status, err := f.ExecStatus(ctx, opts...) + if err != nil { + return "", err + } + return status.Info.ID, nil +} + // IsHealthy checks whether the prepared Elastic Agent reports itself as healthy. // It returns an error if either the reported state isn't healthy or if it fails // to fetch the current state. If the status is successfully fetched, but it // isn't healthy, the error will contain the reported status. // This function is compatible with any Elastic Agent version 8.6 or later. -func (f *Fixture) IsHealthy(ctx context.Context, opts ...process.CmdOption) error { +func (f *Fixture) IsHealthy(ctx context.Context, opts ...statusOpt) error { status, err := f.ExecStatus(ctx, opts...) if err != nil { return fmt.Errorf("agent status returned an error: %w", err) @@ -1450,3 +1535,10 @@ func (v *AgentBinaryVersion) String() string { type AgentVersionOutput struct { Binary AgentBinaryVersion `yaml:"binary"` } + +func sleepFor(ctx context.Context, amount time.Duration) { + select { + case <-ctx.Done(): + case <-time.After(amount): + } +} diff --git a/pkg/testing/tools/check/check.go b/pkg/testing/tools/check/check.go index 90925b4d94..288586d47a 100644 --- a/pkg/testing/tools/check/check.go +++ b/pkg/testing/tools/check/check.go @@ -49,10 +49,14 @@ func FleetAgentStatus(ctx context.Context, t *testing.T, fixture *integrationtest.Fixture, client *kibana.Client, - policyID, expectedStatus string) func() bool { return func() bool { - currentStatus, err := fleettools.GetAgentStatus(ctx, client, policyID) + agentID, err := fixture.AgentID(ctx) + if err != nil { + t.Errorf("failed to get agent ID: %s", err.Error()) + return false + } + currentStatus, err := fleettools.GetAgentStatus(ctx, client, agentID) if err != nil { t.Errorf("unable to determine agent status: %s", err.Error()) return false diff --git a/pkg/testing/tools/fleettools/fleet.go b/pkg/testing/tools/fleettools/fleet.go index 20acc6c9f8..3ab42833ed 100644 --- a/pkg/testing/tools/fleettools/fleet.go +++ b/pkg/testing/tools/fleettools/fleet.go @@ -6,13 +6,8 @@ package fleettools import ( "context" - "encoding/json" "errors" "fmt" - "io" - "net/http" - "net/url" - "os" "github.com/gofrs/uuid/v5" @@ -25,151 +20,44 @@ type EnrollParams struct { PolicyID string `json:"policy_id"` } -func extractError(result []byte) error { - var kibanaResult struct { - Message string - Attributes struct { - Objects []struct { - ID string - Error struct { - Message string - } - } - } - } - if err := json.Unmarshal(result, &kibanaResult); err != nil { - return fmt.Errorf("error extracting JSON for error response: %w", err) - } - var errs []error - if kibanaResult.Message != "" { - for _, err := range kibanaResult.Attributes.Objects { - errs = append(errs, fmt.Errorf("id: %s, message: %s", err.ID, err.Error.Message)) - } - if len(errs) == 0 { - return fmt.Errorf("%s", kibanaResult.Message) - } - return fmt.Errorf("%s: %w", kibanaResult.Message, errors.Join(errs...)) - - } - return nil -} - -// GetAgentByPolicyIDAndHostnameFromList get an agent by the local_metadata.host.name property, reading from the agents list -func GetAgentByPolicyIDAndHostnameFromList(ctx context.Context, client *kibana.Client, policyID, hostname string) (*kibana.AgentExisting, error) { - params := url.Values{} - params.Add("kuery", fmt.Sprintf(`local_metadata.host.name:"%s" and policy_id:"%s" and active:true`, hostname, policyID)) - - resp, err := client.Connection.SendWithContext(ctx, http.MethodGet, "/api/fleet/agents", params, nil, nil) - if err != nil { - return nil, fmt.Errorf("error calling list agents API: %w", err) - } - defer resp.Body.Close() - - b, err := io.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("reading response body: %w", err) - } - - if resp.StatusCode != http.StatusOK { - return nil, extractError(b) - } - var r kibana.ListAgentsResponse - err = json.Unmarshal(b, &r) - if err != nil { - return nil, fmt.Errorf("unmarshalling response json: %w", err) - } - - if len(r.Items) == 0 { - return nil, fmt.Errorf("unable to find agent with hostname [%s] for policy [%s]", - hostname, policyID) - } - - if len(r.Items) > 1 { - return nil, fmt.Errorf("found %d agents with hostname [%s] for policy [%s]; expected to find only one, response:\n%s", len(r.Items), hostname, policyID, b) - } - - return &r.Items[0], nil -} - -func GetAgentIDByHostname(ctx context.Context, client *kibana.Client, policyID, hostname string) (string, error) { - agent, err := GetAgentByPolicyIDAndHostnameFromList(ctx, client, policyID, hostname) +func GetAgentStatus(ctx context.Context, client *kibana.Client, agentID string) (string, error) { + agent, err := client.GetAgent(ctx, kibana.GetAgentRequest{ID: agentID}) if err != nil { return "", err } - return agent.Agent.ID, nil -} - -func GetAgentStatus(ctx context.Context, client *kibana.Client, policyID string) (string, error) { - hostname, err := os.Hostname() - if err != nil { - return "", err - } - - agent, err := GetAgentByPolicyIDAndHostnameFromList(ctx, client, policyID, hostname) - if err != nil { - return "", err - } - return agent.Status, nil } -func GetAgentVersion(ctx context.Context, client *kibana.Client, policyID string) (string, error) { - hostname, err := os.Hostname() - if err != nil { - return "", err - } - - agent, err := GetAgentByPolicyIDAndHostnameFromList(ctx, client, policyID, hostname) +func GetAgentVersion(ctx context.Context, client *kibana.Client, agentID string) (string, error) { + agent, err := client.GetAgent(ctx, kibana.GetAgentRequest{ID: agentID}) if err != nil { return "", err } - - return agent.Agent.Version, err + return agent.Agent.Version, nil } -func UnEnrollAgent(ctx context.Context, client *kibana.Client, policyID string) error { - hostname, err := os.Hostname() - if err != nil { - return err - } - agentID, err := GetAgentIDByHostname(ctx, client, policyID, hostname) - if err != nil { - return err - } - +func UnEnrollAgent(ctx context.Context, client *kibana.Client, agentID string) error { unEnrollAgentReq := kibana.UnEnrollAgentRequest{ ID: agentID, Revoke: true, } - _, err = client.UnEnrollAgent(ctx, unEnrollAgentReq) + _, err := client.UnEnrollAgent(ctx, unEnrollAgentReq) if err != nil { return fmt.Errorf("unable to unenroll agent with ID [%s]: %w", agentID, err) } - return nil } -func UpgradeAgent(ctx context.Context, client *kibana.Client, policyID, version string, force bool) error { - // TODO: fix me: this does not work if FQDN is enabled - hostname, err := os.Hostname() - if err != nil { - return err - } - agentID, err := GetAgentIDByHostname(ctx, client, policyID, hostname) - if err != nil { - return err - } - +func UpgradeAgent(ctx context.Context, client *kibana.Client, agentID, version string, force bool) error { upgradeAgentReq := kibana.UpgradeAgentRequest{ ID: agentID, Version: version, Force: force, } - _, err = client.UpgradeAgent(ctx, upgradeAgentReq) + _, err := client.UpgradeAgent(ctx, upgradeAgentReq) if err != nil { return fmt.Errorf("unable to upgrade agent with ID [%s]: %w", agentID, err) } - return nil } diff --git a/pkg/testing/tools/tools.go b/pkg/testing/tools/tools.go index df105f20d3..37a18abaae 100644 --- a/pkg/testing/tools/tools.go +++ b/pkg/testing/tools/tools.go @@ -154,7 +154,7 @@ func InstallAgentForPolicyWithToken(ctx context.Context, t *testing.T, // Wait for Agent to be healthy require.Eventually( t, - check.FleetAgentStatus(ctx, t, agentFixture, kibClient, policyID, "online"), + check.FleetAgentStatus(ctx, t, agentFixture, kibClient, "online"), timeout, 10*time.Second, "Elastic Agent status is not online", diff --git a/testing/integration/container_cmd_test.go b/testing/integration/container_cmd_test.go index 3eb6669f57..9e2f1a0ce3 100644 --- a/testing/integration/container_cmd_test.go +++ b/testing/integration/container_cmd_test.go @@ -181,7 +181,7 @@ func TestContainerCMD(t *testing.T) { // the agent logs will be present in the error message // which should help to explain why the agent was not // healthy. - err = agentFixture.IsHealthy(ctx, withEnv(env)) + err = agentFixture.IsHealthy(ctx, atesting.WithCmdOptions(withEnv(env))) return err == nil }, 5*time.Minute, time.Second, @@ -265,7 +265,7 @@ func TestContainerCMDWithAVeryLongStatePath(t *testing.T) { // the agent logs will be present in the error message // which should help to explain why the agent was not // healthy. - err = agentFixture.IsHealthy(ctx, withEnv(env)) + err = agentFixture.IsHealthy(ctx, atesting.WithCmdOptions(withEnv(env))) return err == nil }, 1*time.Minute, time.Second, @@ -369,7 +369,7 @@ func TestContainerCMDEventToStderr(t *testing.T) { // the agent logs will be present in the error message // which should help to explain why the agent was not // healthy. - err := agentFixture.IsHealthy(ctx, withEnv(env)) + err := agentFixture.IsHealthy(ctx, atesting.WithCmdOptions(withEnv(env))) return err == nil }, 2*time.Minute, time.Second, @@ -402,7 +402,7 @@ func createMockESOutput(t *testing.T, info *define.Info) (string, string) { "%s" ], "preset": "latency" -} +} ` // The API will return an error if the output ID/name contains an // UUID substring, so we replace the '-' by '_' to keep the API happy. diff --git a/testing/integration/endpoint_security_test.go b/testing/integration/endpoint_security_test.go index 56940d3a9f..f872596819 100644 --- a/testing/integration/endpoint_security_test.go +++ b/testing/integration/endpoint_security_test.go @@ -195,13 +195,15 @@ func testInstallAndCLIUninstallWithEndpointSecurity(t *testing.T, info *define.I defer cancel() fixture, policy := installSecurityAgent(ctx, t, info, protected) + agentID, err := fixture.AgentID(ctx) + require.NoError(t, err) t.Cleanup(func() { t.Log("Un-enrolling Elastic Agent...") // Use a separate context as the one in the test body will have been cancelled at this point. cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), time.Minute) defer cleanupCancel() - assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, policy.ID)) + assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, agentID)) }) t.Log("Installing Elastic Defend") @@ -230,9 +232,11 @@ func testInstallAndUnenrollWithEndpointSecurity(t *testing.T, info *define.Info, defer cn() fixture, policy := installSecurityAgent(ctx, t, info, protected) + agentID, err := fixture.AgentID(ctx) + require.NoError(t, err) t.Log("Installing Elastic Defend") - _, err := installElasticDefendPackage(t, info, policy.ID) + _, err = installElasticDefendPackage(t, info, policy.ID) require.NoError(t, err) t.Log("Polling for endpoint-security to become Healthy") @@ -254,12 +258,6 @@ func testInstallAndUnenrollWithEndpointSecurity(t *testing.T, info *define.Info, // Unenroll the agent t.Log("Unenrolling the agent") - hostname, err := os.Hostname() - require.NoError(t, err) - - agentID, err := fleettools.GetAgentIDByHostname(ctx, info.KibanaClient, policy.ID, hostname) - require.NoError(t, err) - _, err = info.KibanaClient.UnEnrollAgent(ctx, kibana.UnEnrollAgentRequest{ID: agentID}) require.NoError(t, err) @@ -655,13 +653,15 @@ func TestEndpointLogsAreCollectedInDiagnostics(t *testing.T) { policyResp, err := tools.InstallAgentWithPolicy(ctx, t, installOpts, fixture, info.KibanaClient, createPolicyReq) require.NoErrorf(t, err, "Policy Response was: %v", policyResp) + agentID, err := fixture.AgentID(ctx) + require.NoError(t, err) t.Cleanup(func() { t.Log("Un-enrolling Elastic Agent...") // Use a separate context as the one in the test body will have been cancelled at this point. cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), time.Minute) defer cleanupCancel() - assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, policyResp.ID)) + assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, agentID)) }) t.Log("Installing Elastic Defend") @@ -852,13 +852,16 @@ func TestForceInstallOverProtectedPolicy(t *testing.T) { defer cancel() fixture, policy := installSecurityAgent(ctx, t, info, true) + agentID, err := fixture.AgentID(ctx) + require.NoError(t, err) + t.Logf("Agent ID: %q", agentID) t.Cleanup(func() { t.Log("Un-enrolling Elastic Agent...") // Use a separate context as the one in the test body will have been cancelled at this point. cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), time.Minute) defer cleanupCancel() - assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, policy.ID)) + assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, agentID)) }) t.Log("Installing Elastic Defend") diff --git a/testing/integration/enroll_unprivileged_test.go b/testing/integration/enroll_unprivileged_test.go index 48bf6ab4f3..81c572f64f 100644 --- a/testing/integration/enroll_unprivileged_test.go +++ b/testing/integration/enroll_unprivileged_test.go @@ -8,7 +8,6 @@ package integration import ( "context" - "os" "testing" "github.com/gofrs/uuid/v5" @@ -57,13 +56,11 @@ func TestEnrollUnprivileged(t *testing.T) { err = tools.InstallAgentForPolicyWithToken(ctx, t, installOpts, fixture, info.KibanaClient, policy.ID, enrollmentApiKey) require.NoError(t, err) - hostname, err := os.Hostname() + agentID, err := fixture.AgentID(ctx) require.NoError(t, err) + t.Logf("Agent ID: %q", agentID) - agent, err := fleettools.GetAgentByPolicyIDAndHostnameFromList(ctx, info.KibanaClient, policy.ID, hostname) - require.NoError(t, err) - - _, err = info.KibanaClient.UnEnrollAgent(ctx, kibana.UnEnrollAgentRequest{ID: agent.ID}) + _, err = info.KibanaClient.UnEnrollAgent(ctx, kibana.UnEnrollAgentRequest{ID: agentID}) require.NoError(t, err) enrollUrl, err := fleettools.DefaultURL(ctx, info.KibanaClient) diff --git a/testing/integration/fqdn_test.go b/testing/integration/fqdn_test.go index d57f11fd53..f583b8d97d 100644 --- a/testing/integration/fqdn_test.go +++ b/testing/integration/fqdn_test.go @@ -91,13 +91,16 @@ func TestFQDN(t *testing.T) { policy, err := tools.InstallAgentWithPolicy(ctx, t, installOpts, agentFixture, kibClient, createPolicyReq) require.NoError(t, err) + agentID, err := agentFixture.AgentID(ctx) + require.NoError(t, err) + t.Cleanup(func() { // Use a separate context as the one in the test body will have been cancelled at this point. cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), time.Minute) defer cleanupCancel() t.Log("Un-enrolling Elastic Agent...") - assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, policy.ID)) + assert.NoError(t, fleettools.UnEnrollAgent(cleanupCtx, info.KibanaClient, agentID)) t.Log("Restoring hostname...") err := setHostname(cleanupCtx, origHostname, t.Log) @@ -109,7 +112,7 @@ func TestFQDN(t *testing.T) { }) t.Log("Verify that agent name is short hostname") - agent := verifyAgentName(ctx, t, policy.ID, shortName, info.KibanaClient) + agent := verifyAgentName(ctx, t, agentID, shortName, info.KibanaClient) t.Log("Verify that hostname in `logs-*` and `metrics-*` is short hostname") verifyHostNameInIndices(t, "logs-*", shortName, info.Namespace, info.ESClient) @@ -140,7 +143,7 @@ func TestFQDN(t *testing.T) { ) t.Log("Verify that agent name is FQDN") - verifyAgentName(ctx, t, policy.ID, fqdn, info.KibanaClient) + verifyAgentName(ctx, t, agentID, fqdn, info.KibanaClient) t.Log("Verify that hostname in `logs-*` and `metrics-*` is FQDN") verifyHostNameInIndices(t, "logs-*", fqdn, info.Namespace, info.ESClient) @@ -171,7 +174,7 @@ func TestFQDN(t *testing.T) { ) t.Log("Verify that agent name is short hostname again") - verifyAgentName(ctx, t, policy.ID, shortName, info.KibanaClient) + verifyAgentName(ctx, t, agentID, shortName, info.KibanaClient) // TODO: Re-enable assertion once https://github.com/elastic/elastic-agent/issues/3078 is // investigated for root cause and resolved. @@ -180,17 +183,17 @@ func TestFQDN(t *testing.T) { // verifyHostNameInIndices(t, "metrics-*", shortName, info.ESClient) } -func verifyAgentName(ctx context.Context, t *testing.T, policyID, hostname string, kibClient *kibana.Client) *kibana.AgentExisting { +func verifyAgentName(ctx context.Context, t *testing.T, agentID, hostname string, kibClient *kibana.Client) kibana.GetAgentResponse { t.Helper() - var agent *kibana.AgentExisting + var agent kibana.GetAgentResponse var err error require.Eventually( t, func() bool { - agent, err = fleettools.GetAgentByPolicyIDAndHostnameFromList(ctx, kibClient, policyID, hostname) - return err == nil && agent != nil + agent, err = kibClient.GetAgent(ctx, kibana.GetAgentRequest{ID: agentID}) + return err == nil && agent.LocalMetadata.Host.Hostname == hostname }, 5*time.Minute, 5*time.Second, diff --git a/testing/integration/linux_deb_test.go b/testing/integration/linux_deb_test.go index a7de3170ff..48b617390e 100644 --- a/testing/integration/linux_deb_test.go +++ b/testing/integration/linux_deb_test.go @@ -166,6 +166,10 @@ func TestDebFleetUpgrade(t *testing.T) { createPolicyReq) require.NoError(t, err) t.Logf("created policy: %s", policy.ID) + + agentID, err := startFixture.AgentID(ctx) + require.NoError(t, err) + t.Logf("Agent ID: %q", agentID) check.ConnectedToFleet(ctx, t, startFixture, 5*time.Minute) // 3. Upgrade deb to the build version @@ -180,7 +184,7 @@ func TestDebFleetUpgrade(t *testing.T) { // Fleet will not include the `-SNAPSHOT` in the `GetAgentVersion` result noSnapshotVersion := strings.TrimSuffix(define.Version(), "-SNAPSHOT") require.Eventually(t, func() bool { - newVersion, err := fleettools.GetAgentVersion(ctx, info.KibanaClient, policy.ID) + newVersion, err := fleettools.GetAgentVersion(ctx, info.KibanaClient, agentID) if err != nil { t.Logf("error getting agent version: %v", err) return false diff --git a/testing/integration/linux_rpm_test.go b/testing/integration/linux_rpm_test.go index b76313cd33..ceb0381947 100644 --- a/testing/integration/linux_rpm_test.go +++ b/testing/integration/linux_rpm_test.go @@ -167,6 +167,10 @@ func TestRpmFleetUpgrade(t *testing.T) { createPolicyReq) require.NoError(t, err) t.Logf("created policy: %s", policy.ID) + + agentID, err := startFixture.AgentID(ctx) + require.NoError(t, err) + t.Logf("Agent ID: %q", agentID) check.ConnectedToFleet(ctx, t, startFixture, 5*time.Minute) // 3. Upgrade rpm to the build version @@ -180,7 +184,7 @@ func TestRpmFleetUpgrade(t *testing.T) { noSnapshotVersion := strings.TrimSuffix(define.Version(), "-SNAPSHOT") require.Eventually(t, func() bool { t.Log("Getting Agent version...") - newVersion, err := fleettools.GetAgentVersion(ctx, info.KibanaClient, policy.ID) + newVersion, err := fleettools.GetAgentVersion(ctx, info.KibanaClient, agentID) if err != nil { t.Logf("error getting agent version: %v", err) return false diff --git a/testing/integration/logs_ingestion_test.go b/testing/integration/logs_ingestion_test.go index daefef20bc..82dd9ccdd7 100644 --- a/testing/integration/logs_ingestion_test.go +++ b/testing/integration/logs_ingestion_test.go @@ -32,7 +32,6 @@ import ( atesting "github.com/elastic/elastic-agent/pkg/testing" "github.com/elastic/elastic-agent/pkg/testing/define" "github.com/elastic/elastic-agent/pkg/testing/tools" - "github.com/elastic/elastic-agent/pkg/testing/tools/fleettools" "github.com/elastic/elastic-agent/pkg/testing/tools/testcontext" "github.com/elastic/elastic-agent/testing/installtest" "github.com/elastic/elastic-transport-go/v8/elastictransport" @@ -179,6 +178,10 @@ func testMonitoringLogsAreShipped( "Looking for logs in dataset 'elastic_agent.metricbeat'") // Stage 2: make sure all components are healthy + t.Logf("Getting Agent ID") + agentID, err := agentFixture.AgentID(ctx) + require.NoError(t, err) + t.Logf("Agent ID: %q", agentID) t.Log("Making sure all components are healthy") status, err := agentFixture.ExecStatus(ctx) require.NoError(t, err, @@ -237,16 +240,6 @@ func testMonitoringLogsAreShipped( require.NotZero(t, len(docs.Hits.Hits)) // Stage 4: verify logs from the monitoring components are not sent to the output - t.Log("Check monitoring logs") - hostname, err := os.Hostname() - if err != nil { - t.Fatalf("could not get hostname to filter Agent: %s", err) - } - - agentID, err := fleettools.GetAgentIDByHostname(ctx, info.KibanaClient, policy.ID, hostname) - require.NoError(t, err, "could not get Agent ID by hostname") - t.Logf("Agent ID: %q", agentID) - // We cannot search for `component.id` because at the moment of writing // this field is not mapped. There is an issue for that: // https://github.com/elastic/integrations/issues/6545 diff --git a/testing/integration/upgrade_fleet_test.go b/testing/integration/upgrade_fleet_test.go index dc78e4a24c..89fe91079c 100644 --- a/testing/integration/upgrade_fleet_test.go +++ b/testing/integration/upgrade_fleet_test.go @@ -412,23 +412,25 @@ func testUpgradeFleetManagedElasticAgent( t.Log("Waiting for enrolled Agent status to be online...") require.Eventually(t, check.FleetAgentStatus( - ctx, t, startFixture, kibClient, policyResp.ID, "online"), + ctx, t, startFixture, kibClient, "online"), 2*time.Minute, 10*time.Second, "Agent status is not online") + agentID, err := startFixture.AgentID(ctx) + require.NoError(t, err) + t.Logf("Agent ID: %q", agentID) + t.Logf("Upgrading from version \"%s-%s\" to version \"%s-%s\"...", startParsedVersion, startVersionInfo.Binary.Commit, endVersionInfo.Binary.String(), endVersionInfo.Binary.Commit) - err = fleettools.UpgradeAgent(ctx, kibClient, policyResp.ID, endVersionInfo.Binary.String(), true) + err = fleettools.UpgradeAgent(ctx, kibClient, agentID, endVersionInfo.Binary.String(), true) require.NoError(t, err) t.Log("Waiting from upgrade details to show up in Fleet") - hostname, err := os.Hostname() - require.NoError(t, err) - var agent *kibana.AgentExisting + var agent kibana.GetAgentResponse require.Eventuallyf(t, func() bool { - agent, err = fleettools.GetAgentByPolicyIDAndHostnameFromList(ctx, kibClient, policy.ID, hostname) + agent, err = kibClient.GetAgent(ctx, kibana.GetAgentRequest{ID: agentID}) return err == nil && agent.UpgradeDetails != nil }, 5*time.Minute, time.Second, @@ -446,12 +448,12 @@ func testUpgradeFleetManagedElasticAgent( require.NoError(t, err) t.Log("Waiting for enrolled Agent status to be online...") - require.Eventually(t, check.FleetAgentStatus(ctx, t, startFixture, kibClient, policyResp.ID, "online"), 10*time.Minute, 15*time.Second, "Agent status is not online") + require.Eventually(t, check.FleetAgentStatus(ctx, t, startFixture, kibClient, "online"), 10*time.Minute, 15*time.Second, "Agent status is not online") // wait for version require.Eventually(t, func() bool { t.Log("Getting Agent version...") - newVersion, err := fleettools.GetAgentVersion(ctx, kibClient, policyResp.ID) + newVersion, err := fleettools.GetAgentVersion(ctx, kibClient, agentID) if err != nil { t.Logf("error getting agent version: %v", err) return false @@ -563,10 +565,7 @@ func newArtifactsServer(ctx context.Context, t *testing.T, version string, packa })) } -func agentUpgradeDetailsString(a *kibana.AgentExisting) string { - if a == nil { - return "agent is NIL" - } +func agentUpgradeDetailsString(a kibana.GetAgentResponse) string { if a.UpgradeDetails == nil { return "upgrade details is NIL" }