Skip to content
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(vm): update ext4-filesystem parser for parse multi block extents #4616

Merged
merged 8 commits into from
Jun 18, 2023
Prev Previous commit
Next Next commit
test(vm): add gzip decompresser for sparse file
  • Loading branch information
masahiro331 committed Jun 18, 2023
commit f7988e4bb7ea0ab1014a19b5f37b0af5fe644b12
2 changes: 1 addition & 1 deletion integration/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestVM(t *testing.T) {

// Decompress the gzipped image file
imagePath := filepath.Join(tmpDir, imageFile)
testutil.DecompressGzip(t, tt.args.input, imagePath)
testutil.DecompressSparseGzip(t, tt.args.input, imagePath)

// Change the current working directory so that targets in the result could be the same as golden files.
err = os.Chdir(tmpDir)
Expand Down
51 changes: 50 additions & 1 deletion internal/testutil/gzip.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutil

import (
"bytes"
"compress/gzip"
"io"
"os"
Expand All @@ -9,7 +10,10 @@ import (
"github.com/stretchr/testify/require"
)

const max = int64(10) << 30 // 10GB
const (
max = int64(10) << 30 // 10GB
blockSize = 4096
)

func DecompressGzip(t *testing.T, src, dst string) {
w, err := os.Create(dst)
Expand All @@ -26,3 +30,48 @@ func DecompressGzip(t *testing.T, src, dst string) {
_, err = io.CopyN(w, gr, max)
require.ErrorIs(t, err, io.EOF)
}

// DecompressSparseGzip decompresses a sparse gzip file for virtual machine image.
func DecompressSparseGzip(t *testing.T, src, dst string) {
w, err := os.Create(dst)
require.NoError(t, err)
defer w.Close()

f, err := os.Open(src)
require.NoError(t, err)
defer f.Close()

gr, err := gzip.NewReader(f)
require.NoError(t, err)

buf := make([]byte, blockSize)
var size int
var written int64
for {
n, err := gr.Read(buf)
if n == 0 && err != nil {
if err == io.EOF {
break
}
require.NoError(t, err)
}

size += n
err = w.Truncate(int64(size))
require.NoError(t, err)

if !bytes.Equal(buf[:n], make([]byte, n)) {
wn, err := w.WriteAt(buf[:n], int64(size-n))
if err != nil {
if err == io.EOF {
break
}
require.NoError(t, err)
}
written += int64(wn)
if written > max {
require.Fail(t, "written size exceeds max")
}
}
}
}