-
Notifications
You must be signed in to change notification settings - Fork 32
/
rpm_test.go
202 lines (180 loc) · 4.8 KB
/
rpm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package rpmpack
import (
"bytes"
"io"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/klauspost/compress/zstd"
gzip "github.com/klauspost/pgzip"
"github.com/ulikunitz/xz"
"github.com/ulikunitz/xz/lzma"
)
func TestFileOwner(t *testing.T) {
r, err := NewRPM(RPMMetaData{})
if err != nil {
t.Fatalf("NewRPM returned error %v", err)
}
group := "testGroup"
user := "testUser"
r.AddFile(RPMFile{
Name: "/usr/local/hello",
Body: []byte("content of the file"),
Group: group,
Owner: user,
})
if err := r.Write(io.Discard); err != nil {
t.Errorf("NewRPM returned error %v", err)
}
if r.fileowners[0] != user {
t.Errorf("File owner shoud be %s but is %s", user, r.fileowners[0])
}
if r.filegroups[0] != group {
t.Errorf("File owner shoud be %s but is %s", group, r.filegroups[0])
}
}
// https://github.com/google/rpmpack/issues/49
func Test100644(t *testing.T) {
r, err := NewRPM(RPMMetaData{})
if err != nil {
t.Fatalf("NewRPM returned error %v", err)
}
r.AddFile(RPMFile{
Name: "/usr/local/hello",
Body: []byte("content of the file"),
Mode: 0100644,
})
if err := r.Write(io.Discard); err != nil {
t.Errorf("Write returned error %v", err)
}
if r.filemodes[0] != 0100644 {
t.Errorf("file mode want 0100644, got %o", r.filemodes[0])
}
if r.filelinktos[0] != "" {
t.Errorf("linktos want empty (not a symlink), got %q", r.filelinktos[0])
}
}
func TestCompression(t *testing.T) {
testCases := []struct {
Type string
Compressors []string
ExpectedWriter io.Writer
}{
{
Type: "gzip",
Compressors: []string{
"", "gzip", "gzip:1", "gzip:2", "gzip:3",
"gzip:4", "gzip:5", "gzip:6", "gzip:7", "gzip:8", "gzip:9",
},
ExpectedWriter: &gzip.Writer{},
},
{
Type: "gzip",
Compressors: []string{"gzip:fast", "gzip:10"},
ExpectedWriter: nil, // gzip requires an integer level from -2 to 9
},
{
Type: "lzma",
Compressors: []string{"lzma"},
ExpectedWriter: &lzma.Writer{},
},
{
Type: "lzma",
Compressors: []string{"lzma:fast", "lzma:1"},
ExpectedWriter: nil, // lzma does not support specifying the compression level
},
{
Type: "xz",
Compressors: []string{"xz"},
ExpectedWriter: &xz.Writer{},
},
{
Type: "xz",
Compressors: []string{"xz:fast", "xz:1"},
ExpectedWriter: nil, // xz does not support specifying the compression level
},
{
Type: "zstd",
Compressors: []string{
"zstd", "zstd:fastest", "zstd:default", "zstd:better",
"zstd:best", "zstd:BeSt", "zstd:0", "zstd:4", "zstd:8", "zstd:15",
},
ExpectedWriter: &zstd.Encoder{},
},
{
Type: "zstd",
Compressors: []string{"xz:worst"},
ExpectedWriter: nil, // only integers levels or one of the pre-defined string values
},
}
for _, testCase := range testCases {
testCase := testCase
for _, compressor := range testCase.Compressors {
t.Run(compressor, func(t *testing.T) {
r, err := NewRPM(RPMMetaData{
Compressor: compressor,
})
if err != nil {
if testCase.ExpectedWriter == nil {
return // an error is expected
}
t.Fatalf("NewRPM returned error %v", err)
}
if testCase.ExpectedWriter == nil {
t.Fatalf("compressor %q should have produced an error", compressor)
}
if r.RPMMetaData.Compressor != testCase.Type {
t.Fatalf("expected compressor %q, got %q", compressor,
r.RPMMetaData.Compressor)
}
expectedWriterType := reflect.Indirect(reflect.ValueOf(
testCase.ExpectedWriter)).String()
actualWriterType := reflect.Indirect(reflect.ValueOf(
r.compressedPayload)).String()
if expectedWriterType != actualWriterType {
t.Fatalf("expected writer to be %T, got %T instead",
testCase.ExpectedWriter, r.compressedPayload)
}
})
}
}
}
func TestAllowListDirs(t *testing.T) {
r, err := NewRPM(RPMMetaData{})
if err != nil {
t.Fatalf("NewRPM returned error %v", err)
}
r.AddFile(RPMFile{
Name: "/usr/local/dir1",
Mode: 040000,
})
r.AddFile(RPMFile{
Name: "/usr/local/dir2",
Mode: 040000,
})
r.AllowListDirs(map[string]bool{"/usr/local/dir1": true})
if err := r.Write(io.Discard); err != nil {
t.Errorf("NewRPM returned error %v", err)
}
expected := map[string]RPMFile{"/usr/local/dir1": {Name: "/usr/local/dir1", Mode: 040000}}
if d := cmp.Diff(expected, r.files); d != "" {
t.Errorf("Expected dirs differs (want->got):\n%v", d)
}
}
func TestMinimalSpec(t *testing.T) {
r, err := NewRPM(RPMMetaData{
Name: "test",
Version: "1.0",
Release: "1",
Summary: "test summary",
Description: "test description",
Licence: "test license",
})
if err != nil {
t.Fatalf("NewRPM returned error %v", err)
}
var b bytes.Buffer
if err := r.Write(&b); err != nil {
t.Errorf("Write returned error %v", err)
}
}