podman
138 строк · 3.6 Кб
1package compression
2
3import (
4"os"
5"testing"
6
7"github.com/containers/podman/v5/pkg/machine/define"
8"github.com/stretchr/testify/assert"
9"github.com/stretchr/testify/require"
10)
11
12func Test_compressionFromFile(t *testing.T) {
13type args struct {
14path string
15}
16var tests = []struct {
17name string
18args args
19want ImageCompression
20}{
21{
22name: "xz",
23args: args{
24path: "/tmp/foo.xz",
25},
26want: Xz,
27},
28{
29name: "gzip",
30args: args{
31path: "/tmp/foo.gz",
32},
33want: Gz,
34},
35{
36name: "bz2",
37args: args{
38path: "/tmp/foo.bz2",
39},
40want: Bz2,
41},
42{
43name: "default is zstd",
44args: args{
45path: "/tmp/foo",
46},
47want: Zstd,
48},
49}
50for _, tt := range tests {
51t.Run(tt.name, func(t *testing.T) {
52if got := KindFromFile(tt.args.path); got != tt.want {
53t.Errorf("KindFromFile() = %v, want %v", got, tt.want)
54}
55})
56}
57}
58
59func TestImageCompression_String(t *testing.T) {
60tests := []struct {
61name string
62c ImageCompression
63want string
64}{
65{
66name: "xz",
67c: Xz,
68want: "xz",
69},
70{
71name: "gz",
72c: Gz,
73want: "gz",
74},
75{
76name: "bz2",
77c: Bz2,
78want: "bz2",
79},
80{
81name: "zip",
82c: Zip,
83want: "zip",
84},
85{
86name: "zstd is default",
87c: 99,
88want: "zst",
89},
90}
91for _, tt := range tests {
92t.Run(tt.name, func(t *testing.T) {
93if got := tt.c.String(); got != tt.want {
94t.Errorf("String() = %v, want %v", got, tt.want)
95}
96})
97}
98}
99
100func Test_Decompress(t *testing.T) {
101type args struct {
102src string
103dst string
104}
105
106type want string
107
108tests := []struct {
109name string
110args args
111want want
112}{
113{name: "zip", args: args{src: "./testdata/sample.zip", dst: "./testdata/hellozip"}, want: "zip\n"},
114{name: "zip with trailing zeros", args: args{src: "./testdata/sample-withzeros.zip", dst: "./testdata/hellozip-withzeros"}, want: "zip\n\x00\x00\x00\x00\x00\x00"},
115{name: "xz", args: args{src: "./testdata/sample.xz", dst: "./testdata/helloxz"}, want: "xz\n"},
116{name: "xz with trailing zeros", args: args{src: "./testdata/sample-withzeros.xz", dst: "./testdata/helloxz-withzeros"}, want: "xz\n\x00\x00\x00\x00\x00\x00\x00"},
117{name: "gzip", args: args{src: "./testdata/sample.gz", dst: "./testdata/hellogz"}, want: "gzip\n"},
118{name: "gzip with trailing zeros", args: args{src: "./testdata/sample-withzeros.gz", dst: "./testdata/hellogzip-withzeros"}, want: "gzip\n\x00\x00\x00\x00\x00"},
119{name: "bzip2", args: args{src: "./testdata/sample.bz2", dst: "./testdata/hellobz2"}, want: "bzip2\n"},
120{name: "bzip2 with trailing zeros", args: args{src: "./testdata/sample-withzeros.bz2", dst: "./testdata/hellobz2-withzeros"}, want: "bzip2\n\x00\x00\x00\x00"},
121{name: "zstd", args: args{src: "./testdata/sample.zst", dst: "./testdata/hellozstd"}, want: "zstd\n"},
122{name: "zstd with trailing zeros", args: args{src: "./testdata/sample-withzeros.zst", dst: "./testdata/hellozstd-withzeros"}, want: "zstd\n\x00\x00\x00\x00\x00"},
123{name: "uncompressed", args: args{src: "./testdata/sample.uncompressed", dst: "./testdata/hellouncompressed"}, want: "uncompressed\n"},
124{name: "uncompressed with trailing zeros", args: args{src: "./testdata/sample-withzeros.uncompressed", dst: "./testdata/hellozuncompressed-withzeros"}, want: "uncompressed\n\x00\x00\x00\x00\x00\x00\x00"},
125}
126for _, tt := range tests {
127t.Run(tt.name, func(t *testing.T) {
128srcVMFile := &define.VMFile{Path: tt.args.src}
129dstFilePath := tt.args.dst
130defer os.Remove(dstFilePath)
131err := Decompress(srcVMFile, dstFilePath)
132require.NoError(t, err)
133data, err := os.ReadFile(dstFilePath)
134require.NoError(t, err)
135assert.Equal(t, string(tt.want), string(data))
136})
137}
138}
139