Skip to content

Commit 61ab378

Browse files
Fix tests in preparation for making otel self monitoring default (#10537) (#10553)
* Fix leak integration test * Improve detection of agent processes in integration tests * Explicitly set the runtime in monitoring integration test * Make the check for component metrics more generic * Check the right build hash in integration tests (cherry picked from commit 3ccf28d) Co-authored-by: Mikołaj Świątek <[email protected]>
1 parent d1be992 commit 61ab378

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

pkg/testing/fixture_install.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,23 @@ func mapProcess(p agentsystemprocess.ProcState) runningProcess {
409409
return mappedProcess
410410
}
411411

412+
// getElasticAgentProcesses returns the running elastic-agent control plane processes. Note that this does not mean
413+
// all processes running using the elastic-agent binary.
412414
func getElasticAgentProcesses(t *gotesting.T) []runningProcess {
413-
return getProcesses(t, `.*elastic\-agent.*`)
415+
agentProcesses := getProcesses(t, `.*elastic\-agent.*`)
416+
// the otel collector might be running as a subprocess using the elastic-agent otel command
417+
// we don't want to count these, so we drop all processes which are children of another process on the list
418+
pids := make(map[int]struct{}, len(agentProcesses))
419+
for _, p := range agentProcesses {
420+
pids[p.Pid] = struct{}{}
421+
}
422+
agentControlPlaneProcesses := make([]runningProcess, 0, len(agentProcesses))
423+
for _, p := range agentProcesses {
424+
if _, ok := pids[p.Ppid]; !ok {
425+
agentControlPlaneProcesses = append(agentControlPlaneProcesses, p)
426+
}
427+
}
428+
return agentControlPlaneProcesses
414429
}
415430

416431
// Includes both the main elastic-agent process and the agentbeat sub-processes for ensuring

testing/integration/ess/beat_receivers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,12 @@ func TestClassicAndReceiverAgentMonitoring(t *testing.T) {
219219
require.NoError(t, err, "error unmarshalling policy: %s", string(policyBytes))
220220
d, prs := policy.Outputs["default"]
221221
require.True(t, prs, "default must be in outputs")
222-
d.ApiKey = string(apiKey)
222+
d.ApiKey = apiKey
223223
policy.Outputs["default"] = d
224224

225225
processNamespace := fmt.Sprintf("%s-%s", info.Namespace, "process")
226226
policy.Agent.Monitoring["namespace"] = processNamespace
227+
policy.Agent.Monitoring["_runtime_experimental"] = "process"
227228

228229
updatedPolicyBytes, err := yaml.Marshal(policy)
229230
require.NoErrorf(t, err, "error marshalling policy, struct was %v", policy)

testing/integration/ess/metrics_monitoring_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ func (runner *MetricsRunner) TestBeatsMetrics() {
172172
query = genESQuery(agentStatus.Info.ID,
173173
[][]string{
174174
{"match", "component.id", cid},
175-
{"exists", "field", "system.process.cpu.total.value"},
176-
{"exists", "field", "system.process.memory.size"},
175+
{"match", "agent.type", "metricbeat"},
177176
})
178177
now = time.Now()
179178
res, err := estools.PerformQueryForRawQuery(ctx, query, "metrics-elastic_agent*", runner.info.ESClient)

testing/integration/ess/package_version_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
194194
require.Lenf(t, splits, 2,
195195
"expected split of beats output version to be split into 2, it was split into %q",
196196
strings.Join(splits, "|"))
197-
wantBuildHash := splits[0]
197+
filebeatBuildHash := splits[0]
198+
199+
// get the elastic-agent build hash
200+
agentVersion, err := f.ExecVersion(ctx)
201+
require.NoError(t, err, "failed to get agent version")
202+
agentCommit := agentVersion.Binary.Commit
198203

199204
diagZip, err := f.ExecDiagnostics(ctx)
200205
require.NoError(t, err, "failed collecting diagnostics")
@@ -251,9 +256,20 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
251256
require.NoError(t, err, "could not parse state.yaml (%s)", stateYAML.Name())
252257

253258
for _, c := range state.Components {
254-
assert.Equalf(t, wantBuildHash, c.State.VersionInfo.BuildHash,
259+
var expectedCommit, expectedBuildHash string
260+
switch c.State.VersionInfo.Name {
261+
case "beats-receiver":
262+
expectedBuildHash = agentCommit
263+
expectedCommit = agentCommit
264+
case "beat-v2-client":
265+
expectedBuildHash = filebeatBuildHash
266+
expectedCommit = filebeatBuildHash
267+
default:
268+
t.Errorf("got unknown value in version_info.name: %s", c.State.VersionInfo.Name)
269+
}
270+
assert.Equalf(t, expectedBuildHash, c.State.VersionInfo.BuildHash,
255271
"component %s: VersionInfo.BuildHash mismatch", c.ID)
256-
assert.Equalf(t, wantBuildHash, c.State.VersionInfo.Meta.Commit,
272+
assert.Equalf(t, expectedCommit, c.State.VersionInfo.Meta.Commit,
257273
"component %s: VersionInfo.Meta.Commit mismatch", c.ID)
258274
}
259275

testing/integration/leak/long_running_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ func (handleMon *handleMonitor) Init(ctx context.Context, t *testing.T, fixture
382382

383383
for _, comp := range status.Components {
384384
pidStr := pidInStatusMessageRegex.FindString(comp.Message)
385+
if pidStr == "" { // could be an otel receiver, not a process
386+
continue
387+
}
385388
pid, err := strconv.ParseInt(pidStr, 10, 64)
386389
require.NoError(t, err)
387390

0 commit comments

Comments
 (0)