-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
internal/pkg/diagnostics - Fix creation of sub-directories in logs/ #2523
Merged
michalpristas
merged 5 commits into
elastic:main
from
andrewkroh:bugfix/diagnostic-zip-logs-dirs
Apr 21, 2023
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f83ea6c
internal/pkg/diagnostics - Fix creation of sub-directories in logs/
andrewkroh 19dc073
Merge remote-tracking branch 'elastic/main' into bugfix/diagnostic-zi…
andrewkroh eea8a87
Update test to include the versioned sub-directory
andrewkroh f72186f
Add missing directory entry for logs/elastic-agent-{commit}
andrewkroh 204912f
All files within the zip must use forward slash
andrewkroh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
kind: bug-fix | ||
summary: Fix diagnostic zip file handling of sub-directories in logs/. | ||
component: diagnostics | ||
pr: https://github.com/elastic/elastic-agent/pull/2523 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package diagnostics | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" | ||
) | ||
|
||
func TestZipLogs(t *testing.T) { | ||
// Setup a directory structure of: logs/httpjson/log.ndjson | ||
{ | ||
paths.SetTop(t.TempDir()) | ||
dir := filepath.Join(paths.Home(), "logs/sub-dir") | ||
require.NoError(t, os.MkdirAll(dir, 0o700)) | ||
require.NoError(t, os.WriteFile(filepath.Join(dir, "log.ndjson"), []byte(".\n"), 0o600)) | ||
} | ||
|
||
// Zip the logs directory. | ||
buf := new(bytes.Buffer) | ||
w := zip.NewWriter(buf) | ||
require.NoError(t, zipLogs(w, time.Now())) | ||
require.NoError(t, w.Close()) | ||
|
||
type zippedItem struct { | ||
Name string | ||
IsDir bool | ||
} | ||
|
||
// Read back the contents. | ||
r, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) | ||
require.NoError(t, err) | ||
var observed []zippedItem | ||
for _, f := range r.File { | ||
observed = append(observed, zippedItem{Name: f.Name, IsDir: f.FileInfo().IsDir()}) | ||
} | ||
|
||
// Verify the results. | ||
expected := []zippedItem{ | ||
{"logs/", true}, | ||
{"logs/sub-dir/", true}, | ||
{"logs/sub-dir/log.ndjson", false}, | ||
} | ||
assert.Equal(t, expected, observed) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalpristas I pulled in the latest code and this test points out a problem with the newly added
elastic-agent-{commit}
directory in that it is missing a directory entry within the Zip file.These directory entries may not be required, but IIRC depending on how robust a particular unzip tool used is, this can lead to problems when unzipping. Sometimes a tool will try to write files into a directory that doesn't exist and fail (or worse, silently ignore the problem and not write any of the files under that dir).
The test is passing as is (at least on posix),
so this could be followed up in a separate change.Fixed everything I found here.