From 85d601dd8100cb3a39793ae5ad633c3715710d9c Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Thu, 7 Feb 2019 16:13:35 -0600 Subject: [PATCH] feature: bsu, prepare function and test --- builder/osc/bsu/builder.go | 38 +++++++++++- builder/osc/bsu/builder_test.go | 104 ++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/builder/osc/bsu/builder.go b/builder/osc/bsu/builder.go index eccddfc30..4d27171c5 100644 --- a/builder/osc/bsu/builder.go +++ b/builder/osc/bsu/builder.go @@ -10,6 +10,7 @@ import ( osccommon "github.com/hashicorp/packer/builder/osc/common" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" @@ -35,7 +36,42 @@ type Builder struct { } func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { - log.Println("[Debug] BSU Builder Prerare function") + b.config.ctx.Funcs = osccommon.TemplateFuncs + err := config.Decode(&b.config, &config.DecodeOpts{ + Interpolate: true, + InterpolateContext: &b.config.ctx, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{ + "omi_description", + "run_tags", + "run_volume_tags", + "spot_tags", + "snapshot_tags", + "tags", + }, + }, + }, raws...) + if err != nil { + return nil, err + } + + if b.config.PackerConfig.PackerForce { + b.config.OMIForceDeregister = true + } + + // Accumulate any errors + var errs *packer.MultiError + errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, + b.config.OMIConfig.Prepare(&b.config.AccessConfig, &b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.BlockDevices.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) + + if errs != nil && len(errs.Errors) > 0 { + return nil, errs + } + + packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token) return nil, nil } diff --git a/builder/osc/bsu/builder_test.go b/builder/osc/bsu/builder_test.go index 9f91f8184..1736044df 100644 --- a/builder/osc/bsu/builder_test.go +++ b/builder/osc/bsu/builder_test.go @@ -25,3 +25,107 @@ func TestBuilder_ImplementsBuilder(t *testing.T) { t.Fatalf("Builder should be a builder") } } + +func TestBuilder_Prepare_BadType(t *testing.T) { + b := &Builder{} + c := map[string]interface{}{ + "access_key": []string{}, + } + + warnings, err := b.Prepare(c) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err == nil { + t.Fatalf("prepare should fail") + } +} + +func TestBuilderPrepare_OMIName(t *testing.T) { + var b Builder + config := testConfig() + + // Test good + config["omi_name"] = "foo" + config["skip_region_validation"] = true + warnings, err := b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Test bad + config["omi_name"] = "foo {{" + b = Builder{} + warnings, err = b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err == nil { + t.Fatal("should have error") + } + + // Test bad + delete(config, "omi_name") + b = Builder{} + warnings, err = b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err == nil { + t.Fatal("should have error") + } +} + +func TestBuilderPrepare_InvalidKey(t *testing.T) { + var b Builder + config := testConfig() + + // Add a random key + config["i_should_not_be_valid"] = true + warnings, err := b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err == nil { + t.Fatal("should have error") + } +} + +func TestBuilderPrepare_InvalidShutdownBehavior(t *testing.T) { + var b Builder + config := testConfig() + + // Test good + config["shutdown_behavior"] = "terminate" + config["skip_region_validation"] = true + warnings, err := b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Test good + config["shutdown_behavior"] = "stop" + warnings, err = b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Test bad + config["shutdown_behavior"] = "foobar" + warnings, err = b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err == nil { + t.Fatal("should have error") + } +}