From 056fcb7ceaec7170035feefb2aea26b4ac79d967 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 3 Apr 2019 11:32:49 -0700 Subject: [PATCH] fix tests and add a few new ones --- packer/build_test.go | 76 ++++++++++++++++--- packer/plugin/post_processor_test.go | 4 +- packer/rpc/post_processor_test.go | 6 +- .../checksum/post-processor_test.go | 2 +- .../compress/post-processor_test.go | 2 +- .../docker-push/post-processor_test.go | 15 +++- .../docker-tag/post-processor_test.go | 10 ++- post-processor/vagrant/post-processor_test.go | 4 +- template/parse_test.go | 6 +- 9 files changed, 101 insertions(+), 24 deletions(-) diff --git a/packer/build_test.go b/packer/build_test.go index 057de0127..5300a7fdd 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -5,6 +5,10 @@ import ( "testing" ) +func boolPointer(tf bool) *bool { + return &tf +} + func testBuild() *coreBuild { return &coreBuild{ name: "test", @@ -19,7 +23,7 @@ func testBuild() *coreBuild { }, 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), @@ -245,7 +249,7 @@ func TestBuild_Run_Artifacts(t *testing.T) { build = testBuild() 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.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.postProcessors = [][]coreBuildPostProcessor{ { - {&MockPostProcessor{ArtifactId: "pp1a"}, "pp", make(map[string]interface{}), false}, - {&MockPostProcessor{ArtifactId: "pp1b"}, "pp", make(map[string]interface{}), true}, + {&MockPostProcessor{ArtifactId: "pp1a"}, "pp", make(map[string]interface{}), boolPointer(false)}, + {&MockPostProcessor{ArtifactId: "pp1b"}, "pp", make(map[string]interface{}), boolPointer(true)}, }, { - {&MockPostProcessor{ArtifactId: "pp2a"}, "pp", make(map[string]interface{}), false}, - {&MockPostProcessor{ArtifactId: "pp2b"}, "pp", make(map[string]interface{}), false}, + {&MockPostProcessor{ArtifactId: "pp2a"}, "pp", make(map[string]interface{}), boolPointer(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{ { { - &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, }, }, } diff --git a/packer/plugin/post_processor_test.go b/packer/plugin/post_processor_test.go index a1e2f0f65..b6276611c 100644 --- a/packer/plugin/post_processor_test.go +++ b/packer/plugin/post_processor_test.go @@ -13,8 +13,8 @@ func (helperPostProcessor) Configure(...interface{}) error { return nil } -func (helperPostProcessor) PostProcess(packer.Ui, packer.Artifact) (packer.Artifact, bool, error) { - return nil, false, nil +func (helperPostProcessor) PostProcess(packer.Ui, packer.Artifact) (packer.Artifact, bool, bool, error) { + return nil, false, false, nil } func TestPostProcessor_NoExist(t *testing.T) { diff --git a/packer/rpc/post_processor_test.go b/packer/rpc/post_processor_test.go index 683b6dc16..f7438bf00 100644 --- a/packer/rpc/post_processor_test.go +++ b/packer/rpc/post_processor_test.go @@ -24,12 +24,12 @@ func (pp *TestPostProcessor) Configure(v ...interface{}) error { 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.ppArtifact = a pp.ppArtifactId = a.Id() pp.ppUi = ui - return testPostProcessorArtifact, false, nil + return testPostProcessorArtifact, false, false, nil } func TestPostProcessorRPC(t *testing.T) { @@ -65,7 +65,7 @@ func TestPostProcessorRPC(t *testing.T) { IdValue: "ppTestId", } ui := new(testUi) - artifact, _, err := ppClient.PostProcess(ui, a) + artifact, _, _, err := ppClient.PostProcess(ui, a) if err != nil { t.Fatalf("err: %s", err) } diff --git a/post-processor/checksum/post-processor_test.go b/post-processor/checksum/post-processor_test.go index f2b91aca9..55e147859 100644 --- a/post-processor/checksum/post-processor_test.go +++ b/post-processor/checksum/post-processor_test.go @@ -98,7 +98,7 @@ func testChecksum(t *testing.T, config string) packer.Artifact { checksum.config.PackerBuildName = "vanilla" checksum.config.PackerBuilderType = "file" - artifactOut, _, err := checksum.PostProcess(ui, artifact) + artifactOut, _, _, err := checksum.PostProcess(ui, artifact) if err != nil { t.Fatalf("Failed to checksum artifact: %s", err) } diff --git a/post-processor/compress/post-processor_test.go b/post-processor/compress/post-processor_test.go index a3c6a8232..564964f65 100644 --- a/post-processor/compress/post-processor_test.go +++ b/post-processor/compress/post-processor_test.go @@ -238,7 +238,7 @@ func testArchive(t *testing.T, config string) packer.Artifact { compressor.config.PackerBuildName = "vanilla" compressor.config.PackerBuilderType = "file" - artifactOut, _, err := compressor.PostProcess(ui, artifact) + artifactOut, _, _, err := compressor.PostProcess(ui, artifact) if err != nil { t.Fatalf("Failed to compress artifact: %s", err) } diff --git a/post-processor/docker-push/post-processor_test.go b/post-processor/docker-push/post-processor_test.go index 1eee102cb..9309554e1 100644 --- a/post-processor/docker-push/post-processor_test.go +++ b/post-processor/docker-push/post-processor_test.go @@ -41,13 +41,16 @@ func TestPostProcessor_PostProcess(t *testing.T) { IdValue: "foo/bar", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, forceOverride, err := p.PostProcess(testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } if !keep { t.Fatal("should keep") } + if forceOverride { + t.Fatal("Should default to keep, but not override user wishes") + } if err != nil { t.Fatalf("err: %s", err) } @@ -71,13 +74,16 @@ func TestPostProcessor_PostProcess_portInName(t *testing.T) { 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 { t.Fatal("should be instance of Artifact") } if !keep { t.Fatal("should keep") } + if forceOverride { + t.Fatal("Should default to keep, but not override user wishes") + } if err != nil { t.Fatalf("err: %s", err) } @@ -101,13 +107,16 @@ func TestPostProcessor_PostProcess_tags(t *testing.T) { 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 { t.Fatal("should be instance of Artifact") } if !keep { t.Fatal("should keep") } + if forceOverride { + t.Fatal("Should default to keep, but not override user wishes") + } if err != nil { t.Fatalf("err: %s", err) } diff --git a/post-processor/docker-tag/post-processor_test.go b/post-processor/docker-tag/post-processor_test.go index fef434ee9..110c5568a 100644 --- a/post-processor/docker-tag/post-processor_test.go +++ b/post-processor/docker-tag/post-processor_test.go @@ -48,13 +48,16 @@ func TestPostProcessor_PostProcess(t *testing.T) { IdValue: "1234567890abcdef", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, forceOverride, err := p.PostProcess(testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } if !keep { t.Fatal("should keep") } + if !forceOverride { + t.Fatal("Should force keep no matter what user sets.") + } if err != nil { t.Fatalf("err: %s", err) } @@ -87,13 +90,16 @@ func TestPostProcessor_PostProcess_Force(t *testing.T) { IdValue: "1234567890abcdef", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, forceOverride, err := p.PostProcess(testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } if !keep { t.Fatal("should keep") } + if !forceOverride { + t.Fatal("Should force keep no matter what user sets.") + } if err != nil { t.Fatalf("err: %s", err) } diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go index 8a5368737..749ed162a 100644 --- a/post-processor/vagrant/post-processor_test.go +++ b/post-processor/vagrant/post-processor_test.go @@ -151,7 +151,7 @@ func TestPostProcessorPostProcess_badId(t *testing.T) { BuilderIdValue: "invalid.packer", } - _, _, err := testPP(t).PostProcess(testUi(), artifact) + _, _, _, err := testPP(t).PostProcess(testUi(), artifact) if !strings.Contains(err.Error(), "artifact type") { t.Fatalf("err: %s", err) } @@ -181,7 +181,7 @@ func TestPostProcessorPostProcess_vagrantfileUserVariable(t *testing.T) { a := &packer.MockArtifact{ BuilderIdValue: "packer.parallels", } - a2, _, err := p.PostProcess(testUi(), a) + a2, _, _, err := p.PostProcess(testUi(), a) if a2 != nil { for _, fn := range a2.Files() { defer os.Remove(fn) diff --git a/template/parse_test.go b/template/parse_test.go index 08d7781f5..d67f31ec3 100644 --- a/template/parse_test.go +++ b/template/parse_test.go @@ -14,6 +14,10 @@ import ( "github.com/google/go-cmp/cmp" ) +func boolPointer(tf bool) *bool { + return &tf +} + func TestParse(t *testing.T) { cases := []struct { File string @@ -205,7 +209,7 @@ func TestParse(t *testing.T) { { Name: "foo", Type: "foo", - KeepInputArtifact: true, + KeepInputArtifact: boolPointer(true), }, }, },