From a3740bb9be69a8665dd4849797cc47bc56a25197 Mon Sep 17 00:00:00 2001 From: Moss Date: Thu, 12 Mar 2020 17:54:31 +0100 Subject: [PATCH 01/11] Interpolate shell inline config --- provisioner/shell/provisioner.go | 28 ++++++++++++++++------------ template.json | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 template.json diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 3828c07b4..38ab4c474 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -70,12 +70,6 @@ type Provisioner struct { config Config } -type ExecuteCommandTemplate struct { - Vars string - EnvVarFile string - Path string -} - func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() } func (p *Provisioner) Prepare(raws ...interface{}) error { @@ -181,7 +175,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error { + if generatedData == nil { + generatedData = make(map[string]interface{}) + } + scripts := make([]string, len(p.config.Scripts)) copy(scripts, p.config.Scripts) @@ -201,6 +199,11 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C writer := bufio.NewWriter(tf) writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang)) for _, command := range p.config.Inline { + p.config.ctx.Data = generatedData + command, err := interpolate.Render(command, &p.config.ctx); + if err != nil { + return fmt.Errorf("Error interpolating Inline: %s", err) + } if _, err := writer.WriteString(command + "\n"); err != nil { return fmt.Errorf("Error preparing shell script: %s", err) } @@ -279,11 +282,12 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C defer f.Close() // Compile the command - p.config.ctx.Data = &ExecuteCommandTemplate{ - Vars: flattenedEnvVars, - EnvVarFile: p.config.envVarFile, - Path: p.config.RemotePath, - } + // These are extra variables that will be made available for interpolation. + generatedData["Vars"] = flattenedEnvVars + generatedData["EnvVarFile"] = p.config.envVarFile + generatedData["Path"] = p.config.RemotePath + p.config.ctx.Data = generatedData + command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx) if err != nil { return fmt.Errorf("Error processing command: %s", err) diff --git a/template.json b/template.json new file mode 100644 index 000000000..fff4778a5 --- /dev/null +++ b/template.json @@ -0,0 +1,29 @@ +{ + "builders": [ + { + "type": "amazon-ebs", + "ami_name": "moss-packer-whee", + "instance_type": "t2.micro", + "source_ami_filter": { + "filters": { + "virtualization-type": "hvm", + "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*", + "root-device-type": "ebs" + }, + "owners": ["099720109477"], + "most_recent": true + }, + "ssh_username": "ubuntu" + } + ], + "provisioners": [ + { + "type": "shell-local", + "inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"] + }, + { + "type": "shell", + "inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"] + } + ] +} From ebdc694b250ffc38f042cf546c517c62f355faab Mon Sep 17 00:00:00 2001 From: Moss Date: Thu, 12 Mar 2020 17:58:11 +0100 Subject: [PATCH 02/11] Remove template.json --- template.json | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 template.json diff --git a/template.json b/template.json deleted file mode 100644 index fff4778a5..000000000 --- a/template.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "builders": [ - { - "type": "amazon-ebs", - "ami_name": "moss-packer-whee", - "instance_type": "t2.micro", - "source_ami_filter": { - "filters": { - "virtualization-type": "hvm", - "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*", - "root-device-type": "ebs" - }, - "owners": ["099720109477"], - "most_recent": true - }, - "ssh_username": "ubuntu" - } - ], - "provisioners": [ - { - "type": "shell-local", - "inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"] - }, - { - "type": "shell", - "inline": ["echo MOSS packer run uuid is '{{ build `PackerRunUUID`}}'"] - } - ] -} From 6c06a2a0485f959d86d9a623f53eba8fb480bbad Mon Sep 17 00:00:00 2001 From: Moss Date: Thu, 12 Mar 2020 17:59:34 +0100 Subject: [PATCH 03/11] Fix format --- provisioner/shell/provisioner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 38ab4c474..6035c5e97 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -200,7 +200,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang)) for _, command := range p.config.Inline { p.config.ctx.Data = generatedData - command, err := interpolate.Render(command, &p.config.ctx); + command, err := interpolate.Render(command, &p.config.ctx) if err != nil { return fmt.Errorf("Error interpolating Inline: %s", err) } From 7fbbbffd5c178e66c6ad2cbc7a0095a2e6e316ec Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 16:17:40 +0100 Subject: [PATCH 04/11] Interpolate file provisioner and add integration tests --- provisioner/file/provisioner.go | 27 ++++++- test/helper/aws.go | 42 ++++++++++ test/provisioner/shell/provisioner_test.go | 78 +++++++++++++++++++ .../test-fixtures/shell-provisioner.json | 29 +++++++ 4 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 test/helper/aws.go create mode 100644 test/provisioner/shell/provisioner_test.go create mode 100644 test/provisioner/shell/test-fixtures/shell-provisioner.json diff --git a/provisioner/file/provisioner.go b/provisioner/file/provisioner.go index 7adf0197b..82e69570d 100644 --- a/provisioner/file/provisioner.go +++ b/provisioner/file/provisioner.go @@ -95,7 +95,12 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { return nil } -func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error { +func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error { + if generatedData == nil { + generatedData = make(map[string]interface{}) + } + p.config.ctx.Data = generatedData + if p.config.Direction == "download" { return p.ProvisionDownload(ui, comm) } else { @@ -105,7 +110,15 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator) error { for _, src := range p.config.Sources { - dst := p.config.Destination + src, err := interpolate.Render(src, &p.config.ctx) + if err != nil { + return fmt.Errorf("Error interpolating source: %s", err) + } + dst, err := interpolate.Render(p.config.Destination, &p.config.ctx) + if err != nil { + return fmt.Errorf("Error interpolating destination: %s", err) + } + ui.Say(fmt.Sprintf("Downloading %s => %s", src, dst)) // ensure destination dir exists. p.config.Destination may either be a file or a dir. dir := dst @@ -146,7 +159,15 @@ func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator) func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) error { for _, src := range p.config.Sources { - dst := p.config.Destination + src, err := interpolate.Render(src, &p.config.ctx) + if err != nil { + return fmt.Errorf("Error interpolating source: %s", err) + } + + dst, err := interpolate.Render(p.config.Destination, &p.config.ctx) + if err != nil { + return fmt.Errorf("Error interpolating destination: %s", err) + } ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst)) diff --git a/test/helper/aws.go b/test/helper/aws.go new file mode 100644 index 000000000..df758b23d --- /dev/null +++ b/test/helper/aws.go @@ -0,0 +1,42 @@ +package helper + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + awscommon "github.com/hashicorp/packer/builder/amazon/common" + "testing" +) + +type AWSHelper struct { + Region string + AMIName string +} + +func (a *AWSHelper) CleanUpAmi(t *testing.T) { + accessConfig := &awscommon.AccessConfig{} + session, err := accessConfig.Session() + if err != nil { + t.Errorf("AWSAMICleanUp: Unable to create aws session %s", err.Error()) + } + + regionconn := ec2.New(session.Copy(&aws.Config{ + Region: aws.String(a.Region), + })) + + resp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{ + Owners: aws.StringSlice([]string{"self"}), + Filters: []*ec2.Filter{{ + Name: aws.String("name"), + Values: aws.StringSlice([]string{a.AMIName}), + }}}) + if err != nil { + t.Errorf("AWSAMICleanUp: Unable to find Image %s: %s",a.AMIName, err.Error()) + } + + _, err = regionconn.DeregisterImage(&ec2.DeregisterImageInput{ + ImageId: resp.Images[0].ImageId, + }) + if err != nil { + t.Errorf("AWSAMICleanUp: Unable to Deregister Image %s", err.Error()) + } +} \ No newline at end of file diff --git a/test/provisioner/shell/provisioner_test.go b/test/provisioner/shell/provisioner_test.go new file mode 100644 index 000000000..419acc77d --- /dev/null +++ b/test/provisioner/shell/provisioner_test.go @@ -0,0 +1,78 @@ +// +build integration + +package shell_integration + +import ( + "bytes" + "github.com/hashicorp/go-uuid" + amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" + "github.com/hashicorp/packer/command" + "github.com/hashicorp/packer/packer" + fileprovisioner "github.com/hashicorp/packer/provisioner/file" + "github.com/hashicorp/packer/provisioner/shell" + testshelper "github.com/hashicorp/packer/test/helper" + "os" + "path/filepath" + "testing" +) + +func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { + UUID, _ := uuid.GenerateUUID() + os.Setenv("PACKER_RUN_UUID", UUID) + c := &command.BuildCommand{ + Meta: testMetaFile(t), + } + + file := "provisioner.shell." + UUID + ".txt" + defer os.RemoveAll(file) + + args := []string{ + filepath.Join("./test-fixtures", "shell-provisioner.json"), + } + if code := c.Run(args); code != 0 { + ui := c.Meta.Ui.(*packer.BasicUi) + out := ui.Writer.(*bytes.Buffer) + err := ui.ErrorWriter.(*bytes.Buffer) + t.Fatalf( + "Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s", + out.String(), + err.String()) + } + + if _, err := os.Stat(file); err != nil { + t.Errorf("Expected to find %s", file) + } else { + helper := testshelper.AWSHelper{ + Region: "us-east-1", + AMIName: "packer-test-shell-interpolate", + } + helper.CleanUpAmi(t) + } +} + +func testMetaFile(t *testing.T) command.Meta { + var out, err bytes.Buffer + return command.Meta{ + CoreConfig: testCoreConfigBuilder(t), + Ui: &packer.BasicUi{ + Writer: &out, + ErrorWriter: &err, + }, + } +} + +func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { + components := packer.ComponentFinder{ + BuilderStore: packer.MapOfBuilder{ + "amazon-ebs": func() (packer.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, + }, + ProvisionerStore: packer.MapOfProvisioner{ + "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, + "file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, + }, + PostProcessorStore: packer.MapOfPostProcessor{}, + } + return &packer.CoreConfig{ + Components: components, + } +} diff --git a/test/provisioner/shell/test-fixtures/shell-provisioner.json b/test/provisioner/shell/test-fixtures/shell-provisioner.json new file mode 100644 index 000000000..df3604670 --- /dev/null +++ b/test/provisioner/shell/test-fixtures/shell-provisioner.json @@ -0,0 +1,29 @@ +{ + "builders": [ + { + "type": "amazon-ebs", + "ami_name": "packer-test-shell-interpolate", + "instance_type": "m1.small", + "region": "us-east-1", + "ssh_username": "ubuntu", + "source_ami": "ami-0568456c", + "tags": { + "packer-test": "true" + } + } + ], + "provisioners": [ + { + "type": "shell", + "inline": [ + "echo {{ build `ID`}} > provisioner.{{ build `PackerRunUUID`}}.txt" + ] + }, + { + "type": "file", + "source": "provisioner.{{ build `PackerRunUUID`}}.txt", + "destination": "provisioner.shell.{{ build `PackerRunUUID`}}.txt", + "direction": "download" + } + ] +} \ No newline at end of file From 2a7847a2ab0da0ec41b5c19579fa5609c790d502 Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 16:35:31 +0100 Subject: [PATCH 05/11] Add integration tests helper on test package --- test/helper/core.go | 66 ++++++++++++++++++++++ test/provisioner/shell/provisioner_test.go | 45 ++------------- 2 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 test/helper/core.go diff --git a/test/helper/core.go b/test/helper/core.go new file mode 100644 index 000000000..052610b0e --- /dev/null +++ b/test/helper/core.go @@ -0,0 +1,66 @@ +package helper + +import ( + "bytes" + amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" + "github.com/hashicorp/packer/command" + "github.com/hashicorp/packer/packer" + fileprovisioner "github.com/hashicorp/packer/provisioner/file" + "github.com/hashicorp/packer/provisioner/shell" + "os" + "testing" +) + +// fileExists returns true if the filename is found +func FileExists(filename string) bool { + if _, err := os.Stat(filename); err == nil { + return true + } + return false +} + +// testCoreConfigBuilder creates a packer CoreConfig that has a file builder +// available. This allows us to test a builder that writes files to disk. +func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { + components := packer.ComponentFinder{ + BuilderStore: packer.MapOfBuilder{ + "amazon-ebs": func() (packer.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, + }, + ProvisionerStore: packer.MapOfProvisioner{ + "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, + "file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, + }, + PostProcessorStore: packer.MapOfPostProcessor{}, + } + return &packer.CoreConfig{ + Components: components, + } +} + +// TestMetaFile creates a Meta object that includes a file builder +func TestMetaFile(t *testing.T) command.Meta { + var out, err bytes.Buffer + return command.Meta{ + CoreConfig: testCoreConfigBuilder(t), + Ui: &packer.BasicUi{ + Writer: &out, + ErrorWriter: &err, + }, + } +} + +func CleanupFiles(moreFiles ...string) { + for _, file := range moreFiles { + os.RemoveAll(file) + } +} + +func FatalCommand(t *testing.T, m command.Meta) { + ui := m.Ui.(*packer.BasicUi) + out := ui.Writer.(*bytes.Buffer) + err := ui.ErrorWriter.(*bytes.Buffer) + t.Fatalf( + "Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s", + out.String(), + err.String()) +} \ No newline at end of file diff --git a/test/provisioner/shell/provisioner_test.go b/test/provisioner/shell/provisioner_test.go index 419acc77d..8cb801b90 100644 --- a/test/provisioner/shell/provisioner_test.go +++ b/test/provisioner/shell/provisioner_test.go @@ -3,13 +3,8 @@ package shell_integration import ( - "bytes" "github.com/hashicorp/go-uuid" - amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/packer" - fileprovisioner "github.com/hashicorp/packer/provisioner/file" - "github.com/hashicorp/packer/provisioner/shell" testshelper "github.com/hashicorp/packer/test/helper" "os" "path/filepath" @@ -20,26 +15,20 @@ func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { UUID, _ := uuid.GenerateUUID() os.Setenv("PACKER_RUN_UUID", UUID) c := &command.BuildCommand{ - Meta: testMetaFile(t), + Meta: testshelper.TestMetaFile(t), } file := "provisioner.shell." + UUID + ".txt" - defer os.RemoveAll(file) + defer testshelper.CleanupFiles(file) args := []string{ filepath.Join("./test-fixtures", "shell-provisioner.json"), } if code := c.Run(args); code != 0 { - ui := c.Meta.Ui.(*packer.BasicUi) - out := ui.Writer.(*bytes.Buffer) - err := ui.ErrorWriter.(*bytes.Buffer) - t.Fatalf( - "Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s", - out.String(), - err.String()) + testshelper.FatalCommand(t, c.Meta) } - if _, err := os.Stat(file); err != nil { + if !testshelper.FileExists(file) { t.Errorf("Expected to find %s", file) } else { helper := testshelper.AWSHelper{ @@ -50,29 +39,3 @@ func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { } } -func testMetaFile(t *testing.T) command.Meta { - var out, err bytes.Buffer - return command.Meta{ - CoreConfig: testCoreConfigBuilder(t), - Ui: &packer.BasicUi{ - Writer: &out, - ErrorWriter: &err, - }, - } -} - -func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { - components := packer.ComponentFinder{ - BuilderStore: packer.MapOfBuilder{ - "amazon-ebs": func() (packer.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, - }, - ProvisionerStore: packer.MapOfProvisioner{ - "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, - "file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, - }, - PostProcessorStore: packer.MapOfPostProcessor{}, - } - return &packer.CoreConfig{ - Components: components, - } -} From 449a299ee326e2be228d150929d280b42710a969 Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 16:36:30 +0100 Subject: [PATCH 06/11] Fix format --- test/helper/aws.go | 6 +++--- test/helper/core.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/helper/aws.go b/test/helper/aws.go index df758b23d..76f27c8b1 100644 --- a/test/helper/aws.go +++ b/test/helper/aws.go @@ -8,7 +8,7 @@ import ( ) type AWSHelper struct { - Region string + Region string AMIName string } @@ -30,7 +30,7 @@ func (a *AWSHelper) CleanUpAmi(t *testing.T) { Values: aws.StringSlice([]string{a.AMIName}), }}}) if err != nil { - t.Errorf("AWSAMICleanUp: Unable to find Image %s: %s",a.AMIName, err.Error()) + t.Errorf("AWSAMICleanUp: Unable to find Image %s: %s", a.AMIName, err.Error()) } _, err = regionconn.DeregisterImage(&ec2.DeregisterImageInput{ @@ -39,4 +39,4 @@ func (a *AWSHelper) CleanUpAmi(t *testing.T) { if err != nil { t.Errorf("AWSAMICleanUp: Unable to Deregister Image %s", err.Error()) } -} \ No newline at end of file +} diff --git a/test/helper/core.go b/test/helper/core.go index 052610b0e..ca37a1119 100644 --- a/test/helper/core.go +++ b/test/helper/core.go @@ -27,8 +27,8 @@ func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { "amazon-ebs": func() (packer.Builder, error) { return &amazonebsbuilder.Builder{}, nil }, }, ProvisionerStore: packer.MapOfProvisioner{ - "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, - "file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, + "shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil }, + "file": func() (packer.Provisioner, error) { return &fileprovisioner.Provisioner{}, nil }, }, PostProcessorStore: packer.MapOfPostProcessor{}, } @@ -63,4 +63,4 @@ func FatalCommand(t *testing.T, m command.Meta) { "Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s", out.String(), err.String()) -} \ No newline at end of file +} From 2ca680482779da44988ba6a0fb27373e995a8f90 Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 17:10:51 +0100 Subject: [PATCH 07/11] Move shell prov acc test to the same folder --- {test/helper => helper/tests}/aws.go | 2 +- {test/helper => helper/tests}/core.go | 2 +- .../shell/provisioner_acc_test.go | 6 ++---- .../shell/test-fixtures/shell-provisioner.json | 0 4 files changed, 4 insertions(+), 6 deletions(-) rename {test/helper => helper/tests}/aws.go (98%) rename {test/helper => helper/tests}/core.go (98%) rename test/provisioner/shell/provisioner_test.go => provisioner/shell/provisioner_acc_test.go (88%) rename {test/provisioner => provisioner}/shell/test-fixtures/shell-provisioner.json (100%) diff --git a/test/helper/aws.go b/helper/tests/aws.go similarity index 98% rename from test/helper/aws.go rename to helper/tests/aws.go index 76f27c8b1..e579bcde1 100644 --- a/test/helper/aws.go +++ b/helper/tests/aws.go @@ -1,4 +1,4 @@ -package helper +package testshelper import ( "github.com/aws/aws-sdk-go/aws" diff --git a/test/helper/core.go b/helper/tests/core.go similarity index 98% rename from test/helper/core.go rename to helper/tests/core.go index ca37a1119..05af712de 100644 --- a/test/helper/core.go +++ b/helper/tests/core.go @@ -1,4 +1,4 @@ -package helper +package testshelper import ( "bytes" diff --git a/test/provisioner/shell/provisioner_test.go b/provisioner/shell/provisioner_acc_test.go similarity index 88% rename from test/provisioner/shell/provisioner_test.go rename to provisioner/shell/provisioner_acc_test.go index 8cb801b90..0c4d40301 100644 --- a/test/provisioner/shell/provisioner_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -1,11 +1,9 @@ -// +build integration - -package shell_integration +package shell_test import ( "github.com/hashicorp/go-uuid" "github.com/hashicorp/packer/command" - testshelper "github.com/hashicorp/packer/test/helper" + "github.com/hashicorp/packer/helper/tests" "os" "path/filepath" "testing" diff --git a/test/provisioner/shell/test-fixtures/shell-provisioner.json b/provisioner/shell/test-fixtures/shell-provisioner.json similarity index 100% rename from test/provisioner/shell/test-fixtures/shell-provisioner.json rename to provisioner/shell/test-fixtures/shell-provisioner.json From 0cc1092222401f62ecb53669c4b377f8e6d5a66a Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 17:11:13 +0100 Subject: [PATCH 08/11] Fix format --- provisioner/shell/provisioner_acc_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index 0c4d40301..02508976d 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -36,4 +36,3 @@ func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { helper.CleanUpAmi(t) } } - From 3f49b7c66e8a3838c5db63617ec9c080dc0681c2 Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 17:17:42 +0100 Subject: [PATCH 09/11] Fix linter --- helper/tests/aws.go | 3 ++- helper/tests/core.go | 5 +++-- provisioner/shell/provisioner_acc_test.go | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/helper/tests/aws.go b/helper/tests/aws.go index e579bcde1..32f7e4754 100644 --- a/helper/tests/aws.go +++ b/helper/tests/aws.go @@ -1,10 +1,11 @@ package testshelper import ( + "testing" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" awscommon "github.com/hashicorp/packer/builder/amazon/common" - "testing" ) type AWSHelper struct { diff --git a/helper/tests/core.go b/helper/tests/core.go index 05af712de..91ccef4de 100644 --- a/helper/tests/core.go +++ b/helper/tests/core.go @@ -2,13 +2,14 @@ package testshelper import ( "bytes" + "os" + "testing" + amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/packer" fileprovisioner "github.com/hashicorp/packer/provisioner/file" "github.com/hashicorp/packer/provisioner/shell" - "os" - "testing" ) // fileExists returns true if the filename is found diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index 02508976d..ee25a7a67 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -1,12 +1,13 @@ package shell_test import ( - "github.com/hashicorp/go-uuid" - "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/helper/tests" "os" "path/filepath" "testing" + + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/packer/command" + "github.com/hashicorp/packer/helper/tests" ) func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { From 5a8c628880a881fa74b552ba16f70ca61f4a941e Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 17:50:05 +0100 Subject: [PATCH 10/11] Add acc test validation to avoid running with unit tests --- helper/tests/core.go | 20 +++++++++++++++++++ provisioner/shell/provisioner_acc_test.go | 2 ++ .../test-fixtures/shell-provisioner.json | 1 + 3 files changed, 23 insertions(+) diff --git a/helper/tests/core.go b/helper/tests/core.go index 91ccef4de..f48d92a75 100644 --- a/helper/tests/core.go +++ b/helper/tests/core.go @@ -2,6 +2,7 @@ package testshelper import ( "bytes" + "fmt" "os" "testing" @@ -65,3 +66,22 @@ func FatalCommand(t *testing.T, m command.Meta) { out.String(), err.String()) } + +const TestEnvVar = "PACKER_ACC" + +func AccTestPreValidate(t *testing.T) { + // We only run acceptance tests if an env var is set because they're + // slow and generally require some outside configuration. + if os.Getenv(TestEnvVar) == "" { + t.Skip(fmt.Sprintf( + "Acceptance tests skipped unless env '%s' set", + TestEnvVar)) + return + } + + // We require verbose mode so that the user knows what is going on. + if !testing.Verbose() { + t.Fatal("Acceptance tests must be run with the -v flag on tests") + return + } +} diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index ee25a7a67..889eaa56f 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -11,6 +11,8 @@ import ( ) func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) { + testshelper.AccTestPreValidate(t) + UUID, _ := uuid.GenerateUUID() os.Setenv("PACKER_RUN_UUID", UUID) c := &command.BuildCommand{ diff --git a/provisioner/shell/test-fixtures/shell-provisioner.json b/provisioner/shell/test-fixtures/shell-provisioner.json index df3604670..63ef3b0bb 100644 --- a/provisioner/shell/test-fixtures/shell-provisioner.json +++ b/provisioner/shell/test-fixtures/shell-provisioner.json @@ -7,6 +7,7 @@ "region": "us-east-1", "ssh_username": "ubuntu", "source_ami": "ami-0568456c", + "force_deregister" : true, "tags": { "packer-test": "true" } From 112d4daa3d3f7a51a8199a8ce90056dc59eb77bb Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 13 Mar 2020 17:52:33 +0100 Subject: [PATCH 11/11] Fix linter --- provisioner/shell/provisioner_acc_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index 889eaa56f..e25c97ef8 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/go-uuid" "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/helper/tests" + testshelper "github.com/hashicorp/packer/helper/tests" ) func TestBuildShellProvisionerWithBuildVariablesSharing(t *testing.T) {