Merge pull request #10340 from hashicorp/acceptance_test_fix

Modify vmware_vmx and docker acceptance tests to use builderT framework
This commit is contained in:
Megan Marsh 2020-12-04 09:28:38 -08:00 committed by GitHub
commit 4c0adb7d67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
46 changed files with 481 additions and 684 deletions

View File

@ -447,7 +447,7 @@ For the Shell provisioner this is:
os.Setenv("PACKER_RUN_UUID", UUID) os.Setenv("PACKER_RUN_UUID", UUID)
} }
file := "provisioner.shell." + UUID + ".txt" file := "provisioner.shell." + UUID + ".txt"
defer testshelper.CleanupFiles(file) defer testutils.CleanupFiles(file)
// Run build // Run build
// All provisioner acc tests should contain this code and validation // All provisioner acc tests should contain this code and validation
@ -462,7 +462,7 @@ For the Shell provisioner this is:
} }
// Any other extra specific validation // Any other extra specific validation
if !testshelper.FileExists(file) { if !testutils.FileExists(file) {
return fmt.Errorf("Expected to find %s", file) return fmt.Errorf("Expected to find %s", file)
} }
return nil return nil

View File

@ -144,7 +144,7 @@ provisioners-acctest: install-build-deps generate
ACC_TEST_BUILDERS=$(ACC_TEST_BUILDERS) ACC_TEST_PROVISIONERS=$(ACC_TEST_PROVISIONERS) go test ./provisioner/... -timeout=1h ACC_TEST_BUILDERS=$(ACC_TEST_BUILDERS) ACC_TEST_PROVISIONERS=$(ACC_TEST_PROVISIONERS) go test ./provisioner/... -timeout=1h
# testacc runs acceptance tests # testacc runs acceptance tests
testacc: install-build-deps generate ## Run acceptance tests testacc: # install-build-deps generate ## Run acceptance tests
@echo "WARN: Acceptance tests will take a long time to run and may cost money. Ctrl-C if you want to cancel." @echo "WARN: Acceptance tests will take a long time to run and may cost money. Ctrl-C if you want to cancel."
PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) $(TESTARGS) -timeout=120m PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) $(TESTARGS) -timeout=120m

View File

@ -8,7 +8,7 @@ import (
"testing" "testing"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
) )

View File

@ -1,7 +1,8 @@
package testshelper package amazon_acc
import ( import (
"fmt" "fmt"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
awscommon "github.com/hashicorp/packer/builder/amazon/common" awscommon "github.com/hashicorp/packer/builder/amazon/common"

View File

@ -12,8 +12,6 @@ import (
amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
testshelper "github.com/hashicorp/packer/helper/tests"
) )
type AmazonEBSAccTest struct{} type AmazonEBSAccTest struct{}
@ -47,7 +45,7 @@ func (s *AmazonEBSAccTest) GetConfigs() (map[string]string, error) {
} }
func (s *AmazonEBSAccTest) CleanUp() error { func (s *AmazonEBSAccTest) CleanUp() error {
helper := testshelper.AWSHelper{ helper := AWSHelper{
Region: "us-east-1", Region: "us-east-1",
AMIName: "packer-acc-test", AMIName: "packer-acc-test",
} }

View File

@ -12,7 +12,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/builder/amazon/common" "github.com/hashicorp/packer/builder/amazon/common"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
) )

View File

@ -8,7 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/builder/amazon/common" "github.com/hashicorp/packer/builder/amazon/common"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
) )

View File

