Skip to content

Commit

Permalink
feat: support RHEL/CentOS 5 (#6)
Browse files Browse the repository at this point in the history
* test(rpmdb): add a failure case

* feat: support RHEL/CentOS 5
  • Loading branch information
knqyf263 authored Dec 15, 2020
1 parent 9f953f9 commit a9e3110
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 61 deletions.
6 changes: 4 additions & 2 deletions pkg/bdb/bdb.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package bdb

import (
"golang.org/x/xerrors"
"io"
"os"

"golang.org/x/xerrors"
)

var validPageSizes = map[uint32]struct{}{
Expand Down Expand Up @@ -93,7 +94,8 @@ func (db *BerkeleyDB) Read() <-chan Entry {
return
}

if hashPageHeader.PageType != HashPageType {
if hashPageHeader.PageType != HashUnsortedPageType && // for RHEL/CentOS 5
hashPageHeader.PageType != HashPageType {
// skip over pages that do not have hash values
continue
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/bdb/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ const (
PageHeaderSize = 26

// all page types supported
// https://github.com/berkeleydb/libdb/blob/v5.3.28/src/dbinc/db_page.h#L35-L53
HashUnsortedPageType PageType = 2 // Hash pages created pre 4.6. DEPRECATED
OverflowPageType PageType = 7
HashMetadataPageType PageType = 8
HashPageType PageType = 13
HashPageType PageType = 13 // Sorted hash page.

// https://github.com/berkeleydb/libdb/blob/v5.3.28/src/dbinc/db_page.h#L569-L573
HashOffIndexPageType PageType = 3 // aka HOFFPAGE

HashOffPageSize = 12 // (in bytes)
Expand Down
76 changes: 27 additions & 49 deletions pkg/rpmdb_test.go
Original file line number Diff line number Diff line change
@@ -1,101 +1,79 @@
package rpmdb

import (
"path"
"testing"

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

func TestPackageList(t *testing.T) {
vectors := []struct {
tests := []struct {
name string
file string // Test input file
pkgList []PackageInfo
pkgList []*PackageInfo
}{
{
name: "CentOS5 plain",
file: "testdata/centos5-plain/Packages",
pkgList: CentOS5Plain,
},
{
name: "CentOS6 Plain",
file: "testdata/centos6-plain/Packages",
pkgList: CentOS6Plain,
},
{
name: "CentOS6 with Development tools",
file: "testdata/centos6-devtools/Packages",
pkgList: CentOS6DevTools,
},
{
name: "CentOS6 with many packages",
file: "testdata/centos6-many/Packages",
pkgList: CentOS6Many,
},
{
name: "CentOS7 Plain",
file: "testdata/centos7-plain/Packages",
pkgList: CentOS7Plain,
},
{
name: "CentOS7 with Development tools",
file: "testdata/centos7-devtools/Packages",
pkgList: CentOS7DevTools,
},
{
name: "CentOS7 with many packages",
file: "testdata/centos7-many/Packages",
pkgList: CentOS7Many,
},
{
name: "CentOS7 with Python 3.5",
file: "testdata/centos7-python35/Packages",
pkgList: CentOS7Python35,
},
{
name: "CentOS7 with httpd 2.4",
file: "testdata/centos7-httpd24/Packages",
pkgList: CentOS7Httpd24,
},
{
name: "CentOS8 with modules",
file: "testdata/centos8-modularitylabel/Packages",
pkgList: CentOS8Modularitylabel,
},
}

for _, v := range vectors {
t.Run(path.Base(v.file), func(t *testing.T) {
db, err := Open(v.file)
if err != nil {
t.Fatalf("Open() error: %v", err)
}
pkgList, err := db.ListPackages()
if err != nil {
t.Fatalf("ListPackagges() error: %v", err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db, err := Open(tt.file)
require.NoError(t, err)

if len(pkgList) != len(v.pkgList) {
t.Errorf("pkg length: got %v, want %v", len(pkgList), len(v.pkgList))
}
got, err := db.ListPackages()
require.NoError(t, err)

for i, got := range pkgList {
want := v.pkgList[i]
if want.Epoch != got.Epoch {
t.Errorf("%d: Epoch: got %d, want %d", i, got.Epoch, want.Epoch)
}
if want.Name != got.Name {
t.Errorf("%d: Name: got %s, want %s", i, got.Name, want.Name)
}
if want.Version != got.Version {
t.Errorf("%d: Version: got %s, want %s", i, got.Version, want.Version)
}
if want.Release != got.Release {
t.Errorf("%d: Release: got %s, want %s", i, got.Release, want.Release)
}
if want.Arch != got.Arch {
t.Errorf("%d: Arch: got %s, want %s", i, got.Arch, want.Arch)
}
if want.SourceRpm != got.SourceRpm {
t.Errorf("%d: SourceRpm: got %s, want %s", i, got.SourceRpm, want.SourceRpm)
}
if want.Vendor != got.Vendor {
t.Errorf("%d: Vendor: got %s, want %s", i, got.Vendor, want.Vendor)
}
if want.Size != got.Size {
t.Errorf("%d: Size: got %d, want %d", i, got.Size, want.Size)
}
if want.License != got.License {
t.Errorf("%d: License: got %s, want %s", i, got.License, want.License)
}
if want.Modularitylabel != got.Modularitylabel {
t.Errorf("%d: Modularitylabel: got %s, want %s", i, got.Modularitylabel, want.Modularitylabel)
}
}
assert.ElementsMatch(t, tt.pkgList, got)
})
}
}
Loading

0 comments on commit a9e3110

Please sign in to comment.