|
| 1 | +package sparseio_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/cirruslabs/vetu/internal/sparseio" |
| 5 | + "github.com/dustin/go-humanize" |
| 6 | + "github.com/opencontainers/go-digest" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "math/rand" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCopySmall(t *testing.T) { |
| 15 | + // Create a small file |
| 16 | + originalFilePath := filepath.Join(t.TempDir(), "original.txt") |
| 17 | + err := os.WriteFile(originalFilePath, []byte("Hello, World!\n"), 0600) |
| 18 | + require.NoError(t, err) |
| 19 | + |
| 20 | + // Sparsely copy it |
| 21 | + sparseFilePath := filepath.Join(t.TempDir(), "sparse.txt") |
| 22 | + copySparse(t, originalFilePath, sparseFilePath) |
| 23 | + |
| 24 | + // Ensure that both files have identical contents |
| 25 | + require.Equal(t, fileDigest(t, originalFilePath), fileDigest(t, sparseFilePath)) |
| 26 | +} |
| 27 | + |
| 28 | +//nolint:gosec // we don't need cryptographically secure randomness here |
| 29 | +func TestCopyRandomized(t *testing.T) { |
| 30 | + // Create a sufficiently large file that contains |
| 31 | + // interleaved random-filled and zero-filled parts |
| 32 | + originalFilePath := filepath.Join(t.TempDir(), "original.bin") |
| 33 | + originalFile, err := os.Create(originalFilePath) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + var wroteBytes int64 |
| 37 | + |
| 38 | + for wroteBytes < 1*humanize.GByte { |
| 39 | + chunk := randomlySizedChunk(1*humanize.KByte, 4*humanize.MByte) |
| 40 | + |
| 41 | + // Randomize the contents of some chunks |
| 42 | + if rand.Intn(2) == 1 { |
| 43 | + //nolint:staticcheck // what's the alternative to the deprecated rand.Read() anyways? |
| 44 | + _, err = rand.Read(chunk) |
| 45 | + require.NoError(t, err) |
| 46 | + } |
| 47 | + |
| 48 | + n, err := originalFile.Write(chunk) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + wroteBytes += int64(n) |
| 52 | + } |
| 53 | + |
| 54 | + require.NoError(t, originalFile.Close()) |
| 55 | + |
| 56 | + // Sparsely copy the original file |
| 57 | + sparseFilePath := filepath.Join(t.TempDir(), "sparse.bin") |
| 58 | + copySparse(t, originalFilePath, sparseFilePath) |
| 59 | + |
| 60 | + // Ensure that the copied file has the same contents as the original file |
| 61 | + require.Equal(t, fileDigest(t, originalFilePath), fileDigest(t, sparseFilePath)) |
| 62 | +} |
| 63 | + |
| 64 | +func copySparse(t *testing.T, originalFilePath string, sparseFilePath string) { |
| 65 | + originalFile, err := os.Open(originalFilePath) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + originalFileInfo, err := originalFile.Stat() |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + sparseFile, err := os.Create(sparseFilePath) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + require.NoError(t, sparseFile.Truncate(originalFileInfo.Size())) |
| 75 | + require.NoError(t, sparseio.Copy(sparseFile, originalFile)) |
| 76 | + |
| 77 | + require.NoError(t, originalFile.Close()) |
| 78 | + require.NoError(t, sparseFile.Close()) |
| 79 | +} |
| 80 | + |
| 81 | +//nolint:gosec // we don't need cryptographically secure randomness here |
| 82 | +func randomlySizedChunk(minBytes int, maxBytes int) []byte { |
| 83 | + return make([]byte, rand.Intn(maxBytes-minBytes+1)+minBytes) |
| 84 | +} |
| 85 | + |
| 86 | +func fileDigest(t *testing.T, path string) digest.Digest { |
| 87 | + file, err := os.Open(path) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + digest, err := digest.FromReader(file) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + require.NoError(t, file.Close()) |
| 94 | + |
| 95 | + return digest |
| 96 | +} |
0 commit comments