|
| 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 | + "strings" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/gofrs/uuid/v5" |
| 18 | + |
| 19 | + "github.com/elastic/elastic-agent-libs/kibana" |
| 20 | + |
| 21 | + atesting "github.com/elastic/elastic-agent/pkg/testing" |
| 22 | + "github.com/elastic/elastic-agent/pkg/testing/define" |
| 23 | + "github.com/elastic/elastic-agent/pkg/testing/tools" |
| 24 | + "github.com/elastic/elastic-agent/pkg/testing/tools/check" |
| 25 | + "github.com/elastic/elastic-agent/pkg/testing/tools/fleettools" |
| 26 | + "github.com/elastic/elastic-agent/pkg/testing/tools/testcontext" |
| 27 | + "github.com/elastic/elastic-agent/testing/upgradetest" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func TestDebLogIngestFleetManaged(t *testing.T) { |
| 33 | + info := define.Require(t, define.Requirements{ |
| 34 | + Group: Deb, |
| 35 | + Stack: &define.Stack{}, |
| 36 | + OS: []define.OS{ |
| 37 | + { |
| 38 | + Type: define.Linux, |
| 39 | + Distro: "ubuntu", |
| 40 | + }, |
| 41 | + }, |
| 42 | + Local: false, |
| 43 | + Sudo: true, |
| 44 | + }) |
| 45 | + |
| 46 | + ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute)) |
| 47 | + defer cancel() |
| 48 | + |
| 49 | + agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version(), atesting.WithPackageFormat("deb")) |
| 50 | + require.NoError(t, err) |
| 51 | + |
| 52 | + // 1. Create a policy in Fleet with monitoring enabled. |
| 53 | + // To ensure there are no conflicts with previous test runs against |
| 54 | + // the same ESS stack, we add the current time at the end of the policy |
| 55 | + // name. This policy does not contain any integration. |
| 56 | + t.Log("Enrolling agent in Fleet with a test policy") |
| 57 | + createPolicyReq := kibana.AgentPolicy{ |
| 58 | + Name: fmt.Sprintf("test-policy-enroll-%s", uuid.Must(uuid.NewV4()).String()), |
| 59 | + Namespace: info.Namespace, |
| 60 | + Description: "test policy for agent enrollment", |
| 61 | + MonitoringEnabled: []kibana.MonitoringEnabledOption{ |
| 62 | + kibana.MonitoringEnabledLogs, |
| 63 | + kibana.MonitoringEnabledMetrics, |
| 64 | + }, |
| 65 | + AgentFeatures: []map[string]interface{}{ |
| 66 | + { |
| 67 | + "name": "test_enroll", |
| 68 | + "enabled": true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + installOpts := atesting.InstallOpts{ |
| 74 | + NonInteractive: true, |
| 75 | + Force: true, |
| 76 | + } |
| 77 | + |
| 78 | + // 2. Install the Elastic-Agent with the policy that |
| 79 | + // was just created. |
| 80 | + policy, err := tools.InstallAgentWithPolicy( |
| 81 | + ctx, |
| 82 | + t, |
| 83 | + installOpts, |
| 84 | + agentFixture, |
| 85 | + info.KibanaClient, |
| 86 | + createPolicyReq) |
| 87 | + require.NoError(t, err) |
| 88 | + t.Logf("created policy: %s", policy.ID) |
| 89 | + check.ConnectedToFleet(ctx, t, agentFixture, 5*time.Minute) |
| 90 | + |
| 91 | + t.Run("Monitoring logs are shipped", func(t *testing.T) { |
| 92 | + testMonitoringLogsAreShipped(t, ctx, info, agentFixture, policy) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("Normal logs with flattened data_stream are shipped", func(t *testing.T) { |
| 96 | + testFlattenedDatastreamFleetPolicy(t, ctx, info, policy) |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +func TestDebFleetUpgrade(t *testing.T) { |
| 101 | + info := define.Require(t, define.Requirements{ |
| 102 | + Group: Deb, |
| 103 | + Stack: &define.Stack{}, |
| 104 | + OS: []define.OS{ |
| 105 | + { |
| 106 | + Type: define.Linux, |
| 107 | + Distro: "ubuntu", |
| 108 | + }, |
| 109 | + }, |
| 110 | + Local: false, |
| 111 | + Sudo: true, |
| 112 | + }) |
| 113 | + |
| 114 | + ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute)) |
| 115 | + defer cancel() |
| 116 | + |
| 117 | + // start from previous minor |
| 118 | + upgradeFromVersion, err := upgradetest.PreviousMinor() |
| 119 | + require.NoError(t, err) |
| 120 | + startFixture, err := atesting.NewFixture( |
| 121 | + t, |
| 122 | + upgradeFromVersion.String(), |
| 123 | + atesting.WithFetcher(atesting.ArtifactFetcher()), |
| 124 | + atesting.WithPackageFormat("deb"), |
| 125 | + ) |
| 126 | + require.NoError(t, err) |
| 127 | + |
| 128 | + // end on the current build with deb |
| 129 | + endFixture, err := define.NewFixtureFromLocalBuild(t, define.Version(), atesting.WithPackageFormat("deb")) |
| 130 | + require.NoError(t, err) |
| 131 | + |
| 132 | + // 1. Create a policy in Fleet with monitoring enabled. |
| 133 | + // To ensure there are no conflicts with previous test runs against |
| 134 | + // the same ESS stack, we add the current time at the end of the policy |
| 135 | + // name. This policy does not contain any integration. |
| 136 | + t.Log("Enrolling agent in Fleet with a test policy") |
| 137 | + createPolicyReq := kibana.AgentPolicy{ |
| 138 | + Name: fmt.Sprintf("test-policy-enroll-%s", uuid.Must(uuid.NewV4()).String()), |
| 139 | + Namespace: info.Namespace, |
| 140 | + Description: "test policy for agent enrollment", |
| 141 | + MonitoringEnabled: []kibana.MonitoringEnabledOption{ |
| 142 | + kibana.MonitoringEnabledLogs, |
| 143 | + kibana.MonitoringEnabledMetrics, |
| 144 | + }, |
| 145 | + AgentFeatures: []map[string]interface{}{ |
| 146 | + { |
| 147 | + "name": "test_enroll", |
| 148 | + "enabled": true, |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + installOpts := atesting.InstallOpts{ |
| 154 | + NonInteractive: true, |
| 155 | + Force: true, |
| 156 | + } |
| 157 | + |
| 158 | + // 2. Install the Elastic-Agent with the policy that |
| 159 | + // was just created. |
| 160 | + policy, err := tools.InstallAgentWithPolicy( |
| 161 | + ctx, |
| 162 | + t, |
| 163 | + installOpts, |
| 164 | + startFixture, |
| 165 | + info.KibanaClient, |
| 166 | + createPolicyReq) |
| 167 | + require.NoError(t, err) |
| 168 | + t.Logf("created policy: %s", policy.ID) |
| 169 | + check.ConnectedToFleet(ctx, t, startFixture, 5*time.Minute) |
| 170 | + |
| 171 | + // 3. Upgrade deb to the build version |
| 172 | + srcPackage, err := endFixture.SrcPackage(ctx) |
| 173 | + require.NoError(t, err) |
| 174 | + cmd := exec.CommandContext(ctx, "sudo", "apt-get", "install", "-y", "-qq", "-o", "Dpkg::Options::=--force-confdef", "-o", "Dpkg::Options::=--force-confold", srcPackage) |
| 175 | + cmd.Env = append(cmd.Env, "DEBIAN_FRONTEND=noninteractive") |
| 176 | + out, err := cmd.CombinedOutput() // #nosec G204 -- Need to pass in name of package |
| 177 | + require.NoError(t, err, string(out)) |
| 178 | + |
| 179 | + // 4. Wait for version in Fleet to match |
| 180 | + // Fleet will not include the `-SNAPSHOT` in the `GetAgentVersion` result |
| 181 | + noSnapshotVersion := strings.TrimSuffix(define.Version(), "-SNAPSHOT") |
| 182 | + require.Eventually(t, func() bool { |
| 183 | + newVersion, err := fleettools.GetAgentVersion(ctx, info.KibanaClient, policy.ID) |
| 184 | + if err != nil { |
| 185 | + t.Logf("error getting agent version: %v", err) |
| 186 | + return false |
| 187 | + } |
| 188 | + if noSnapshotVersion == newVersion { |
| 189 | + return true |
| 190 | + } |
| 191 | + t.Logf("Got Agent version %s != %s", newVersion, noSnapshotVersion) |
| 192 | + return false |
| 193 | + }, 5*time.Minute, time.Second) |
| 194 | +} |
0 commit comments