-
Notifications
You must be signed in to change notification settings - Fork 17
/
packages_test.go
327 lines (308 loc) · 11.9 KB
/
packages_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"os"
"testing"
)
var goodOutputGz = `Package: vim-tiny
Source: vim
Version: 2:7.4.052-1ubuntu3
Architecture: amd64
Maintainer: Ubuntu Developers <[email protected]>
Installed-Size: 931
Depends: vim-common (= 2:7.4.052-1ubuntu3), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libselinux1 (>= 1.32), libtinfo5
Suggests: indent
Provides: editor
Section: editors
Priority: important
Homepage: http://www.vim.org/
Description: Vi IMproved - enhanced vi editor - compact version
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a minimal version of vim compiled with no
GUI and a small subset of features in order to keep small the
package size. This package does not depend on the vim-runtime
package, but installing it you will get its additional benefits
(online documentation, plugins, ...).
Original-Maintainer: Debian Vim Maintainers <[email protected]>
`
var goodOutputLzma = `Package: ifupdown2
Version: 3.0.0-1
Architecture: all
Maintainer: Julien Fortin <[email protected]>
Installed-Size: 1576
Depends: python3:any, iproute2
Suggests: isc-dhcp-client, bridge-utils, ethtool, python3-gvgen, python3-mako
Conflicts: ifupdown
Replaces: ifupdown
Provides: ifupdown
Section: admin
Priority: optional
Homepage: https://github.com/cumulusnetworks/ifupdown2
Description: Network Interface Management tool similar to ifupdown
ifupdown2 is ifupdown re-written in Python. It replaces ifupdown and provides
the same user interface as ifupdown for network interface configuration.
Like ifupdown, ifupdown2 is a high level tool to configure (or, respectively
deconfigure) network interfaces based on interface definitions in
/etc/network/interfaces. It is capable of detecting network interface
dependencies and comes with several new features which are available as
new command options to ifup/ifdown/ifquery commands. It also comes with a new
command ifreload to reload interface configuration with minimum
disruption. Most commands are also capable of input and output in JSON format.
It is backward compatible with ifupdown /etc/network/interfaces format and
supports newer simplified format. It also supports interface templates with
python-mako for large scale interface deployments. See
/usr/share/doc/ifupdown2/README.rst for details about ifupdown2. Examples
are available under /usr/share/doc/ifupdown2/examples.
`
var goodPkgGzOutput = `Package: vim-tiny
Source: vim
Version: 2:7.4.052-1ubuntu3
Architecture: amd64
Maintainer: Ubuntu Developers <[email protected]>
Installed-Size: 931
Depends: vim-common (= 2:7.4.052-1ubuntu3), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libselinux1 (>= 1.32), libtinfo5
Suggests: indent
Provides: editor
Section: editors
Priority: important
Homepage: http://www.vim.org/
Description: Vi IMproved - enhanced vi editor - compact version
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a minimal version of vim compiled with no
GUI and a small subset of features in order to keep small the
package size. This package does not depend on the vim-runtime
package, but installing it you will get its additional benefits
(online documentation, plugins, ...).
Original-Maintainer: Debian Vim Maintainers <[email protected]>
Filename: dists/stable/main/binary-cats/test.deb
Size: 391240
MD5sum: 0ec79417129746ff789fcff0976730c5
SHA1: b2ac976af80f0f50a8336402d5a29c67a2880b9b
SHA256: 9938ec82a8c882ebc2d59b64b0bf2ac01e9cbc5a235be4aa268d4f8484e75eab
`
var goodPkgGzOutputNonDefault = `Package: vim-tiny
Source: vim
Version: 2:7.4.052-1ubuntu3
Architecture: amd64
Maintainer: Ubuntu Developers <[email protected]>
Installed-Size: 931
Depends: vim-common (= 2:7.4.052-1ubuntu3), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libselinux1 (>= 1.32), libtinfo5
Suggests: indent
Provides: editor
Section: editors
Priority: important
Homepage: http://www.vim.org/
Description: Vi IMproved - enhanced vi editor - compact version
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a minimal version of vim compiled with no
GUI and a small subset of features in order to keep small the
package size. This package does not depend on the vim-runtime
package, but installing it you will get its additional benefits
(online documentation, plugins, ...).
Original-Maintainer: Debian Vim Maintainers <[email protected]>
Filename: dists/blah/main/binary-cats/test.deb
Size: 391240
MD5sum: 0ec79417129746ff789fcff0976730c5
SHA1: b2ac976af80f0f50a8336402d5a29c67a2880b9b
SHA256: 9938ec82a8c882ebc2d59b64b0bf2ac01e9cbc5a235be4aa268d4f8484e75eab
`
func TestInspectPackage(t *testing.T) {
_, err := inspectPackage("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
t.Errorf("inspectPackage() error: %s", err)
}
_, err = inspectPackage("thisfileshouldnotexist")
if err == nil {
t.Error("inspectPackage() should have failed, it did not")
}
}
func TestInspectPackageControlGz(t *testing.T) {
sampleDeb, err := ioutil.ReadFile("samples/control.tar.gz")
if err != nil {
t.Errorf("error opening sample deb file: %s", err)
}
var controlBuf bytes.Buffer
cfReader := bytes.NewReader(sampleDeb)
io.Copy(&controlBuf, cfReader)
parsedControl, err := inspectPackageControl(GZIP, controlBuf)
if err != nil {
t.Errorf("error inspecting GZIP control file: %s", err)
}
if parsedControl != goodOutputGz {
t.Errorf("control file does not match")
}
var failControlBuf bytes.Buffer
_, err = inspectPackageControl(GZIP, failControlBuf)
if err == nil {
t.Error("inspectPackageControl() should have failed, it did not")
}
}
func TestInspectPackageControlLzma(t *testing.T) {
sampleDeb, err := ioutil.ReadFile("samples/control.tar.xz")
if err != nil {
t.Errorf("error opening sample deb file: %s", err)
}
var controlBuf bytes.Buffer
cfReader := bytes.NewReader(sampleDeb)
io.Copy(&controlBuf, cfReader)
parsedControl, err := inspectPackageControl(LZMA, controlBuf)
if err != nil {
t.Errorf("error inspecting LZMA control file: %s", err)
}
if parsedControl != goodOutputLzma {
t.Errorf("control file does not match")
}
var failControlBuf bytes.Buffer
_, err = inspectPackageControl(LZMA, failControlBuf)
if err == nil {
t.Error("inspectPackageControl() should have failed, it did not")
}
}
func TestCreatePackagesGz(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("Unable to get current working directory: %s", err)
}
config := conf{ListenPort: "9666", RootRepoPath: pwd + "/testing", SupportArch: []string{"cats", "dogs"}, DistroNames: []string{"stable"}, Sections: []string{"main", "blah"}, EnableSSL: false}
// sanity check...
if config.RootRepoPath != pwd+"/testing" {
t.Errorf("RootRepoPath is %s, should be %s\n ", config.RootRepoPath, pwd+"/testing")
}
// copy sample deb to repo location (assuming it exists)
origDeb, err := os.Open("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
t.Errorf("error opening up sample deb: %s", err)
}
defer origDeb.Close()
for _, archDir := range config.SupportArch {
// do not use the built-in createDirs() in case it is broken
if err := os.MkdirAll(config.RootRepoPath+"/dists/stable/main/binary-"+archDir, 0755); err != nil {
t.Errorf("error creating directory for %s: %s\n", archDir, err)
}
copyDeb, err := os.Create(config.RootRepoPath + "/dists/stable/main/binary-" + archDir + "/test.deb")
if err != nil {
t.Errorf("error creating copy of deb: %s", err)
}
_, err = io.Copy(copyDeb, origDeb)
if err != nil {
t.Errorf("error writing copy of deb: %s", err)
}
if err := copyDeb.Close(); err != nil {
t.Errorf("error saving copy of deb: %s", err)
}
}
if err := createPackagesGz(config, "stable", "main", "cats"); err != nil {
t.Errorf("error creating packages gzip for cats")
}
pkgGzip, err := ioutil.ReadFile(config.RootRepoPath + "/dists/stable/main/binary-cats/Packages.gz")
if err != nil {
t.Errorf("error reading Packages.gz: %s", err)
}
pkgReader, err := gzip.NewReader(bytes.NewReader(pkgGzip))
if err != nil {
t.Errorf("error reading existing Packages.gz: %s", err)
}
buf := bytes.NewBuffer(nil)
io.Copy(buf, pkgReader)
if goodPkgGzOutput != buf.String() {
t.Errorf("Packages.gz does not match, returned value is:\n %s \n\n should be:\n %s", buf.String(), goodPkgGzOutput)
}
pkgFile, err := ioutil.ReadFile(config.RootRepoPath + "/dists/stable/main/binary-cats/Packages")
if err != nil {
t.Errorf("error reading Packages: %s", err)
}
if goodPkgGzOutput != string(pkgFile) {
t.Errorf("Packages does not match, returned value is:\n %s \n\n should be:\n %s", buf.String(), goodPkgGzOutput)
}
// cleanup
if err := os.RemoveAll(config.RootRepoPath); err != nil {
t.Errorf("error cleaning up after createPackagesGz(): %s", err)
}
// create temp file
tempFile, err := os.Create(pwd + "/tempFile")
if err != nil {
t.Fatalf("create %s: %s", pwd+"/tempFile", err)
}
defer tempFile.Close()
config.RootRepoPath = pwd + "/tempFile"
// Can't make directory named after file
if err := createPackagesGz(config, "stable", "main", "cats"); err == nil {
t.Errorf("createPackagesGz() should have failed, it did not")
}
// cleanup
if err := os.RemoveAll(pwd + "/tempFile"); err != nil {
t.Errorf("error cleaning up after createDirs(): %s", err)
}
}
func TestCreatePackagesGzNonDefault(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("Unable to get current working directory: %s", err)
}
config := conf{ListenPort: "9666", RootRepoPath: pwd + "/testing", SupportArch: []string{"cats", "dogs"}, DistroNames: []string{"blah"}, EnableSSL: false}
// sanity check...
if config.RootRepoPath != pwd+"/testing" {
t.Errorf("RootRepoPath is %s, should be %s\n ", config.RootRepoPath, pwd+"/testing")
}
// copy sample deb to repo location (assuming it exists)
origDeb, err := os.Open("samples/vim-tiny_7.4.052-1ubuntu3_amd64.deb")
if err != nil {
t.Errorf("error opening up sample deb: %s", err)
}
defer origDeb.Close()
for _, archDir := range config.SupportArch {
// do not use the built-in createDirs() in case it is broken
if err := os.MkdirAll(config.RootRepoPath+"/dists/blah/main/binary-"+archDir, 0755); err != nil {
t.Errorf("error creating directory for %s: %s\n", archDir, err)
}
copyDeb, err := os.Create(config.RootRepoPath + "/dists/blah/main/binary-" + archDir + "/test.deb")
if err != nil {
t.Errorf("error creating copy of deb: %s", err)
}
_, err = io.Copy(copyDeb, origDeb)
if err != nil {
t.Errorf("error writing copy of deb: %s", err)
}
if err := copyDeb.Close(); err != nil {
t.Errorf("error saving copy of deb: %s", err)
}
}
if err := createPackagesGz(config, "blah", "main", "cats"); err != nil {
t.Errorf("error creating packages gzip for cats")
}
pkgGzip, err := ioutil.ReadFile(config.RootRepoPath + "/dists/blah/main/binary-cats/Packages.gz")
if err != nil {
t.Errorf("error reading Packages.gz: %s", err)
}
pkgReader, err := gzip.NewReader(bytes.NewReader(pkgGzip))
if err != nil {
t.Errorf("error reading existing Packages.gz: %s", err)
}
buf := bytes.NewBuffer(nil)
io.Copy(buf, pkgReader)
if goodPkgGzOutputNonDefault != buf.String() {
t.Errorf("Packages.gz does not match, returned value is: %s", buf.String())
}
// cleanup
if err := os.RemoveAll(config.RootRepoPath); err != nil {
t.Errorf("error cleaning up after createPackagesGz(): %s", err)
}
}