Skip to content

Commit da541ed

Browse files
committed
test: refactor testdata to be generated
* Removed hand-maintained test data * Added support for test fixture generation * Added additional test cases for amazonlinux, oraclelinux, fedora38
1 parent 067d98b commit da541ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2775
-4602
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ jobs:
44
test:
55
runs-on: ubuntu-latest
66
steps:
7-
- name: Install Go
8-
uses: actions/setup-go@v3
9-
with:
10-
go-version: 1.18
11-
- name: Checkout code
12-
uses: actions/checkout@v3
13-
- name: golangci-lint
14-
uses: golangci/golangci-lint-action@v3
15-
with:
16-
version: v1.46
17-
- name: Test
18-
run: go test ./...
7+
- name: Install Go
8+
uses: actions/setup-go@v3
9+
- name: Checkout code
10+
uses: actions/checkout@v3
11+
- name: golangci-lint
12+
uses: golangci/golangci-lint-action@v3
13+
with:
14+
version: v1.54
15+
- name: Test
16+
run: go test ./...

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# go-rpmdb
2+
[![Go Reference](https://pkg.go.dev/badge/github.com/knqyf263/go-rpmdb.svg)](https://pkg.go.dev/github.com/knqyf263/go-rpmdb)
3+
24
Library for enumerating packages in an RPM DB `Packages` file (without bindings).
35

46
```go
@@ -37,3 +39,7 @@ func run() error {
3739
return nil
3840
}
3941
```
42+
43+
## License
44+
45+
[MIT License](LICENSE)

pkg/package_content_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package rpmdb
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
10+
_ "github.com/glebarez/go-sqlite"
11+
)
12+
13+
type packageContentTestCase struct {
14+
name string
15+
pkgName string
16+
testDir string
17+
databaseFile string
18+
wantPackageFile string
19+
wantInstalledFilesFile string
20+
wantInstalledFileNamesFile string
21+
}
22+
23+
func TestPackageContent(t *testing.T) {
24+
tests := []packageContentTestCase{
25+
{
26+
name: "fedora35 python3",
27+
pkgName: "python3",
28+
testDir: "testdata/fedora35",
29+
databaseFile: "rpmdb.sqlite",
30+
wantPackageFile: "python3.json",
31+
wantInstalledFilesFile: "python3_files.json",
32+
wantInstalledFileNamesFile: "python3_file_names.json",
33+
},
34+
{
35+
name: "centos5 python",
36+
pkgName: "python",
37+
testDir: "testdata/centos5-plain",
38+
databaseFile: "Packages",
39+
wantPackageFile: "python.json",
40+
wantInstalledFilesFile: "python_files.json",
41+
wantInstalledFileNamesFile: "python_file_names.json",
42+
},
43+
{
44+
name: "centos6 glibc",
45+
pkgName: "glibc",
46+
testDir: "testdata/centos6-plain",
47+
databaseFile: "Packages",
48+
wantPackageFile: "glibc.json",
49+
wantInstalledFilesFile: "glibc_files.json",
50+
wantInstalledFileNamesFile: "glibc_file_names.json",
51+
},
52+
{
53+
name: "centos8 nodejs",
54+
pkgName: "nodejs",
55+
testDir: "testdata/fedora38-modules",
56+
databaseFile: "rpmdb.sqlite",
57+
wantPackageFile: "nodejs.json",
58+
wantInstalledFilesFile: "nodejs_files.json",
59+
wantInstalledFileNamesFile: "nodejs_file_names.json",
60+
},
61+
{
62+
name: "CBL-Mariner 2.0 curl",
63+
pkgName: "curl",
64+
testDir: "testdata/cbl-mariner-2.0",
65+
databaseFile: "rpmdb.sqlite",
66+
wantPackageFile: "curl.json",
67+
wantInstalledFilesFile: "curl_files.json",
68+
wantInstalledFileNamesFile: "curl_file_names.json",
69+
},
70+
}
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
74+
db, err := Open(filepath.Join(tt.testDir, tt.databaseFile))
75+
require.NoError(t, err)
76+
77+
got, err := db.Package(tt.pkgName)
78+
assert.NoError(t, err)
79+
80+
gotInstalledFiles, err := got.InstalledFiles()
81+
assert.NoError(t, err)
82+
83+
gotInstalledFileNames, err := got.InstalledFileNames()
84+
assert.NoError(t, err)
85+
86+
if *update && tt.wantPackageFile != "" {
87+
err = updatePackageFile(t, tt, got)
88+
assert.NoError(t, err)
89+
err = updateInstalledFiles(t, tt, gotInstalledFiles)
90+
assert.NoError(t, err)
91+
err = updateInstalledFileNames(t, tt, gotInstalledFileNames)
92+
assert.NoError(t, err)
93+
}
94+
95+
want, err := readPackageFile(t, tt)
96+
assert.NoError(t, err)
97+
98+
wantInstalledFiles, err := readInstalledFiles(t, tt)
99+
assert.NoError(t, err)
100+
101+
wantInstalledFileNames, err := readInstalledFileNames(t, tt)
102+
assert.NoError(t, err)
103+
104+
assert.Equal(t, wantInstalledFiles, gotInstalledFiles)
105+
assert.Equal(t, wantInstalledFileNames, gotInstalledFileNames)
106+
assert.Equal(t, want, got)
107+
})
108+
}
109+
}

