mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-22 07:10:41 -08:00
Skip downloading DB if a remote DB is not updated (#717)
* Skip downloading DB if a remote DB is not updated * Apply suggestions from code review Co-authored-by: Teppei Fukuda <knqyf263@gmail.com> * update github.com/aquasecurity/trivy-db version * fix lint * Use UTC datetime * display DownloadedAt info in debug log * refactor(db): merge isLatestDB into isNewDB Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
This commit is contained in:
@@ -133,6 +133,30 @@ func TestClient_NeedsUpdate(t *testing.T) {
|
||||
skip: true,
|
||||
expectedError: xerrors.New("--skip-update cannot be specified with the old DB"),
|
||||
},
|
||||
{
|
||||
name: "happy with old DownloadedAt",
|
||||
light: false,
|
||||
clock: clocktesting.NewFakeClock(time.Date(2019, 10, 1, 0, 0, 0, 0, time.UTC)),
|
||||
metadata: db.Metadata{
|
||||
Version: 1,
|
||||
Type: db.TypeFull,
|
||||
NextUpdate: timeNextUpdateDay1,
|
||||
DownloadedAt: time.Date(2019, 9, 30, 22, 30, 0, 0, time.UTC),
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "skip downloading DB with recent DownloadedAt",
|
||||
light: false,
|
||||
clock: clocktesting.NewFakeClock(time.Date(2019, 10, 1, 0, 0, 0, 0, time.UTC)),
|
||||
metadata: db.Metadata{
|
||||
Version: 1,
|
||||
Type: db.TypeFull,
|
||||
NextUpdate: timeNextUpdateDay1,
|
||||
DownloadedAt: time.Date(2019, 9, 30, 23, 30, 0, 0, time.UTC),
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if err := log.InitLogger(false, true); err != nil {
|
||||
@@ -252,14 +276,17 @@ func TestClient_Download(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClient_UpdateMetadata(t *testing.T) {
|
||||
timeDownloadedAt := clocktesting.NewFakeClock(time.Date(2019, 10, 1, 0, 0, 0, 0, time.UTC))
|
||||
testCases := []struct {
|
||||
name string
|
||||
clock clock.Clock
|
||||
getMetadataExpectation dbOperationGetMetadataExpectation
|
||||
storeMetadataExpectation dbOperationStoreMetadataExpectation
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
name: "happy path",
|
||||
name: "happy path",
|
||||
clock: timeDownloadedAt,
|
||||
getMetadataExpectation: dbOperationGetMetadataExpectation{
|
||||
Returns: dbOperationGetMetadataReturns{
|
||||
Metadata: db.Metadata{
|
||||
@@ -273,15 +300,17 @@ func TestClient_UpdateMetadata(t *testing.T) {
|
||||
},
|
||||
storeMetadataExpectation: dbOperationStoreMetadataExpectation{
|
||||
Metadata: db.Metadata{
|
||||
Version: 1,
|
||||
Type: 1,
|
||||
NextUpdate: time.Date(2020, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
UpdatedAt: time.Date(2006, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
Version: 1,
|
||||
Type: 1,
|
||||
NextUpdate: time.Date(2020, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
UpdatedAt: time.Date(2006, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
DownloadedAt: timeDownloadedAt.Now(),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sad path, get metadata fails",
|
||||
name: "sad path, get metadata fails",
|
||||
clock: timeDownloadedAt,
|
||||
getMetadataExpectation: dbOperationGetMetadataExpectation{
|
||||
Returns: dbOperationGetMetadataReturns{
|
||||
Err: errors.New("get metadata failed"),
|
||||
@@ -290,7 +319,8 @@ func TestClient_UpdateMetadata(t *testing.T) {
|
||||
expectedError: errors.New("unable to get metadata: get metadata failed"),
|
||||
},
|
||||
{
|
||||
name: "sad path, store metadata fails",
|
||||
name: "sad path, store metadata fails",
|
||||
clock: timeDownloadedAt,
|
||||
getMetadataExpectation: dbOperationGetMetadataExpectation{
|
||||
Returns: dbOperationGetMetadataReturns{
|
||||
Metadata: db.Metadata{
|
||||
@@ -304,10 +334,11 @@ func TestClient_UpdateMetadata(t *testing.T) {
|
||||
},
|
||||
storeMetadataExpectation: dbOperationStoreMetadataExpectation{
|
||||
Metadata: db.Metadata{
|
||||
Version: 1,
|
||||
Type: 1,
|
||||
NextUpdate: time.Date(2020, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
UpdatedAt: time.Date(2006, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
Version: 1,
|
||||
Type: 1,
|
||||
NextUpdate: time.Date(2020, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
UpdatedAt: time.Date(2006, 4, 30, 23, 59, 59, 0, time.UTC),
|
||||
DownloadedAt: timeDownloadedAt.Now(),
|
||||
},
|
||||
Returns: dbOperationStoreMetadataReturns{
|
||||
Err: errors.New("store metadata failed"),
|
||||
@@ -334,7 +365,7 @@ func TestClient_UpdateMetadata(t *testing.T) {
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
pb := indicator.NewProgressBar(true)
|
||||
client := NewClient(mockConfig, nil, pb, nil, metadata)
|
||||
client := NewClient(mockConfig, nil, pb, tc.clock, metadata)
|
||||
|
||||
err = client.UpdateMetadata(dir)
|
||||
switch {
|
||||
|
||||
Reference in New Issue
Block a user