-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Upgrade Details] Fix corruption when writing upgrade marker file (#3948
) (#3951) * Truncate marker file before writing * Add unit test for truncation (cherry picked from commit 8f543bb) Co-authored-by: Shaunak Kashyap <[email protected]>
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
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
56 changes: 56 additions & 0 deletions
56
internal/pkg/agent/application/upgrade/marker_access_common_test.go
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 upgrade | ||
|
||
import ( | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWriteMarkerFileWithTruncation(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testMarkerFile := filepath.Join(tmpDir, markerFilename) | ||
|
||
// Write a long marker file | ||
err := writeMarkerFileCommon(testMarkerFile, randomBytes(40), true) | ||
require.NoError(t, err) | ||
|
||
// Get length of file | ||
fileInfo, err := os.Stat(testMarkerFile) | ||
require.NoError(t, err) | ||
originalSize := fileInfo.Size() | ||
|
||
// Write a shorter marker file | ||
err = writeMarkerFileCommon(testMarkerFile, randomBytes(25), true) | ||
require.NoError(t, err) | ||
|
||
// Get length of file | ||
fileInfo, err = os.Stat(testMarkerFile) | ||
require.NoError(t, err) | ||
newSize := fileInfo.Size() | ||
|
||
// Make sure shorter file has is smaller in length than | ||
// the original long marker file | ||
require.Less(t, newSize, originalSize) | ||
} | ||
|
||
func randomBytes(length int) []byte { | ||
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ" + | ||
"abcdefghijklmnopqrstuvwxyzåäö" + | ||
"0123456789" + | ||
"~=+%^*/()[]{}/!@#$?|") | ||
|
||
var b []byte | ||
for i := 0; i < length; i++ { | ||
rune := chars[rand.Intn(len(chars))] | ||
b = append(b, byte(rune)) | ||
} | ||
|
||
return b | ||
} |