podman

Форк
0
/
compression_test.go 
138 строк · 3.6 Кб
1
package compression
2

3
import (
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

12
func Test_compressionFromFile(t *testing.T) {
13
	type args struct {
14
		path string
15
	}
16
	var tests = []struct {
17
		name string
18
		args args
19
		want ImageCompression
20
	}{
21
		{
22
			name: "xz",
23
			args: args{
24
				path: "/tmp/foo.xz",
25
			},
26
			want: Xz,
27
		},
28
		{
29
			name: "gzip",
30
			args: args{
31
				path: "/tmp/foo.gz",
32
			},
33
			want: Gz,
34
		},
35
		{
36
			name: "bz2",
37
			args: args{
38
				path: "/tmp/foo.bz2",
39
			},
40
			want: Bz2,
41
		},
42
		{
43
			name: "default is zstd",
44
			args: args{
45
				path: "/tmp/foo",
46
			},
47
			want: Zstd,
48
		},
49
	}
50
	for _, tt := range tests {
51
		t.Run(tt.name, func(t *testing.T) {
52
			if got := KindFromFile(tt.args.path); got != tt.want {
53
				t.Errorf("KindFromFile() = %v, want %v", got, tt.want)
54
			}
55
		})
56
	}
57
}
58

59
func TestImageCompression_String(t *testing.T) {
60
	tests := []struct {
61
		name string
62
		c    ImageCompression
63
		want string
64
	}{
65
		{
66
			name: "xz",
67
			c:    Xz,
68
			want: "xz",
69
		},
70
		{
71
			name: "gz",
72
			c:    Gz,
73
			want: "gz",
74
		},
75
		{
76
			name: "bz2",
77
			c:    Bz2,
78
			want: "bz2",
79
		},
80
		{
81
			name: "zip",
82
			c:    Zip,
83
			want: "zip",
84
		},
85
		{
86
			name: "zstd is default",
87
			c:    99,
88
			want: "zst",
89
		},
90
	}
91
	for _, tt := range tests {
92
		t.Run(tt.name, func(t *testing.T) {
93
			if got := tt.c.String(); got != tt.want {
94
				t.Errorf("String() = %v, want %v", got, tt.want)
95
			}
96
		})
97
	}
98
}
99

100
func Test_Decompress(t *testing.T) {
101
	type args struct {
102
		src string
103
		dst string
104
	}
105

106
	type want string
107

108
	tests := []struct {
109
		name string
110
		args args
111
		want 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
	}
126
	for _, tt := range tests {
127
		t.Run(tt.name, func(t *testing.T) {
128
			srcVMFile := &define.VMFile{Path: tt.args.src}
129
			dstFilePath := tt.args.dst
130
			defer os.Remove(dstFilePath)
131
			err := Decompress(srcVMFile, dstFilePath)
132
			require.NoError(t, err)
133
			data, err := os.ReadFile(dstFilePath)
134
			require.NoError(t, err)
135
			assert.Equal(t, string(tt.want), string(data))
136
		})
137
	}
138
}
139

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.