Skip to content

Commit

Permalink
test: add tests for repos endpoint
Browse files Browse the repository at this point in the history
RHINENG-13556
  • Loading branch information
Dugowitch committed Dec 19, 2024
1 parent ba4220b commit d6eef0d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 2 deletions.
78 changes: 76 additions & 2 deletions vmaas/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,54 @@ func TestPackageIDs2Nevras(t *testing.T) {
assert.Equal(t, "kernel-devel-1:1-1.src", sourcePackages[0])
}

func TestBuildRepoID2ErratumIDs(t *testing.T) {
c := mockCache()
modifiedSince, _ := time.Parse(time.RFC3339, "2020-12-15T07:00:00+01:00")

// showPackages = false
repoID2ErratumIDsMap := c.buildRepoID2ErratumIDs(&time.Time{}, false)
assert.Nil(t, repoID2ErratumIDsMap)

// missing modifiedSince
repoID2ErratumIDsMap = c.buildRepoID2ErratumIDs(nil, true)
assert.Nil(t, repoID2ErratumIDsMap)

// usual case
repoID2ErratumIDsMap = c.buildRepoID2ErratumIDs(&time.Time{}, true)
assert.Equal(t, 4, len(repoID2ErratumIDsMap))
assert.Equal(t, 1, len(repoID2ErratumIDsMap[41]))
assert.Equal(t, 2, len(repoID2ErratumIDsMap[42]))
assert.Equal(t, 1, len(repoID2ErratumIDsMap[43]))
assert.Equal(t, 1, len(repoID2ErratumIDsMap[44]))

// filter by modifiedSince
repoID2ErratumIDsMap = c.buildRepoID2ErratumIDs(&modifiedSince, true)
assert.Equal(t, 3, len(repoID2ErratumIDsMap))
assert.Equal(t, 1, len(repoID2ErratumIDsMap[42]))
assert.Equal(t, 1, len(repoID2ErratumIDsMap[43]))
assert.Equal(t, 1, len(repoID2ErratumIDsMap[44]))
}

func TestCpeIDs2Labels(t *testing.T) {
c := mockCache()
labels := c.cpeIDs2Labels([]CpeID{1, 3, 2, 3, 4, 5, 6})
assert.Equal(t, 5, len(labels))
}

func TestErratumIDs2PackageNames(t *testing.T) {
c := mockCache()
erratumIDs := []ErratumID{1, 2, 2}
pkgNames := c.erratumIDs2PackageNames(erratumIDs)
assert.Equal(t, 2, len(pkgNames))
}

//nolint:funlen
func mockCache() *Cache {
modifiedDate, _ := time.Parse(time.RFC3339, "2024-10-03T11:44:00+02:00")
publishedDate, _ := time.Parse(time.RFC3339, "2024-10-03T11:44:00+02:00")
lastChange, _ := time.Parse(time.RFC3339, "2024-11-18T17:58:00+01:00")
updated1, _ := time.Parse(time.RFC3339, "2020-10-10T11:00:45+02:00")
updated2, _ := time.Parse(time.RFC3339, "2021-10-10T11:00:45+02:00")
return &Cache{
ID2Packagename: map[NameID]string{1: "kernel", 2: "kernel-devel"},

Expand All @@ -59,15 +103,19 @@ func mockCache() *Cache {

ErratumDetails: map[string]ErratumDetail{
"RHSA-2024:0042": {
ID: 1,
ThirdParty: false,
Type: "security",
Severity: "Important",
PkgIDs: []int{2, 3},
Updated: &updated1,
},
"RHSA-2024:1111": {
ID: 2,
ThirdParty: true,
Type: "bugfix",
Severity: "Low",
Updated: &updated2,
},
},

Expand All @@ -91,8 +139,8 @@ func mockCache() *Cache {
RepoDetails: map[RepoID]RepoDetail{
41: {},
42: {Releasever: "8.2"},
43: {Releasever: "8.3"},
44: {Releasever: "8.4"},
43: {Releasever: "8.3", ThirdParty: true},
44: {Releasever: "8.4", LastChange: &lastChange},
},

CveDetail: map[string]CveDetail{
Expand All @@ -107,6 +155,32 @@ func mockCache() *Cache {
"CVE-2024-1111111": {},
},

RepoLabel2IDs: map[string][]RepoID{
"rhel-6-server-rpms": {41, 42},
"rhel-7-server-rpms": {43},
"rhel-8-server-rpms": {44},
},

CpeID2Label: map[CpeID]CpeLabel{
1: "foo",
2: "bar",
3: "baz",
4: "qux",
5: "quux",
44: "this",
},

RepoID2CpeIDs: map[RepoID][]CpeID{
41: {1, 2},
42: {2, 3},
43: {3, 4},
44: {4, 5, 44},
},

ContentSetID2CpeIDs: map[ContentSetID][]CpeID{
111: {1, 2, 3},
},

DBChange: DBChange{LastChange: "2024-10-02T16:08:00+02:00"},
}
}
66 changes: 66 additions & 0 deletions vmaas/repos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package vmaas

import (
"testing"
"time"

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

func TestFilterInputRepos(t *testing.T) {
repos := []string{"rhel-6-server-rpms", "rhel-7-server-rpms", "rhel-8-server-rpms", "rhel-9-server-rpms"}
c := mockCache()

// usual case
filtered := filterInputRepos(c, repos, &ReposRequest{})
assert.Equal(t, 2, len(filtered))

// ThirdParty
req := &ReposRequest{ThirdParty: true}
filtered = filterInputRepos(c, repos, req)
assert.Equal(t, 3, len(filtered))

// With LastChange before req.ModifiedSince
testTime, _ := time.Parse(time.RFC3339, "2024-11-19T18:01:01+01:00")
req = &ReposRequest{ModifiedSince: &testTime}
filtered = filterInputRepos(c, repos, req)
assert.Equal(t, 1, len(filtered))
assert.Equal(t, "rhel-6-server-rpms", filtered[0])
}

func TestRepoID2CPEs(t *testing.T) {
c := mockCache()
cpes := c.repoID2CPEs(42, 0)
assert.Equal(t, 2, len(cpes))

cpes = c.repoID2CPEs(44, 111)
assert.Equal(t, 3, len(cpes))
}

func TestLoadRepoDetailSlice(t *testing.T) {
c := mockCache()
repoDetailSlice, _ := c.loadRepoDetailSlice("rhel-6-server-rpms", map[RepoID][]ErratumID{})
assert.Equal(t, 2, len(repoDetailSlice))
}

func TestLoadRepoDetails(t *testing.T) {
c := mockCache()

repos := []string{"rhel-6-server-rpms", "rhel-7-server-rpms", "rhel-8-server-rpms"}
expectedChange, _ := time.Parse(time.RFC3339, "2024-11-18T17:58:00+01:00")
_, latestRepoChange, actualPageSize := c.loadRepoDetails(repos, map[RepoID][]ErratumID{})
assert.Equal(t, expectedChange, *latestRepoChange)
assert.Equal(t, 4, actualPageSize)

_, latestRepoChange, _ = c.loadRepoDetails([]string{"rhel-7-server-rpms"}, map[RepoID][]ErratumID{})
assert.Nil(t, latestRepoChange)
}

func TestRepos(t *testing.T) {
req := &ReposRequest{}
c := mockCache()

// empty repository list
_, err := req.repos(c)
assert.Error(t, err)
}

0 comments on commit d6eef0d

Please sign in to comment.