fix tests

This commit is contained in:
Alex Goodman 2019-10-07 18:25:35 -04:00
parent 4714aca123
commit 2640bdced8
No known key found for this signature in database
GPG key ID: 98AF011C5C78EB7E
5 changed files with 85 additions and 19 deletions

View file

@ -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)
}
}
}

View file

@ -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
}

View file

@ -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}},

View file

@ -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 {

View file

@ -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)
}