Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman committed Nov 21, 2024
1 parent 8b50e31 commit ba4a2b4
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 185 deletions.
3 changes: 2 additions & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ tasks:
- task: check-licenses
- task: lint
- task: validate-cyclonedx-schema
- task: validate-grype-db-schema
# TODO: while developing v6, we need to disable this check (since v5 and v6 are imported in the same codebase)
# - task: validate-grype-db-schema

test:
desc: Run all levels of test
Expand Down
4 changes: 2 additions & 2 deletions cmd/grype/cli/commands/db_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package commands

import (
"fmt"
"github.com/anchore/grype/grype/db/v6/distribution"
"github.com/anchore/grype/grype/db/v6/installation"
"path/filepath"
"strings"

Expand All @@ -12,6 +10,8 @@ import (
"github.com/anchore/clio"
"github.com/anchore/grype/cmd/grype/cli/options"
legacyDistribution "github.com/anchore/grype/grype/db/legacy/distribution"
"github.com/anchore/grype/grype/db/v6/distribution"
"github.com/anchore/grype/grype/db/v6/installation"
"github.com/anchore/grype/internal"
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/grype/cli/options/database.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package options

import (
"github.com/anchore/grype/grype/db/v6/distribution"
"github.com/anchore/grype/grype/db/v6/installation"
"path"
"time"

"github.com/adrg/xdg"

"github.com/anchore/clio"
legacyDistribution "github.com/anchore/grype/grype/db/legacy/distribution"
"github.com/anchore/grype/grype/db/v6/distribution"
"github.com/anchore/grype/grype/db/v6/installation"
"github.com/anchore/grype/internal"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func (cfg Database) ToClientConfig() distribution.Config {
LatestURL: cfg.UpdateURL,
CACert: cfg.CACert,
RequireUpdateCheck: cfg.RequireUpdateCheck,
CheckTimeout: cfg.UpdateAvailableTimeout, // TODO: is this right?
CheckTimeout: cfg.UpdateAvailableTimeout,
UpdateTimeout: cfg.UpdateDownloadTimeout,
}
}
Expand Down
116 changes: 13 additions & 103 deletions grype/db/v6/description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,38 @@ package v6

import (
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"

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

"github.com/anchore/grype/grype/db/internal/schemaver"
)

func TestNewDatabaseDescriptionFromDir(t *testing.T) {
func TestReadDescription(t *testing.T) {
tempDir := t.TempDir()

// make a test DB
s, err := NewWriter(Config{DBDirPath: tempDir})
require.NoError(t, err)
require.NoError(t, s.SetDBMetadata())
expected, err := s.GetDBMetadata()
require.NoError(t, err)
require.NoError(t, s.Close())

// get the xxhash of the db file
hasher := xxhash.New64()
dbFilePath := path.Join(tempDir, VulnerabilityDBFileName)
f, err := os.Open(dbFilePath)
require.NoError(t, err)
_, err = io.Copy(hasher, f)
require.NoError(t, err)
require.NoError(t, f.Close())
expectedHash := fmt.Sprintf("xxh64:%x", hasher.Sum(nil))

// run the test subject
description, err := CalculateDescription(dbFilePath)
description, err := ReadDescription(dbFilePath)
require.NoError(t, err)
require.NotNil(t, description)

// did it work?
assert.Equal(t, Description{
SchemaVersion: schemaver.New(expected.Model, expected.Revision, expected.Addition),
Built: Time{*expected.BuildTimestamp},
Checksum: expectedHash,
}, *description)
}

Expand Down Expand Up @@ -131,30 +116,23 @@ func TestTime_JSONUnmarshalling(t *testing.T) {
func TestWriteChecksums(t *testing.T) {

cases := []struct {
name string
description Description
expected string
wantErr require.ErrorAssertionFunc
name string
digest string
expected string
wantErr require.ErrorAssertionFunc
}{
{
name: "go case",
description: Description{
SchemaVersion: "1.0.0",
Built: Time{Time: time.Date(2023, 9, 26, 12, 2, 3, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
name: "go case",
digest: "xxh64:dummychecksum",
expected: "xxh64:dummychecksum",
},
{
name: "empty checksum",
description: Description{},
wantErr: require.Error,
name: "empty checksum",
wantErr: require.Error,
},
{
name: "missing prefix",
description: Description{
Checksum: "dummychecksum",
},
name: "missing prefix",
digest: "dummychecksum",
wantErr: require.Error,
},
}
Expand All @@ -165,7 +143,7 @@ func TestWriteChecksums(t *testing.T) {
tc.wantErr = require.NoError
}
sb := strings.Builder{}
err := WriteChecksums(&sb, tc.description)
err := WriteChecksums(&sb, tc.digest)
tc.wantErr(t, err)
if err == nil {
assert.Equal(t, tc.expected, sb.String())
Expand All @@ -174,74 +152,6 @@ func TestWriteChecksums(t *testing.T) {
}
}

func TestReadDescriptionAndCalculateDescription(t *testing.T) {
tests := []struct {
name string
setupFiles func(t testing.TB, dir string) error
expectedErr string
}{
{
name: "database file missing",
setupFiles: func(t testing.TB, dir string) error {
return nil
},
expectedErr: "database does not exist",
},
{
name: "checksum file missing",
setupFiles: func(t testing.TB, dir string) error {
s := setupTestStore(t, dir)
require.NoError(t, s.SetDBMetadata())
// since we don't close, there is no checksums
return nil
},
expectedErr: "failed to read checksums file",
},
{
name: "checksum file empty",
setupFiles: func(t testing.TB, dir string) error {
s := setupTestStore(t, dir)
require.NoError(t, s.SetDBMetadata())
require.NoError(t, s.Close())
// truncate the checksums file
require.NoError(t, os.Truncate(filepath.Join(dir, ChecksumFileName), 0))
return nil
},
expectedErr: "checksums file is empty",
},
{
name: "valid database",
setupFiles: func(t testing.TB, dir string) error {
s := setupTestStore(t, dir)
require.NoError(t, s.SetDBMetadata())
require.NoError(t, s.Close())
return nil
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
err := tt.setupFiles(t, dir)
require.NoError(t, err)

desc, err := ReadDescription(dir)

if tt.expectedErr != "" {
require.ErrorContains(t, err, tt.expectedErr)
require.Nil(t, desc)
} else {
require.NoError(t, err)
require.NotNil(t, desc)
calcDesc, err := CalculateDescription(filepath.Join(dir, VulnerabilityDBFileName))
require.NoError(t, err)
assert.Equal(t, calcDesc, desc)
}
})
}
}

func TestReadDBChecksum(t *testing.T) {
tests := []struct {
name string
Expand Down
27 changes: 6 additions & 21 deletions grype/db/v6/distribution/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ func TestClient_LatestFromURL(t *testing.T) {
name: "go case",
setupServer: func() *httptest.Server {
doc := LatestDocument{
SchemaVersion: "1.0.0",
Status: "active",
Status: "active",
Archive: Archive{
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 26, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive",
Checksum: "checksum123",
Expand All @@ -49,13 +47,11 @@ func TestClient_LatestFromURL(t *testing.T) {
}))
},
expectedDoc: &LatestDocument{
SchemaVersion: "1.0.0",
Status: "active",
Status: "active",
Archive: Archive{
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 26, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive",
Checksum: "checksum123",
Expand Down Expand Up @@ -193,13 +189,11 @@ func TestClient_IsUpdateAvailable(t *testing.T) {
{
name: "update available",
candidate: &LatestDocument{
SchemaVersion: "1.0.0",
Status: StatusActive,
Status: StatusActive,
Archive: Archive{
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 27, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive.tar.gz",
Checksum: "checksum123",
Expand All @@ -209,7 +203,6 @@ func TestClient_IsUpdateAvailable(t *testing.T) {
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 27, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive.tar.gz",
Checksum: "checksum123",
Expand All @@ -218,13 +211,11 @@ func TestClient_IsUpdateAvailable(t *testing.T) {
{
name: "no update available",
candidate: &LatestDocument{
SchemaVersion: "1.0.0",
Status: "active",
Status: "active",
Archive: Archive{
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 26, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive.tar.gz",
Checksum: "checksum123",
Expand All @@ -240,13 +231,11 @@ func TestClient_IsUpdateAvailable(t *testing.T) {
{
name: "candidate deprecated",
candidate: &LatestDocument{
SchemaVersion: "1.0.0",
Status: StatusDeprecated,
Status: StatusDeprecated,
Archive: Archive{
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 27, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive.tar.gz",
Checksum: "checksum123",
Expand All @@ -256,7 +245,6 @@ func TestClient_IsUpdateAvailable(t *testing.T) {
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 27, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive.tar.gz",
Checksum: "checksum123",
Expand All @@ -266,13 +254,11 @@ func TestClient_IsUpdateAvailable(t *testing.T) {
{
name: "candidate end of life",
candidate: &LatestDocument{
SchemaVersion: "1.0.0",
Status: StatusEndOfLife,
Status: StatusEndOfLife,
Archive: Archive{
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 27, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive.tar.gz",
Checksum: "checksum123",
Expand All @@ -282,7 +268,6 @@ func TestClient_IsUpdateAvailable(t *testing.T) {
Description: db.Description{
SchemaVersion: "1.0.0",
Built: db.Time{Time: time.Date(2023, 9, 27, 12, 0, 0, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
},
Path: "path/to/archive.tar.gz",
Checksum: "checksum123",
Expand Down
18 changes: 13 additions & 5 deletions grype/db/v6/distribution/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package distribution
import (
"encoding/json"
"fmt"
"github.com/mholt/archiver/v3"
"io"
"os"
"path/filepath"
"sort"

"github.com/mholt/archiver/v3"

db "github.com/anchore/grype/grype/db/v6"
)

Expand All @@ -35,17 +36,24 @@ type Archive struct {
}

func NewLatestDocument(entries ...Archive) *LatestDocument {
if len(entries) == 0 {
var validEntries []Archive
for _, entry := range entries {
if modelPart, ok := entry.SchemaVersion.ModelPart(); ok && modelPart == db.ModelVersion {
validEntries = append(validEntries, entry)
}
}

if len(validEntries) == 0 {
return nil
}

// sort from most recent to the least recent
sort.SliceStable(entries, func(i, j int) bool {
return entries[i].Description.Built.After(entries[j].Description.Built.Time)
sort.SliceStable(validEntries, func(i, j int) bool {
return validEntries[i].Description.Built.After(entries[j].Description.Built.Time)
})

return &LatestDocument{
Archive: entries[0],
Archive: validEntries[0],
Status: LifecycleStatus,
}
}
Expand Down
Loading

0 comments on commit ba4a2b4

Please sign in to comment.