2013-04-20 21:55:02 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-06-19 01:45:53 -04:00
|
|
|
"reflect"
|
2013-04-20 21:55:02 -04:00
|
|
|
"testing"
|
2019-12-17 16:41:48 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/common"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
|
2020-11-09 20:09:32 -05:00
|
|
|
"github.com/hashicorp/packer/version"
|
2013-04-20 21:55:02 -04:00
|
|
|
)
|
|
|
|
|
2019-04-03 14:32:49 -04:00
|
|
|
func boolPointer(tf bool) *bool {
|
|
|
|
return &tf
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func testBuild() *CoreBuild {
|
|
|
|
return &CoreBuild{
|
|
|
|
Type: "test",
|
2020-12-09 06:39:54 -05:00
|
|
|
Builder: &packersdk.MockBuilder{ArtifactId: "b"},
|
2019-12-17 05:25:56 -05:00
|
|
|
BuilderConfig: 42,
|
|
|
|
BuilderType: "foo",
|
2020-11-20 13:21:29 -05:00
|
|
|
hooks: map[string][]packersdk.Hook{
|
|
|
|
"foo": {&packersdk.MockHook{}},
|
2013-05-22 19:46:23 -04:00
|
|
|
},
|
2019-12-17 05:25:56 -05:00
|
|
|
Provisioners: []CoreBuildProvisioner{
|
2020-01-16 10:24:33 -05:00
|
|
|
{
|
|
|
|
PType: "mock-provisioner",
|
2020-12-09 06:39:54 -05:00
|
|
|
Provisioner: &packersdk.MockProvisioner{},
|
2020-01-16 10:24:33 -05:00
|
|
|
config: []interface{}{42}},
|
2013-05-22 19:29:07 -04:00
|
|
|
},
|
2019-12-17 05:25:56 -05:00
|
|
|
PostProcessors: [][]CoreBuildPostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
{&MockPostProcessor{ArtifactId: "pp"}, "testPP", "testPPName", make(map[string]interface{}), boolPointer(true)},
|
2013-06-18 13:31:52 -04:00
|
|
|
},
|
|
|
|
},
|
2019-12-17 05:25:56 -05:00
|
|
|
Variables: make(map[string]string),
|
2016-09-13 20:04:18 -04:00
|
|
|
onError: "cleanup",
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 17:07:49 -04:00
|
|
|
func testDefaultPackerConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2020-12-02 15:12:47 -05:00
|
|
|
common.BuildNameConfigKey: "test",
|
|
|
|
common.BuilderTypeConfigKey: "foo",
|
|
|
|
common.CoreVersionConfigKey: version.FormattedVersion(),
|
|
|
|
common.DebugConfigKey: false,
|
|
|
|
common.ForceConfigKey: false,
|
|
|
|
common.OnErrorConfigKey: "cleanup",
|
|
|
|
common.TemplatePathKey: "",
|
|
|
|
common.UserVariablesConfigKey: make(map[string]string),
|
2013-08-09 17:07:49 -04:00
|
|
|
}
|
|
|
|
}
|
2013-05-09 14:32:03 -04:00
|
|
|
func TestBuild_Name(t *testing.T) {
|
|
|
|
build := testBuild()
|
2013-10-17 03:09:27 -04:00
|
|
|
if build.Name() != "test" {
|
|
|
|
t.Fatalf("bad: %s", build.Name())
|
|
|
|
}
|
2013-05-09 14:32:03 -04:00
|
|
|
}
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
func TestBuild_Prepare(t *testing.T) {
|
2013-08-09 17:07:49 -04:00
|
|
|
packerConfig := testDefaultPackerConfig()
|
2013-06-14 15:38:54 -04:00
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
build := testBuild()
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2013-06-14 15:38:54 -04:00
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2013-11-02 23:31:12 -04:00
|
|
|
if !builder.PrepareCalled {
|
2013-10-17 03:09:27 -04:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-11-02 23:31:12 -04:00
|
|
|
if !reflect.DeepEqual(builder.PrepareConfig, []interface{}{42, packerConfig}) {
|
|
|
|
t.Fatalf("bad: %#v", builder.PrepareConfig)
|
2013-10-17 03:09:27 -04:00
|
|
|
}
|
2013-05-22 19:46:23 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
coreProv := build.Provisioners[0]
|
2020-12-09 06:39:54 -05:00
|
|
|
prov := coreProv.Provisioner.(*packersdk.MockProvisioner)
|
2013-10-17 03:09:27 -04:00
|
|
|
if !prov.PrepCalled {
|
|
|
|
t.Fatal("prep should be called")
|
|
|
|
}
|
2019-12-17 00:23:05 -05:00
|
|
|
if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig, BasicPlaceholderData()}) {
|
2013-10-17 03:09:27 -04:00
|
|
|
t.Fatalf("bad: %#v", prov.PrepConfigs)
|
|
|
|
}
|
2013-06-18 13:31:52 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
corePP := build.PostProcessors[0][0]
|
|
|
|
pp := corePP.PostProcessor.(*MockPostProcessor)
|
2015-05-26 12:28:59 -04:00
|
|
|
if !pp.ConfigureCalled {
|
2013-10-17 03:09:27 -04:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
if !reflect.DeepEqual(pp.ConfigureConfigs, []interface{}{make(map[string]interface{}), packerConfig, BasicPlaceholderData()}) {
|
2015-05-26 12:28:59 -04:00
|
|
|
t.Fatalf("bad: %#v", pp.ConfigureConfigs)
|
2013-10-17 03:09:27 -04:00
|
|
|
}
|
2013-06-14 15:38:54 -04:00
|
|
|
}
|
|
|
|
|
2020-02-14 11:39:32 -05:00
|
|
|
func TestBuild_Prepare_SkipWhenBuilderAlreadyInitialized(t *testing.T) {
|
|
|
|
build := testBuild()
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2020-02-14 11:39:32 -05:00
|
|
|
|
|
|
|
build.Prepared = true
|
|
|
|
build.Prepare()
|
|
|
|
if builder.PrepareCalled {
|
|
|
|
t.Fatal("should not be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:37:14 -04:00
|
|
|
func TestBuild_Prepare_Twice(t *testing.T) {
|
|
|
|
build := testBuild()
|
2013-12-27 11:17:51 -05:00
|
|
|
warn, err := build.Prepare()
|
2013-11-02 23:31:12 -04:00
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warn)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2013-06-18 12:37:14 -04:00
|
|
|
t.Fatalf("bad error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
|
|
|
if p == nil {
|
|
|
|
t.Fatalf("should've paniced")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.(string) != "prepare already called" {
|
|
|
|
t.Fatalf("Invalid panic: %s", p)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2013-06-18 12:37:14 -04:00
|
|
|
}
|
|
|
|
|
2018-03-13 23:29:50 -04:00
|
|
|
func TestBuildPrepare_BuilderWarnings(t *testing.T) {
|
2013-11-02 23:31:12 -04:00
|
|
|
expected := []string{"foo"}
|
|
|
|
|
|
|
|
build := testBuild()
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2013-11-02 23:31:12 -04:00
|
|
|
builder.PrepareWarnings = expected
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
warn, err := build.Prepare()
|
2013-11-02 23:31:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(warn, expected) {
|
|
|
|
t.Fatalf("bad: %#v", warn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:38:54 -04:00
|
|
|
func TestBuild_Prepare_Debug(t *testing.T) {
|
2013-08-09 17:07:49 -04:00
|
|
|
packerConfig := testDefaultPackerConfig()
|
2020-12-02 15:12:47 -05:00
|
|
|
packerConfig[common.DebugConfigKey] = true
|
2013-06-14 15:38:54 -04:00
|
|
|
|
|
|
|
build := testBuild()
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2013-04-20 21:55:02 -04:00
|
|
|
|
2013-06-14 15:38:54 -04:00
|
|
|
build.SetDebug(true)
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2013-11-02 23:31:12 -04:00
|
|
|
if !builder.PrepareCalled {
|
2013-10-17 03:09:27 -04:00
|
|
|
t.Fatalf("should be called")
|
|
|
|
}
|
2013-11-02 23:31:12 -04:00
|
|
|
if !reflect.DeepEqual(builder.PrepareConfig, []interface{}{42, packerConfig}) {
|
|
|
|
t.Fatalf("bad: %#v", builder.PrepareConfig)
|
2013-10-17 03:09:27 -04:00
|
|
|
}
|
2013-05-22 19:46:23 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
coreProv := build.Provisioners[0]
|
2020-12-09 06:39:54 -05:00
|
|
|
prov := coreProv.Provisioner.(*packersdk.MockProvisioner)
|
2013-10-17 03:09:27 -04:00
|
|
|
if !prov.PrepCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
2019-12-17 00:23:05 -05:00
|
|
|
if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig, BasicPlaceholderData()}) {
|
2013-10-17 03:09:27 -04:00
|
|
|
t.Fatalf("bad: %#v", prov.PrepConfigs)
|
|
|
|
}
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 17:07:49 -04:00
|
|
|
func TestBuildPrepare_variables_default(t *testing.T) {
|
|
|
|
packerConfig := testDefaultPackerConfig()
|
2020-12-02 15:12:47 -05:00
|
|
|
packerConfig[common.UserVariablesConfigKey] = map[string]string{
|
2013-08-09 17:07:49 -04:00
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
build := testBuild()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.Variables["foo"] = "bar"
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2013-08-09 17:07:49 -04:00
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
warn, err := build.Prepare()
|
2013-11-02 23:31:12 -04:00
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warn)
|
|
|
|
}
|
2013-08-09 17:07:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:31:12 -04:00
|
|
|
if !builder.PrepareCalled {
|
2013-08-09 17:07:49 -04:00
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:31:12 -04:00
|
|
|
if !reflect.DeepEqual(builder.PrepareConfig[1], packerConfig) {
|
|
|
|
t.Fatalf("prepare bad: %#v", builder.PrepareConfig[1])
|
2013-08-09 17:07:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:41:48 -05:00
|
|
|
func TestBuildPrepare_ProvisionerGetsGeneratedMap(t *testing.T) {
|
|
|
|
packerConfig := testDefaultPackerConfig()
|
|
|
|
|
|
|
|
build := testBuild()
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2019-12-17 16:41:48 -05:00
|
|
|
builder.GeneratedVars = []string{"PartyVar"}
|
|
|
|
|
|
|
|
build.Prepare()
|
|
|
|
if !builder.PrepareCalled {
|
|
|
|
t.Fatalf("should be called")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(builder.PrepareConfig, []interface{}{42, packerConfig}) {
|
|
|
|
t.Fatalf("bad: %#v", builder.PrepareConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
coreProv := build.Provisioners[0]
|
2020-12-09 06:39:54 -05:00
|
|
|
prov := coreProv.Provisioner.(*packersdk.MockProvisioner)
|
2019-12-17 16:41:48 -05:00
|
|
|
if !prov.PrepCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
generated := BasicPlaceholderData()
|
2020-11-04 18:44:05 -05:00
|
|
|
generated["PartyVar"] = "Build_PartyVar. " + packerbuilderdata.PlaceholderMsg
|
2019-12-17 16:41:48 -05:00
|
|
|
if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig, generated}) {
|
|
|
|
t.Fatalf("bad: %#v", prov.PrepConfigs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
func TestBuild_Run(t *testing.T) {
|
|
|
|
ui := testUi()
|
|
|
|
|
|
|
|
build := testBuild()
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2019-03-22 09:53:28 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
artifacts, err := build.Run(ctx, ui)
|
2013-10-17 03:09:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifacts) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", artifacts)
|
|
|
|
}
|
2013-04-20 21:55:02 -04:00
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
// Verify builder was run
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2013-11-02 23:31:12 -04:00
|
|
|
if !builder.RunCalled {
|
2013-10-17 03:09:27 -04:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-05-22 19:29:07 -04:00
|
|
|
|
2018-03-13 03:38:01 -04:00
|
|
|
// Verify hooks are dispatchable
|
2013-11-02 23:31:12 -04:00
|
|
|
dispatchHook := builder.RunHook
|
2019-03-22 09:50:33 -04:00
|
|
|
dispatchHook.Run(ctx, "foo", nil, nil, 42)
|
2013-05-22 19:46:23 -04:00
|
|
|
|
2020-11-20 13:21:29 -05:00
|
|
|
hook := build.hooks["foo"][0].(*packersdk.MockHook)
|
2013-10-17 03:09:27 -04:00
|
|
|
if !hook.RunCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if hook.RunData != 42 {
|
|
|
|
t.Fatalf("bad: %#v", hook.RunData)
|
|
|
|
}
|
2013-05-24 00:26:24 -04:00
|
|
|
|
|
|
|
// Verify provisioners run
|
2020-11-20 13:53:16 -05:00
|
|
|
err = dispatchHook.Run(ctx, packersdk.HookProvision, nil, new(packersdk.MockCommunicator), 42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have errored")
|
|
|
|
}
|
2020-12-09 06:39:54 -05:00
|
|
|
prov := build.Provisioners[0].Provisioner.(*packersdk.MockProvisioner)
|
2013-10-17 03:09:27 -04:00
|
|
|
if !prov.ProvCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-06-18 13:54:22 -04:00
|
|
|
|
|
|
|
// Verify post-processor was run
|
2019-12-17 05:25:56 -05:00
|
|
|
pp := build.PostProcessors[0][0].PostProcessor.(*MockPostProcessor)
|
2015-05-26 12:28:59 -04:00
|
|
|
if !pp.PostProcessCalled {
|
2013-10-17 03:09:27 -04:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
2013-04-20 22:03:53 -04:00
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
func TestBuild_Run_Artifacts(t *testing.T) {
|
|
|
|
ui := testUi()
|
|
|
|
|
|
|
|
// Test case: Test that with no post-processors, we only get the
|
|
|
|
// main build.
|
|
|
|
build := testBuild()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.PostProcessors = [][]CoreBuildPostProcessor{}
|
2013-06-19 01:45:53 -04:00
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2019-03-22 09:53:28 -04:00
|
|
|
artifacts, err := build.Run(context.Background(), ui)
|
2013-06-19 01:45:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds := []string{"b"}
|
|
|
|
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 doesn't keep
|
|
|
|
// inputs, only that post-processors results are returned.
|
|
|
|
build = testBuild()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.PostProcessors = [][]CoreBuildPostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
{&MockPostProcessor{ArtifactId: "pp"}, "pp", "testPPName", make(map[string]interface{}), boolPointer(false)},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2019-03-22 09:53:28 -04:00
|
|
|
artifacts, err = build.Run(context.Background(), ui)
|
2013-06-19 01:45:53 -04:00
|
|
|
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 multiple post-processors, as long as one
|
|
|
|
// keeps the original, the original is kept.
|
|
|
|
build = testBuild()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.PostProcessors = [][]CoreBuildPostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
{&MockPostProcessor{ArtifactId: "pp1"}, "pp", "testPPName", make(map[string]interface{}), boolPointer(false)},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
{&MockPostProcessor{ArtifactId: "pp2"}, "pp", "testPPName", make(map[string]interface{}), boolPointer(true)},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2019-03-22 09:53:28 -04:00
|
|
|
artifacts, err = build.Run(context.Background(), ui)
|
2013-06-19 01:45:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds = []string{"b", "pp1", "pp2"}
|
|
|
|
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 sequences, intermediaries are kept if they
|
|
|
|
// want to be.
|
|
|
|
build = testBuild()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.PostProcessors = [][]CoreBuildPostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
{&MockPostProcessor{ArtifactId: "pp1a"}, "pp", "testPPName", make(map[string]interface{}), boolPointer(false)},
|
|
|
|
{&MockPostProcessor{ArtifactId: "pp1b"}, "pp", "testPPName", make(map[string]interface{}), boolPointer(true)},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
{&MockPostProcessor{ArtifactId: "pp2a"}, "pp", "testPPName", make(map[string]interface{}), boolPointer(false)},
|
|
|
|
{&MockPostProcessor{ArtifactId: "pp2b"}, "pp", "testPPName", make(map[string]interface{}), boolPointer(false)},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2019-03-22 09:53:28 -04:00
|
|
|
artifacts, err = build.Run(context.Background(), ui)
|
2013-06-19 01:45:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds = []string{"pp1a", "pp1b", "pp2b"}
|
|
|
|
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)
|
|
|
|
}
|
2013-07-01 14:30:39 -04:00
|
|
|
|
|
|
|
// Test case: Test that with a single post-processor that forcibly
|
|
|
|
// keeps inputs, that the artifacts are kept.
|
|
|
|
build = testBuild()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.PostProcessors = [][]CoreBuildPostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
&MockPostProcessor{ArtifactId: "pp", Keep: true, ForceOverride: true}, "pp", "testPPName", make(map[string]interface{}), boolPointer(false),
|
2019-04-03 14:32:49 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
build.Prepare()
|
2019-04-08 13:59:42 -04:00
|
|
|
|
|
|
|
artifacts, err = build.Run(context.Background(), ui)
|
2019-04-03 14:32:49 -04:00
|
|
|
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()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.PostProcessors = [][]CoreBuildPostProcessor{
|
2019-04-03 14:32:49 -04:00
|
|
|
{
|
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
&MockPostProcessor{ArtifactId: "pp", Keep: true, ForceOverride: false}, "pp", "testPPName", make(map[string]interface{}), boolPointer(false),
|
2019-04-03 14:32:49 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
build.Prepare()
|
2019-04-08 13:59:42 -04:00
|
|
|
artifacts, err = build.Run(context.Background(), ui)
|
2019-04-03 14:32:49 -04:00
|
|
|
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()
|
2019-12-17 05:25:56 -05:00
|
|
|
build.PostProcessors = [][]CoreBuildPostProcessor{
|
2019-04-03 14:32:49 -04:00
|
|
|
{
|
|
|
|
{
|
2020-01-15 18:36:52 -05:00
|
|
|
&MockPostProcessor{ArtifactId: "pp", Keep: true, ForceOverride: false}, "pp", "testPPName", make(map[string]interface{}), nil,
|
2013-07-01 14:30:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:17:51 -05:00
|
|
|
build.Prepare()
|
2019-03-22 09:53:28 -04:00
|
|
|
artifacts, err = build.Run(context.Background(), ui)
|
2013-07-01 14:30:39 -04:00
|
|
|
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)
|
|
|
|
}
|
2013-06-19 01:45:53 -04:00
|
|
|
}
|
|
|
|
|
2013-04-20 22:03:53 -04:00
|
|
|
func TestBuild_RunBeforePrepare(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
2013-10-17 03:09:27 -04:00
|
|
|
if p == nil {
|
|
|
|
t.Fatal("should panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.(string) != "Prepare must be called first" {
|
|
|
|
t.Fatalf("bad: %s", p.(string))
|
|
|
|
}
|
2013-04-20 22:03:53 -04:00
|
|
|
}()
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
testBuild().Run(context.Background(), testUi())
|
2013-04-20 22:03:53 -04:00
|
|
|
}
|
2013-06-03 19:06:04 -04:00
|
|
|
|
|
|
|
func TestBuild_Cancel(t *testing.T) {
|
|
|
|
build := testBuild()
|
2019-03-22 09:53:28 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
build.Prepare()
|
|
|
|
|
|
|
|
topCtx, topCtxCancel := context.WithCancel(context.Background())
|
2013-06-03 19:06:04 -04:00
|
|
|
|
2020-12-09 06:39:54 -05:00
|
|
|
builder := build.Builder.(*packersdk.MockBuilder)
|
2019-03-27 07:29:09 -04:00
|
|
|
|
|
|
|
builder.RunFn = func(ctx context.Context) {
|
|
|
|
topCtxCancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := build.Run(topCtx, testUi())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("build should err")
|
2013-10-17 03:09:27 -04:00
|
|
|
}
|
2013-06-03 19:06:04 -04:00
|
|
|
}
|