@ -25,7 +25,7 @@ import (
"fmt" "fmt"
"os" "os"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST" const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST"

View File

@ -28,7 +28,7 @@ package dtl
import ( import (
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST" const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST"

View File

@ -7,7 +7,7 @@ import (
"testing" "testing"
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )

View File

@ -35,5 +35,8 @@ func (a *ExportArtifact) State(name string) interface{} {
} }
func (a *ExportArtifact) Destroy() error { func (a *ExportArtifact) Destroy() error {
if a.path != "" {
return os.Remove(a.path) return os.Remove(a.path)
} }
return nil
}

View File

@ -1,99 +1,105 @@
package docker package docker
import ( import (
"context"
"crypto/sha256" "crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings"
"testing" "testing"
"github.com/hashicorp/packer/packer" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/template"
"github.com/hashicorp/packer/provisioner/file"
"github.com/hashicorp/packer/provisioner/shell"
) )
// RenderConfig helps create dynamic packer template configs for parsing by
// builderT without having to write the config to a file.
func RenderConfig(builderConfig map[string]interface{}, provisionerConfig []map[string]interface{}) string {
// set up basic build template
t := map[string][]map[string]interface{}{
"builders": {
// Setup basic docker config
map[string]interface{}{
"type": "test",
"image": "ubuntu",
"discard": true,
},
},
"provisioners": []map[string]interface{}{},
}
// apply special builder overrides
for k, v := range builderConfig {
t["builders"][0][k] = v
}
// Apply special provisioner overrides
t["provisioners"] = append(t["provisioners"], provisionerConfig...)
j, _ := json.Marshal(t)
return string(j)
}
// TestUploadDownload verifies that basic upload / download functionality works // TestUploadDownload verifies that basic upload / download functionality works
func TestUploadDownload(t *testing.T) { func TestUploadDownload(t *testing.T) {
ui := packersdk.TestUi(t)
tpl, err := template.Parse(strings.NewReader(dockerBuilderConfig))
if err != nil {
t.Fatalf("Unable to parse config: %s", err)
}
if os.Getenv("PACKER_ACC") == "" { if os.Getenv("PACKER_ACC") == "" {
t.Skip("This test is only run with PACKER_ACC=1") t.Skip("This test is only run with PACKER_ACC=1")
} }
dockerBuilderExtraConfig := map[string]interface{}{
"run_command": []string{"-d", "-i", "-t", "{{.Image}}", "/bin/sh"},
}
dockerProvisionerConfig := []map[string]interface{}{
{
"type": "file",
"source": "test-fixtures/onecakes/strawberry",
"destination": "/strawberry-cake",
},
{
"type": "file",
"source": "/strawberry-cake",
"destination": "my-strawberry-cake",
"direction": "download",
},
}
configString := RenderConfig(dockerBuilderExtraConfig, dockerProvisionerConfig)
// this should be a precheck
cmd := exec.Command("docker", "-v") cmd := exec.Command("docker", "-v")
cmd.Run() err := cmd.Run()
if !cmd.ProcessState.Success() { if err != nil {
t.Error("docker command not found; please make sure docker is installed") t.Error("docker command not found; please make sure docker is installed")
} }
// Setup the builder builderT.Test(t, builderT.TestCase{
builder := &Builder{} Builder: &Builder{},
_, warnings, err := builder.Prepare(tpl.Builders["docker"].Config) Template: configString,
if err != nil { Check: func(a []packersdk.Artifact) error {
t.Fatalf("Error preparing configuration %s", err)
}
if len(warnings) > 0 {
t.Fatal("Encountered configuration warnings; aborting")
}
// Setup the provisioners
upload := &file.Provisioner{}
err = upload.Prepare(tpl.Provisioners[0].Config)
if err != nil {
t.Fatalf("Error preparing upload: %s", err)
}
download := &file.Provisioner{}
err = download.Prepare(tpl.Provisioners[1].Config)
if err != nil {
t.Fatalf("Error preparing download: %s", err)
}
// Preemptive cleanup. Honestly I don't know why you would want to get rid
// of my strawberry cake. It's so tasty! Do you not like cake? Are you a
// cake-hater? Or are you keeping all the cake all for yourself? So selfish!
defer os.Remove("my-strawberry-cake")
// 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: upload, Config: nil, TypeName: ""},
{Provisioner: download, Config: nil, TypeName: ""},
},
},
}
hook := &packersdk.DispatchHook{Mapping: hooks}
// Run things
artifact, err := builder.Run(context.Background(), ui, hook)
if err != nil {
t.Fatalf("Error running build %s", err)
}
// Preemptive cleanup
defer artifact.Destroy()
// Verify that the thing we downloaded is the same thing we sent up. // Verify that the thing we downloaded is the same thing we sent up.
// Complain loudly if it isn't. // Complain loudly if it isn't.
inputFile, err := ioutil.ReadFile("test-fixtures/onecakes/strawberry") inputFile, err := ioutil.ReadFile("test-fixtures/onecakes/strawberry")
if err != nil { if err != nil {
t.Fatalf("Unable to read input file: %s", err) return fmt.Errorf("Unable to read input file: %s", err)
} }
outputFile, err := ioutil.ReadFile("my-strawberry-cake") outputFile, err := ioutil.ReadFile("my-strawberry-cake")
if err != nil { if err != nil {
t.Fatalf("Unable to read output file: %s", err) return fmt.Errorf("Unable to read output file: %s", err)
} }
if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) { if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) {
t.Fatalf("Input and output files do not match\n"+ return fmt.Errorf("Input and output files do not match\n"+
"Input:\n%s\nOutput:\n%s\n", inputFile, outputFile) "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile)
} }
return nil
},
Teardown: func() error {
// Cleanup. Honestly I don't know why you would want to get rid
// of my strawberry cake. It's so tasty! Do you not like cake? Are you a
// cake-hater? Or are you keeping all the cake all for yourself? So selfish!
os.Remove("my-strawberry-cake")
return nil
},
})
} }
// TestLargeDownload verifies that files are the appropriate size after being // TestLargeDownload verifies that files are the appropriate size after being
@ -101,74 +107,47 @@ func TestUploadDownload(t *testing.T) {
// need to use github.com/cbednarski/rerun to verify since this problem occurs // need to use github.com/cbednarski/rerun to verify since this problem occurs
// only intermittently. // only intermittently.
func TestLargeDownload(t *testing.T) { func TestLargeDownload(t *testing.T) {
ui := packersdk.TestUi(t)
tpl, err := template.Parse(strings.NewReader(dockerLargeBuilderConfig))
if err != nil {
t.Fatalf("Unable to parse config: %s", err)
}
if os.Getenv("PACKER_ACC") == "" { if os.Getenv("PACKER_ACC") == "" {
t.Skip("This test is only run with PACKER_ACC=1") t.Skip("This test is only run with PACKER_ACC=1")
} }
dockerProvisionerConfig := []map[string]interface{}{
{
"type": "shell",
"inline": []string{
"dd if=/dev/urandom of=/tmp/cupcake bs=1M count=2",
"dd if=/dev/urandom of=/tmp/bigcake bs=1M count=100",
"sync",
"md5sum /tmp/cupcake /tmp/bigcake",
},
},
{
"type": "file",
"source": "/tmp/cupcake",
"destination": "cupcake",
"direction": "download",
},
{
"type": "file",
"source": "/tmp/bigcake",
"destination": "bigcake",
"direction": "download",
},
}
configString := RenderConfig(map[string]interface{}{}, dockerProvisionerConfig)
// this should be a precheck
cmd := exec.Command("docker", "-v") cmd := exec.Command("docker", "-v")
cmd.Run() err := cmd.Run()
if !cmd.ProcessState.Success() { if err != nil {
t.Error("docker command not found; please make sure docker is installed") t.Error("docker command not found; please make sure docker is installed")
} }
// Setup the builder builderT.Test(t, builderT.TestCase{
builder := &Builder{} Builder: &Builder{},
_, warnings, err := builder.Prepare(tpl.Builders["docker"].Config) Template: configString,
if err != nil { Check: func(a []packersdk.Artifact) error {
t.Fatalf("Error preparing configuration %s", err)
}
if len(warnings) > 0 {
t.Fatal("Encountered configuration warnings; aborting")
}
// Setup the provisioners
shell := &shell.Provisioner{}
err = shell.Prepare(tpl.Provisioners[0].Config)
if err != nil {
t.Fatalf("Error preparing shell provisioner: %s", err)
}
downloadCupcake := &file.Provisioner{}
err = downloadCupcake.Prepare(tpl.Provisioners[1].Config)
if err != nil {
t.Fatalf("Error preparing downloadCupcake: %s", err)
}
downloadBigcake := &file.Provisioner{}
err = downloadBigcake.Prepare(tpl.Provisioners[2].Config)
if err != nil {
t.Fatalf("Error preparing downloadBigcake: %s", err)
}
// Preemptive cleanup.
defer os.Remove("cupcake")
defer os.Remove("bigcake")
// 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: shell, Config: nil, TypeName: ""},
{Provisioner: downloadCupcake, Config: nil, TypeName: ""},
{Provisioner: downloadBigcake, Config: nil, TypeName: ""},
},
},
}
hook := &packersdk.DispatchHook{Mapping: hooks}
// Run things
artifact, err := builder.Run(context.Background(), ui, hook)
if err != nil {
t.Fatalf("Error running build %s", err)
}
// Preemptive cleanup
defer artifact.Destroy()
// Verify that the things we downloaded are the right size. Complain loudly // Verify that the things we downloaded are the right size. Complain loudly
// if they are not. // if they are not.
// //
@ -200,177 +179,61 @@ func TestLargeDownload(t *testing.T) {
// t.Fatalf("Input and output files do not match\n"+ // t.Fatalf("Input and output files do not match\n"+
// "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile) // "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile)
// } // }
return nil
},
Teardown: func() error {
os.Remove("cupcake")
os.Remove("bigcake")
return nil
},
})
} }
// TestFixUploadOwner verifies that owner of uploaded files is the user the container is running as. // TestFixUploadOwner verifies that owner of uploaded files is the user the container is running as.
func TestFixUploadOwner(t *testing.T) { func TestFixUploadOwner(t *testing.T) {
ui := packersdk.TestUi(t)
tpl, err := template.Parse(strings.NewReader(testFixUploadOwnerTemplate))
if err != nil {
t.Fatalf("Unable to parse config: %s", err)
}
if os.Getenv("PACKER_ACC") == "" { if os.Getenv("PACKER_ACC") == "" {
t.Skip("This test is only run with PACKER_ACC=1") t.Skip("This test is only run with PACKER_ACC=1")
} }
cmd := exec.Command("docker", "-v") cmd := exec.Command("docker", "-v")
cmd.Run() err := cmd.Run()
if !cmd.ProcessState.Success() { if err != nil {
t.Error("docker command not found; please make sure docker is installed") t.Error("docker command not found; please make sure docker is installed")
} }
// Setup the builder dockerBuilderExtraConfig := map[string]interface{}{
builder := &Builder{} "run_command": []string{"-d", "-i", "-t", "-u", "42", "{{.Image}}", "/bin/sh"},
_, 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")
} }
// Setup the provisioners testFixUploadOwnerProvisionersTemplate := []map[string]interface{}{
fileProvisioner := &file.Provisioner{}
err = fileProvisioner.Prepare(tpl.Provisioners[0].Config)
if err != nil {
t.Fatalf("Error preparing single file upload provisioner: %s", err)
}
dirProvisioner := &file.Provisioner{}
err = dirProvisioner.Prepare(tpl.Provisioners[1].Config)
if err != nil {
t.Fatalf("Error preparing directory upload provisioner: %s", err)
}
shellProvisioner := &shell.Provisioner{}
err = shellProvisioner.Prepare(tpl.Provisioners[2].Config)
if err != nil {
t.Fatalf("Error preparing shell provisioner: %s", err)
}
verifyProvisioner := &shell.Provisioner{}
err = verifyProvisioner.Prepare(tpl.Provisioners[3].Config)
if err != nil {
t.Fatalf("Error preparing verification provisioner: %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: fileProvisioner, Config: nil, TypeName: ""},
{Provisioner: dirProvisioner, Config: nil, TypeName: ""},
{Provisioner: shellProvisioner, Config: nil, TypeName: ""},
{Provisioner: verifyProvisioner, 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 artifact.Destroy()
}
const dockerBuilderConfig = `
{
"builders": [
{
"type": "docker",
"image": "ubuntu",
"discard": true,
"run_command": ["-d", "-i", "-t", "{{.Image}}", "/bin/sh"]
}
],
"provisioners": [
{ {
"type": "file", "type": "file",
"source": "test-fixtures/onecakes/strawberry", "source": "test-fixtures/onecakes/strawberry",
"destination": "/strawberry-cake" "destination": "/tmp/strawberry-cake",
},
{
"type": "file",
"source": "/strawberry-cake",
"destination": "my-strawberry-cake",
"direction": "download"
}
]
}
`
const dockerLargeBuilderConfig = `
{
"builders": [
{
"type": "docker",
"image": "ubuntu",
"discard": true
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"dd if=/dev/urandom of=/tmp/cupcake bs=1M count=2",
"dd if=/dev/urandom of=/tmp/bigcake bs=1M count=100",
"sync",
"md5sum /tmp/cupcake /tmp/bigcake"
]
},
{
"type": "file",
"source": "/tmp/cupcake",
"destination": "cupcake",
"direction": "download"
},
{
"type": "file",
"source": "/tmp/bigcake",
"destination": "bigcake",
"direction": "download"
}
]
}
`
const testFixUploadOwnerTemplate = `
{
"builders": [
{
"type": "docker",
"image": "ubuntu",
"discard": true,
"run_command": ["-d", "-i", "-t", "-u", "42", "{{.Image}}", "/bin/sh"]
}
],
"provisioners": [
{
"type": "file",
"source": "test-fixtures/onecakes/strawberry",
"destination": "/tmp/strawberry-cake"
}, },
{ {
"type": "file", "type": "file",
"source": "test-fixtures/manycakes", "source": "test-fixtures/manycakes",
"destination": "/tmp/" "destination": "/tmp/",
}, },
{ {
"type": "shell", "type": "shell",
"inline": "touch /tmp/testUploadOwner" "inline": "touch /tmp/testUploadOwner",
}, },
{ {
"type": "shell", "type": "shell",
"inline": [ "inline": []string{
"[ $(stat -c %u /tmp/strawberry-cake) -eq 42 ] || (echo 'Invalid owner of /tmp/strawberry-cake' && exit 1)", "[ $(stat -c %u /tmp/strawberry-cake) -eq 42 ] || (echo 'Invalid owner of /tmp/strawberry-cake' && exit 1)",
"[ $(stat -c %u /tmp/testUploadOwner) -eq 42 ] || (echo 'Invalid owner of /tmp/testUploadOwner' && exit 1)", "[ $(stat -c %u /tmp/testUploadOwner) -eq 42 ] || (echo 'Invalid owner of /tmp/testUploadOwner' && exit 1)",
"find /tmp/manycakes | xargs -n1 -IFILE /bin/sh -c '[ $(stat -c %u FILE) -eq 42 ] || (echo \"Invalid owner of FILE\" && exit 1)'" "find /tmp/manycakes | xargs -n1 -IFILE /bin/sh -c '[ $(stat -c %u FILE) -eq 42 ] || (echo \"Invalid owner of FILE\" && exit 1)'",
] },
},
} }
]
configString := RenderConfig(dockerBuilderExtraConfig, testFixUploadOwnerProvisionersTemplate)
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: configString,
})
} }
`

View File

@ -5,7 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
) )

