Files
podman/pkg/machine/compression/compression_test.go
Mario Loriedo 88af8852db Refactor machine decompress.go
Added some tests to verify that files extractions works
with different compression format.

Created a decompressor interface with 2 main methods:
  reader(): returns an io.Reader for the specific compression algorithm
  copy(): extracts the compressed file into the file provided as param

Created 5 decompressor types:
- gzip: extract gzip files
- xz: extract xz files
- zip: extract zip files
- generic: extract any other file using github.com/containers/image/v5/pkg/compression
- uncompressed: only do a copy of the file

Minor fix to the progress bar instances: added a call to bar.Abort(false)
that happens before Progress.Wait() to avoid that it hangs when a bar is
not set as completed although extraction is done.

Signed-off-by: Mario Loriedo <mario.loriedo@gmail.com>
2024-02-25 22:29:02 +01:00

196 lines
3.0 KiB
Go

package compression
import (
"os"
"testing"
"github.com/containers/podman/v5/pkg/machine/define"
)
func Test_compressionFromFile(t *testing.T) {
type args struct {
path string
}
var tests = []struct {
name string
args args
want ImageCompression
}{
{
name: "xz",
args: args{
path: "/tmp/foo.xz",
},
want: Xz,
},
{
name: "gzip",
args: args{
path: "/tmp/foo.gz",
},
want: Gz,
},
{
name: "bz2",
args: args{
path: "/tmp/foo.bz2",
},
want: Bz2,
},
{
name: "default is zstd",
args: args{
path: "/tmp/foo",
},
want: Zstd,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := KindFromFile(tt.args.path); got != tt.want {
t.Errorf("KindFromFile() = %v, want %v", got, tt.want)
}
})
}
}
func TestImageCompression_String(t *testing.T) {
tests := []struct {
name string
c ImageCompression
want string
}{
{
name: "xz",
c: Xz,
want: "xz",
},
{
name: "gz",
c: Gz,
want: "gz",
},
{
name: "bz2",
c: Bz2,
want: "bz2",
},
{
name: "zip",
c: Zip,
want: "zip",
},
{
name: "zstd is default",
c: 99,
want: "zst",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.c.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
func Test_Decompress(t *testing.T) {
type args struct {
src string
dst string
}
type want struct {
content string
}
tests := []struct {
name string
args args
want want
}{
{
name: "zip",
args: args{
src: "./testfiles/sample.zip",
dst: "./testfiles/hellozip",
},
want: want{
content: "zip\n",
},
},
{
name: "xz",
args: args{
src: "./testfiles/sample.xz",
dst: "./testfiles/helloxz",
},
want: want{
content: "xz\n",
},
},
{
name: "gzip",
args: args{
src: "./testfiles/sample.gz",
dst: "./testfiles/hellogz",
},
want: want{
content: "gzip\n",
},
},
{
name: "bzip2",
args: args{
src: "./testfiles/sample.bz2",
dst: "./testfiles/hellobz2",
},
want: want{
content: "bzip2\n",
},
},
{
name: "zstd",
args: args{
src: "./testfiles/sample.zst",
dst: "./testfiles/hellozstd",
},
want: want{
content: "zstd\n",
},
},
{
name: "uncompressed",
args: args{
src: "./testfiles/sample.uncompressed",
dst: "./testfiles/hellozuncompressed",
},
want: want{
content: "uncompressed\n",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srcVMFile := &define.VMFile{Path: tt.args.src}
dstFilePath := tt.args.dst
defer os.Remove(dstFilePath)
if err := Decompress(srcVMFile, dstFilePath); err != nil {
t.Fatalf("decompress() error = %v", err)
}
data, err := os.ReadFile(dstFilePath)
if err != nil {
t.Fatalf("ReadFile() error = %v", err)
}
if got := string(data); got != tt.want.content {
t.Fatalf("content = %v, want %v", got, tt.want.content)
}
})
}
}