diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 7acea7daf..e028b0446 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -447,7 +447,7 @@ For the Shell provisioner this is: os.Setenv("PACKER_RUN_UUID", UUID) } file := "provisioner.shell." + UUID + ".txt" - defer testshelper.CleanupFiles(file) + defer testutils.CleanupFiles(file) // Run build // 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 - if !testshelper.FileExists(file) { + if !testutils.FileExists(file) { return fmt.Errorf("Expected to find %s", file) } return nil diff --git a/Makefile b/Makefile index 3a50eaf44..0d0657bba 100644 --- a/Makefile +++ b/Makefile @@ -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 # 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." PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) $(TESTARGS) -timeout=120m diff --git a/builder/alicloud/ecs/builder_acc_test.go b/builder/alicloud/ecs/builder_acc_test.go index c3efa2129..31a416eb1 100644 --- a/builder/alicloud/ecs/builder_acc_test.go +++ b/builder/alicloud/ecs/builder_acc_test.go @@ -8,7 +8,7 @@ import ( "testing" "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" ) diff --git a/helper/tests/aws.go b/builder/amazon/ebs/acceptance/aws.go similarity index 98% rename from helper/tests/aws.go rename to builder/amazon/ebs/acceptance/aws.go index 220e1a6e4..210bce310 100644 --- a/helper/tests/aws.go +++ b/builder/amazon/ebs/acceptance/aws.go @@ -1,7 +1,8 @@ -package testshelper +package amazon_acc import ( "fmt" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" awscommon "github.com/hashicorp/packer/builder/amazon/common" diff --git a/builder/amazon/ebs/acceptance/builder_acceptance.go b/builder/amazon/ebs/acceptance/builder_acceptance.go index 67097fbc3..c63bbfa97 100644 --- a/builder/amazon/ebs/acceptance/builder_acceptance.go +++ b/builder/amazon/ebs/acceptance/builder_acceptance.go @@ -12,8 +12,6 @@ import ( amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" - - testshelper "github.com/hashicorp/packer/helper/tests" ) type AmazonEBSAccTest struct{} @@ -47,7 +45,7 @@ func (s *AmazonEBSAccTest) GetConfigs() (map[string]string, error) { } func (s *AmazonEBSAccTest) CleanUp() error { - helper := testshelper.AWSHelper{ + helper := AWSHelper{ Region: "us-east-1", AMIName: "packer-acc-test", } diff --git a/builder/amazon/ebs/builder_acc_test.go b/builder/amazon/ebs/builder_acc_test.go index e81d226ab..b2f84683e 100644 --- a/builder/amazon/ebs/builder_acc_test.go +++ b/builder/amazon/ebs/builder_acc_test.go @@ -12,7 +12,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "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" ) diff --git a/builder/amazon/ebs/tags_acc_test.go b/builder/amazon/ebs/tags_acc_test.go index 640e27cc4..933b73ce1 100644 --- a/builder/amazon/ebs/tags_acc_test.go +++ b/builder/amazon/ebs/tags_acc_test.go @@ -8,7 +8,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "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" ) diff --git a/builder/azure/arm/builder_acc_test.go b/builder/azure/arm/builder_acc_test.go index 24a36bf72..dc7b2fc00 100644 --- a/builder/azure/arm/builder_acc_test.go +++ b/builder/azure/arm/builder_acc_test.go @@ -25,7 +25,7 @@ import ( "fmt" "os" - builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" + builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest" ) const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST" diff --git a/builder/azure/dtl/builder_acc_test.go b/builder/azure/dtl/builder_acc_test.go index 825090890..218d9910d 100644 --- a/builder/azure/dtl/builder_acc_test.go +++ b/builder/azure/dtl/builder_acc_test.go @@ -28,7 +28,7 @@ package dtl import ( "testing" - builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" + builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest" ) const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST" diff --git a/builder/digitalocean/builder_acc_test.go b/builder/digitalocean/builder_acc_test.go index 619736ab6..4273def7c 100644 --- a/builder/digitalocean/builder_acc_test.go +++ b/builder/digitalocean/builder_acc_test.go @@ -7,7 +7,7 @@ import ( "testing" "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" ) diff --git a/builder/docker/artifact_export.go b/builder/docker/artifact_export.go index eb26f96c3..fd945ee97 100644 --- a/builder/docker/artifact_export.go +++ b/builder/docker/artifact_export.go @@ -35,5 +35,8 @@ func (a *ExportArtifact) State(name string) interface{} { } func (a *ExportArtifact) Destroy() error { - return os.Remove(a.path) + if a.path != "" { + return os.Remove(a.path) + } + return nil } diff --git a/builder/docker/communicator_test.go b/builder/docker/communicator_test.go index 3b30f2c48..31d6d00d7 100644 --- a/builder/docker/communicator_test.go +++ b/builder/docker/communicator_test.go @@ -1,99 +1,105 @@ package docker import ( - "context" "crypto/sha256" + "encoding/json" + "fmt" "io/ioutil" "os" "os/exec" - "strings" "testing" - "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" - "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 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") == "" { 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.Run() - if !cmd.ProcessState.Success() { + err := cmd.Run() + if err != nil { t.Error("docker command not found; please make sure docker is installed") } - // Setup the builder - builder := &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") - } - - // 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: ""}, - }, + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: configString, + Check: func(a []packersdk.Artifact) error { + // Verify that the thing we downloaded is the same thing we sent up. + // Complain loudly if it isn't. + inputFile, err := ioutil.ReadFile("test-fixtures/onecakes/strawberry") + if err != nil { + return fmt.Errorf("Unable to read input file: %s", err) + } + outputFile, err := ioutil.ReadFile("my-strawberry-cake") + if err != nil { + return fmt.Errorf("Unable to read output file: %s", err) + } + if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) { + return fmt.Errorf("Input and output files do not match\n"+ + "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile) + } + return nil }, - } - 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. - // Complain loudly if it isn't. - inputFile, err := ioutil.ReadFile("test-fixtures/onecakes/strawberry") - if err != nil { - t.Fatalf("Unable to read input file: %s", err) - } - outputFile, err := ioutil.ReadFile("my-strawberry-cake") - if err != nil { - t.Fatalf("Unable to read output file: %s", err) - } - if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) { - t.Fatalf("Input and output files do not match\n"+ - "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile) - } + 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 @@ -101,276 +107,133 @@ func TestUploadDownload(t *testing.T) { // need to use github.com/cbednarski/rerun to verify since this problem occurs // only intermittently. 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") == "" { 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.Run() - if !cmd.ProcessState.Success() { + err := cmd.Run() + if err != nil { t.Error("docker command not found; please make sure docker is installed") } - // Setup the builder - builder := &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") - } + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: configString, + Check: func(a []packersdk.Artifact) error { + // Verify that the things we downloaded are the right size. Complain loudly + // if they are not. + // + // cupcake should be 2097152 bytes + // bigcake should be 104857600 bytes + cupcake, err := os.Stat("cupcake") + if err != nil { + t.Fatalf("Unable to stat cupcake file: %s", err) + } + cupcakeExpected := int64(2097152) + if cupcake.Size() != cupcakeExpected { + t.Errorf("Expected cupcake to be %d bytes; found %d", cupcakeExpected, cupcake.Size()) + } - // 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) - } + bigcake, err := os.Stat("bigcake") + if err != nil { + t.Fatalf("Unable to stat bigcake file: %s", err) + } + bigcakeExpected := int64(104857600) + if bigcake.Size() != bigcakeExpected { + t.Errorf("Expected bigcake to be %d bytes; found %d", bigcakeExpected, bigcake.Size()) + } - // Preemptive cleanup. - defer os.Remove("cupcake") - defer os.Remove("bigcake") + // TODO if we can, calculate a sha inside the container and compare to the + // one we get after we pull it down. We will probably have to parse the log + // or ui output to do this because we use /dev/urandom to create the file. - // 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: ""}, - }, + // if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) { + // t.Fatalf("Input and output files do not match\n"+ + // "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile) + // } + return nil }, - } - 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 - // if they are not. - // - // cupcake should be 2097152 bytes - // bigcake should be 104857600 bytes - cupcake, err := os.Stat("cupcake") - if err != nil { - t.Fatalf("Unable to stat cupcake file: %s", err) - } - cupcakeExpected := int64(2097152) - if cupcake.Size() != cupcakeExpected { - t.Errorf("Expected cupcake to be %d bytes; found %d", cupcakeExpected, cupcake.Size()) - } - - bigcake, err := os.Stat("bigcake") - if err != nil { - t.Fatalf("Unable to stat bigcake file: %s", err) - } - bigcakeExpected := int64(104857600) - if bigcake.Size() != bigcakeExpected { - t.Errorf("Expected bigcake to be %d bytes; found %d", bigcakeExpected, bigcake.Size()) - } - - // TODO if we can, calculate a sha inside the container and compare to the - // one we get after we pull it down. We will probably have to parse the log - // or ui output to do this because we use /dev/urandom to create the file. - - // if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) { - // t.Fatalf("Input and output files do not match\n"+ - // "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile) - // } + 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. 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") == "" { t.Skip("This test is only run with PACKER_ACC=1") } + cmd := exec.Command("docker", "-v") - cmd.Run() - if !cmd.ProcessState.Success() { + err := cmd.Run() + if err != nil { t.Error("docker command not found; please make sure docker is installed") } - // Setup the builder - builder := &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") + dockerBuilderExtraConfig := map[string]interface{}{ + "run_command": []string{"-d", "-i", "-t", "-u", "42", "{{.Image}}", "/bin/sh"}, } - // Setup the provisioners - 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: ""}, + testFixUploadOwnerProvisionersTemplate := []map[string]interface{}{ + { + "type": "file", + "source": "test-fixtures/onecakes/strawberry", + "destination": "/tmp/strawberry-cake", + }, + { + "type": "file", + "source": "test-fixtures/manycakes", + "destination": "/tmp/", + }, + { + "type": "shell", + "inline": "touch /tmp/testUploadOwner", + }, + { + "type": "shell", + "inline": []string{ + "[ $(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)", + "find /tmp/manycakes | xargs -n1 -IFILE /bin/sh -c '[ $(stat -c %u FILE) -eq 42 ] || (echo \"Invalid owner of FILE\" && exit 1)'", }, }, } - 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() + configString := RenderConfig(dockerBuilderExtraConfig, testFixUploadOwnerProvisionersTemplate) + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: configString, + }) } - -const dockerBuilderConfig = ` -{ - "builders": [ - { - "type": "docker", - "image": "ubuntu", - "discard": true, - "run_command": ["-d", "-i", "-t", "{{.Image}}", "/bin/sh"] - } - ], - "provisioners": [ - { - "type": "file", - "source": "test-fixtures/onecakes/strawberry", - "destination": "/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", - "source": "test-fixtures/manycakes", - "destination": "/tmp/" - }, - { - "type": "shell", - "inline": "touch /tmp/testUploadOwner" - }, - { - "type": "shell", - "inline": [ - "[ $(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)", - "find /tmp/manycakes | xargs -n1 -IFILE /bin/sh -c '[ $(stat -c %u FILE) -eq 42 ] || (echo \"Invalid owner of FILE\" && exit 1)'" - ] - } - ] -} -` diff --git a/builder/file/builder_test.go b/builder/file/builder_test.go index edaab32f5..b72e8a2b6 100644 --- a/builder/file/builder_test.go +++ b/builder/file/builder_test.go @@ -5,7 +5,7 @@ import ( "io/ioutil" "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" ) diff --git a/builder/hcloud/builder_acc_test.go b/builder/hcloud/builder_acc_test.go index 51e833d10..7c5ee1e85 100644 --- a/builder/hcloud/builder_acc_test.go +++ b/builder/hcloud/builder_acc_test.go @@ -4,7 +4,7 @@ import ( "os" "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) { diff --git a/builder/hyperone/builder_acc_test.go b/builder/hyperone/builder_acc_test.go index aa295b618..625309f37 100644 --- a/builder/hyperone/builder_acc_test.go +++ b/builder/hyperone/builder_acc_test.go @@ -4,7 +4,7 @@ import ( "os" "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) { diff --git a/builder/linode/builder_acc_test.go b/builder/linode/builder_acc_test.go index bd5af8fb8..19a74ccad 100644 --- a/builder/linode/builder_acc_test.go +++ b/builder/linode/builder_acc_test.go @@ -4,7 +4,7 @@ import ( "os" "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) { diff --git a/builder/oneandone/builder_acc_test.go b/builder/oneandone/builder_acc_test.go index b348f805d..b3811e2b9 100644 --- a/builder/oneandone/builder_acc_test.go +++ b/builder/oneandone/builder_acc_test.go @@ -4,7 +4,7 @@ import ( "os" "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) { diff --git a/builder/osc/bsu/builder_acc_test.go b/builder/osc/bsu/builder_acc_test.go index 7d5e25f59..bc25e445b 100644 --- a/builder/osc/bsu/builder_acc_test.go +++ b/builder/osc/bsu/builder_acc_test.go @@ -4,7 +4,7 @@ package bsu import ( "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) { diff --git a/builder/osc/bsusurrogate/builder_acc_test.go b/builder/osc/bsusurrogate/builder_acc_test.go index b8a980a0c..bda5366e0 100644 --- a/builder/osc/bsusurrogate/builder_acc_test.go +++ b/builder/osc/bsusurrogate/builder_acc_test.go @@ -3,7 +3,7 @@ package bsusurrogate import ( "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) { diff --git a/builder/osc/bsuvolume/builder_acc_test.go b/builder/osc/bsuvolume/builder_acc_test.go index dd3bbef6a..e0046c706 100644 --- a/builder/osc/bsuvolume/builder_acc_test.go +++ b/builder/osc/bsuvolume/builder_acc_test.go @@ -4,7 +4,7 @@ package bsuvolume import ( "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) { diff --git a/builder/profitbricks/builder_acc_test.go b/builder/profitbricks/builder_acc_test.go index c3a829920..ce22fa877 100644 --- a/builder/profitbricks/builder_acc_test.go +++ b/builder/profitbricks/builder_acc_test.go @@ -4,7 +4,7 @@ import ( "os" "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) { diff --git a/builder/ucloud/uhost/builder_acc_test.go b/builder/ucloud/uhost/builder_acc_test.go index 5d3c68888..264875f60 100644 --- a/builder/ucloud/uhost/builder_acc_test.go +++ b/builder/ucloud/uhost/builder_acc_test.go @@ -9,7 +9,7 @@ import ( packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "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) { diff --git a/builder/virtualbox/iso/acceptance/builder_acceptance.go b/builder/virtualbox/iso/acceptance/builder_acceptance.go index e394ca6c4..11db2c342 100644 --- a/builder/virtualbox/iso/acceptance/builder_acceptance.go +++ b/builder/virtualbox/iso/acceptance/builder_acceptance.go @@ -11,9 +11,8 @@ import ( "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" - - testshelper "github.com/hashicorp/packer/helper/tests" ) type VirtualBoxISOAccTest struct{} @@ -34,8 +33,8 @@ func (v *VirtualBoxISOAccTest) GetConfigs() (map[string]string, error) { } func (v *VirtualBoxISOAccTest) CleanUp() error { - testshelper.CleanupFiles("virtualbox-iso-packer-acc-test") - testshelper.CleanupFiles("packer_cache") + testutils.CleanupFiles("virtualbox-iso-packer-acc-test") + testutils.CleanupFiles("packer_cache") return nil } diff --git a/builder/virtualbox/iso/builder_acc_test.go b/builder/virtualbox/iso/builder_acc_test.go index 1be8de534..10f80e6db 100644 --- a/builder/virtualbox/iso/builder_acc_test.go +++ b/builder/virtualbox/iso/builder_acc_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "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) { diff --git a/builder/vmware/iso/builder_acc_test.go b/builder/vmware/iso/builder_acc_test.go index 1be8de534..10f80e6db 100644 --- a/builder/vmware/iso/builder_acc_test.go +++ b/builder/vmware/iso/builder_acc_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "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) { diff --git a/builder/vmware/iso/step_create_vmx_test.go b/builder/vmware/iso/step_create_vmx_test.go index a741eddde..051a49dc7 100644 --- a/builder/vmware/iso/step_create_vmx_test.go +++ b/builder/vmware/iso/step_create_vmx_test.go @@ -2,81 +2,39 @@ package iso import ( "bytes" - "context" + "encoding/json" "fmt" "io/ioutil" - "math" - "math/rand" "os" "path/filepath" "runtime" - "strconv" "strings" - "testing" - "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/shell" + "github.com/hashicorp/packer/packer-plugin-sdk/tmp" ) -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": `["","rootpassword","udhcpc"]`, - "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}]}` -func tmpnam(prefix string) string { - var path string - 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) +func createFloppyOutput(prefix string) (string, map[string]string, error) { + f, err := tmp.File(prefix) 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() - vmxData := []string{ - `"floppy0.present":"TRUE"`, - `"floppy0.fileType":"file"`, - `"floppy0.clientDevice":"FALSE"`, - `"floppy0.fileName":"%s"`, - `"floppy0.startConnected":"TRUE"`, - } - + output := f.Name() outputFile := strings.Replace(output, "\\", "\\\\", -1) - vmxString := fmt.Sprintf("{"+strings.Join(vmxData, ",")+"}", outputFile) - return output, vmxString, nil + vmxData := map[string]string{ + "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) { @@ -95,114 +53,42 @@ func readFloppyOutput(path string) (string, error) { return string(data[:bytes.IndexByte(data, 0)]), nil } -func setupVMwareBuild(t *testing.T, builderConfig map[string]string, provisionerConfig map[string]string) error { - ui := packersdk.TestUi(t) - - // create builder config and update with user-supplied options - cfgBuilder := map[string]string{} - for k, v := range vmxTestBuilderConfig { - cfgBuilder[k] = v +// 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]string) string { + // set up basic build template + t := map[string][]map[string]interface{}{ + "builders": { + 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{"", "rootpassword", "udhcpc"}, + "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 { - cfgBuilder[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 + t["builders"][0][k] = v } + // Apply special provisioner overrides for k, v := range provisionerConfig { - cfgProvisioner[k] = v + t["provisioners"][0][k] = v } - // convert our provisioner config into a single sprintfable string - provisionerLines := []string{} - 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 + j, _ := json.Marshal(t) + return string(j) } 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.") } - tmpfile := tmpnam("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) + tmpfile, err := tmp.File("SerialFileInput.") 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())), } - if f != nil { - if err := os.Remove(tmpfile); err != nil { - t.Fatalf("Unable to remove file %s: %s", tmpfile, err) - } - } + 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 err := os.Remove(tmpfile.Name()); err != nil { + return fmt.Errorf("Unable to remove file %s: %s", tmpfile.Name(), err) + } + } + return nil + }, + }) } func TestStepCreateVmx_SerialPort(t *testing.T) { @@ -245,11 +140,11 @@ func TestStepCreateVmx_SerialPort(t *testing.T) { defaultSerial = "/dev/ttyS0" } - config := map[string]string{ - "serial": fmt.Sprintf(`"device:%s"`, filepath.ToSlash(defaultSerial)), + config := map[string]interface{}{ + "serial": fmt.Sprintf("device:%s", filepath.ToSlash(defaultSerial)), } 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 @@ -257,29 +152,35 @@ func TestStepCreateVmx_SerialPort(t *testing.T) { if err != nil { t.Fatalf("Error creating output: %s", err) } - defer func() { - if _, err := os.Stat(output); err == nil { - os.Remove(output) - } - }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: configString, + Check: func(a []packersdk.Artifact) 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 + data, err := readFloppyOutput(output) + if err != nil { + return fmt.Errorf("%s", err) + } - // whee - err = setupVMwareBuild(t, config, provision) - if err != nil { - t.Errorf("%s", err) - } - - // check the output - data, err := readFloppyOutput(output) - if err != nil { - t.Errorf("%s", err) - } - - if data != "serial8250: ttyS1 at\n" { - t.Errorf("Serial port not detected : %v", data) - } + if data != "serial8250: ttyS1 at\n" { + 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) { @@ -294,11 +195,11 @@ func TestStepCreateVmx_ParallelPort(t *testing.T) { defaultParallel = "/dev/lp0" } - config := map[string]string{ - "parallel": fmt.Sprintf(`"device:%s,uni"`, filepath.ToSlash(defaultParallel)), + config := map[string]interface{}{ + "parallel": fmt.Sprintf("device:%s,uni", filepath.ToSlash(defaultParallel)), } 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 @@ -306,29 +207,35 @@ func TestStepCreateVmx_ParallelPort(t *testing.T) { if err != nil { t.Fatalf("Error creating output: %s", err) } - defer func() { - if _, err := os.Stat(output); err == nil { - os.Remove(output) - } - }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: configString, + Check: func(a []packersdk.Artifact) 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 + data, err := readFloppyOutput(output) + if err != nil { + t.Errorf("%s", err) + } - // whee - error := setupVMwareBuild(t, config, provision) - if error != nil { - t.Errorf("%s", error) - } - - // check the output - data, err := readFloppyOutput(output) - if err != nil { - t.Errorf("%s", err) - } - - if data != "parport \n" { - t.Errorf("Parallel port not detected : %v", data) - } + if data != "parport \n" { + 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) { @@ -336,41 +243,46 @@ 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.") } - config := map[string]string{ - "usb": `"TRUE"`, + config := map[string]interface{}{ + "usb": "TRUE", } 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.") if err != nil { t.Fatalf("Error creating output: %s", err) } - defer func() { - if _, err := os.Stat(output); err == nil { - os.Remove(output) - } - }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: configString, + Check: func(a []packersdk.Artifact) 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 + data, err := readFloppyOutput(output) + if err != nil { + t.Errorf("%s", err) + } - // whee - error := setupVMwareBuild(t, config, provision) - if error != nil { - t.Errorf("%s", error) - } - - // check the output - data, err := readFloppyOutput(output) - if err != nil { - t.Errorf("%s", err) - } - - if data != "USB hub found\n" { - t.Errorf("USB support not detected : %v", data) - } + if data != "USB hub found\n" { + 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) { @@ -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.") } - config := map[string]string{ - "sound": `"TRUE"`, + config := map[string]interface{}{ + "sound": "TRUE", } 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 @@ -395,22 +307,33 @@ func TestStepCreateVmx_Sound(t *testing.T) { os.Remove(output) } }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: configString, + Check: func(a []packersdk.Artifact) 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 + data, err := readFloppyOutput(output) + if err != nil { + t.Errorf("%s", err) + } - // whee - error := setupVMwareBuild(t, config, provision) - if error != nil { - t.Errorf("Unable to read file: %s", error) - } - - // check the output - data, err := readFloppyOutput(output) - if err != nil { - t.Errorf("%s", err) - } - - if data != "soundcore\n" { - t.Errorf("Soundcard not detected : %v", data) - } + if data != "soundcore\n" { + t.Errorf("Soundcard not detected : %v", data) + } + return nil + }, + Teardown: func() error { + if _, err := os.Stat(output); err == nil { + os.Remove(output) + } + return nil + }, + }) } diff --git a/builder/vsphere/clone/builder_acc_test.go b/builder/vsphere/clone/builder_acc_test.go index 83d34ae8f..dfdd9ecb9 100644 --- a/builder/vsphere/clone/builder_acc_test.go +++ b/builder/vsphere/clone/builder_acc_test.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/packer/builder/vsphere/common" 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" "github.com/vmware/govmomi/vim25/types" ) diff --git a/builder/vsphere/iso/builder_acc_test.go b/builder/vsphere/iso/builder_acc_test.go index 1ded0723b..939a2f407 100644 --- a/builder/vsphere/iso/builder_acc_test.go +++ b/builder/vsphere/iso/builder_acc_test.go @@ -7,7 +7,7 @@ import ( "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" "github.com/vmware/govmomi/vim25/types" ) diff --git a/builder/yandex/builder_acc_test.go b/builder/yandex/builder_acc_test.go index 3d201a171..3e99bc33f 100644 --- a/builder/yandex/builder_acc_test.go +++ b/builder/yandex/builder_acc_test.go @@ -7,7 +7,7 @@ import ( "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" diff --git a/main.go b/main.go index 026ed3f41..64d1e51df 100644 --- a/main.go +++ b/main.go @@ -200,7 +200,7 @@ func wrappedMain() int { Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stdout, - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, } ui = basicUi if !inPlugin { diff --git a/helper/tests/core.go b/packer-plugin-sdk/acctest/provisioneracc/core.go similarity index 82% rename from helper/tests/core.go rename to packer-plugin-sdk/acctest/provisioneracc/core.go index 5e8e2764f..11e55b052 100644 --- a/helper/tests/core.go +++ b/packer-plugin-sdk/acctest/provisioneracc/core.go @@ -1,8 +1,7 @@ -package testshelper +package provisioneracc import ( "bytes" - "os" "testing" amazonebsbuilder "github.com/hashicorp/packer/builder/amazon/ebs" @@ -13,14 +12,6 @@ import ( "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 // available. This allows us to test a builder that writes files to disk. 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) - } -} diff --git a/helper/tests/acc/provisioners.go b/packer-plugin-sdk/acctest/provisioneracc/provisioners.go similarity index 96% rename from helper/tests/acc/provisioners.go rename to packer-plugin-sdk/acctest/provisioneracc/provisioners.go index 5c7a780c9..56ecaae8a 100644 --- a/helper/tests/acc/provisioners.go +++ b/packer-plugin-sdk/acctest/provisioneracc/provisioners.go @@ -1,4 +1,4 @@ -package acc +package provisioneracc import ( "bytes" @@ -9,11 +9,10 @@ import ( "strings" "testing" - testshelper "github.com/hashicorp/packer/helper/tests" - amazonEBS "github.com/hashicorp/packer/builder/amazon/ebs/acceptance" virtualboxISO "github.com/hashicorp/packer/builder/virtualbox/iso/acceptance" "github.com/hashicorp/packer/command" + "github.com/hashicorp/packer/packer-plugin-sdk/acctest/testutils" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -56,7 +55,7 @@ func TestProvisionersAgainstBuilders(provisionerAcc ProvisionerAcceptance, t *te err = provisionerAcc.RunTest(c, args) // Cleanup created resources - testshelper.CleanupFiles(fileName) + testutils.CleanupFiles(fileName) cleanErr := builderAcc.CleanUp() if cleanErr != nil { 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 { c := &command.BuildCommand{ - Meta: testshelper.TestMetaFile(t), + Meta: TestMetaFile(t), } c.CoreConfig.Components.BuilderStore = builder.GetBuilderStore() c.CoreConfig.Components.ProvisionerStore = provisioner.GetProvisionerStore() diff --git a/packer-plugin-sdk/builder/testing/testing.go b/packer-plugin-sdk/acctest/testing.go similarity index 92% rename from packer-plugin-sdk/builder/testing/testing.go rename to packer-plugin-sdk/acctest/testing.go index 746147866..799682842 100644 --- a/packer-plugin-sdk/builder/testing/testing.go +++ b/packer-plugin-sdk/acctest/testing.go @@ -1,4 +1,4 @@ -package testing +package acctest import ( "context" @@ -12,6 +12,8 @@ import ( "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "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. @@ -123,6 +125,10 @@ 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 }, + }, }, Template: tpl, }) @@ -157,10 +163,12 @@ func Test(t TestT, c TestCase) { // Run it! We use a temporary directory for caching and discard // any UI output. We discard since it shows up in logs anyways. log.Printf("[DEBUG] Running 'test' build") + // ui := packersdk.TestUi(t) ui := &packersdk.BasicUi{ Reader: os.Stdin, Writer: ioutil.Discard, ErrorWriter: ioutil.Discard, + PB: &packersdk.NoopProgressTracker{}, } artifacts, err := build.Run(context.Background(), ui) if err != nil { diff --git a/packer-plugin-sdk/builder/testing/testing_test.go b/packer-plugin-sdk/acctest/testing_test.go similarity index 98% rename from packer-plugin-sdk/builder/testing/testing_test.go rename to packer-plugin-sdk/acctest/testing_test.go index 347bde639..ca2240470 100644 --- a/packer-plugin-sdk/builder/testing/testing_test.go +++ b/packer-plugin-sdk/acctest/testing_test.go @@ -1,4 +1,4 @@ -package testing +package acctest import ( "os" diff --git a/packer-plugin-sdk/acctest/testutils/utils.go b/packer-plugin-sdk/acctest/testutils/utils.go new file mode 100644 index 000000000..a0470a4f2 --- /dev/null +++ b/packer-plugin-sdk/acctest/testutils/utils.go @@ -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 +} diff --git a/packer-plugin-sdk/multistep/commonsteps/step_download_test.go b/packer-plugin-sdk/multistep/commonsteps/step_download_test.go index 460100731..4ed23856a 100644 --- a/packer-plugin-sdk/multistep/commonsteps/step_download_test.go +++ b/packer-plugin-sdk/multistep/commonsteps/step_download_test.go @@ -17,7 +17,6 @@ import ( "github.com/google/go-cmp/cmp" urlhelper "github.com/hashicorp/go-getter/v2/helper/url" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/tmp" @@ -246,7 +245,7 @@ func TestStepDownload_download(t *testing.T) { ui := &packersdk.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, } dir := createTempDir(t) diff --git a/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go b/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go index 66b37f033..4e7c8771a 100644 --- a/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go +++ b/packer-plugin-sdk/multistep/commonsteps/step_output_dir_test.go @@ -7,7 +7,6 @@ import ( "os" "testing" - "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" 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{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, }) return state } diff --git a/packer-plugin-sdk/packer/ui.go b/packer-plugin-sdk/packer/ui.go index 16ad5d9e6..4c97b2a0c 100644 --- a/packer-plugin-sdk/packer/ui.go +++ b/packer-plugin-sdk/packer/ui.go @@ -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) { 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 +} diff --git a/packer-plugin-sdk/packer/ui_mock.go b/packer-plugin-sdk/packer/ui_mock.go index 8ca8beafb..e1e165f5e 100644 --- a/packer-plugin-sdk/packer/ui_mock.go +++ b/packer-plugin-sdk/packer/ui_mock.go @@ -13,6 +13,7 @@ func TestUi(t *testing.T) Ui { Reader: &buf, Writer: ioutil.Discard, ErrorWriter: ioutil.Discard, + PB: &NoopProgressTracker{}, } } diff --git a/packer/progressbar_noop.go b/packer/progressbar_noop.go deleted file mode 100644 index e8891afaa..000000000 --- a/packer/progressbar_noop.go +++ /dev/null @@ -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 -} diff --git a/packer/progressbar_solaris.go b/packer/progressbar_solaris.go index 868037c38..28f989f1a 100644 --- a/packer/progressbar_solaris.go +++ b/packer/progressbar_solaris.go @@ -1,3 +1,7 @@ package packer -type UiProgressBar = NoopProgressTracker +import ( + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" +) + +type UiProgressBar = packersdk.NoopProgressTracker diff --git a/packer/ui.go b/packer/ui.go index dd70b6d02..4dc2c3490 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -31,7 +31,7 @@ const ( ) type NoopUi struct { - PB NoopProgressTracker + PB packersdk.NoopProgressTracker } var _ packersdk.Ui = new(NoopUi) @@ -173,7 +173,7 @@ func (u *TargetedUI) TrackProgress(src string, currentSize, totalSize int64, str // to the given Writer. type MachineReadableUi struct { Writer io.Writer - PB NoopProgressTracker + PB packersdk.NoopProgressTracker } var _ packersdk.Ui = new(MachineReadableUi) diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 510dabfc7..07a9d2871 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -10,7 +10,6 @@ import ( "strings" "testing" - "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" ) @@ -127,7 +126,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packersdk.BasicUi{ Writer: b, - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, } comm := &packersdk.MockCommunicator{} 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) ui := &packersdk.BasicUi{ Writer: b, - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, } comm := &packersdk.MockCommunicator{} 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) ui := &packersdk.BasicUi{ Writer: b, - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, } comm := &packersdk.MockCommunicator{} 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) ui := &packersdk.BasicUi{ Writer: b, - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, } comm := &packersdk.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -366,7 +365,7 @@ func TestProvisionDownloadMkdirAll(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packersdk.BasicUi{ Writer: b, - PB: &packer.NoopProgressTracker{}, + PB: &packersdk.NoopProgressTracker{}, } comm := &packersdk.MockCommunicator{} err = p.ProvisionDownload(ui, comm) diff --git a/provisioner/powershell/provisioner_acc_test.go b/provisioner/powershell/provisioner_acc_test.go index 62837e992..e6a8c4af7 100644 --- a/provisioner/powershell/provisioner_acc_test.go +++ b/provisioner/powershell/provisioner_acc_test.go @@ -10,7 +10,7 @@ import ( "github.com/hashicorp/go-uuid" "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" "github.com/hashicorp/packer/provisioner/powershell" windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell" @@ -19,24 +19,24 @@ import ( const TestProvisionerName = "powershell" func TestAccPowershellProvisioner_basic(t *testing.T) { - acc.TestProvisionersPreCheck(TestProvisionerName, t) + provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t) testProvisioner := PowershellProvisionerAccTest{"powershell-provisioner-cleanup.txt"} - acc.TestProvisionersAgainstBuilders(&testProvisioner, t) + provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t) } func TestAccPowershellProvisioner_Inline(t *testing.T) { - acc.TestProvisionersPreCheck(TestProvisionerName, t) + provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t) testProvisioner := PowershellProvisionerAccTest{"powershell-inline-provisioner.txt"} - acc.TestProvisionersAgainstBuilders(&testProvisioner, t) + provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t) } func TestAccPowershellProvisioner_Script(t *testing.T) { - acc.TestProvisionersPreCheck(TestProvisionerName, t) + provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t) testProvisioner := PowershellProvisionerAccTest{"powershell-script-provisioner.txt"} - acc.TestProvisionersAgainstBuilders(&testProvisioner, t) + provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t) } type PowershellProvisionerAccTest struct { diff --git a/provisioner/shell-local/provisioner_acc_test.go b/provisioner/shell-local/provisioner_acc_test.go index 91c58534b..9aef8d04f 100644 --- a/provisioner/shell-local/provisioner_acc_test.go +++ b/provisioner/shell-local/provisioner_acc_test.go @@ -8,7 +8,8 @@ import ( "path/filepath" "testing" - "github.com/hashicorp/packer/helper/tests/acc" + "github.com/hashicorp/packer/packer-plugin-sdk/acctest/provisioneracc" + "github.com/hashicorp/packer/provisioner/shell" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" @@ -17,8 +18,8 @@ import ( ) func TestShellLocalProvisionerWithRetryOption(t *testing.T) { - acc.TestProvisionersPreCheck("shell-local", t) - acc.TestProvisionersAgainstBuilders(new(ShellLocalProvisionerAccTest), t) + provisioneracc.TestProvisionersPreCheck("shell-local", t) + provisioneracc.TestProvisionersAgainstBuilders(new(ShellLocalProvisionerAccTest), t) } type ShellLocalProvisionerAccTest struct{} diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index 4cde4db80..3704feeba 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -8,7 +8,8 @@ import ( "path/filepath" "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/shell" @@ -16,12 +17,11 @@ import ( "github.com/hashicorp/go-uuid" "github.com/hashicorp/packer/command" - testshelper "github.com/hashicorp/packer/helper/tests" ) func TestShellProvisioner(t *testing.T) { - acc.TestProvisionersPreCheck("shell", t) - acc.TestProvisionersAgainstBuilders(new(ShellProvisionerAccTest), t) + provisioneracc.TestProvisionersPreCheck("shell", t) + provisioneracc.TestProvisionersAgainstBuilders(new(ShellProvisionerAccTest), t) } type ShellProvisionerAccTest struct{} @@ -61,7 +61,7 @@ func (s *ShellProvisionerAccTest) RunTest(c *command.BuildCommand, args []string } file := "provisioner.shell." + UUID + ".txt" - defer testshelper.CleanupFiles(file) + defer testutils.CleanupFiles(file) if code := c.Run(args); code != 0 { ui := c.Meta.Ui.(*packersdk.BasicUi) @@ -73,7 +73,7 @@ func (s *ShellProvisionerAccTest) RunTest(c *command.BuildCommand, args []string err.String()) } - if !testshelper.FileExists(file) { + if !testutils.FileExists(file) { return fmt.Errorf("Expected to find %s", file) } return nil