Convert legacy Packer json template to hcl2 template (#247)
* convert legacy Packer json template to hcl2 template
This commit is contained in:
@ -2,6 +2,9 @@ package e2e
|
||||
|
||||
import (
|
||||
"github.com/gruntwork-io/terratest/modules/files"
|
||||
"github.com/gruntwork-io/terratest/modules/packer"
|
||||
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -11,6 +14,10 @@ import (
|
||||
"github.com/gruntwork-io/terratest/modules/terraform"
|
||||
)
|
||||
|
||||
var speicalTests = map[string]func(*testing.T){
|
||||
"quickstart/201-vmss-packer-jumpbox": test201VmssPackerJumpbox,
|
||||
}
|
||||
|
||||
func Test_Quickstarts(t *testing.T) {
|
||||
msiId := os.Getenv("MSI_ID")
|
||||
if msiId != "" {
|
||||
@ -25,6 +32,7 @@ func Test_Quickstarts(t *testing.T) {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
folders = removeDuplicates(folders)
|
||||
for _, f := range folders {
|
||||
f = strings.TrimSpace(f)
|
||||
if filepath.Dir(f) != "quickstart" {
|
||||
@ -35,11 +43,16 @@ func Test_Quickstarts(t *testing.T) {
|
||||
if !files.IsExistingDir(path) {
|
||||
continue
|
||||
}
|
||||
t.Run(f, func(t *testing.T) {
|
||||
helper.RunE2ETest(t, rootPath, f, terraform.Options{
|
||||
Upgrade: true,
|
||||
}, nil)
|
||||
})
|
||||
test, ok := speicalTests[f]
|
||||
if !ok {
|
||||
test = func(t *testing.T) {
|
||||
helper.RunE2ETest(t, rootPath, f, terraform.Options{
|
||||
Upgrade: true,
|
||||
}, nil)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run(f, test)
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,3 +70,65 @@ func allExamples() ([]string, error) {
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func test201VmssPackerJumpbox(t *testing.T) {
|
||||
examplePath := filepath.Join("..", "..", "quickstart", "201-vmss-packer-jumpbox")
|
||||
examplePath = test_structure.CopyTerraformFolderToTemp(t, examplePath, "")
|
||||
defer func() {
|
||||
_ = os.RemoveAll(examplePath)
|
||||
}()
|
||||
harnessPath := filepath.Join(examplePath, "packer_image_resource_group")
|
||||
harnessOptions := &terraform.Options{
|
||||
TerraformDir: harnessPath,
|
||||
}
|
||||
defer terraform.Destroy(t, harnessOptions)
|
||||
terraform.InitAndApply(t, harnessOptions)
|
||||
harnessOutput := terraform.OutputAll(t, harnessOptions)
|
||||
imageResourceGroupName := harnessOutput["resource_group_name"].(string)
|
||||
pkrCfg := filepath.Join(examplePath, "ubuntu.pkr.hcl")
|
||||
packerVars := map[string]string{
|
||||
"image_resource_group_name": imageResourceGroupName,
|
||||
}
|
||||
useMsi := false
|
||||
if clientId := os.Getenv("ARM_CLIENT_ID"); clientId != "" {
|
||||
packerVars["client_id"] = clientId
|
||||
}
|
||||
if identityId := os.Getenv("MSI_ID"); identityId != "" {
|
||||
packerVars["client_id"] = identityId
|
||||
useMsi = true
|
||||
}
|
||||
if clientSecret := os.Getenv("ARM_CLIENT_SECRET"); clientSecret != "" {
|
||||
packerVars["client_secret"] = clientSecret
|
||||
}
|
||||
if subscriptionId := os.Getenv("ARM_SUBSCRIPTION_ID"); subscriptionId != "" {
|
||||
packerVars["subscription_id"] = subscriptionId
|
||||
}
|
||||
if tenantId := os.Getenv("ARM_TENANT_ID"); !useMsi && tenantId != "" {
|
||||
packerVars["tenant_id"] = tenantId
|
||||
}
|
||||
_, err := packer.BuildArtifactE(t, &packer.Options{
|
||||
Template: pkrCfg,
|
||||
Vars: packerVars,
|
||||
VarFiles: nil,
|
||||
WorkingDir: examplePath,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
helper.RunE2ETest(t, examplePath, "", terraform.Options{
|
||||
Upgrade: true,
|
||||
Vars: map[string]interface{}{
|
||||
"packer_resource_group_name": imageResourceGroupName,
|
||||
},
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func removeDuplicates(s []string) []string {
|
||||
m := make(map[string]struct{})
|
||||
result := []string{}
|
||||
for _, item := range s {
|
||||
if _, ok := m[item]; !ok {
|
||||
m[item] = struct{}{}
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ go 1.19
|
||||
require (
|
||||
github.com/Azure/terraform-module-test-helper v0.8.0
|
||||
github.com/gruntwork-io/terratest v0.41.9
|
||||
github.com/stretchr/testify v1.8.1
|
||||
)
|
||||
|
||||
require (
|
||||
@ -66,7 +67,6 @@ require (
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/spf13/afero v1.9.3 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/stretchr/testify v1.8.1 // indirect
|
||||
github.com/tmccombs/hcl2json v0.3.3 // indirect
|
||||
github.com/ulikunitz/xz v0.5.8 // indirect
|
||||
github.com/urfave/cli v1.22.2 // indirect
|
||||
|
Reference in New Issue
Block a user