-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package v6 | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/anchore/grype/internal/log" | ||
) | ||
|
||
type ProviderStoreWriter interface { | ||
AddProvider(p *Provider) error | ||
} | ||
|
||
type ProviderStoreReader interface { | ||
GetProvider(name string) (*Provider, error) | ||
} | ||
|
||
type providerStore struct { | ||
db *gorm.DB | ||
} | ||
|
||
func newProviderStore(db *gorm.DB) *providerStore { | ||
return &providerStore{ | ||
db: db, | ||
} | ||
} | ||
|
||
func (s *providerStore) AddProvider(p *Provider) error { | ||
log.WithFields("name", p.ID).Trace("writing provider record") | ||
|
||
var existingProvider Provider | ||
result := s.db.Where("id = ? AND version = ?", p.ID, p.Version).First(&existingProvider) | ||
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { | ||
return fmt.Errorf("failed to find provider (name=%q version=%q): %w", p.ID, p.Version, result.Error) | ||
} | ||
|
||
if result.Error == nil { | ||
// overwrite the existing provider if found | ||
existingProvider.Processor = p.Processor | ||
existingProvider.DateCaptured = p.DateCaptured | ||
existingProvider.InputDigest = p.InputDigest | ||
} else { | ||
// create a new provider record if not found | ||
existingProvider = *p | ||
} | ||
|
||
if err := s.db.Save(&existingProvider).Error; err != nil { | ||
return fmt.Errorf("failed to save provider (name=%q version=%q): %w", p.ID, p.Version, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *providerStore) GetProvider(name string) (*Provider, error) { | ||
log.WithFields("name", name).Trace("fetching provider record") | ||
|
||
var provider Provider | ||
result := s.db.Where("id = ?", name).First(&provider) | ||
if result.Error != nil { | ||
return nil, fmt.Errorf("failed to fetch provider (name=%q): %w", name, result.Error) | ||
} | ||
|
||
return &provider, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package v6 | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProviderStore(t *testing.T) { | ||
now := time.Date(2021, 1, 1, 2, 3, 4, 5, time.UTC) | ||
other := time.Date(2022, 2, 3, 4, 5, 6, 7, time.UTC) | ||
tests := []struct { | ||
name string | ||
providers []Provider | ||
wantErr require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "add new provider", | ||
providers: []Provider{ | ||
{ | ||
ID: "ubuntu", | ||
Version: "1.0", | ||
Processor: "vunnel", | ||
DateCaptured: &now, | ||
InputDigest: "sha256:abcd1234", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "add existing provider", | ||
providers: []Provider{ | ||
{ // original | ||
ID: "ubuntu", | ||
Version: "1.0", | ||
Processor: "vunnel", | ||
DateCaptured: &now, | ||
InputDigest: "sha256:abcd1234", | ||
}, | ||
{ // overwrite... | ||
ID: "ubuntu", | ||
Version: "2.0", | ||
Processor: "something-else", | ||
DateCaptured: &other, | ||
InputDigest: "sha256:cdef5678", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := newProviderStore(setupTestDB(t)) | ||
if tt.wantErr == nil { | ||
tt.wantErr = require.NoError | ||
} | ||
for i, p := range tt.providers { | ||
isLast := i == len(tt.providers)-1 | ||
err := s.AddProvider(&p) | ||
if !isLast { | ||
require.NoError(t, err) | ||
continue | ||
} | ||
|
||
tt.wantErr(t, err) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
provider, err := s.GetProvider(p.ID) | ||
tt.wantErr(t, err) | ||
if err != nil { | ||
assert.Nil(t, provider) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, provider) | ||
if d := cmp.Diff(p, *provider); d != "" { | ||
t.Errorf("unexpected provider (-want +got): %s", d) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestProviderStore_GetProvider(t *testing.T) { | ||
s := newProviderStore(setupTestDB(t)) | ||
p, err := s.GetProvider("fake") | ||
require.Error(t, err) | ||
assert.Nil(t, p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters