fix tests and add a few new ones

This commit is contained in:
Megan Marsh 2019-04-03 11:32:49 -07:00
parent 365b32eb9c
commit 056fcb7cea
9 changed files with 101 additions and 24 deletions

View File

@ -5,6 +5,10 @@ import (
"testing" "testing"
) )
func boolPointer(tf bool) *bool {
return &tf
}
func testBuild() *coreBuild { func testBuild() *coreBuild {
return &coreBuild{ return &coreBuild{
name: "test", name: "test",
@ -19,7 +23,7 @@ func testBuild() *coreBuild {
}, },
postProcessors: [][]coreBuildPostProcessor{ postProcessors: [][]coreBuildPostProcessor{
{ {
{&MockPostProcessor{ArtifactId: "pp"}, "testPP", make(map[string]interface{}), true}, {&MockPostProcessor{ArtifactId: "pp"}, "testPP", make(map[string]interface{}), boolPointer(true)},
}, },
}, },
variables: make(map[string]string), variables: make(map[string]string),
@ -245,7 +249,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build = testBuild() build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{ build.postProcessors = [][]coreBuildPostProcessor{
{ {
{&MockPostProcessor{ArtifactId: "pp"}, "pp", make(map[string]interface{}), false}, {&MockPostProcessor{ArtifactId: "pp"}, "pp", make(map[string]interface{}), boolPointer(false)},
}, },
} }
@ -270,10 +274,10 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build = testBuild() build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{ build.postProcessors = [][]coreBuildPostProcessor{
{ {
{&MockPostProcessor{ArtifactId: "pp1"}, "pp", make(map[string]interface{}), false}, {&MockPostProcessor{ArtifactId: "pp1"}, "pp", make(map[string]interface{}), boolPointer(false)},
}, },
{ {
{&MockPostProcessor{ArtifactId: "pp2"}, "pp", make(map[string]interface{}), true}, {&MockPostProcessor{ArtifactId: "pp2"}, "pp", make(map[string]interface{}), boolPointer(true)},
}, },
} }
@ -298,12 +302,12 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build = testBuild() build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{ build.postProcessors = [][]coreBuildPostProcessor{
{ {
{&MockPostProcessor{ArtifactId: "pp1a"}, "pp", make(map[string]interface{}), false}, {&MockPostProcessor{ArtifactId: "pp1a"}, "pp", make(map[string]interface{}), boolPointer(false)},
{&MockPostProcessor{ArtifactId: "pp1b"}, "pp", make(map[string]interface{}), true}, {&MockPostProcessor{ArtifactId: "pp1b"}, "pp", make(map[string]interface{}), boolPointer(true)},
}, },
{ {
{&MockPostProcessor{ArtifactId: "pp2a"}, "pp", make(map[string]interface{}), false}, {&MockPostProcessor{ArtifactId: "pp2a"}, "pp", make(map[string]interface{}), boolPointer(false)},
{&MockPostProcessor{ArtifactId: "pp2b"}, "pp", make(map[string]interface{}), false}, {&MockPostProcessor{ArtifactId: "pp2b"}, "pp", make(map[string]interface{}), boolPointer(false)},
}, },
} }
@ -329,7 +333,61 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build.postProcessors = [][]coreBuildPostProcessor{ build.postProcessors = [][]coreBuildPostProcessor{
{ {
{ {
&MockPostProcessor{ArtifactId: "pp", Keep: true}, "pp", make(map[string]interface{}), false, &MockPostProcessor{ArtifactId: "pp", Keep: true, ForceOverride: true}, "pp", make(map[string]interface{}), boolPointer(false),
},
},
}
build.Prepare()
artifacts, err = build.Run(ui)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedIds = []string{"b", "pp"}
artifactIds = make([]string, len(artifacts))
for i, artifact := range artifacts {
artifactIds[i] = artifact.Id()
}
if !reflect.DeepEqual(artifactIds, expectedIds) {
t.Fatalf("unexpected ids: %#v", artifactIds)
}
// Test case: Test that with a single post-processor that non-forcibly
// keeps inputs, that the artifacts are discarded if user overrides.
build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{
{
{
&MockPostProcessor{ArtifactId: "pp", Keep: true, ForceOverride: false}, "pp", make(map[string]interface{}), boolPointer(false),
},
},
}
build.Prepare()
artifacts, err = build.Run(ui)
if err != nil {
t.Fatalf("err: %s", err)
}
expectedIds = []string{"pp"}
artifactIds = make([]string, len(artifacts))
for i, artifact := range artifacts {
artifactIds[i] = artifact.Id()
}
if !reflect.DeepEqual(artifactIds, expectedIds) {
t.Fatalf("unexpected ids: %#v", artifactIds)
}
// Test case: Test that with a single post-processor that non-forcibly
// keeps inputs, that the artifacts are kept if user does not have preference.
build = testBuild()
build.postProcessors = [][]coreBuildPostProcessor{
{
{
&MockPostProcessor{ArtifactId: "pp", Keep: true, ForceOverride: false}, "pp", make(map[string]interface{}), nil,
}, },
}, },
} }

View File

@ -13,8 +13,8 @@ func (helperPostProcessor) Configure(...interface{}) error {
return nil return nil
} }
func (helperPostProcessor) PostProcess(packer.Ui, packer.Artifact) (packer.Artifact, bool, error) { func (helperPostProcessor) PostProcess(packer.Ui, packer.Artifact) (packer.Artifact, bool, bool, error) {
return nil, false, nil return nil, false, false, nil
} }
func TestPostProcessor_NoExist(t *testing.T) { func TestPostProcessor_NoExist(t *testing.T) {

View File

@ -24,12 +24,12 @@ func (pp *TestPostProcessor) Configure(v ...interface{}) error {
return nil return nil
} }
func (pp *TestPostProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) { func (pp *TestPostProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, bool, error) {
pp.ppCalled = true pp.ppCalled = true
pp.ppArtifact = a pp.ppArtifact = a
pp.ppArtifactId = a.Id() pp.ppArtifactId = a.Id()
pp.ppUi = ui pp.ppUi = ui
return testPostProcessorArtifact, false, nil return testPostProcessorArtifact, false, false, nil
} }
func TestPostProcessorRPC(t *testing.T) { func TestPostProcessorRPC(t *testing.T) {
@ -65,7 +65,7 @@ func TestPostProcessorRPC(t *testing.T) {
IdValue: "ppTestId", IdValue: "ppTestId",
} }
ui := new(testUi) ui := new(testUi)
artifact, _, err := ppClient.PostProcess(ui, a) artifact, _, _, err := ppClient.PostProcess(ui, a)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -98,7 +98,7 @@ func testChecksum(t *testing.T, config string) packer.Artifact {
checksum.config.PackerBuildName = "vanilla" checksum.config.PackerBuildName = "vanilla"
checksum.config.PackerBuilderType = "file" checksum.config.PackerBuilderType = "file"
artifactOut, _, err := checksum.PostProcess(ui, artifact) artifactOut, _, _, err := checksum.PostProcess(ui, artifact)
if err != nil { if err != nil {
t.Fatalf("Failed to checksum artifact: %s", err) t.Fatalf("Failed to checksum artifact: %s", err)
} }

View File

@ -238,7 +238,7 @@ func testArchive(t *testing.T, config string) packer.Artifact {
compressor.config.PackerBuildName = "vanilla" compressor.config.PackerBuildName = "vanilla"
compressor.config.PackerBuilderType = "file" compressor.config.PackerBuilderType = "file"
artifactOut, _, err := compressor.PostProcess(ui, artifact) artifactOut, _, _, err := compressor.PostProcess(ui, artifact)
if err != nil { if err != nil {
t.Fatalf("Failed to compress artifact: %s", err) t.Fatalf("Failed to compress artifact: %s", err)
} }

View File

@ -41,13 +41,16 @@ func TestPostProcessor_PostProcess(t *testing.T) {
IdValue: "foo/bar", IdValue: "foo/bar",
} }
result, keep, err := p.PostProcess(testUi(), artifact) result, keep, forceOverride, err := p.PostProcess(testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
if !keep { if !keep {
t.Fatal("should keep") t.Fatal("should keep")
} }
if forceOverride {
t.Fatal("Should default to keep, but not override user wishes")
}
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -71,13 +74,16 @@ func TestPostProcessor_PostProcess_portInName(t *testing.T) {
IdValue: "localhost:5000/foo/bar", IdValue: "localhost:5000/foo/bar",
} }
result, keep, err := p.PostProcess(testUi(), artifact) result, keep, forceOverride, err := p.PostProcess(testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
if !keep { if !keep {
t.Fatal("should keep") t.Fatal("should keep")
} }
if forceOverride {
t.Fatal("Should default to keep, but not override user wishes")
}
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -101,13 +107,16 @@ func TestPostProcessor_PostProcess_tags(t *testing.T) {
IdValue: "hashicorp/ubuntu:precise", IdValue: "hashicorp/ubuntu:precise",
} }
result, keep, err := p.PostProcess(testUi(), artifact) result, keep, forceOverride, err := p.PostProcess(testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
if !keep { if !keep {
t.Fatal("should keep") t.Fatal("should keep")
} }
if forceOverride {
t.Fatal("Should default to keep, but not override user wishes")
}
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -48,13 +48,16 @@ func TestPostProcessor_PostProcess(t *testing.T) {
IdValue: "1234567890abcdef", IdValue: "1234567890abcdef",
} }
result, keep, err := p.PostProcess(testUi(), artifact) result, keep, forceOverride, err := p.PostProcess(testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
if !keep { if !keep {
t.Fatal("should keep") t.Fatal("should keep")
} }
if !forceOverride {
t.Fatal("Should force keep no matter what user sets.")
}
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -87,13 +90,16 @@ func TestPostProcessor_PostProcess_Force(t *testing.T) {
IdValue: "1234567890abcdef", IdValue: "1234567890abcdef",
} }
result, keep, err := p.PostProcess(testUi(), artifact) result, keep, forceOverride, err := p.PostProcess(testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
if !keep { if !keep {
t.Fatal("should keep") t.Fatal("should keep")
} }
if !forceOverride {
t.Fatal("Should force keep no matter what user sets.")
}
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -151,7 +151,7 @@ func TestPostProcessorPostProcess_badId(t *testing.T) {
BuilderIdValue: "invalid.packer", BuilderIdValue: "invalid.packer",
} }
_, _, err := testPP(t).PostProcess(testUi(), artifact) _, _, _, err := testPP(t).PostProcess(testUi(), artifact)
if !strings.Contains(err.Error(), "artifact type") { if !strings.Contains(err.Error(), "artifact type") {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -181,7 +181,7 @@ func TestPostProcessorPostProcess_vagrantfileUserVariable(t *testing.T) {
a := &packer.MockArtifact{ a := &packer.MockArtifact{
BuilderIdValue: "packer.parallels", BuilderIdValue: "packer.parallels",
} }
a2, _, err := p.PostProcess(testUi(), a) a2, _, _, err := p.PostProcess(testUi(), a)
if a2 != nil { if a2 != nil {
for _, fn := range a2.Files() { for _, fn := range a2.Files() {
defer os.Remove(fn) defer os.Remove(fn)

View File

@ -14,6 +14,10 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
) )
func boolPointer(tf bool) *bool {
return &tf
}
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
cases := []struct { cases := []struct {
File string File string
@ -205,7 +209,7 @@ func TestParse(t *testing.T) {
{ {
Name: "foo", Name: "foo",
Type: "foo", Type: "foo",
KeepInputArtifact: true, KeepInputArtifact: boolPointer(true),
}, },
}, },
}, },