From 6fd39e0f42709b912fc9c7512f5c4df3b03ed809 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Thu, 26 Sep 2024 16:30:08 -0400 Subject: [PATCH] set instance should be automatic Signed-off-by: Alex Goodman --- grype/db/v6/db_metadata_store.go | 15 ++++++-- grype/db/v6/db_metadata_store_test.go | 49 ++++++++++++++------------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/grype/db/v6/db_metadata_store.go b/grype/db/v6/db_metadata_store.go index b0288ca076b..9c78f2501de 100644 --- a/grype/db/v6/db_metadata_store.go +++ b/grype/db/v6/db_metadata_store.go @@ -2,12 +2,13 @@ package v6 import ( "fmt" + "time" "github.com/anchore/grype/internal/log" ) type DBMetadataStoreWriter interface { - SetDBMetadata(instance DBMetadata) error + SetDBMetadata() error } type DBMetadataStoreReader interface { @@ -33,11 +34,21 @@ func (s *dbMetadataStore) GetDBMetadata() (*DBMetadata, error) { return &model, result.Error } -func (s *dbMetadataStore) SetDBMetadata(instance DBMetadata) error { +func (s *dbMetadataStore) SetDBMetadata() error { + log.Trace("writing DB metadata record") + if err := s.db.Unscoped().Where("true").Delete(&DBMetadata{}).Error; err != nil { return fmt.Errorf("failed to delete existing DB metadata record: %w", err) } + ts := time.Now().UTC() + instance := &DBMetadata{ + BuildTimestamp: &ts, + Model: ModelVersion, + Revision: Revision, + Addition: Addition, + } + if err := s.db.Create(instance).Error; err != nil { return fmt.Errorf("failed to create DB metadata record: %w", err) } diff --git a/grype/db/v6/db_metadata_store_test.go b/grype/db/v6/db_metadata_store_test.go index 10b55d18b4d..97389a7ecc9 100644 --- a/grype/db/v6/db_metadata_store_test.go +++ b/grype/db/v6/db_metadata_store_test.go @@ -2,43 +2,46 @@ package v6 import ( "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gorm.io/gorm" ) -func TestDbMetadataStore(t *testing.T) { +func TestDbMetadataStore_empty(t *testing.T) { conn := setupTestDB(t) s := newDBMetadataStore(*conn) - // set one record - ts := time.Date(2021, 11, 22, 33, 44, 55, 66, time.UTC) - expectedMetadata := DBMetadata{ - BuildTimestamp: &ts, - Model: 66, - Revision: 77, - Addition: 88, - } - err := s.SetDBMetadata(expectedMetadata) - require.NoError(t, err) + // attempt to fetch a non-existent record + actualMetadata, err := s.GetDBMetadata() + require.ErrorIs(t, err, gorm.ErrRecordNotFound) + require.NotNil(t, actualMetadata) +} - // overwrite with a new record - ts = time.Date(2022, 2, 3, 4, 5, 6, 7, time.UTC) - expectedMetadata = DBMetadata{ - BuildTimestamp: &ts, - Model: 6, - Revision: 9, - Addition: 10, - } - err = s.SetDBMetadata(expectedMetadata) - require.NoError(t, err) +func TestDbMetadataStore(t *testing.T) { + conn := setupTestDB(t) + s := newDBMetadataStore(*conn) + + require.NoError(t, s.SetDBMetadata()) // fetch the record actualMetadata, err := s.GetDBMetadata() require.NoError(t, err) require.NotNil(t, actualMetadata) - assert.Equal(t, expectedMetadata, *actualMetadata) + + assert.NotZero(t, *actualMetadata.BuildTimestamp) // a timestamp was set + name, _ := actualMetadata.BuildTimestamp.Zone() + assert.Equal(t, "UTC", name) // the timestamp is in UTC + + actualMetadata.BuildTimestamp = nil // value not under test + + assert.Equal(t, DBMetadata{ + BuildTimestamp: nil, + // expect the correct version info + Model: ModelVersion, + Revision: Revision, + Addition: Addition, + }, *actualMetadata) } func setupTestDB(t *testing.T) *connection {