View File

@ -4,7 +4,7 @@ import (
"os" "os"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -4,7 +4,7 @@ import (
"os" "os"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -4,7 +4,7 @@ import (
"os" "os"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -4,7 +4,7 @@ import (
"os" "os"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -4,7 +4,7 @@ package bsu
import ( import (
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -3,7 +3,7 @@ package bsusurrogate
import ( import (
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -4,7 +4,7 @@ package bsuvolume
import ( import (
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -4,7 +4,7 @@ import (
"os" "os"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -9,7 +9,7 @@ import (
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_validateRegion(t *testing.T) { func TestBuilderAcc_validateRegion(t *testing.T) {

View File

@ -11,9 +11,8 @@ import (
"github.com/hashicorp/packer/builder/virtualbox/iso" "github.com/hashicorp/packer/builder/virtualbox/iso"
"github.com/hashicorp/packer/packer-plugin-sdk/acctest/testutils"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
testshelper "github.com/hashicorp/packer/helper/tests"
) )
type VirtualBoxISOAccTest struct{} type VirtualBoxISOAccTest struct{}
@ -34,8 +33,8 @@ func (v *VirtualBoxISOAccTest) GetConfigs() (map[string]string, error) {
} }
func (v *VirtualBoxISOAccTest) CleanUp() error { func (v *VirtualBoxISOAccTest) CleanUp() error {
testshelper.CleanupFiles("virtualbox-iso-packer-acc-test") testutils.CleanupFiles("virtualbox-iso-packer-acc-test")
testshelper.CleanupFiles("packer_cache") testutils.CleanupFiles("packer_cache")
return nil return nil
} }

View File

@ -5,7 +5,7 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -5,7 +5,7 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {

View File

@ -2,81 +2,39 @@ package iso
import ( import (
"bytes" "bytes"
"context" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math"
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/packer/packer" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/template" "github.com/hashicorp/packer/packer-plugin-sdk/tmp"
"github.com/hashicorp/packer/provisioner/shell"
) )
var vmxTestBuilderConfig = map[string]string{
"type": `"vmware-iso"`,
"iso_url": `"https://archive.org/download/ut-ttylinux-i686-12.6/ut-ttylinux-i686-12.6.iso"`,
"iso_checksum": `"md5:43c1feeae55a44c6ef694b8eb18408a6"`,
"ssh_username": `"root"`,
"ssh_password": `"password"`,
"ssh_wait_timeout": `"45s"`,
"boot_command": `["<enter><wait5><wait10>","root<enter><wait>password<enter><wait>","udhcpc<enter><wait>"]`,
"shutdown_command": `"/sbin/shutdown -h; exit 0"`,
"ssh_key_exchange_algorithms": `["diffie-hellman-group1-sha1"]`,
}
var vmxTestProvisionerConfig = map[string]string{
"type": `"shell"`,
"inline": `["echo hola mundo"]`,
}
const vmxTestTemplate string = `{"builders":[{%s}],"provisioners":[{%s}]}` const vmxTestTemplate string = `{"builders":[{%s}],"provisioners":[{%s}]}`
func tmpnam(prefix string) string { func createFloppyOutput(prefix string) (string, map[string]string, error) {
var path string f, err := tmp.File(prefix)
var err error
const length = 16
dir := os.TempDir()
max := int(math.Pow(2, float64(length)))
// FIXME use ioutil.TempFile() or at least mimic implementation, this could loop forever
n, err := rand.Intn(max), nil
for path = filepath.Join(dir, prefix+strconv.Itoa(n)); err == nil; _, err = os.Stat(path) {
n = rand.Intn(max)
path = filepath.Join(dir, prefix+strconv.Itoa(n))
}
return path
}
func createFloppyOutput(prefix string) (string, string, error) {
output := tmpnam(prefix)
f, err := os.Create(output)
if err != nil { if err != nil {
return "", "", fmt.Errorf("Unable to create empty %s: %s", output, err) return "", map[string]string{}, fmt.Errorf("unable to create temp file")
} }
f.Close() f.Close()
vmxData := []string{ output := f.Name()
`"floppy0.present":"TRUE"`,
`"floppy0.fileType":"file"`,
`"floppy0.clientDevice":"FALSE"`,
`"floppy0.fileName":"%s"`,
`"floppy0.startConnected":"TRUE"`,
}
outputFile := strings.Replace(output, "\\", "\\\\", -1) outputFile := strings.Replace(output, "\\", "\\\\", -1)
vmxString := fmt.Sprintf("{"+strings.Join(vmxData, ",")+"}", outputFile) vmxData := map[string]string{
return output, vmxString, nil "floppy0.present": "TRUE",
"floppy0.fileType": "file",
"floppy0.clientDevice": "FALSE",
"floppy0.fileName": outputFile,
"floppy0.startConnected": "TRUE",
}
return output, vmxData, nil
} }
func readFloppyOutput(path string) (string, error) { func readFloppyOutput(path string) (string, error) {
@ -95,114 +53,42 @@ func readFloppyOutput(path string) (string, error) {
return string(data[:bytes.IndexByte(data, 0)]), nil return string(data[:bytes.IndexByte(data, 0)]), nil
} }
func setupVMwareBuild(t *testing.T, builderConfig map[string]string, provisionerConfig map[string]string) error { // RenderConfig helps create dynamic packer template configs for parsing by
ui := packersdk.TestUi(t) // builderT without having to write the config to a file.
func RenderConfig(builderConfig map[string]interface{}, provisionerConfig map[string]string) string {
// create builder config and update with user-supplied options // set up basic build template
cfgBuilder := map[string]string{} t := map[string][]map[string]interface{}{
for k, v := range vmxTestBuilderConfig { "builders": {
cfgBuilder[k] = v map[string]interface{}{
"type": "test",
"iso_url": "https://archive.org/download/ut-ttylinux-i686-12.6/ut-ttylinux-i686-12.6.iso",
"iso_checksum": "md5:43c1feeae55a44c6ef694b8eb18408a6",
"ssh_username": "root",
"ssh_password": "password",
"ssh_wait_timeout": "45s",
"boot_command": []string{"<enter><wait5><wait10>", "root<enter><wait>password<enter><wait>", "udhcpc<enter><wait>"},
"shutdown_command": "/sbin/shutdown -h; exit 0",
"ssh_key_exchange_algorithms": []string{"diffie-hellman-group1-sha1"},
},
},
"provisioners": {
map[string]interface{}{
"type": "shell",
"inline": []string{"echo hola mundo"},
},
},
} }
// apply special builder overrides
for k, v := range builderConfig { for k, v := range builderConfig {
cfgBuilder[k] = v t["builders"][0][k] = v
}
// convert our builder config into a single sprintfable string
builderLines := []string{}
for k, v := range cfgBuilder {
builderLines = append(builderLines, fmt.Sprintf(`"%s":%s`, k, v))
}
// create provisioner config and update with user-supplied options
cfgProvisioner := map[string]string{}
for k, v := range vmxTestProvisionerConfig {
cfgProvisioner[k] = v
} }
// Apply special provisioner overrides
for k, v := range provisionerConfig { for k, v := range provisionerConfig {
cfgProvisioner[k] = v t["provisioners"][0][k] = v
} }
// convert our provisioner config into a single sprintfable string j, _ := json.Marshal(t)
provisionerLines := []string{} return string(j)
for k, v := range cfgProvisioner {
provisionerLines = append(provisionerLines, fmt.Sprintf(`"%s":%s`, k, v))
}
// and now parse them into a template
configString := fmt.Sprintf(vmxTestTemplate, strings.Join(builderLines, `,`), strings.Join(provisionerLines, `,`))
tpl, err := template.Parse(strings.NewReader(configString))
if err != nil {
t.Fatalf("Unable to parse test config: %s", err)
}
// create our config to test the vmware-iso builder
components := packer.ComponentFinder{
BuilderStore: packersdk.MapOfBuilder{
"vmware-iso": func() (packersdk.Builder, error) { return &Builder{}, nil },
},
Hook: func(n string) (packersdk.Hook, error) {
return &packersdk.DispatchHook{}, nil
},
ProvisionerStore: packersdk.MapOfProvisioner{
"shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil },
},
PostProcessorStore: packersdk.MapOfPostProcessor{
"something": func() (packersdk.PostProcessor, error) { return &packer.MockPostProcessor{}, nil },
},
}
config := packer.CoreConfig{
Template: tpl,
Components: components,
}
// create a core using our template
core := packer.NewCore(&config)
err = core.Initialize()
if err != nil {
t.Fatalf("Unable to create core: %s", err)
}
// now we can prepare our build
b, err := core.Build("vmware-iso")
if err != nil {
t.Fatalf("Unable to create build: %s", err)
}
warn, err := b.Prepare()
if err != nil {
t.Fatalf("error preparing build: %v", err)
}
if len(warn) > 0 {
for _, w := range warn {
t.Logf("Configuration warning: %s", w)
}
}
// and then finally build it
artifacts, err := b.Run(context.Background(), ui)
if err != nil {
t.Fatalf("Failed to build artifact: %s", err)
}
// check to see that we only got one artifact back
if len(artifacts) == 1 {
return artifacts[0].Destroy()
}
// otherwise some number of errors happened
t.Logf("Unexpected number of artifacts returned: %d", len(artifacts))
errors := make([]error, 0)
for _, artifact := range artifacts {
if err := artifact.Destroy(); err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
t.Errorf("%d Errors returned while trying to destroy artifacts", len(errors))
return fmt.Errorf("Error while trying to destroy artifacts: %v", errors)
}
return nil
} }
func TestStepCreateVmx_SerialFile(t *testing.T) { func TestStepCreateVmx_SerialFile(t *testing.T) {
@ -210,27 +96,36 @@ func TestStepCreateVmx_SerialFile(t *testing.T) {
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.") t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
} }
tmpfile := tmpnam("SerialFileInput.") tmpfile, err := tmp.File("SerialFileInput.")
serialConfig := map[string]string{
"serial": fmt.Sprintf(`"file:%s"`, filepath.ToSlash(tmpfile)),
}
error := setupVMwareBuild(t, serialConfig, map[string]string{})
if error != nil {
t.Errorf("Unable to read file: %s", error)
}
f, err := os.Stat(tmpfile)
if err != nil { if err != nil {
t.Errorf("VMware builder did not create a file for serial port: %s", err) t.Fatalf("unable to create temp file")
}
serialConfig := map[string]interface{}{
"serial": fmt.Sprintf("file:%s", filepath.ToSlash(tmpfile.Name())),
} }
configString := RenderConfig(serialConfig, map[string]string{})
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: configString,
Check: func(a []packersdk.Artifact) error {
_, err := os.Stat(tmpfile.Name())
if err != nil {
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
}
return nil
},
Teardown: func() error {
f, _ := os.Stat(tmpfile.Name())
if f != nil { if f != nil {
if err := os.Remove(tmpfile); err != nil { if err := os.Remove(tmpfile.Name()); err != nil {
t.Fatalf("Unable to remove file %s: %s", tmpfile, err) return fmt.Errorf("Unable to remove file %s: %s", tmpfile.Name(), err)
} }
} }
return nil
},
})
} }
func TestStepCreateVmx_SerialPort(t *testing.T) { func TestStepCreateVmx_SerialPort(t *testing.T) {
@ -245,11 +140,11 @@ func TestStepCreateVmx_SerialPort(t *testing.T) {
defaultSerial = "/dev/ttyS0" defaultSerial = "/dev/ttyS0"
} }
config := map[string]string{ config := map[string]interface{}{
"serial": fmt.Sprintf(`"device:%s"`, filepath.ToSlash(defaultSerial)), "serial": fmt.Sprintf("device:%s", filepath.ToSlash(defaultSerial)),
} }
provision := map[string]string{ provision := map[string]string{
"inline": `"dmesg | egrep -o '^serial8250: ttyS1 at' > /dev/fd0"`, "inline": "dmesg | egrep -o '^serial8250: ttyS1 at' > /dev/fd0",
} }
// where to write output // where to write output
@ -257,29 +152,35 @@ func TestStepCreateVmx_SerialPort(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error creating output: %s", err) t.Fatalf("Error creating output: %s", err)
} }
defer func() {
if _, err := os.Stat(output); err == nil {
os.Remove(output)
}
}()
config["vmx_data"] = vmxData config["vmx_data"] = vmxData
t.Logf("Preparing to write output to %s", output) configString := RenderConfig(config, provision)
builderT.Test(t, builderT.TestCase{
// whee Builder: &Builder{},
err = setupVMwareBuild(t, config, provision) Template: configString,
Check: func(a []packersdk.Artifact) error {
_, err := os.Stat(output)
if err != nil { if err != nil {
t.Errorf("%s", err) return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
} }
// check the output // check the output
data, err := readFloppyOutput(output) data, err := readFloppyOutput(output)
if err != nil { if err != nil {
t.Errorf("%s", err) return fmt.Errorf("%s", err)
} }
if data != "serial8250: ttyS1 at\n" { if data != "serial8250: ttyS1 at\n" {
t.Errorf("Serial port not detected : %v", data) return fmt.Errorf("Serial port not detected : %v", data)
} }
return nil
},
Teardown: func() error {
if _, err := os.Stat(output); err == nil {
os.Remove(output)
}
return nil
},
})
} }
func TestStepCreateVmx_ParallelPort(t *testing.T) { func TestStepCreateVmx_ParallelPort(t *testing.T) {
@ -294,11 +195,11 @@ func TestStepCreateVmx_ParallelPort(t *testing.T) {
defaultParallel = "/dev/lp0" defaultParallel = "/dev/lp0"
} }
config := map[string]string{ config := map[string]interface{}{
"parallel": fmt.Sprintf(`"device:%s,uni"`, filepath.ToSlash(defaultParallel)), "parallel": fmt.Sprintf("device:%s,uni", filepath.ToSlash(defaultParallel)),
} }
provision := map[string]string{ provision := map[string]string{
"inline": `"cat /proc/modules | egrep -o '^parport ' > /dev/fd0"`, "inline": "cat /proc/modules | egrep -o '^parport ' > /dev/fd0",
} }
// where to write output // where to write output
@ -306,20 +207,17 @@ func TestStepCreateVmx_ParallelPort(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error creating output: %s", err) t.Fatalf("Error creating output: %s", err)
} }
defer func() {
if _, err := os.Stat(output); err == nil {
os.Remove(output)
}
}()
config["vmx_data"] = vmxData config["vmx_data"] = vmxData
t.Logf("Preparing to write output to %s", output) configString := RenderConfig(config, provision)
builderT.Test(t, builderT.TestCase{
// whee Builder: &Builder{},
error := setupVMwareBuild(t, config, provision) Template: configString,
if error != nil { Check: func(a []packersdk.Artifact) error {
t.Errorf("%s", error) _, err := os.Stat(output)
if err != nil {
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
} }
// check the output // check the output
data, err := readFloppyOutput(output) data, err := readFloppyOutput(output)
if err != nil { if err != nil {
@ -329,6 +227,15 @@ func TestStepCreateVmx_ParallelPort(t *testing.T) {
if data != "parport \n" { if data != "parport \n" {
t.Errorf("Parallel port not detected : %v", data) t.Errorf("Parallel port not detected : %v", data)
} }
return nil
},
Teardown: func() error {
if _, err := os.Stat(output); err == nil {
os.Remove(output)
}
return nil
},
})
} }
func TestStepCreateVmx_Usb(t *testing.T) { func TestStepCreateVmx_Usb(t *testing.T) {
@ -336,32 +243,28 @@ func TestStepCreateVmx_Usb(t *testing.T) {
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.") t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
} }
config := map[string]string{ config := map[string]interface{}{
"usb": `"TRUE"`, "usb": "TRUE",
} }
provision := map[string]string{ provision := map[string]string{
"inline": `"dmesg | egrep -m1 -o 'USB hub found$' > /dev/fd0"`, "inline": "dmesg | egrep -m1 -o 'USB hub found$' > /dev/fd0",
} }
// where to write output
output, vmxData, err := createFloppyOutput("UsbOutput.") output, vmxData, err := createFloppyOutput("UsbOutput.")
if err != nil { if err != nil {
t.Fatalf("Error creating output: %s", err) t.Fatalf("Error creating output: %s", err)
} }
defer func() {
if _, err := os.Stat(output); err == nil {
os.Remove(output)
}
}()
config["vmx_data"] = vmxData config["vmx_data"] = vmxData
t.Logf("Preparing to write output to %s", output) configString := RenderConfig(config, provision)
builderT.Test(t, builderT.TestCase{
// whee Builder: &Builder{},
error := setupVMwareBuild(t, config, provision) Template: configString,
if error != nil { Check: func(a []packersdk.Artifact) error {
t.Errorf("%s", error) _, err := os.Stat(output)
if err != nil {
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
} }
// check the output // check the output
data, err := readFloppyOutput(output) data, err := readFloppyOutput(output)
if err != nil { if err != nil {
@ -371,6 +274,15 @@ func TestStepCreateVmx_Usb(t *testing.T) {
if data != "USB hub found\n" { if data != "USB hub found\n" {
t.Errorf("USB support not detected : %v", data) t.Errorf("USB support not detected : %v", data)
} }
return nil
},
Teardown: func() error {
if _, err := os.Stat(output); err == nil {
os.Remove(output)
}
return nil
},
})
} }
func TestStepCreateVmx_Sound(t *testing.T) { func TestStepCreateVmx_Sound(t *testing.T) {
@ -378,11 +290,11 @@ func TestStepCreateVmx_Sound(t *testing.T) {
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.") t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
} }
config := map[string]string{ config := map[string]interface{}{
"sound": `"TRUE"`, "sound": "TRUE",
} }
provision := map[string]string{ provision := map[string]string{
"inline": `"cat /proc/modules | egrep -o '^soundcore' > /dev/fd0"`, "inline": "cat /proc/modules | egrep -o '^soundcore' > /dev/fd0",
} }
// where to write output // where to write output
@ -395,15 +307,17 @@ func TestStepCreateVmx_Sound(t *testing.T) {
os.Remove(output) os.Remove(output)
} }
}() }()
config["vmx_data"] = vmxData config["vmx_data"] = vmxData
t.Logf("Preparing to write output to %s", output) configString := RenderConfig(config, provision)
builderT.Test(t, builderT.TestCase{
// whee Builder: &Builder{},
error := setupVMwareBuild(t, config, provision) Template: configString,
if error != nil { Check: func(a []packersdk.Artifact) error {
t.Errorf("Unable to read file: %s", error) _, err := os.Stat(output)
if err != nil {
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
} }
// check the output // check the output
data, err := readFloppyOutput(output) data, err := readFloppyOutput(output)
if err != nil { if err != nil {
@ -413,4 +327,13 @@ func TestStepCreateVmx_Sound(t *testing.T) {
if data != "soundcore\n" { if data != "soundcore\n" {
t.Errorf("Soundcard not detected : %v", data) t.Errorf("Soundcard not detected : %v", data)
} }
return nil
},
Teardown: func() error {
if _, err := os.Stat(output); err == nil {
os.Remove(output)
}
return nil
},
})
} }

View File

@ -6,7 +6,7 @@ import (
"github.com/hashicorp/packer/builder/vsphere/common" "github.com/hashicorp/packer/builder/vsphere/common"
commonT "github.com/hashicorp/packer/builder/vsphere/common/testing" commonT "github.com/hashicorp/packer/builder/vsphere/common/testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/vim25/types"
) )

View File

@ -7,7 +7,7 @@ import (
"testing" "testing"
commonT "github.com/hashicorp/packer/builder/vsphere/common/testing" commonT "github.com/hashicorp/packer/builder/vsphere/common/testing"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/vim25/types"
) )

View File

@ -7,7 +7,7 @@ import (
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
) )
const InstanceMetadataAddr = "169.254.169.254" const InstanceMetadataAddr = "169.254.169.254"

View File

@ -200,7 +200,7 @@ func wrappedMain() int {
Reader: os.Stdin, Reader: os.Stdin,
Writer: os.Stdout, Writer: os.Stdout,
ErrorWriter: os.Stdout, ErrorWriter: os.Stdout,
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
} }
ui = basicUi ui = basicUi
if !inPlugin { if !inPlugin {

View File

@ -1,8 +1,7 @@
package testshelper package provisioneracc
import ( import (
"bytes" "bytes"
"os"
"testing" "testing"
amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs"
@ -13,14 +12,6 @@ import (
"github.com/hashicorp/packer/provisioner/shell" "github.com/hashicorp/packer/provisioner/shell"
) )
// 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 // testCoreConfigBuilder creates a packer CoreConfig that has a file builder
// available. This allows us to test a builder that writes files to disk. // available. This allows us to test a builder that writes files to disk.
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
@ -50,9 +41,3 @@ func TestMetaFile(t *testing.T) command.Meta {
}, },
} }
} }
func CleanupFiles(moreFiles ...string) {
for _, file := range moreFiles {
os.RemoveAll(file)
}
}

View File

@ -1,4 +1,4 @@
package acc package provisioneracc
import ( import (
"bytes" "bytes"
@ -9,11 +9,10 @@ import (
"strings" "strings"
"testing" "testing"
testshelper "github.com/hashicorp/packer/helper/tests"
amazonEBS "github.com/hashicorp/packer/builder/amazon/ebs/acceptance" amazonEBS "github.com/hashicorp/packer/builder/amazon/ebs/acceptance"
virtualboxISO "github.com/hashicorp/packer/builder/virtualbox/iso/acceptance" virtualboxISO "github.com/hashicorp/packer/builder/virtualbox/iso/acceptance"
"github.com/hashicorp/packer/command" "github.com/hashicorp/packer/command"
"github.com/hashicorp/packer/packer-plugin-sdk/acctest/testutils"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
) )
@ -56,7 +55,7 @@ func TestProvisionersAgainstBuilders(provisionerAcc ProvisionerAcceptance, t *te
err = provisionerAcc.RunTest(c, args) err = provisionerAcc.RunTest(c, args)
// Cleanup created resources // Cleanup created resources
testshelper.CleanupFiles(fileName) testutils.CleanupFiles(fileName)
cleanErr := builderAcc.CleanUp() cleanErr := builderAcc.CleanUp()
if cleanErr != nil { if cleanErr != nil {
log.Printf("bad: failed to clean up resources: %s", cleanErr.Error()) log.Printf("bad: failed to clean up resources: %s", cleanErr.Error())
@ -122,7 +121,7 @@ func writeJsonTemplate(out *bytes.Buffer, filePath string, t *testing.T) {
func buildCommand(t *testing.T, builder BuilderAcceptance, provisioner ProvisionerAcceptance) *command.BuildCommand { func buildCommand(t *testing.T, builder BuilderAcceptance, provisioner ProvisionerAcceptance) *command.BuildCommand {
c := &command.BuildCommand{ c := &command.BuildCommand{
Meta: testshelper.TestMetaFile(t), Meta: TestMetaFile(t),
} }
c.CoreConfig.Components.BuilderStore = builder.GetBuilderStore() c.CoreConfig.Components.BuilderStore = builder.GetBuilderStore()
c.CoreConfig.Components.ProvisionerStore = provisioner.GetProvisionerStore() c.CoreConfig.Components.ProvisionerStore = provisioner.GetProvisionerStore()

View File

@ -1,4 +1,4 @@
package testing package acctest
import ( import (
"context" "context"
@ -12,6 +12,8 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/template" "github.com/hashicorp/packer/packer-plugin-sdk/template"
"github.com/hashicorp/packer/provisioner/file"
shellprovisioner "github.com/hashicorp/packer/provisioner/shell"
) )
// TestEnvVar must be set to a non-empty value for acceptance tests to run. // TestEnvVar must be set to a non-empty value for acceptance tests to run.
@ -123,6 +125,10 @@ func Test(t TestT, c TestCase) {
return nil, nil return nil, nil
}, },
}, },
ProvisionerStore: packersdk.MapOfProvisioner{
"shell": func() (packersdk.Provisioner, error) { return &shellprovisioner.Provisioner{}, nil },
"file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil },
},
}, },
Template: tpl, Template: tpl,
}) })
@ -157,10 +163,12 @@ func Test(t TestT, c TestCase) {
// Run it! We use a temporary directory for caching and discard // Run it! We use a temporary directory for caching and discard
// any UI output. We discard since it shows up in logs anyways. // any UI output. We discard since it shows up in logs anyways.
log.Printf("[DEBUG] Running 'test' build") log.Printf("[DEBUG] Running 'test' build")
// ui := packersdk.TestUi(t)
ui := &packersdk.BasicUi{ ui := &packersdk.BasicUi{
Reader: os.Stdin, Reader: os.Stdin,
Writer: ioutil.Discard, Writer: ioutil.Discard,
ErrorWriter: ioutil.Discard, ErrorWriter: ioutil.Discard,
PB: &packersdk.NoopProgressTracker{},
} }
artifacts, err := build.Run(context.Background(), ui) artifacts, err := build.Run(context.Background(), ui)
if err != nil { if err != nil {

View File

@ -1,4 +1,4 @@
package testing package acctest
import ( import (
"os" "os"

View File

@ -0,0 +1,18 @@
package testutils
import "os"
// CleanupFiles removes all files in the given strings.
func CleanupFiles(moreFiles ...string) {
for _, file := range moreFiles {
os.RemoveAll(file)
}
}
// FileExists returns true if the filename is found.
func FileExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
}
return false
}

View File

@ -17,7 +17,6 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
urlhelper "github.com/hashicorp/go-getter/v2/helper/url" urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep" "github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/tmp" "github.com/hashicorp/packer/packer-plugin-sdk/tmp"
@ -246,7 +245,7 @@ func TestStepDownload_download(t *testing.T) {
ui := &packersdk.BasicUi{ ui := &packersdk.BasicUi{
Reader: new(bytes.Buffer), Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer), Writer: new(bytes.Buffer),
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
} }
dir := createTempDir(t) dir := createTempDir(t)

View File

@ -7,7 +7,6 @@ import (
"os" "os"
"testing" "testing"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep" "github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
) )
@ -17,7 +16,7 @@ func testState(t *testing.T) multistep.StateBag {
state.Put("ui", &packersdk.BasicUi{ state.Put("ui", &packersdk.BasicUi{
Reader: new(bytes.Buffer), Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer), Writer: new(bytes.Buffer),
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
}) })
return state return state
} }

View File

@ -150,3 +150,12 @@ func (rw *BasicUi) Machine(t string, args ...string) {
func (rw *BasicUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { func (rw *BasicUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) {
return rw.PB.TrackProgress(src, currentSize, totalSize, stream) return rw.PB.TrackProgress(src, currentSize, totalSize, stream)
} }
// NoopProgressTracker is a progress tracker
// that displays nothing.
type NoopProgressTracker struct{}
// TrackProgress returns stream
func (*NoopProgressTracker) TrackProgress(_ string, _, _ int64, stream io.ReadCloser) io.ReadCloser {
return stream
}

View File

@ -13,6 +13,7 @@ func TestUi(t *testing.T) Ui {
Reader: &buf, Reader: &buf,
Writer: ioutil.Discard, Writer: ioutil.Discard,
ErrorWriter: ioutil.Discard, ErrorWriter: ioutil.Discard,
PB: &NoopProgressTracker{},
} }
} }

View File

@ -1,12 +0,0 @@
package packer
import "io"
// NoopProgressTracker is a progress tracker
// that displays nothing.
type NoopProgressTracker struct{}
// TrackProgress returns stream
func (*NoopProgressTracker) TrackProgress(_ string, _, _ int64, stream io.ReadCloser) io.ReadCloser {
return stream
}

View File

@ -1,3 +1,7 @@
package packer package packer
type UiProgressBar = NoopProgressTracker import (
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
type UiProgressBar = packersdk.NoopProgressTracker

View File

@ -31,7 +31,7 @@ const (
) )
type NoopUi struct { type NoopUi struct {
PB NoopProgressTracker PB packersdk.NoopProgressTracker
} }
var _ packersdk.Ui = new(NoopUi) var _ packersdk.Ui = new(NoopUi)
@ -173,7 +173,7 @@ func (u *TargetedUI) TrackProgress(src string, currentSize, totalSize int64, str
// to the given Writer. // to the given Writer.
type MachineReadableUi struct { type MachineReadableUi struct {
Writer io.Writer Writer io.Writer
PB NoopProgressTracker PB packersdk.NoopProgressTracker
} }
var _ packersdk.Ui = new(MachineReadableUi) var _ packersdk.Ui = new(MachineReadableUi)

View File

@ -10,7 +10,6 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/packer/packer"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
) )
@ -127,7 +126,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{ ui := &packersdk.BasicUi{
Writer: b, Writer: b,
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
} }
comm := &packersdk.MockCommunicator{} comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -186,7 +185,7 @@ func TestProvisionerProvision_SendsFileMultipleFiles(t *testing.T) {
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{ ui := &packersdk.BasicUi{
Writer: b, Writer: b,
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
} }
comm := &packersdk.MockCommunicator{} comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -256,7 +255,7 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) {
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{ ui := &packersdk.BasicUi{
Writer: b, Writer: b,
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
} }
comm := &packersdk.MockCommunicator{} comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -308,7 +307,7 @@ func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) {
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{ ui := &packersdk.BasicUi{
Writer: b, Writer: b,
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
} }
comm := &packersdk.MockCommunicator{} comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -366,7 +365,7 @@ func TestProvisionDownloadMkdirAll(t *testing.T) {
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{ ui := &packersdk.BasicUi{
Writer: b, Writer: b,
PB: &packer.NoopProgressTracker{}, PB: &packersdk.NoopProgressTracker{},
} }
comm := &packersdk.MockCommunicator{} comm := &packersdk.MockCommunicator{}
err = p.ProvisionDownload(ui, comm) err = p.ProvisionDownload(ui, comm)

View File

@ -10,7 +10,7 @@ import (
"github.com/hashicorp/go-uuid" "github.com/hashicorp/go-uuid"
"github.com/hashicorp/packer/command" "github.com/hashicorp/packer/command"
"github.com/hashicorp/packer/helper/tests/acc" "github.com/hashicorp/packer/packer-plugin-sdk/acctest/provisioneracc"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/provisioner/powershell" "github.com/hashicorp/packer/provisioner/powershell"
windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell" windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell"
@ -19,24 +19,24 @@ import (
const TestProvisionerName = "powershell" const TestProvisionerName = "powershell"
func TestAccPowershellProvisioner_basic(t *testing.T) { func TestAccPowershellProvisioner_basic(t *testing.T) {
acc.TestProvisionersPreCheck(TestProvisionerName, t) provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t)
testProvisioner := PowershellProvisionerAccTest{"powershell-provisioner-cleanup.txt"} testProvisioner := PowershellProvisionerAccTest{"powershell-provisioner-cleanup.txt"}
acc.TestProvisionersAgainstBuilders(&testProvisioner, t) provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t)
} }
func TestAccPowershellProvisioner_Inline(t *testing.T) { func TestAccPowershellProvisioner_Inline(t *testing.T) {
acc.TestProvisionersPreCheck(TestProvisionerName, t) provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t)
testProvisioner := PowershellProvisionerAccTest{"powershell-inline-provisioner.txt"} testProvisioner := PowershellProvisionerAccTest{"powershell-inline-provisioner.txt"}
acc.TestProvisionersAgainstBuilders(&testProvisioner, t) provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t)
} }
func TestAccPowershellProvisioner_Script(t *testing.T) { func TestAccPowershellProvisioner_Script(t *testing.T) {
acc.TestProvisionersPreCheck(TestProvisionerName, t) provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t)
testProvisioner := PowershellProvisionerAccTest{"powershell-script-provisioner.txt"} testProvisioner := PowershellProvisionerAccTest{"powershell-script-provisioner.txt"}
acc.TestProvisionersAgainstBuilders(&testProvisioner, t) provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t)
} }
type PowershellProvisionerAccTest struct { type PowershellProvisionerAccTest struct {

View File

@ -8,7 +8,8 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/hashicorp/packer/helper/tests/acc" "github.com/hashicorp/packer/packer-plugin-sdk/acctest/provisioneracc"
"github.com/hashicorp/packer/provisioner/shell" "github.com/hashicorp/packer/provisioner/shell"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
@ -17,8 +18,8 @@ import (
) )
func TestShellLocalProvisionerWithRetryOption(t *testing.T) { func TestShellLocalProvisionerWithRetryOption(t *testing.T) {
acc.TestProvisionersPreCheck("shell-local", t) provisioneracc.TestProvisionersPreCheck("shell-local", t)
acc.TestProvisionersAgainstBuilders(new(ShellLocalProvisionerAccTest), t) provisioneracc.TestProvisionersAgainstBuilders(new(ShellLocalProvisionerAccTest), t)
} }
type ShellLocalProvisionerAccTest struct{} type ShellLocalProvisionerAccTest struct{}

View File

@ -8,7 +8,8 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/hashicorp/packer/helper/tests/acc" "github.com/hashicorp/packer/packer-plugin-sdk/acctest/provisioneracc"
"github.com/hashicorp/packer/packer-plugin-sdk/acctest/testutils"
"github.com/hashicorp/packer/provisioner/file" "github.com/hashicorp/packer/provisioner/file"
"github.com/hashicorp/packer/provisioner/shell" "github.com/hashicorp/packer/provisioner/shell"
@ -16,12 +17,11 @@ import (
"github.com/hashicorp/go-uuid" "github.com/hashicorp/go-uuid"
"github.com/hashicorp/packer/command" "github.com/hashicorp/packer/command"
testshelper "github.com/hashicorp/packer/helper/tests"
) )
func TestShellProvisioner(t *testing.T) { func TestShellProvisioner(t *testing.T) {
acc.TestProvisionersPreCheck("shell", t) provisioneracc.TestProvisionersPreCheck("shell", t)
acc.TestProvisionersAgainstBuilders(new(ShellProvisionerAccTest), t) provisioneracc.TestProvisionersAgainstBuilders(new(ShellProvisionerAccTest), t)
} }
type ShellProvisionerAccTest struct{} type ShellProvisionerAccTest struct{}
@ -61,7 +61,7 @@ func (s *ShellProvisionerAccTest) RunTest(c *command.BuildCommand, args []string
} }
file := "provisioner.shell." + UUID + ".txt" file := "provisioner.shell." + UUID + ".txt"
defer testshelper.CleanupFiles(file) defer testutils.CleanupFiles(file)
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
ui := c.Meta.Ui.(*packersdk.BasicUi) ui := c.Meta.Ui.(*packersdk.BasicUi)
@ -73,7 +73,7 @@ func (s *ShellProvisionerAccTest) RunTest(c *command.BuildCommand, args []string
err.String()) err.String())
} }
if !testshelper.FileExists(file) { if !testutils.FileExists(file) {
return fmt.Errorf("Expected to find %s", file) return fmt.Errorf("Expected to find %s", file)
} }
return nil return nil