pkg/package_list_test.go

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package rpmdb
2+
3+
import (
4+
"flag"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
11+
_ "github.com/glebarez/go-sqlite"
12+
)
13+
14+
// To update all test fixtures, run:
15+
//
16+
// go test ./... -update-testcases=true
17+
var update = flag.Bool("update-testcases", false, "update testcases")
18+
19+
type testCase struct {
20+
name string
21+
databaseFile string // Test input file
22+
testDir string
23+
pkgList []*PackageInfo
24+
image string
25+
}
26+
27+
func TestPackageList(t *testing.T) {
28+
tests := []testCase{
29+
{
30+
name: "amazonlinux2 plain",
31+
databaseFile: "testdata/amazonlinux2-plain/Packages",
32+
testDir: "testdata/amazonlinux2-plain",
33+
image: "public.ecr.aws/amazonlinux/amazonlinux:2",
34+
},
35+
{
36+
name: "amazonlinux2023 plain",
37+
databaseFile: "testdata/amazonlinux2023-plain/rpmdb.sqlite",
38+
testDir: "testdata/amazonlinux2023-plain",
39+
image: "public.ecr.aws/amazonlinux/amazonlinux:2023",
40+
},
41+
{
42+
name: "amazonlinux2023 devtools",
43+
testDir: "testdata/amazonlinux2023-devtools",
44+
image: "public.ecr.aws/amazonlinux/amazonlinux:2023",
45+
},
46+
{
47+
name: "oraclelinux9 plain",
48+
testDir: "testdata/oraclelinux9-plain",
49+
image: "oraclelinux:9",
50+
},
51+
{
52+
name: "CentOS5 plain",
53+
databaseFile: "testdata/centos5-plain/Packages",
54+
testDir: "testdata/centos5-plain",
55+
image: "centos:5",
56+
},
57+
{
58+
name: "CentOS6 Plain",
59+
databaseFile: "testdata/centos6-plain/Packages",
60+
image: "centos:6",
61+
testDir: "testdata/centos6-plain",
62+
},
63+
{
64+
name: "CentOS6 with Development tools",
65+
databaseFile: "testdata/centos6-devtools/Packages",
66+
image: "centos:6",
67+
testDir: "testdata/centos6-devtools",
68+
},
69+
{
70+
name: "CentOS6 with many packages",
71+
databaseFile: "testdata/centos6-many/Packages",
72+
image: "centos:6",
73+
testDir: "testdata/centos6-many",
74+
},
75+
{
76+
name: "CentOS7 Plain",
77+
databaseFile: "testdata/centos7-plain/Packages",
78+
image: "centos:7",
79+
testDir: "testdata/centos7-plain",
80+
},
81+
{
82+
name: "CentOS7 with Development tools",
83+
databaseFile: "testdata/centos7-devtools/Packages",
84+
image: "centos:7",
85+
testDir: "testdata/centos7-devtools",
86+
},
87+
// TODO: Flakey test, not sure why?
88+
// {
89+
// name: "CentOS7 with many packages",
90+
// databaseFile: "testdata/centos7-many/Packages",
91+
// image: "centos:7",
92+
// testDir: "testdata/centos7-many",
93+
// },
94+
{
95+
name: "CentOS7 with Python 3.5",
96+
databaseFile: "testdata/centos7-python35/Packages",
97+
image: "centos/python-35-centos7",
98+
testDir: "testdata/centos7-python35",
99+
},
100+
{
101+
name: "CentOS7 with httpd 2.4",
102+
databaseFile: "testdata/centos7-httpd24/Packages",
103+
image: "centos/httpd-24-centos7",
104+
testDir: "testdata/centos7-httpd24",
105+
},
106+
107+
/*
108+
// TODO: remove?
109+
{
110+
name: "RHEL UBI8 from s390x",
111+
databaseFile: "testdata/ubi8-s390x/Packages",
112+
pkgList: UBI8s390x(),
113+
},
114+
*/
115+
// TODO: had to manually add m-dashes in liblzma5 summary
116+
{
117+
name: "SLE15 with NDB style rpm database",
118+
databaseFile: "testdata/sle15-bci/Packages.db",
119+
image: "registry.suse.com/bci/bci-minimal:15.3",
120+
testDir: "testdata/sle15-bci",
121+
},
122+
{
123+
name: "Fedora35 with SQLite3 style rpm database",
124+
databaseFile: "testdata/fedora35/rpmdb.sqlite",
125+
image: "fedora:35",
126+
testDir: "testdata/fedora35",
127+
},
128+
{
129+
name: "Fedora35 plus MongoDB with SQLite3 style rpm database",
130+
databaseFile: "testdata/fedora35-plus-mongo/rpmdb.sqlite",
131+
image: "fedora:35",
132+
testDir: "testdata/fedora35-plus-mongo",
133+
},
134+
{
135+
name: "Fedora38 modules",
136+
databaseFile: "testdata/fedora38-modules/rpmdb.sqlite",
137+
image: "fedora:38",
138+
testDir: "testdata/fedora38-modules",
139+
},
140+
}
141+
142+
for _, tt := range tests {
143+
t.Run(tt.name, func(t *testing.T) {
144+
if *update && tt.image != "" && tt.pkgList == nil {
145+
err := updateTestCase(t, tt)
146+
require.NoError(t, err)
147+
}
148+
149+
if tt.testDir != "" {
150+
var err error
151+
tt.pkgList, err = readTestCase(t, tt)
152+
if err != nil {
153+
require.NoError(t, err)
154+
}
155+
}
156+
// TODO: remove once ubi8-s390x is gone
157+
if tt.databaseFile == "" {
158+
tt.databaseFile = filepath.Join(tt.testDir, "rpmdb.sqlite")
159+
}
160+
161+
db, err := Open(tt.databaseFile)
162+
require.NoError(t, err)
163+
164+
got, err := db.ListPackages()
165+
require.NoError(t, err)
166+
167+
// They are tested in another function.
168+
for _, g := range got {
169+
g.PGP = ""
170+
g.DigestAlgorithm = 0
171+
g.InstallTime = 0
172+
g.BaseNames = nil
173+
g.DirIndexes = nil
174+
g.DirNames = nil
175+
g.FileSizes = nil
176+
g.FileDigests = nil
177+
g.FileModes = nil
178+
g.FileFlags = nil
179+
g.UserNames = nil
180+
g.GroupNames = nil
181+
g.Provides = nil
182+
g.Requires = nil
183+
}
184+
185+
if len(tt.pkgList) > len(got) {
186+
t.Fatalf("Got too few packages. Expected: %d, got: %d", len(tt.pkgList), len(got))
187+
}
188+
if len(tt.pkgList) < len(got) {
189+
t.Fatalf("Got too many packages. Expected: %d, got: %d", len(tt.pkgList), len(got))
190+
}
191+
192+
for i, p := range tt.pkgList {
193+
assert.Equal(t, got[i], p)
194+
}
195+
})
196+
}
197+
}

0 commit comments

Comments
 (0)