diff --git a/provisioner/react/provisioner_acc_test.go b/provisioner/react/provisioner_acc_test.go index 75af59b..006814e 100644 --- a/provisioner/react/provisioner_acc_test.go +++ b/provisioner/react/provisioner_acc_test.go @@ -16,8 +16,11 @@ import ( "github.com/hashicorp/packer-plugin-sdk/acctest" ) -//go:embed test-fixtures/template.pkr.hcl -var testProvisionerHCL2Basic string +//go:embed test-fixtures/template-aws.pkr.hcl +var testProvisionerHCL2AWS string + +//go:embed test-fixtures/template-docker.pkr.hcl +var testProvisionerHCL2Docker string func TestAccReactProvisioner(t *testing.T) { tempFile, err := os.CreateTemp(t.TempDir(), "dist") @@ -25,15 +28,15 @@ func TestAccReactProvisioner(t *testing.T) { return } - testCase := &acctest.PluginTestCase{ - Name: "react_provisioner_basic_test", + testCaseAws := &acctest.PluginTestCase{ + Name: "react_provisioner_aws_test", Setup: func() error { return nil }, Teardown: func() error { return nil }, - Template: strings.Replace(testProvisionerHCL2Basic, "/my/path/to/dist", tempFile.Name(), -1), + Template: strings.Replace(testProvisionerHCL2AWS, "/my/path/to/dist", tempFile.Name(), -1), Type: "hashicorp-aws-react-provisioner", Check: func(buildCommand *exec.Cmd, logfile string) error { if buildCommand.ProcessState != nil { @@ -67,5 +70,49 @@ func TestAccReactProvisioner(t *testing.T) { return nil }, } - acctest.TestPlugin(t, testCase) + acctest.TestPlugin(t, testCaseAws) + + testCaseDocker := &acctest.PluginTestCase{ + Name: "react_provisioner_docker_test", + Setup: func() error { + return nil + }, + Teardown: func() error { + return nil + }, + Template: strings.Replace(testProvisionerHCL2Docker, "/my/path/to/dist", tempFile.Name(), -1), + Type: "hashicorp-aws-react-provisioner", + Check: func(buildCommand *exec.Cmd, logfile string) error { + if buildCommand.ProcessState != nil { + if buildCommand.ProcessState.ExitCode() != 0 { + return fmt.Errorf("Bad exit code. Logfile: %s", logfile) + } + } + + logs, err := os.Open(logfile) + if err != nil { + return fmt.Errorf("Unable find %s", logfile) + } + defer logs.Close() + + logsBytes, err := ioutil.ReadAll(logs) + if err != nil { + return fmt.Errorf("Unable to read %s", logfile) + } + logsString := string(logsBytes) + + errorString := "error(s) occurred" + if matched, _ := regexp.MatchString(".*"+errorString+".*", logsString); matched { + t.Fatalf("Acceptance tests for %s failed. Please search for '%s' in log file at %s", "react provisioner", errorString, logfile) + } + + provisionerOutputLog := "docker.hashicorp-aws: Exported Docker file:" + if matched, _ := regexp.MatchString(provisionerOutputLog+".*", logsString); !matched { + t.Fatalf("logs doesn't contain expected output %q", logsString) + } + + return nil + }, + } + acctest.TestPlugin(t, testCaseDocker) } diff --git a/provisioner/react/test-fixtures/template.pkr.hcl b/provisioner/react/test-fixtures/template-aws.pkr.hcl similarity index 100% rename from provisioner/react/test-fixtures/template.pkr.hcl rename to provisioner/react/test-fixtures/template-aws.pkr.hcl diff --git a/provisioner/react/test-fixtures/template-docker.pkr.hcl b/provisioner/react/test-fixtures/template-docker.pkr.hcl new file mode 100644 index 0000000..535d277 --- /dev/null +++ b/provisioner/react/test-fixtures/template-docker.pkr.hcl @@ -0,0 +1,30 @@ +# Copyright (c) Jiaqi +# SPDX-License-Identifier: MPL-2.0 + +packer { + required_plugins { + docker = { + version = ">= 0.0.7" + source = "github.com/hashicorp/docker" + } + } +} + +source "docker" "hashicorp-aws" { + image = "jack20191124/packer-plugin-hashicorp-aws-acc-test-base:latest" + discard = true +} + +build { + sources = [ + "source.docker.hashicorp-aws" + ] + + provisioner "hashicorp-aws-react-provisioner" { + distSource = "/my/path/to/dist" + homeDir = "/" + sslCertBase64 = "YXNkZnNnaHRkeWhyZXJ3ZGZydGV3ZHNmZ3RoeTY0cmV3ZGZyZWd0cmV3d2ZyZw==" + sslCertKeyBase64 = "MzI0NXRnZjk4dmJoIGNsO2VbNDM1MHRdzszNDM1b2l0cmo=" + appDomain = "app.mycompany.com" + } +}