Skip to content

Commit

Permalink
Implement Docker-based acceptance tests for React provisioner (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi authored Jul 10, 2024
1 parent a2a8bd8 commit 08f61f4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
59 changes: 53 additions & 6 deletions provisioner/react/provisioner_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ 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")
if err != nil {
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 {
Expand Down Expand Up @@ -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)
}
30 changes: 30 additions & 0 deletions provisioner/react/test-fixtures/template-docker.pkr.hcl
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 08f61f4

Please sign in to comment.