fix ansible acceptance tests not to import packer core; add override to Builder acctest TestCase so you can supply your own provisionerstore.

This commit is contained in:
Megan Marsh 2020-12-03 17:01:10 -08:00
parent 823ff34434
commit bc7dae2dff
2 changed files with 44 additions and 64 deletions

View File

@ -49,6 +49,15 @@ type TestCase struct {
// If SkipArtifactTeardown is true, we will not attempt to destroy the
// artifact created in this test run.
SkipArtifactTeardown bool
// If set, overrides the default provisioner store with custom provisioners.
// This can be useful for running acceptance tests for a particular
// provisioner using a specific builder.
// Default provisioner store:
// ProvisionerStore: packersdk.MapOfProvisioner{
// "shell": func() (packersdk.Provisioner, error) { return &shellprovisioner.Provisioner{}, nil },
// "file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil },
// },
ProvisionerStore packersdk.MapOfProvisioner
}
// TestCheckFunc is the callback used for Check in TestStep.
@ -112,6 +121,12 @@ func Test(t TestT, c TestCase) {
return
}
if c.ProvisionerStore == nil {
c.ProvisionerStore = packersdk.MapOfProvisioner{
"shell": func() (packersdk.Provisioner, error) { return &shellprovisioner.Provisioner{}, nil },
"file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil },
}
}
// Build the core
log.Printf("[DEBUG] Initializing core...")
core := packer.NewCore(&packer.CoreConfig{
@ -125,10 +140,7 @@ func Test(t TestT, c TestCase) {
return nil, nil
},
},
ProvisionerStore: packersdk.MapOfProvisioner{
"shell": func() (packersdk.Provisioner, error) { return &shellprovisioner.Provisioner{}, nil },
"file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil },
},
ProvisionerStore: c.ProvisionerStore,
},
Template: tpl,
})

View File

@ -12,9 +12,8 @@ import (
"os/exec"
"github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/packer"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/template"
"github.com/hashicorp/packer/provisioner/file"
)
@ -329,69 +328,38 @@ func testProvisionerProvisionDockerWithPlaybookFiles(t *testing.T, templateStrin
t.Skip("This test is only run with PACKER_ACC=1")
}
ui := packersdk.TestUi(t)
tpl, err := template.Parse(strings.NewReader(templateString))
if err != nil {
t.Fatalf("Unable to parse config: %s", err)
}
// Check if docker executable can be found.
_, err = exec.LookPath("docker")
// this should be a precheck
cmd := exec.Command("docker", "-v")
err := cmd.Run()
if err != nil {
t.Error("docker command not found; please make sure docker is installed")
}
// Setup the builder
builder := &docker.Builder{}
_, warnings, err := builder.Prepare(tpl.Builders["docker"].Config)
if err != nil {
t.Fatalf("Error preparing configuration %s", err)
}
if len(warnings) > 0 {
t.Fatal("Encountered configuration warnings; aborting")
}
ansible := &Provisioner{}
err = ansible.Prepare(tpl.Provisioners[0].Config)
if err != nil {
t.Fatalf("Error preparing ansible-local provisioner: %s", err)
}
download := &file.Provisioner{}
err = download.Prepare(tpl.Provisioners[1].Config)
if err != nil {
t.Fatalf("Error preparing download: %s", err)
}
// Add hooks so the provisioners run during the build
hooks := map[string][]packersdk.Hook{}
hooks[packersdk.HookProvision] = []packersdk.Hook{
&packer.ProvisionHook{
Provisioners: []*packer.HookedProvisioner{
{Provisioner: ansible, Config: nil, TypeName: ""},
{Provisioner: download, Config: nil, TypeName: ""},
},
},
}
hook := &packersdk.DispatchHook{Mapping: hooks}
artifact, err := builder.Run(context.Background(), ui, hook)
if err != nil {
t.Fatalf("Error running build %s", err)
}
defer os.Remove("hello_world")
defer artifact.Destroy()
builderT.Test(t, builderT.TestCase{
Builder: &docker.Builder{},
Template: templateString,
Check: func(a []packersdk.Artifact) error {
actualContent, err := ioutil.ReadFile("hello_world")
if err != nil {
t.Fatalf("Expected file not found: %s", err)
return fmt.Errorf("Expected file not found: %s", err)
}
expectedContent := "Hello world!"
if string(actualContent) != expectedContent {
t.Fatalf(`Unexpected file content: expected="%s", actual="%s"`, expectedContent, actualContent)
return fmt.Errorf(`Unexpected file content: expected="%s", actual="%s"`, expectedContent, actualContent)
}
return nil
},
Teardown: func() error {
os.Remove("hello_world")
return nil
},
ProvisionerStore: packersdk.MapOfProvisioner{
"ansible-local": func() (packersdk.Provisioner, error) { return &Provisioner{}, nil },
"file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil },
},
})
}
func assertPlaybooksExecuted(comm *communicatorMock, playbooks []string) {
@ -477,7 +445,7 @@ const playbookFilesDockerTemplate = `
{
"builders": [
{
"type": "docker",
"type": "test",
"image": "williamyeh/ansible:centos7",
"discard": true
}
@ -504,7 +472,7 @@ const playbookFilesWithPlaybookDirDockerTemplate = `
{
"builders": [
{
"type": "docker",
"type": "test",
"image": "williamyeh/ansible:centos7",
"discard": true
}