Convert legacy Packer json template to hcl2 template (#247)

* convert legacy Packer json template to hcl2 template
This commit is contained in:
lonegunmanb
2023-09-19 13:37:17 +08:00
committed by GitHub
parent bb15543608
commit f2a6901f3f
12 changed files with 324 additions and 105 deletions

View File

@ -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
}