From 2640bdced894da864295cd4abf2bd3dd70011fb9 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Mon, 7 Oct 2019 18:25:35 -0400 Subject: [PATCH] fix tests --- .../docker/image_archive_analysis_test.go | 63 +++++++++++++++++++ dive/image/docker/testing.go | 19 ++++-- runtime/ci/evaluator_test.go | 7 +-- runtime/export/export_test.go | 5 +- runtime/ui/filetree_viewmodel_test.go | 10 +-- 5 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 dive/image/docker/image_archive_analysis_test.go diff --git a/dive/image/docker/image_archive_analysis_test.go b/dive/image/docker/image_archive_analysis_test.go new file mode 100644 index 0000000..65a8daf --- /dev/null +++ b/dive/image/docker/image_archive_analysis_test.go @@ -0,0 +1,63 @@ +package docker + +import ( + "github.com/wagoodman/dive/dive/image" + "testing" +) + +func analysisFromImageTar(t *testing.T, path string) *image.AnalysisResult { + archive, err := TestLoadArchive(path) + if err != nil { + t.Fatalf("unable to fetch archive: %v", err) + } + + img, err := archive.ToImage() + if err != nil { + t.Fatalf("unable to convert to image: %v", err) + } + + result, err := img.Analyze() + if err != nil { + t.Fatalf("unable to analyze: %v", err) + } + return result +} + +func Test_Analysis(t *testing.T) { + + table := map[string]struct { + efficiency float64 + sizeBytes uint64 + userSizeBytes uint64 + wastedBytes uint64 + wastedPercent float64 + path string + }{ + "docker-image": {0.9844212134184309, 1220598, 66237, 32025, 0.4834911001404049, "../../../.data/test-docker-image.tar"}, + } + + for name, test := range table { + result := analysisFromImageTar(t, test.path) + + if result.SizeBytes != test.sizeBytes { + t.Errorf("%s.%s: expected sizeBytes=%v, got %v", t.Name(), name, test.sizeBytes, result.SizeBytes) + } + + if result.UserSizeByes != test.userSizeBytes { + t.Errorf("%s.%s: expected userSizeBytes=%v, got %v", t.Name(), name, test.userSizeBytes, result.UserSizeByes) + } + + if result.WastedBytes != test.wastedBytes { + t.Errorf("%s.%s: expected wasterBytes=%v, got %v", t.Name(), name, test.wastedBytes, result.WastedBytes) + } + + if result.WastedUserPercent != test.wastedPercent { + t.Errorf("%s.%s: expected wastedPercent=%v, got %v", t.Name(), name, test.wastedPercent, result.WastedUserPercent) + } + + if result.Efficiency != test.efficiency { + t.Errorf("%s.%s: expected efficiency=%v, got %v", t.Name(), name, test.efficiency, result.Efficiency) + } + } + +} diff --git a/dive/image/docker/testing.go b/dive/image/docker/testing.go index f6d1777..ac70d7c 100644 --- a/dive/image/docker/testing.go +++ b/dive/image/docker/testing.go @@ -3,24 +3,33 @@ package docker import ( "github.com/wagoodman/dive/dive/image" "os" + "testing" ) -func TestLoadDockerImageTar(tarPath string) (*image.AnalysisResult, error) { +func TestLoadArchive(tarPath string) (*ImageArchive, error) { f, err := os.Open(tarPath) if err != nil { return nil, err } defer f.Close() - archive, err := NewImageArchive(f) + return NewImageArchive(f) +} + +func TestAnalysisFromArchive(t *testing.T, path string) *image.AnalysisResult { + archive, err := TestLoadArchive(path) if err != nil { - return nil, err + t.Fatalf("unable to fetch archive: %v", err) } img, err := archive.ToImage() if err != nil { - return nil, err + t.Fatalf("unable to convert to image: %v", err) } - return img.Analyze() + result, err := img.Analyze() + if err != nil { + t.Fatalf("unable to analyze: %v", err) + } + return result } diff --git a/runtime/ci/evaluator_test.go b/runtime/ci/evaluator_test.go index acd6b9b..d929452 100644 --- a/runtime/ci/evaluator_test.go +++ b/runtime/ci/evaluator_test.go @@ -10,10 +10,7 @@ import ( func Test_Evaluator(t *testing.T) { - result, err := docker.TestLoadDockerImageTar("../../.data/test-docker-image.tar") - if err != nil { - t.Fatalf("Test_Export: unable to fetch analysis: %v", err) - } + result := docker.TestAnalysisFromArchive(t, "../../.data/test-docker-image.tar") table := map[string]struct { efficiency string @@ -23,7 +20,7 @@ func Test_Evaluator(t *testing.T) { expectedResult map[string]RuleStatus }{ "allFail": {"0.99", "1B", "0.01", false, map[string]RuleStatus{"lowestEfficiency": RuleFailed, "highestWastedBytes": RuleFailed, "highestUserWastedPercent": RuleFailed}}, - "allPass": {"0.9", "50kB", "0.1", true, map[string]RuleStatus{"lowestEfficiency": RulePassed, "highestWastedBytes": RulePassed, "highestUserWastedPercent": RulePassed}}, + "allPass": {"0.9", "50kB", "0.5", true, map[string]RuleStatus{"lowestEfficiency": RulePassed, "highestWastedBytes": RulePassed, "highestUserWastedPercent": RulePassed}}, "allDisabled": {"disabled", "disabled", "disabled", true, map[string]RuleStatus{"lowestEfficiency": RuleDisabled, "highestWastedBytes": RuleDisabled, "highestUserWastedPercent": RuleDisabled}}, "misconfiguredHigh": {"1.1", "1BB", "10", false, map[string]RuleStatus{"lowestEfficiency": RuleMisconfigured, "highestWastedBytes": RuleMisconfigured, "highestUserWastedPercent": RuleMisconfigured}}, "misconfiguredLow": {"-9", "-1BB", "-0.1", false, map[string]RuleStatus{"lowestEfficiency": RuleMisconfigured, "highestWastedBytes": RuleMisconfigured, "highestUserWastedPercent": RuleMisconfigured}}, diff --git a/runtime/export/export_test.go b/runtime/export/export_test.go index 78cd2bc..f3028a5 100644 --- a/runtime/export/export_test.go +++ b/runtime/export/export_test.go @@ -7,11 +7,8 @@ import ( ) func Test_Export(t *testing.T) { + result := docker.TestAnalysisFromArchive(t, "../../.data/test-docker-image.tar") - result, err := docker.TestLoadDockerImageTar("../../.data/test-docker-image.tar") - if err != nil { - t.Fatalf("Test_Export: unable to fetch analysis: %v", err) - } export := NewExport(result) payload, err := export.marshal() if err != nil { diff --git a/runtime/ui/filetree_viewmodel_test.go b/runtime/ui/filetree_viewmodel_test.go index af2bbb0..a2228f7 100644 --- a/runtime/ui/filetree_viewmodel_test.go +++ b/runtime/ui/filetree_viewmodel_test.go @@ -72,13 +72,13 @@ func assertTestData(t *testing.T, actualBytes []byte) { helperCheckDiff(t, expectedBytes, actualBytes) } + + func initializeTestViewModel(t *testing.T) *FileTreeViewModel { - result, err := docker.TestLoadDockerImageTar("../../.data/test-docker-image.tar") - if err != nil { - t.Fatalf("%s: unable to fetch analysis: %v", t.Name(), err) - } + result := docker.TestAnalysisFromArchive(t, "../../.data/test-docker-image.tar") + cache := filetree.NewFileTreeCache(result.RefTrees) - err = cache.Build() + err := cache.Build() if err != nil { t.Fatalf("%s: unable to build cache: %+v", t.Name(), err) }