-
Notifications
You must be signed in to change notification settings - Fork 173
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
fix: avoid errors when --max-package-size
differs between create runs
#3398
Changes from all commits
1fb1334
fe4f9c5
5698a1e
a00895a
d54578d
61a9e95
b2e0afc
2920864
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
package layout | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
@@ -14,8 +17,115 @@ import ( | |
"github.com/zarf-dev/zarf/src/pkg/layout" | ||
"github.com/zarf-dev/zarf/src/pkg/lint" | ||
"github.com/zarf-dev/zarf/src/test/testutil" | ||
"github.com/zarf-dev/zarf/src/types" | ||
) | ||
|
||
func TestSplitFile(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
fileSize int | ||
chunkSize int | ||
expectedFileSize int64 | ||
expectedLastFileSize int64 | ||
expectedFileCount int | ||
expectedSha256Sum string | ||
}{ | ||
{ | ||
name: "split evenly", | ||
fileSize: 2048, | ||
chunkSize: 16, | ||
expectedFileSize: 16, | ||
expectedLastFileSize: 16, | ||
expectedFileCount: 128, | ||
expectedSha256Sum: "93ecad679eff0df493aaf5d7d615211b0f1d7a919016efb15c98f0b8efb1ba43", | ||
}, | ||
{ | ||
name: "split with remainder", | ||
fileSize: 2048, | ||
chunkSize: 10, | ||
expectedFileSize: 10, | ||
expectedLastFileSize: 8, | ||
expectedFileCount: 205, | ||
expectedSha256Sum: "fe8460f4d53d3578aa37191acf55b3db7bbcb706056f4b6b02a0c70f24b0d95a", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
dir := t.TempDir() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nonblocking: this is more of a style thing, but usually I add some small intent comment (example below) on a series of logically similar operations. There's tradeoffs here with just making self-documenting code, so I'm curious where your opinions are at there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's a reasonable thing to do. I try to break up my tests with indentation and the arrange-act-assert methodology. I don't think comments are always necessary, but when code is non intuitive I do think it helps to provide additional context. In this case, this test case was actually from packager1 so I just copied it over, going forward I'll try to air on the side of more clarity. |
||
name := "random" | ||
p := filepath.Join(dir, name) | ||
f, err := os.Create(p) | ||
require.NoError(t, err) | ||
b := make([]byte, tt.fileSize) | ||
for i := range tt.fileSize { | ||
b[i] = byte(tt.chunkSize) | ||
} | ||
require.NoError(t, err) | ||
_, err = f.Write(b) | ||
require.NoError(t, err) | ||
err = f.Close() | ||
require.NoError(t, err) | ||
|
||
err = splitFile(context.Background(), p, tt.chunkSize) | ||
require.NoError(t, err) | ||
|
||
_, err = os.Stat(p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e.g.
|
||
require.ErrorIs(t, err, os.ErrNotExist) | ||
entries, err := os.ReadDir(dir) | ||
require.NoError(t, err) | ||
require.Len(t, entries, tt.expectedFileCount+1) | ||
for i, entry := range entries[1:] { | ||
require.Equal(t, fmt.Sprintf("%s.part%03d", name, i+1), entry.Name()) | ||
|
||
fi, err := entry.Info() | ||
require.NoError(t, err) | ||
if i == len(entries)-2 { | ||
require.Equal(t, tt.expectedLastFileSize, fi.Size()) | ||
} else { | ||
require.Equal(t, tt.expectedFileSize, fi.Size()) | ||
} | ||
} | ||
|
||
b, err = os.ReadFile(filepath.Join(dir, fmt.Sprintf("%s.part000", name))) | ||
require.NoError(t, err) | ||
var data types.ZarfSplitPackageData | ||
err = json.Unmarshal(b, &data) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expectedFileCount, data.Count) | ||
require.Equal(t, int64(tt.fileSize), data.Bytes) | ||
require.Equal(t, tt.expectedSha256Sum, data.Sha256Sum) | ||
}) | ||
} | ||
} | ||
|
||
func TestSplitDeleteExistingFiles(t *testing.T) { | ||
t.Parallel() | ||
tempDir := t.TempDir() | ||
inputFilename := filepath.Join(tempDir, "testfile.txt") | ||
data := make([]byte, 50) | ||
err := os.WriteFile(inputFilename, data, 0644) | ||
require.NoError(t, err) | ||
// Create many fake split files | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
for i := range 15 { | ||
f, err := os.Create(fmt.Sprintf("%s.part%03d", inputFilename, i)) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
} | ||
|
||
chunkSize := 20 | ||
err = splitFile(context.Background(), inputFilename, chunkSize) | ||
require.NoError(t, err) | ||
|
||
entries, err := os.ReadDir(tempDir) | ||
require.NoError(t, err) | ||
// Verify only header file + 3 data files remain, and not the 15 test split files | ||
require.Len(t, entries, 4) | ||
} | ||
|
||
func TestCreateSkeleton(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
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.
Great test cases 👍