Skip to content

Commit 607287b

Browse files
authored
Merge branch '9.1' into update-version-next-9.1.1
2 parents 23ece04 + 07d47e9 commit 607287b

File tree

5 files changed

+96
-80
lines changed

5 files changed

+96
-80
lines changed

.buildkite/scripts/steps/dra-publish.sh

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
set -euo pipefail
44

@@ -34,39 +34,18 @@ if [[ -z "${BRANCH:-""}" ]]; then
3434
exit 1
3535
fi
3636

37-
# Listing Release manager
38-
function run_release_manager_list() {
39-
local _project_id="${1}" _artifact_set="${2}" _workflow="${3}" _commit="${4}" _branch="${5}" _version="${6}"
40-
echo "+++ :hammer_and_pick: Release manager listing ${_branch} ${_workflow} DRA artifacts..."
37+
function run_release_manager() {
38+
local _command="${1}" _project_id="${2}" _artifact_set="${3}" _workflow="${4}" _commit="${5}" _branch="${6}" _version="${7}" _dry_run="${8:-""}"
39+
echo "+++ :hammer_and_pick: Release manager ${_command} ${_branch} ${_workflow} ${_dry_run} DRA artifacts..."
40+
# shellcheck disable=SC2086
4141
docker run --rm \
4242
--name release-manager \
4343
-e VAULT_ADDR="${VAULT_ADDR_SECRET}" \
4444
-e VAULT_ROLE_ID="${VAULT_ROLE_ID_SECRET}" \
4545
-e VAULT_SECRET_ID="${VAULT_SECRET}" \
4646
--mount type=bind,readonly=false,src="${PWD}",target=/artifacts \
4747
docker.elastic.co/infra/release-manager:latest \
48-
cli list \
49-
--project "${_project_id}" \
50-
--branch "${_branch}" \
51-
--commit "${_commit}" \
52-
--workflow "${_workflow}" \
53-
--version "${_version}" \
54-
--artifact-set "${_artifact_set}" \
55-
--qualifier "${VERSION_QUALIFIER}"
56-
}
57-
58-
# Publish DRA artifacts
59-
function run_release_manager_collect() {
60-
local _project_id="${1}" _artifact_set="${2}" _workflow="${3}" _commit="${4}" _branch="${5}" _version="${6}" _dry_run="${7}"
61-
echo "+++ :hammer_and_pick: Publishing ${_branch} ${_workflow} DRA artifacts..."
62-
docker run --rm \
63-
--name release-manager \
64-
-e VAULT_ADDR="${VAULT_ADDR_SECRET}" \
65-
-e VAULT_ROLE_ID="${VAULT_ROLE_ID_SECRET}" \
66-
-e VAULT_SECRET_ID="${VAULT_SECRET}" \
67-
--mount type=bind,readonly=false,src="${PWD}",target=/artifacts \
68-
docker.elastic.co/infra/release-manager:latest \
69-
cli collect \
48+
cli "${_command}" \
7049
--project "${_project_id}" \
7150
--branch "${_branch}" \
7251
--commit "${_commit}" \
@@ -77,9 +56,12 @@ function run_release_manager_collect() {
7756
${_dry_run}
7857
}
7958

59+
echo "~~~ Fetch Release Manager Docker image"
60+
docker pull docker.elastic.co/infra/release-manager:latest
61+
8062
echo "+++ Release Manager Workflow: ${WORKFLOW} / Branch: ${BRANCH} / VERSION_QUALIFIER: ${VERSION_QUALIFIER} / Commit: ${COMMIT}"
81-
run_release_manager_list "${DRA_PROJECT_ID}" "${DRA_PROJECT_ARTIFACT_ID}" "${WORKFLOW}" "${COMMIT}" "${BRANCH}" "${PACKAGE_VERSION}"
82-
run_release_manager_collect "${DRA_PROJECT_ID}" "${DRA_PROJECT_ARTIFACT_ID}" "${WORKFLOW}" "${COMMIT}" "${BRANCH}" "${PACKAGE_VERSION}" "${DRY_RUN}"
63+
run_release_manager "list" "${DRA_PROJECT_ID}" "${DRA_PROJECT_ARTIFACT_ID}" "${WORKFLOW}" "${COMMIT}" "${BRANCH}" "${PACKAGE_VERSION}"
64+
run_release_manager "collect" "${DRA_PROJECT_ID}" "${DRA_PROJECT_ARTIFACT_ID}" "${WORKFLOW}" "${COMMIT}" "${BRANCH}" "${PACKAGE_VERSION}" "${DRY_RUN}"
8365
RM_EXIT_CODE=$?
8466

8567
exit $RM_EXIT_CODE

dev-tools/mage/pkgtypes.go

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,45 @@ func PackageZip(spec PackageSpec) error {
647647

648648
// PackageTarGz packages a gzipped tar file.
649649
func PackageTarGz(spec PackageSpec) error {
650-
// Create a buffer to write our archive to.
651-
buf := new(bytes.Buffer)
650+
baseDir := spec.rootDir()
651+
652+
// Create the output file.
653+
if spec.OutputFile == "" {
654+
outputTarGz, err := spec.Expand(defaultBinaryName + ".tar.gz")
655+
if err != nil {
656+
return err
657+
}
658+
spec.OutputFile = filepath.Join(distributionsDir, outputTarGz)
659+
}
660+
spec.OutputFile = TarGz.AddFileExtension(spec.OutputFile)
661+
662+
// Open the output file.
663+
log.Println("Creating output file at", spec.OutputFile)
664+
outFile, err := os.Create(CreateDir(spec.OutputFile))
665+
if err != nil {
666+
return err
667+
}
668+
defer func() {
669+
if err := outFile.Close(); err != nil {
670+
log.Printf("failed to close output file: %v", err)
671+
}
672+
}()
673+
674+
// Create a gzip writer to our output file
675+
gzWriter := gzip.NewWriter(outFile)
676+
defer func() {
677+
if err := gzWriter.Close(); err != nil {
678+
log.Printf("failed to close gzip writer: %v", err)
679+
}
680+
}()
652681

653682
// Create a new tar archive.
654-
w := tar.NewWriter(buf)
655-
baseDir := spec.rootDir()
683+
w := tar.NewWriter(gzWriter)
684+
defer func() {
685+
if err := w.Close(); err != nil {
686+
log.Printf("failed to close tar writer: %v", err)
687+
}
688+
}()
656689

657690
// // Replace the darwin-universal by darwin-x86_64 and darwin-arm64. Also
658691
// // keep the other files.
@@ -706,33 +739,10 @@ func PackageTarGz(spec PackageSpec) error {
706739
if err := w.Close(); err != nil {
707740
return err
708741
}
709-
710-
// Output tar.gz to disk.
711-
if spec.OutputFile == "" {
712-
outputTarGz, err := spec.Expand(defaultBinaryName + ".tar.gz")
713-
if err != nil {
714-
return err
715-
}
716-
spec.OutputFile = filepath.Join(distributionsDir, outputTarGz)
717-
}
718-
spec.OutputFile = TarGz.AddFileExtension(spec.OutputFile)
719-
720-
// Open the output file.
721-
log.Println("Creating output file at", spec.OutputFile)
722-
outFile, err := os.Create(CreateDir(spec.OutputFile))
723-
if err != nil {
724-
return err
725-
}
726-
defer outFile.Close()
727-
728-
// Gzip compress the data.
729-
gzWriter := gzip.NewWriter(outFile)
730-
if _, err = gzWriter.Write(buf.Bytes()); err != nil {
742+
if err := gzWriter.Close(); err != nil {
731743
return err
732744
}
733-
734-
// Close and flush.
735-
if err = gzWriter.Close(); err != nil {
745+
if err := outFile.Close(); err != nil {
736746
return err
737747
}
738748

internal/pkg/agent/cmd/logs_test.go

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,18 @@ func TestPrintLogs(t *testing.T) {
254254
createFileContent(t, dir, f.name, bytes.NewBuffer([]byte(f.content)))
255255
}
256256
result := bytes.NewBuffer(nil)
257-
err := printLogs(context.Background(), result, dir, tc.lines, false, nil, nil)
257+
err := printLogs(t.Context(), result, dir, tc.lines, false, nil, nil)
258258
require.NoError(t, err)
259259

260260
require.Equal(t, tc.expected, result.String())
261261
})
262262
}
263263

264+
const waitUntilMatchTimeout = 5 * time.Second
264265
t.Run("returns tail and then follows the logs", func(t *testing.T) {
265266
dir := t.TempDir()
266267
createFileContent(t, dir, file1, bytes.NewBuffer([]byte(generateLines(line1, 1, 10))))
267-
ctx, cancel := context.WithCancel(context.Background())
268+
ctx, cancel := context.WithCancel(t.Context())
268269
defer cancel()
269270

270271
logResult := newChanWriter()
@@ -276,7 +277,8 @@ func TestPrintLogs(t *testing.T) {
276277
var expected string
277278
t.Run("tails the file", func(t *testing.T) {
278279
expected = generateLines(line1, 6, 10)
279-
logResult.waitUntilMatch(t, expected, time.Second)
280+
err := logResult.waitUntilMatch(t.Context(), expected, waitUntilMatchTimeout)
281+
require.NoError(t, err)
280282
})
281283

282284
t.Run("detects new lines and prints them", func(t *testing.T) {
@@ -287,29 +289,32 @@ func TestPrintLogs(t *testing.T) {
287289
f.Close()
288290

289291
expected += generateLines(line1, 11, 20)
290-
logResult.waitUntilMatch(t, expected, 3*watchInterval)
292+
err = logResult.waitUntilMatch(t.Context(), expected, waitUntilMatchTimeout)
293+
require.NoError(t, err)
291294
})
292295

293296
t.Run("detects a new file and switches to it", func(t *testing.T) {
294297
createFileContent(t, dir, file2, bytes.NewBuffer([]byte(generateLines(line2, 1, 20))))
295298

296299
expected += generateLines(line2, 1, 20)
297-
logResult.waitUntilMatch(t, expected, 3*watchInterval)
300+
err := logResult.waitUntilMatch(t.Context(), expected, waitUntilMatchTimeout)
301+
require.NoError(t, err)
298302
})
299303

300304
t.Run("detects another file and switches to it", func(t *testing.T) {
301305
createFileContent(t, dir, file3, bytes.NewBuffer([]byte(generateLines(line3, 1, 30))))
302306

303307
expected += generateLines(line3, 1, 30)
304-
logResult.waitUntilMatch(t, expected, 3*watchInterval)
308+
err := logResult.waitUntilMatch(t.Context(), expected, waitUntilMatchTimeout)
309+
require.NoError(t, err)
305310
})
306311

307312
t.Run("handles interruption correctly", func(t *testing.T) {
308313
cancel()
309314
select {
310315
case err := <-errChan:
311316
require.ErrorIs(t, err, context.Canceled)
312-
case <-time.After(time.Second):
317+
case <-time.After(2 * time.Second):
313318
require.FailNow(t, "context must stop logs following")
314319
}
315320
})
@@ -326,7 +331,7 @@ func TestPrintLogs(t *testing.T) {
326331
`)
327332

328333
createFileContent(t, dir, file1, bytes.NewBuffer(content))
329-
ctx, cancel := context.WithCancel(context.Background())
334+
ctx, cancel := context.WithCancel(t.Context())
330335
defer cancel()
331336
logResult := newChanWriter()
332337
errChan := make(chan error)
@@ -341,7 +346,8 @@ func TestPrintLogs(t *testing.T) {
341346
!{"component":{"id":"match"}, "message":"test4"}!
342347
!{"component":{"id":"match"}, "message":"test6"}!
343348
`
344-
logResult.waitUntilMatch(t, expected, time.Second)
349+
err := logResult.waitUntilMatch(t.Context(), expected, waitUntilMatchTimeout)
350+
require.NoError(t, err)
345351
})
346352

347353
t.Run("detects new lines and prints them with filter", func(t *testing.T) {
@@ -368,7 +374,8 @@ func TestPrintLogs(t *testing.T) {
368374
!{"component":{"id":"match"}, "message":"test12"}!
369375
`
370376

371-
logResult.waitUntilMatch(t, expected, 2*watchInterval)
377+
err = logResult.waitUntilMatch(t.Context(), expected, waitUntilMatchTimeout)
378+
require.NoError(t, err)
372379
})
373380

374381
t.Run("detects a new file and switches to it with filter", func(t *testing.T) {
@@ -385,7 +392,8 @@ func TestPrintLogs(t *testing.T) {
385392
!{"component":{"id":"match"}, "message":"test15"}!
386393
`
387394

388-
logResult.waitUntilMatch(t, expected, 2*watchInterval)
395+
err := logResult.waitUntilMatch(t.Context(), expected, waitUntilMatchTimeout)
396+
require.NoError(t, err)
389397
})
390398

391399
t.Run("handles interruption correctly", func(t *testing.T) {
@@ -657,24 +665,37 @@ func (cw *chanWriter) Write(p []byte) (int, error) {
657665
return len(p), nil
658666
}
659667

660-
// Waits until the combined data written to this object matches the expected
661-
// string (after conversion), or the timeout expires, whichever comes first.
662-
// Reports a fatal test error if the match fails.
668+
// waitUntilMatch waits until the accumulated output matches the expected string.
669+
//
670+
// The timeout is based on data idleness and resets each time new data arrives.
671+
// Returns nil on match, or an error if the timeout expires or the context is canceled.
663672
func (cw *chanWriter) waitUntilMatch(
664-
t *testing.T,
673+
ctx context.Context,
665674
expected string,
666675
timeout time.Duration,
667-
) {
668-
t.Helper()
669-
timeoutChan := time.After(timeout)
670-
for string(cw.result) != expected {
676+
) error {
677+
timeoutChan := time.NewTimer(timeout)
678+
679+
loop:
680+
for {
681+
if len(cw.result) > len(expected) && string(cw.result) != expected {
682+
break loop
683+
}
671684
select {
685+
case <-ctx.Done():
686+
return ctx.Err()
672687
case data := <-cw.ch:
688+
timeoutChan.Stop()
673689
cw.result = append(cw.result, data...)
674-
case <-timeoutChan:
675-
require.FailNow(t, fmt.Sprintf("output does not match. got:\n%v\nexpected:\n%v", string(cw.result), expected))
690+
if string(cw.result) == expected {
691+
return nil
692+
}
693+
timeoutChan.Reset(timeout)
694+
case <-timeoutChan.C:
695+
break loop
676696
}
677697
}
698+
return fmt.Errorf("timed out waiting for output to match. got:\n%v\nexpected:\n%v", string(cw.result), expected)
678699
}
679700

680701
func TestCobraCmd(t *testing.T) {

testing/integration/ess/inspect_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func TestInspect(t *testing.T) {
7474
}})
7575
require.NoErrorf(t, err, "Error when installing agent, output: %s", out)
7676
check.ConnectedToFleet(ctx, t, fixture, 5*time.Minute)
77+
require.Eventually(t, func() bool {
78+
return checkinWithAcker.Acked(policyChangeAction.ActionID)
79+
}, 5*time.Minute, time.Second, "Policy change action should have been acked")
7780

7881
p, err := fixture.Exec(ctx, []string{"inspect"})
7982
require.NoErrorf(t, err, "Error when running inspect, output: %s", p)

testing/integration/testdata/.upgrade-test-agent-versions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# upgrade integration tests.
66

77
testVersions:
8+
- 9.0.5-SNAPSHOT
89
- 9.0.4
9-
- 9.0.4-SNAPSHOT
1010
- 8.19.0-SNAPSHOT
1111
- 8.18.4
1212
- 7.17.30-SNAPSHOT

0 commit comments

Comments
 (0)