add test for error-cleanup provisioner, and fix tests by fixing null builder to use an actual none communicator instead of skipping communicator generation altogether

This commit is contained in:
Megan Marsh 2019-09-25 13:38:12 -07:00
parent 5bd8fee708
commit 0683bc409b
9 changed files with 74 additions and 13 deletions

View File

@ -29,7 +29,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
steps := []multistep.Step{} steps := []multistep.Step{}
if b.config.CommConfig.Type != "none" {
steps = append(steps, steps = append(steps,
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.CommConfig, Config: &b.config.CommConfig,
@ -37,7 +36,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
SSHConfig: b.config.CommConfig.SSHConfigFunc(), SSHConfig: b.config.CommConfig.SSHConfigFunc(),
}, },
) )
}
steps = append(steps, steps = append(steps,
new(common.StepProvision), new(common.StepProvision),

View File

@ -0,0 +1,29 @@
package command
import (
"path/filepath"
"testing"
)
func TestBuildWithCleanupScript(t *testing.T) {
c := &BuildCommand{
Meta: testMetaFile(t),
}
args := []string{
"-parallel=false",
filepath.Join(testFixture("cleanup-script"), "template.json"),
}
defer cleanup()
// build should exit with error code!
if code := c.Run(args); code == 0 {
fatalCommand(t, c.Meta)
}
if !fileExists("ducky.txt") {
t.Errorf("Expected to find ducky.txt")
}
}

View File

@ -10,8 +10,11 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/hashicorp/packer/builder/file" "github.com/hashicorp/packer/builder/file"
"github.com/hashicorp/packer/builder/null"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
shell_local "github.com/hashicorp/packer/post-processor/shell-local" shell_local "github.com/hashicorp/packer/post-processor/shell-local"
"github.com/hashicorp/packer/provisioner/shell"
sl "github.com/hashicorp/packer/provisioner/shell-local"
) )
func TestBuildOnlyFileCommaFlags(t *testing.T) { func TestBuildOnlyFileCommaFlags(t *testing.T) {
@ -176,7 +179,18 @@ func fileExists(filename string) bool {
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
components := packer.ComponentFinder{ components := packer.ComponentFinder{
Builder: func(n string) (packer.Builder, error) { Builder: func(n string) (packer.Builder, error) {
if n == "file" {
return &file.Builder{}, nil return &file.Builder{}, nil
}
return &null.Builder{}, nil
},
Provisioner: func(n string) (packer.Provisioner, error) {
if n == "shell" {
return &shell.Provisioner{}, nil
} else if n == "shell-local" {
return &sl.Provisioner{}, nil
}
return nil, fmt.Errorf("requested provisioner not implemented in this test")
}, },
PostProcessor: func(n string) (packer.PostProcessor, error) { PostProcessor: func(n string) (packer.PostProcessor, error) {
return &shell_local.PostProcessor{}, nil return &shell_local.PostProcessor{}, nil
@ -212,6 +226,7 @@ func cleanup() {
os.RemoveAll("fuchsias.txt") os.RemoveAll("fuchsias.txt")
os.RemoveAll("lilas.txt") os.RemoveAll("lilas.txt")
os.RemoveAll("campanules.txt") os.RemoveAll("campanules.txt")
os.RemoveAll("ducky.txt")
} }
func TestBuildCommand_ParseArgs(t *testing.T) { func TestBuildCommand_ParseArgs(t *testing.T) {

View File

@ -0,0 +1,18 @@
{
"builders": [
{
"type": "null",
"communicator": "none"
}
],
"provisioners": [
{
"type": "shell-local",
"inline": ["exit 2"]
}
],
"on-error-script": {
"type": "shell-local",
"inline": ["echo 'rubber ducky'> ducky.txt"]
}
}

1
go.mod
View File

@ -97,6 +97,7 @@ require (
github.com/mitchellh/go-fs v0.0.0-20180402234041-7b48fa161ea7 github.com/mitchellh/go-fs v0.0.0-20180402234041-7b48fa161ea7
github.com/mitchellh/go-homedir v1.0.0 github.com/mitchellh/go-homedir v1.0.0
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed
github.com/mitchellh/gox v1.0.1 // indirect
github.com/mitchellh/iochan v1.0.0 github.com/mitchellh/iochan v1.0.0
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc
github.com/mitchellh/panicwrap v0.0.0-20170106182340-fce601fe5557 github.com/mitchellh/panicwrap v0.0.0-20170106182340-fce601fe5557

3
go.sum
View File

@ -206,6 +206,7 @@ github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdv
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE= github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0= github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
@ -301,6 +302,8 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed h1:FI2NIv6fpef6BQl2u3IZX/Cj20tfypRF4yd+uaHOMtI= github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed h1:FI2NIv6fpef6BQl2u3IZX/Cj20tfypRF4yd+uaHOMtI=
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed/go.mod h1:3rdaFaCv4AyBgu5ALFM0+tSuHrBh6v692nyQe3ikrq0= github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed/go.mod h1:3rdaFaCv4AyBgu5ALFM0+tSuHrBh6v692nyQe3ikrq0=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4=
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY= github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=

View File

@ -45,7 +45,6 @@ func (h *DispatchHook) Run(ctx context.Context, name string, ui Ui, comm Communi
if err := ctx.Err(); err != nil { if err := ctx.Err(); err != nil {
return err return err
} }
if err := hook.Run(ctx, name, ui, comm, data); err != nil { if err := hook.Run(ctx, name, ui, comm, data); err != nil {
return err return err
} }

View File

@ -50,7 +50,6 @@ func (h *ProvisionHook) Run(ctx context.Context, name string, ui Ui, comm Commun
"`communicator` config was set to \"none\". If you have any provisioners\n" + "`communicator` config was set to \"none\". If you have any provisioners\n" +
"then a communicator is required. Please fix this to continue.") "then a communicator is required. Please fix this to continue.")
} }
for _, p := range h.Provisioners { for _, p := range h.Provisioners {
ts := CheckpointReporter.AddSpan(p.TypeName, "provisioner", p.Config) ts := CheckpointReporter.AddSpan(p.TypeName, "provisioner", p.Config)

View File

@ -245,7 +245,6 @@ func (r *rawTemplate) Template() (*Template, error) {
} }
// Gather the on-error-script // Gather the on-error-script
log.Printf("r.CleanupProvisioner is %#v", r.CleanupProvisioner)
if r.CleanupProvisioner != nil { if r.CleanupProvisioner != nil {
var p Provisioner var p Provisioner
if err := r.decoder(&p, nil).Decode(r.CleanupProvisioner); err != nil { if err := r.decoder(&p, nil).Decode(r.CleanupProvisioner); err != nil {