Files
aquasecurity-trivy/cache/cache_test.go

530 lines
13 KiB
Go
Raw Normal View History

package cache
import (
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
"fmt"
"io"
"io/ioutil"
"os"
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
"path/filepath"
"testing"
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
"time"
"github.com/stretchr/testify/assert"
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
"github.com/stretchr/testify/require"
bolt "go.etcd.io/bbolt"
"github.com/aquasecurity/fanal/types"
depTypes "github.com/aquasecurity/go-dep-parser/pkg/types"
)
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
func newTempDB(dbPath string) (string, error) {
dir, err := ioutil.TempDir("", "cache-test")
if err != nil {
return "", err
}
if dbPath != "" {
d := filepath.Join(dir, "fanal")
if err = os.MkdirAll(d, 0700); err != nil {
return "", err
}
dst := filepath.Join(d, "fanal.db")
if _, err = copyFile(dbPath, dst); err != nil {
return "", err
}
}
return dir, nil
}
func copyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
n, err := io.Copy(destination, source)
return n, err
}
func TestFSCache_GetLayer(t *testing.T) {
type args struct {
layerID string
}
tests := []struct {
name string
dbPath string
args args
want types.LayerInfo
}{
{
name: "happy path",
dbPath: "testdata/fanal.db",
args: args{
layerID: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
want: types.LayerInfo{
SchemaVersion: 2,
OS: &types.OS{
Family: "alpine",
Name: "3.10",
},
},
},
{
name: "happy path: different decompressed layer ID",
dbPath: "testdata/decompressed-layer-id.db",
args: args{
layerID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
},
want: types.LayerInfo{
SchemaVersion: 1,
OS: &types.OS{
Family: "alpine",
Name: "3.10",
},
PackageInfos: []types.PackageInfo{
{
FilePath: "lib/apk/db/installed",
Packages: []types.Package{
{
Name: "musl",
Version: "1.1.22-r3",
},
},
},
},
Applications: []types.Application{
{
Type: "composer",
FilePath: "php-app/composer.lock",
Add layer id info (merge to master) (fanal#88) * analyzer: Include layerID as part of LayerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * Add LayerID to Package struct Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove ID from returned layerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Handle missing layer.ID from cached layer Signed-off-by: Simarpreet Singh <simar@linux.com> * extractor/docker: Cleanup logic to avoid extra slice usage Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Fix golden files to include LayerID Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove condition for adding layer.ID Signed-off-by: Simarpreet Singh <simar@linux.com> * types: Introduce types.LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * docker: Add LayerID to each LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * .github/bench: Bump up docker version Signed-off-by: Simarpreet Singh <simar@linux.com> * intergration/perf: Remove other OSes for the timebeing. Looks like Github CI is running out of space while running other tests. Until we find a better solution we need to comment out bigger OSes. Signed-off-by: Simarpreet Singh <simar@linux.com> * fix(image): call Close() via cleanup funcion * refactor(type): add omitempty * analyzer: Change to types.LibraryInfo in analzyer.go Signed-off-by: Simarpreet Singh <simar@linux.com> * wip: add CleanupDockerExtractorFn for cleanup Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(analyzer): remove un-needed function * test(cache): comment in * Revert "wip: add CleanupDockerExtractorFn for cleanup" This reverts commit dabfae104bf6d63492823c6c3eb94175d26eabad. * Revert ".github/bench: Bump up docker version" This reverts commit b982c46861e1cc0851d53621c0e68ac40918d755. * refactor(analyzer): sort imports * test(cache): remove debug code * test(cache): format * chore(image): remove debug code Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
2020-03-04 08:25:19 -08:00
Libraries: []types.LibraryInfo{
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
{
Add layer id info (merge to master) (fanal#88) * analyzer: Include layerID as part of LayerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * Add LayerID to Package struct Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove ID from returned layerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Handle missing layer.ID from cached layer Signed-off-by: Simarpreet Singh <simar@linux.com> * extractor/docker: Cleanup logic to avoid extra slice usage Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Fix golden files to include LayerID Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove condition for adding layer.ID Signed-off-by: Simarpreet Singh <simar@linux.com> * types: Introduce types.LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * docker: Add LayerID to each LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * .github/bench: Bump up docker version Signed-off-by: Simarpreet Singh <simar@linux.com> * intergration/perf: Remove other OSes for the timebeing. Looks like Github CI is running out of space while running other tests. Until we find a better solution we need to comment out bigger OSes. Signed-off-by: Simarpreet Singh <simar@linux.com> * fix(image): call Close() via cleanup funcion * refactor(type): add omitempty * analyzer: Change to types.LibraryInfo in analzyer.go Signed-off-by: Simarpreet Singh <simar@linux.com> * wip: add CleanupDockerExtractorFn for cleanup Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(analyzer): remove un-needed function * test(cache): comment in * Revert "wip: add CleanupDockerExtractorFn for cleanup" This reverts commit dabfae104bf6d63492823c6c3eb94175d26eabad. * Revert ".github/bench: Bump up docker version" This reverts commit b982c46861e1cc0851d53621c0e68ac40918d755. * refactor(analyzer): sort imports * test(cache): remove debug code * test(cache): format * chore(image): remove debug code Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
2020-03-04 08:25:19 -08:00
Library: depTypes.Library{
Name: "guzzlehttp/guzzle",
Version: "6.2.0",
},
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
},
{
Add layer id info (merge to master) (fanal#88) * analyzer: Include layerID as part of LayerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * Add LayerID to Package struct Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove ID from returned layerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Handle missing layer.ID from cached layer Signed-off-by: Simarpreet Singh <simar@linux.com> * extractor/docker: Cleanup logic to avoid extra slice usage Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Fix golden files to include LayerID Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove condition for adding layer.ID Signed-off-by: Simarpreet Singh <simar@linux.com> * types: Introduce types.LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * docker: Add LayerID to each LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * .github/bench: Bump up docker version Signed-off-by: Simarpreet Singh <simar@linux.com> * intergration/perf: Remove other OSes for the timebeing. Looks like Github CI is running out of space while running other tests. Until we find a better solution we need to comment out bigger OSes. Signed-off-by: Simarpreet Singh <simar@linux.com> * fix(image): call Close() via cleanup funcion * refactor(type): add omitempty * analyzer: Change to types.LibraryInfo in analzyer.go Signed-off-by: Simarpreet Singh <simar@linux.com> * wip: add CleanupDockerExtractorFn for cleanup Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(analyzer): remove un-needed function * test(cache): comment in * Revert "wip: add CleanupDockerExtractorFn for cleanup" This reverts commit dabfae104bf6d63492823c6c3eb94175d26eabad. * Revert ".github/bench: Bump up docker version" This reverts commit b982c46861e1cc0851d53621c0e68ac40918d755. * refactor(analyzer): sort imports * test(cache): remove debug code * test(cache): format * chore(image): remove debug code Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
2020-03-04 08:25:19 -08:00
Library: depTypes.Library{
Name: "guzzlehttp/promises",
Version: "v1.3.1",
},
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
},
},
},
},
OpaqueDirs: []string{"php-app/"},
WhiteoutFiles: []string{"etc/foobar"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := newTempDB(tt.dbPath)
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
fs, err := NewFSCache(tmpDir)
require.NoError(t, err)
defer fs.Clear()
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
got, err := fs.GetLayer(tt.args.layerID)
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestFSCache_PutLayer(t *testing.T) {
type fields struct {
db *bolt.DB
directory string
}
type args struct {
layerID string
decompressedLayerID string
layerInfo types.LayerInfo
}
tests := []struct {
name string
fields fields
args args
want string
wantLayerID string
wantErr string
}{
{
name: "happy path",
args: args{
layerID: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
decompressedLayerID: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
layerInfo: types.LayerInfo{
SchemaVersion: 1,
OS: &types.OS{
Family: "alpine",
Name: "3.10",
},
},
},
want: `
{
"SchemaVersion": 1,
"OS": {
"Family": "alpine",
"Name": "3.10"
}
}`,
wantLayerID: "",
},
{
name: "happy path: different decompressed layer ID",
args: args{
layerID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
decompressedLayerID: "sha256:dab15cac9ebd43beceeeda3ce95c574d6714ed3d3969071caead678c065813ec",
layerInfo: types.LayerInfo{
SchemaVersion: 1,
OS: &types.OS{
Family: "alpine",
Name: "3.10",
},
PackageInfos: []types.PackageInfo{
{
FilePath: "lib/apk/db/installed",
Packages: []types.Package{
{
Name: "musl",
Version: "1.1.22-r3",
},
},
},
},
Applications: []types.Application{
{
Type: "composer",
FilePath: "php-app/composer.lock",
Add layer id info (merge to master) (fanal#88) * analyzer: Include layerID as part of LayerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * Add LayerID to Package struct Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove ID from returned layerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Handle missing layer.ID from cached layer Signed-off-by: Simarpreet Singh <simar@linux.com> * extractor/docker: Cleanup logic to avoid extra slice usage Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Fix golden files to include LayerID Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove condition for adding layer.ID Signed-off-by: Simarpreet Singh <simar@linux.com> * types: Introduce types.LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * docker: Add LayerID to each LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * .github/bench: Bump up docker version Signed-off-by: Simarpreet Singh <simar@linux.com> * intergration/perf: Remove other OSes for the timebeing. Looks like Github CI is running out of space while running other tests. Until we find a better solution we need to comment out bigger OSes. Signed-off-by: Simarpreet Singh <simar@linux.com> * fix(image): call Close() via cleanup funcion * refactor(type): add omitempty * analyzer: Change to types.LibraryInfo in analzyer.go Signed-off-by: Simarpreet Singh <simar@linux.com> * wip: add CleanupDockerExtractorFn for cleanup Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(analyzer): remove un-needed function * test(cache): comment in * Revert "wip: add CleanupDockerExtractorFn for cleanup" This reverts commit dabfae104bf6d63492823c6c3eb94175d26eabad. * Revert ".github/bench: Bump up docker version" This reverts commit b982c46861e1cc0851d53621c0e68ac40918d755. * refactor(analyzer): sort imports * test(cache): remove debug code * test(cache): format * chore(image): remove debug code Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
2020-03-04 08:25:19 -08:00
Libraries: []types.LibraryInfo{
{
Library: depTypes.Library{
Name: "guzzlehttp/guzzle",
Version: "6.2.0",
},
},
{
Library: depTypes.Library{
Name: "guzzlehttp/promises",
Version: "v1.3.1",
},
},
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
},
},
},
OpaqueDirs: []string{"php-app/"},
WhiteoutFiles: []string{"etc/foobar"},
},
},
want: `
{
"SchemaVersion": 1,
"OS": {
"Family": "alpine",
"Name": "3.10"
},
"PackageInfos": [
{
"FilePath": "lib/apk/db/installed",
"Packages": [
{
"Name": "musl",
"Version": "1.1.22-r3"
}
]
}
],
"Applications": [
{
"Type": "composer",
"FilePath": "php-app/composer.lock",
"Libraries": [
Add layer id info (merge to master) (fanal#88) * analyzer: Include layerID as part of LayerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * Add LayerID to Package struct Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove ID from returned layerInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Handle missing layer.ID from cached layer Signed-off-by: Simarpreet Singh <simar@linux.com> * extractor/docker: Cleanup logic to avoid extra slice usage Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Fix golden files to include LayerID Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer: Remove condition for adding layer.ID Signed-off-by: Simarpreet Singh <simar@linux.com> * types: Introduce types.LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * docker: Add LayerID to each LibraryInfo Signed-off-by: Simarpreet Singh <simar@linux.com> * .github/bench: Bump up docker version Signed-off-by: Simarpreet Singh <simar@linux.com> * intergration/perf: Remove other OSes for the timebeing. Looks like Github CI is running out of space while running other tests. Until we find a better solution we need to comment out bigger OSes. Signed-off-by: Simarpreet Singh <simar@linux.com> * fix(image): call Close() via cleanup funcion * refactor(type): add omitempty * analyzer: Change to types.LibraryInfo in analzyer.go Signed-off-by: Simarpreet Singh <simar@linux.com> * wip: add CleanupDockerExtractorFn for cleanup Signed-off-by: Simarpreet Singh <simar@linux.com> * refactor(analyzer): remove un-needed function * test(cache): comment in * Revert "wip: add CleanupDockerExtractorFn for cleanup" This reverts commit dabfae104bf6d63492823c6c3eb94175d26eabad. * Revert ".github/bench: Bump up docker version" This reverts commit b982c46861e1cc0851d53621c0e68ac40918d755. * refactor(analyzer): sort imports * test(cache): remove debug code * test(cache): format * chore(image): remove debug code Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
2020-03-04 08:25:19 -08:00
{
"Library":{
"Name":"guzzlehttp/guzzle",
"Version":"6.2.0"
}
},
{
"Library":{
"Name":"guzzlehttp/promises",
"Version":"v1.3.1"
}
}
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
]
}
],
"OpaqueDirs": [
"php-app/"
],
"WhiteoutFiles": [
"etc/foobar"
]
}`,
wantLayerID: "sha256:dab15cac9ebd43beceeeda3ce95c574d6714ed3d3969071caead678c065813ec",
},
{
name: "sad path no layerID",
args: args{
decompressedLayerID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
},
wantErr: "key required",
},
{
name: "sad path no decompressedLayerID",
args: args{
layerID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
},
wantErr: "key required",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := newTempDB("")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
fs, err := NewFSCache(tmpDir)
require.NoError(t, err)
defer fs.Clear()
err = fs.PutLayer(tt.args.layerID, tt.args.decompressedLayerID, tt.args.layerInfo)
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Contains(t, err.Error(), tt.wantErr, tt.name)
return
} else {
require.NoError(t, err, tt.name)
}
fs.db.View(func(tx *bolt.Tx) error {
// check decompressedDigestBucket
decompressedBucket := tx.Bucket([]byte(decompressedDigestBucket))
res := decompressedBucket.Get([]byte(tt.args.layerID))
assert.Equal(t, tt.wantLayerID, string(res))
layerBucket := tx.Bucket([]byte(layerBucket))
b := layerBucket.Get([]byte(tt.args.decompressedLayerID))
assert.JSONEq(t, tt.want, string(b))
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
return nil
})
})
}
}
func TestFSCache_PutImage(t *testing.T) {
type args struct {
imageID string
imageConfig types.ImageInfo
}
tests := []struct {
name string
args args
want string
wantErr string
}{
{
name: "happy path",
args: args{
imageID: "sha256:58701fd185bda36cab0557bb6438661831267aa4a9e0b54211c4d5317a48aff4",
imageConfig: types.ImageInfo{
SchemaVersion: 1,
Architecture: "amd64",
Created: time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC),
DockerVersion: "18.06.1-ce",
OS: "linux",
HistoryPackages: []types.Package{
{
Name: "musl",
Version: "1.2.3",
},
},
},
},
want: `
{
"SchemaVersion": 1,
"Architecture": "amd64",
"Created": "2020-01-02T03:04:05Z",
"DockerVersion": "18.06.1-ce",
"OS": "linux",
"HistoryPackages": [
{
"Name": "musl",
"Version": "1.2.3"
}
]
}
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := newTempDB("")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
fs, err := NewFSCache(tmpDir)
require.NoError(t, err)
//defer fs.Clear()
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
err = fs.PutImage(tt.args.imageID, tt.args.imageConfig)
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Contains(t, err.Error(), tt.wantErr, tt.name)
return
} else {
require.NoError(t, err, tt.name)
}
fs.db.View(func(tx *bolt.Tx) error {
// check decompressedDigestBucket
imageBucket := tx.Bucket([]byte(imageBucket))
b := imageBucket.Get([]byte(tt.args.imageID))
assert.JSONEq(t, tt.want, string(b))
return nil
})
})
}
}
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
func TestFSCache_MissingLayers(t *testing.T) {
type args struct {
imageID string
layerIDs []string
}
tests := []struct {
name string
dbPath string
args args
wantMissingImage bool
wantMissingLayerIDs []string
wantErr string
}{
{
name: "happy path",
dbPath: "testdata/fanal.db",
args: args{
imageID: "sha256:58701fd185bda36cab0557bb6438661831267aa4a9e0b54211c4d5317a48aff4",
layerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
"sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
"sha256:dab15cac9ebd43beceeeda3ce95c574d6714ed3d3969071caead678c065813ec",
},
},
wantMissingImage: false,
wantMissingLayerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
"sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
"sha256:dab15cac9ebd43beceeeda3ce95c574d6714ed3d3969071caead678c065813ec",
},
},
{
name: "happy path: different decompressed layer ID",
dbPath: "testdata/decompressed-layer-id.db",
args: args{
imageID: "sha256:58701fd185bda36cab0557bb6438661831267aa4a9e0b54211c4d5317a48aff4",
layerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
"sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5",
"sha256:dab15cac9ebd43beceeeda3ce95c574d6714ed3d3969071caead678c065813ec",
},
},
wantMissingImage: true,
wantMissingLayerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
},
{
name: "happy path: broken layer JSON",
dbPath: "testdata/broken-layer.db",
args: args{
imageID: "sha256:58701fd185bda36cab0557bb6438661831267aa4a9e0b54211c4d5317a48aff4",
layerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
},
wantMissingImage: true,
wantMissingLayerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
},
{
name: "happy path: broken image JSON",
dbPath: "testdata/broken-image.db",
args: args{
imageID: "sha256:58701fd185bda36cab0557bb6438661831267aa4a9e0b54211c4d5317a48aff4",
layerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
},
wantMissingImage: true,
wantMissingLayerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
},
{
name: "happy path: the schema version of image JSON doesn't match",
dbPath: "testdata/different-image-schema.db",
args: args{
imageID: "sha256:58701fd185bda36cab0557bb6438661831267aa4a9e0b54211c4d5317a48aff4",
layerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
},
wantMissingImage: true,
wantMissingLayerIDs: []string{
"sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := newTempDB(tt.dbPath)
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
fs, err := NewFSCache(tmpDir)
require.NoError(t, err)
defer fs.Clear()
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
gotMissingImage, gotMissingLayerIDs, err := fs.MissingLayers(tt.args.imageID, tt.args.layerIDs)
if tt.wantErr != "" {
require.NotNil(t, err, tt.name)
assert.Contains(t, err.Error(), tt.wantErr, tt.name)
return
} else {
require.NoError(t, err, tt.name)
}
feat(cache): based on JSON (fanal#84) * temp * update * fix integration test * update * update * test(docker): add a test for ExtractLayerFiles * docker_test: Add opq and wh file paths. Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add sad path for GetLayer Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add invalid file for extractFiles Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: remove old crufty tests Signed-off-by: Simarpreet Singh <simar@linux.com> * docker_test: Add tests for ApplyLayers Signed-off-by: Simarpreet Singh <simar@linux.com> * test(docker): add tests for status.d * test(docker): no mock * temp * analyze_test: Add tests for Analzye() happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * cache: remove mock_cache.go Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Prefer real extractor over mock Signed-off-by: Simarpreet Singh <simar@linux.com> * analyzer_test: Add sad paths for Analyze Signed-off-by: Simarpreet Singh <simar@linux.com> * tests: Fix invocation call sites for new JSON cache Signed-off-by: Simarpreet Singh <simar@linux.com> * test(image): add httptest server * utils_test: Add build tags Signed-off-by: Simarpreet Singh <simar@linux.com> * test(analyzer): add sad path * test(image): remove unused tests * test(integration): fix interface * test(analyzer): add an image including lock files * refactor(analyzer): remove the unused function * test(analyzer): add tests for Applier * test(cache): add cache tests * feat(cache): support schema version * refactor(mock): remove unnecessary mocks * refactor: clean up debug code * test(analyzer): add a test image * test(bench): fix cache * cache_test: Add a layer with SchemaVersion of 2 Signed-off-by: Simarpreet Singh <simar@linux.com> * library_test: remove cruft Signed-off-by: Simarpreet Singh <simar@linux.com> * feat(image): support docker host and cert path * refactor(image): add omitempty * feat(image): add new struct to have image information in cache * feat(cache): add image bucket * refactor(cache): commonize getting layer * test(mock): generate new mocks * feat(analyzer): analyze image config and store it in cache * refactor(analyzer): make errors lowercase * test(cache): update tests * test(analyzer): fix tests * fix(cmd): pass image ID * fix(analyzer): add HistoryPackages to store package from history * fix(cache): return an error in GetLayer * refactor(cache): rename ImageConfig to ImageInfo * refactor(err): wrap errors Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-02-27 21:09:05 +02:00
assert.Equal(t, tt.wantMissingImage, gotMissingImage, tt.name)
assert.Equal(t, tt.wantMissingLayerIDs, gotMissingLayerIDs, tt.name)
})
}
}