Skip to content

Commit 377bbdd

Browse files
Merge branch 'main' into otel/validate
2 parents be10a47 + ba0ea0b commit 377bbdd

File tree

7 files changed

+138
-29
lines changed

7 files changed

+138
-29
lines changed

.buildkite/scripts/steps/integration_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ MAGE_SUBTARGET="${3:-""}"
1111
# Override the agent package version using a string with format <major>.<minor>.<patch>
1212
# NOTE: use only after version bump when the new version is not yet available, for example:
1313
# OVERRIDE_AGENT_PACKAGE_VERSION="8.10.3" otherwise OVERRIDE_AGENT_PACKAGE_VERSION="".
14-
OVERRIDE_AGENT_PACKAGE_VERSION="8.12.0"
14+
OVERRIDE_AGENT_PACKAGE_VERSION=""
1515

1616
if [[ -n "$OVERRIDE_AGENT_PACKAGE_VERSION" ]]; then
1717
OVERRIDE_TEST_AGENT_VERSION=${OVERRIDE_AGENT_PACKAGE_VERSION}"-SNAPSHOT"

internal/pkg/agent/application/upgrade/details/details.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package details
66

77
import (
8+
"math"
89
"sync"
910
"time"
1011

@@ -93,7 +94,17 @@ func (d *Details) SetDownloadProgress(percent, rateBytesPerSecond float64) {
9394
defer d.mu.Unlock()
9495

9596
d.Metadata.DownloadPercent = percent
96-
d.Metadata.DownloadRate = details.DownloadRate(rateBytesPerSecond)
97+
98+
// A download rate of +Inf is possible if the download completes near instantaneously. Since
99+
// +Inf (or -Inf or NaN) are not indexable into Elasticsearch or generally informative since the
100+
// download percent is 100% when this happens, just reset download rate to the default value of
101+
// zero when this happens. https://github.com/elastic/elastic-agent/issues/4041
102+
if math.IsInf(rateBytesPerSecond, 0) || math.IsNaN(rateBytesPerSecond) {
103+
d.Metadata.DownloadRate = 0
104+
} else {
105+
d.Metadata.DownloadRate = details.DownloadRate(rateBytesPerSecond)
106+
}
107+
97108
d.notifyObservers()
98109
}
99110

internal/pkg/agent/application/upgrade/details/details_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,32 @@ func TestDetailsObserver(t *testing.T) {
7171
func TestDetailsDownloadRateJSON(t *testing.T) {
7272
det := NewDetails("99.999.9999", StateRequested, "test_action_id")
7373

74-
// Normal (non-infinity) download rate
75-
t.Run("non_infinity", func(t *testing.T) {
76-
det.SetDownloadProgress(.8, 1794.7)
77-
78-
data, err := json.Marshal(det)
79-
require.NoError(t, err)
80-
81-
var unmarshalledDetails Details
82-
err = json.Unmarshal(data, &unmarshalledDetails)
83-
require.NoError(t, err)
84-
require.Equal(t, float64(1794), float64(unmarshalledDetails.Metadata.DownloadRate))
85-
require.Equal(t, .8, unmarshalledDetails.Metadata.DownloadPercent)
86-
})
87-
88-
// Infinity download rate
89-
t.Run("infinity", func(t *testing.T) {
90-
det.SetDownloadProgress(0.99, math.Inf(1))
91-
92-
data, err := json.Marshal(det)
93-
require.NoError(t, err)
94-
95-
var unmarshalledDetails Details
96-
err = json.Unmarshal(data, &unmarshalledDetails)
97-
require.NoError(t, err)
98-
require.Equal(t, math.Inf(1), float64(unmarshalledDetails.Metadata.DownloadRate))
99-
require.Equal(t, 0.99, unmarshalledDetails.Metadata.DownloadPercent)
100-
})
74+
tests := []struct {
75+
name string
76+
percent float64
77+
rate_in float64
78+
rate_out float64
79+
}{
80+
{"non_infinity", .8, 1794.7, 1794},
81+
{"positive_infinity", 0.99, math.Inf(1), 0},
82+
{"negative_infinity", 0.99, math.Inf(-1), 0},
83+
{"not_a_number", 0.99, math.NaN(), 0},
84+
}
85+
86+
for _, tc := range tests {
87+
t.Run(tc.name, func(t *testing.T) {
88+
det.SetDownloadProgress(tc.percent, tc.rate_in)
89+
90+
data, err := json.Marshal(det)
91+
require.NoError(t, err)
92+
93+
var unmarshalledDetails Details
94+
err = json.Unmarshal(data, &unmarshalledDetails)
95+
require.NoError(t, err)
96+
require.Equal(t, tc.rate_out, float64(unmarshalledDetails.Metadata.DownloadRate))
97+
require.Equal(t, tc.percent, unmarshalledDetails.Metadata.DownloadPercent)
98+
})
99+
}
101100
}
102101

103102
func TestEquals(t *testing.T) {

internal/pkg/agent/cmd/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ func tryDelayEnroll(ctx context.Context, logger *logger.Logger, cfg *configurati
501501
}
502502
options.DelayEnroll = false
503503
options.FleetServer.SpawnAgent = false
504+
// enrollCmd daemonReloadWithBackoff is broken
505+
// see https://github.com/elastic/elastic-agent/issues/4043
506+
// SkipDaemonRestart to true avoids running that code.
507+
options.SkipDaemonRestart = true
504508
c, err := newEnrollCmd(
505509
ctx,
506510
logger,

pkg/testing/fixture_install.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type InstallOpts struct {
5858
NonInteractive bool // --non-interactive
5959
ProxyURL string // --proxy-url
6060
Unprivileged bool // --unprivileged
61+
DelayEnroll bool // --delay-enroll
6162

6263
EnrollOpts
6364
}
@@ -82,6 +83,9 @@ func (i InstallOpts) toCmdArgs() []string {
8283
if i.Unprivileged {
8384
args = append(args, "--unprivileged")
8485
}
86+
if i.DelayEnroll {
87+
args = append(args, "--delay-enroll")
88+
}
8589

8690
args = append(args, i.EnrollOpts.toCmdArgs()...)
8791

pkg/testing/tools/tools.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ func InstallAgentForPolicy(ctx context.Context, t *testing.T,
123123
timeout = time.Until(deadline)
124124
}
125125

126+
// Don't check fleet status if --delay-enroll
127+
if installOpts.DelayEnroll {
128+
return nil
129+
}
130+
126131
// Wait for Agent to be healthy
127132
require.Eventually(
128133
t,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
//go:build integration
6+
7+
package integration
8+
9+
import (
10+
"context"
11+
"fmt"
12+
"os/exec"
13+
"testing"
14+
"time"
15+
16+
"github.com/stretchr/testify/require"
17+
18+
"github.com/elastic/elastic-agent-libs/kibana"
19+
atesting "github.com/elastic/elastic-agent/pkg/testing"
20+
"github.com/elastic/elastic-agent/pkg/testing/define"
21+
"github.com/elastic/elastic-agent/pkg/testing/tools"
22+
"github.com/elastic/elastic-agent/pkg/testing/tools/check"
23+
"github.com/elastic/elastic-agent/pkg/testing/tools/testcontext"
24+
)
25+
26+
func TestDelayEnroll(t *testing.T) {
27+
info := define.Require(t, define.Requirements{
28+
Group: Fleet,
29+
Stack: &define.Stack{},
30+
Local: false,
31+
Sudo: true,
32+
OS: []define.OS{{Type: define.Linux}},
33+
})
34+
35+
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
36+
defer cancel()
37+
38+
agentFixture, err := define.NewFixture(t, define.Version())
39+
require.NoError(t, err)
40+
41+
// 1. Create a policy in Fleet with monitoring enabled.
42+
// To ensure there are no conflicts with previous test runs against
43+
// the same ESS stack, we add the current time at the end of the policy
44+
// name. This policy does not contain any integration.
45+
t.Log("Enrolling agent in Fleet with a test policy")
46+
createPolicyReq := kibana.AgentPolicy{
47+
Name: fmt.Sprintf("test-policy-enroll-%d", time.Now().Unix()),
48+
Namespace: info.Namespace,
49+
Description: "test policy for agent enrollment",
50+
MonitoringEnabled: []kibana.MonitoringEnabledOption{
51+
kibana.MonitoringEnabledLogs,
52+
kibana.MonitoringEnabledMetrics,
53+
},
54+
AgentFeatures: []map[string]interface{}{
55+
{
56+
"name": "test_enroll",
57+
"enabled": true,
58+
},
59+
},
60+
}
61+
62+
installOpts := atesting.InstallOpts{
63+
NonInteractive: true,
64+
Force: true,
65+
DelayEnroll: true,
66+
}
67+
// Install the Elastic-Agent with the policy that was just
68+
// created.
69+
_, err = tools.InstallAgentWithPolicy(
70+
ctx,
71+
t,
72+
installOpts,
73+
agentFixture,
74+
info.KibanaClient,
75+
createPolicyReq)
76+
require.NoError(t, err)
77+
78+
// Start elastic-agent via service, this should do the enrollment
79+
cmd := exec.Command("/usr/bin/systemctl", "start", "elastic-agent")
80+
stdErrStdout, err := cmd.CombinedOutput()
81+
require.NoErrorf(t, err, "systemctl start elastic-agent output was %s", stdErrStdout)
82+
83+
// check to make sure enroll worked
84+
check.ConnectedToFleet(ctx, t, agentFixture, 5*time.Minute)
85+
86+
}

0 commit comments

Comments
 (0)