Skip to content

Commit

Permalink
feat(cache): add close function (aquasecurity#104)
Browse files Browse the repository at this point in the history
* feat(cache): add close function

* test(mock): update mock

* test(analyzer): fix tests according to updated mock
  • Loading branch information
knqyf263 authored May 5, 2020
1 parent c352884 commit 9239a36
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
20 changes: 10 additions & 10 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
getLayerExpectations: []cache.LocalImageCacheGetLayerExpectation{
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
},
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
DiffID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
},
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
DiffID: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand Down Expand Up @@ -447,7 +447,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
getLayerExpectations: []cache.LocalImageCacheGetLayerExpectation{
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:531743b7098cb2aaf615641007a129173f63ed86ca32fe7b5a246a1c47286028",
DiffID: "sha256:531743b7098cb2aaf615641007a129173f63ed86ca32fe7b5a246a1c47286028",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
getLayerExpectations: []cache.LocalImageCacheGetLayerExpectation{
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
},
Returns: cache.LocalImageCacheGetLayerReturns{LayerInfo: types.LayerInfo{}},
},
Expand All @@ -582,7 +582,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
getLayerExpectations: []cache.LocalImageCacheGetLayerExpectation{
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
},
Returns: cache.LocalImageCacheGetLayerReturns{LayerInfo: types.LayerInfo{}},
},
Expand All @@ -602,7 +602,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
getLayerExpectations: []cache.LocalImageCacheGetLayerExpectation{
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand All @@ -627,7 +627,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
},
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
DiffID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand Down Expand Up @@ -655,7 +655,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
},
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
DiffID: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand Down Expand Up @@ -743,7 +743,7 @@ func TestApplier_ApplyLayers(t *testing.T) {
getLayerExpectations: []cache.LocalImageCacheGetLayerExpectation{
{
Args: cache.LocalImageCacheGetLayerArgs{
LayerID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
},
Returns: cache.LocalImageCacheGetLayerReturns{
LayerInfo: types.LayerInfo{
Expand Down
10 changes: 9 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ImageCache interface {
type LocalImageCache interface {
GetImage(imageID string) (imageInfo types.ImageInfo, err error)
GetLayer(diffID string) (layerInfo types.LayerInfo, err error)
Close() (err error)
Clear() (err error)
}

Expand Down Expand Up @@ -196,10 +197,17 @@ func (fs FSCache) MissingLayers(imageID string, layerIDs []string) (bool, []stri
return missingImage, missingLayerIDs, nil
}

func (fs FSCache) Clear() error {
func (fs FSCache) Close() error {
if err := fs.db.Close(); err != nil {
return xerrors.Errorf("unable to close DB: %w", err)
}
return nil
}

func (fs FSCache) Clear() error {
if err := fs.Close(); err != nil {
return err
}
if err := os.RemoveAll(fs.directory); err != nil {
return xerrors.Errorf("failed to remove cache: %w", err)
}
Expand Down
57 changes: 46 additions & 11 deletions cache/mock_local_image_cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9239a36

Please sign in to comment.