Skip to content

Commit

Permalink
test: refactor testdata to be generated
Browse files Browse the repository at this point in the history
* Removed hand-maintained test data
* Added support for test fixture generation
* Added additional test cases for amazonlinux, oraclelinux, fedora38
  • Loading branch information
micahhausler committed Sep 2, 2023
1 parent 067d98b commit da541ed
Show file tree
Hide file tree
Showing 46 changed files with 2,775 additions and 4,602 deletions.
22 changes: 10 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Checkout code
uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.46
- name: Test
run: go test ./...
- name: Install Go
uses: actions/setup-go@v3
- name: Checkout code
uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
- name: Test
run: go test ./...
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# go-rpmdb
[![Go Reference](https://pkg.go.dev/badge/github.com/knqyf263/go-rpmdb.svg)](https://pkg.go.dev/github.com/knqyf263/go-rpmdb)

Library for enumerating packages in an RPM DB `Packages` file (without bindings).

```go
Expand Down Expand Up @@ -37,3 +39,7 @@ func run() error {
return nil
}
```

## License

[MIT License](LICENSE)
109 changes: 109 additions & 0 deletions pkg/package_content_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package rpmdb

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

_ "github.com/glebarez/go-sqlite"
)

type packageContentTestCase struct {
name string
pkgName string
testDir string
databaseFile string
wantPackageFile string
wantInstalledFilesFile string
wantInstalledFileNamesFile string
}

func TestPackageContent(t *testing.T) {
tests := []packageContentTestCase{
{
name: "fedora35 python3",
pkgName: "python3",
testDir: "testdata/fedora35",
databaseFile: "rpmdb.sqlite",
wantPackageFile: "python3.json",
wantInstalledFilesFile: "python3_files.json",
wantInstalledFileNamesFile: "python3_file_names.json",
},
{
name: "centos5 python",
pkgName: "python",
testDir: "testdata/centos5-plain",
databaseFile: "Packages",
wantPackageFile: "python.json",
wantInstalledFilesFile: "python_files.json",
wantInstalledFileNamesFile: "python_file_names.json",
},
{
name: "centos6 glibc",
pkgName: "glibc",
testDir: "testdata/centos6-plain",
databaseFile: "Packages",
wantPackageFile: "glibc.json",
wantInstalledFilesFile: "glibc_files.json",
wantInstalledFileNamesFile: "glibc_file_names.json",
},
{
name: "centos8 nodejs",
pkgName: "nodejs",
testDir: "testdata/fedora38-modules",
databaseFile: "rpmdb.sqlite",
wantPackageFile: "nodejs.json",
wantInstalledFilesFile: "nodejs_files.json",
wantInstalledFileNamesFile: "nodejs_file_names.json",
},
{
name: "CBL-Mariner 2.0 curl",
pkgName: "curl",
testDir: "testdata/cbl-mariner-2.0",
databaseFile: "rpmdb.sqlite",
wantPackageFile: "curl.json",
wantInstalledFilesFile: "curl_files.json",
wantInstalledFileNamesFile: "curl_file_names.json",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

db, err := Open(filepath.Join(tt.testDir, tt.databaseFile))
require.NoError(t, err)

got, err := db.Package(tt.pkgName)
assert.NoError(t, err)

gotInstalledFiles, err := got.InstalledFiles()
assert.NoError(t, err)

gotInstalledFileNames, err := got.InstalledFileNames()
assert.NoError(t, err)

if *update && tt.wantPackageFile != "" {
err = updatePackageFile(t, tt, got)
assert.NoError(t, err)
err = updateInstalledFiles(t, tt, gotInstalledFiles)
assert.NoError(t, err)
err = updateInstalledFileNames(t, tt, gotInstalledFileNames)
assert.NoError(t, err)
}

want, err := readPackageFile(t, tt)
assert.NoError(t, err)

wantInstalledFiles, err := readInstalledFiles(t, tt)
assert.NoError(t, err)

wantInstalledFileNames, err := readInstalledFileNames(t, tt)
assert.NoError(t, err)

assert.Equal(t, wantInstalledFiles, gotInstalledFiles)
assert.Equal(t, wantInstalledFileNames, gotInstalledFileNames)
assert.Equal(t, want, got)
})
}
}
197 changes: 197 additions & 0 deletions pkg/package_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package rpmdb

import (
"flag"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

_ "github.com/glebarez/go-sqlite"
)

// To update all test fixtures, run:
//
// go test ./... -update-testcases=true
var update = flag.Bool("update-testcases", false, "update testcases")

type testCase struct {
name string
databaseFile string // Test input file
testDir string
pkgList []*PackageInfo
image string
}

func TestPackageList(t *testing.T) {
tests := []testCase{
{
name: "amazonlinux2 plain",
databaseFile: "testdata/amazonlinux2-plain/Packages",
testDir: "testdata/amazonlinux2-plain",
image: "public.ecr.aws/amazonlinux/amazonlinux:2",
},
{
name: "amazonlinux2023 plain",
databaseFile: "testdata/amazonlinux2023-plain/rpmdb.sqlite",
testDir: "testdata/amazonlinux2023-plain",
image: "public.ecr.aws/amazonlinux/amazonlinux:2023",
},
{
name: "amazonlinux2023 devtools",
testDir: "testdata/amazonlinux2023-devtools",
image: "public.ecr.aws/amazonlinux/amazonlinux:2023",
},
{
name: "oraclelinux9 plain",
testDir: "testdata/oraclelinux9-plain",
image: "oraclelinux:9",
},
{
name: "CentOS5 plain",
databaseFile: "testdata/centos5-plain/Packages",
testDir: "testdata/centos5-plain",
image: "centos:5",
},
{
name: "CentOS6 Plain",
databaseFile: "testdata/centos6-plain/Packages",
image: "centos:6",
testDir: "testdata/centos6-plain",
},
{
name: "CentOS6 with Development tools",
databaseFile: "testdata/centos6-devtools/Packages",
image: "centos:6",
testDir: "testdata/centos6-devtools",
},
{
name: "CentOS6 with many packages",
databaseFile: "testdata/centos6-many/Packages",
image: "centos:6",
testDir: "testdata/centos6-many",
},
{
name: "CentOS7 Plain",
databaseFile: "testdata/centos7-plain/Packages",
image: "centos:7",
testDir: "testdata/centos7-plain",
},
{
name: "CentOS7 with Development tools",
databaseFile: "testdata/centos7-devtools/Packages",
image: "centos:7",
testDir: "testdata/centos7-devtools",
},
// TODO: Flakey test, not sure why?
// {
// name: "CentOS7 with many packages",
// databaseFile: "testdata/centos7-many/Packages",
// image: "centos:7",
// testDir: "testdata/centos7-many",
// },
{
name: "CentOS7 with Python 3.5",
databaseFile: "testdata/centos7-python35/Packages",
image: "centos/python-35-centos7",
testDir: "testdata/centos7-python35",
},
{
name: "CentOS7 with httpd 2.4",
databaseFile: "testdata/centos7-httpd24/Packages",
image: "centos/httpd-24-centos7",
testDir: "testdata/centos7-httpd24",
},

/*
// TODO: remove?
{
name: "RHEL UBI8 from s390x",
databaseFile: "testdata/ubi8-s390x/Packages",
pkgList: UBI8s390x(),
},
*/
// TODO: had to manually add m-dashes in liblzma5 summary
{
name: "SLE15 with NDB style rpm database",
databaseFile: "testdata/sle15-bci/Packages.db",
image: "registry.suse.com/bci/bci-minimal:15.3",
testDir: "testdata/sle15-bci",
},
{
name: "Fedora35 with SQLite3 style rpm database",
databaseFile: "testdata/fedora35/rpmdb.sqlite",
image: "fedora:35",
testDir: "testdata/fedora35",
},
{
name: "Fedora35 plus MongoDB with SQLite3 style rpm database",
databaseFile: "testdata/fedora35-plus-mongo/rpmdb.sqlite",
image: "fedora:35",
testDir: "testdata/fedora35-plus-mongo",
},
{
name: "Fedora38 modules",
databaseFile: "testdata/fedora38-modules/rpmdb.sqlite",
image: "fedora:38",
testDir: "testdata/fedora38-modules",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if *update && tt.image != "" && tt.pkgList == nil {
err := updateTestCase(t, tt)
require.NoError(t, err)
}

if tt.testDir != "" {
var err error
tt.pkgList, err = readTestCase(t, tt)
if err != nil {
require.NoError(t, err)
}
}
// TODO: remove once ubi8-s390x is gone
if tt.databaseFile == "" {
tt.databaseFile = filepath.Join(tt.testDir, "rpmdb.sqlite")
}

db, err := Open(tt.databaseFile)
require.NoError(t, err)

got, err := db.ListPackages()
require.NoError(t, err)

// They are tested in another function.
for _, g := range got {
g.PGP = ""
g.DigestAlgorithm = 0
g.InstallTime = 0
g.BaseNames = nil
g.DirIndexes = nil
g.DirNames = nil
g.FileSizes = nil
g.FileDigests = nil
g.FileModes = nil
g.FileFlags = nil
g.UserNames = nil
g.GroupNames = nil
g.Provides = nil
g.Requires = nil
}

if len(tt.pkgList) > len(got) {
t.Fatalf("Got too few packages. Expected: %d, got: %d", len(tt.pkgList), len(got))
}
if len(tt.pkgList) < len(got) {
t.Fatalf("Got too many packages. Expected: %d, got: %d", len(tt.pkgList), len(got))
}

for i, p := range tt.pkgList {
assert.Equal(t, got[i], p)
}
})
}
}
Loading

0 comments on commit da541ed

Please sign in to comment.