This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
327 lines
7.8 KiB
Go
327 lines
7.8 KiB
Go
package oci
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/go-ini/ini"
|
|
)
|
|
|
|
func testConfig(accessConfFile *os.File) map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"availability_domain": "aaaa:PHX-AD-3",
|
|
"access_cfg_file": accessConfFile.Name(),
|
|
|
|
// Image
|
|
"base_image_ocid": "ocd1...",
|
|
"shape": "VM.Standard1.1",
|
|
"image_name": "HelloWorld",
|
|
|
|
// Networking
|
|
"subnet_ocid": "ocd1...",
|
|
|
|
// Comm
|
|
"ssh_username": "opc",
|
|
"use_private_ip": false,
|
|
"metadata": map[string]string{
|
|
"key": "value",
|
|
},
|
|
"defined_tags": map[string]map[string]interface{}{
|
|
"namespace": {"key": "value"},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestConfig(t *testing.T) {
|
|
// Shared set-up and deferred deletion
|
|
|
|
cfg, keyFile, err := baseTestConfigWithTmpKeyFile()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(keyFile.Name())
|
|
|
|
cfgFile, err := writeTestConfig(cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(cfgFile.Name())
|
|
|
|
// Temporarily set $HOME to temp directory to bypass default
|
|
// access config loading.
|
|
|
|
tmpHome, err := ioutil.TempDir("", "packer_config_test")
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error when creating temporary directory: %+v", err)
|
|
}
|
|
defer os.Remove(tmpHome)
|
|
|
|
home := os.Getenv("HOME")
|
|
os.Setenv("HOME", tmpHome)
|
|
defer os.Setenv("HOME", home)
|
|
|
|
// Config tests
|
|
t.Run("BaseConfig", func(t *testing.T) {
|
|
raw := testConfig(cfgFile)
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
}
|
|
})
|
|
|
|
t.Run("NoAccessConfig", func(t *testing.T) {
|
|
raw := testConfig(cfgFile)
|
|
delete(raw, "access_cfg_file")
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
|
|
expectedErrors := []string{
|
|
"'user_ocid'", "'tenancy_ocid'", "'fingerprint'", "'key_file'",
|
|
}
|
|
|
|
s := errs.Error()
|
|
for _, expected := range expectedErrors {
|
|
if !strings.Contains(s, expected) {
|
|
t.Errorf("Expected %q to contain '%s'", s, expected)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("AccessConfigTemplateOnly", func(t *testing.T) {
|
|
raw := testConfig(cfgFile)
|
|
delete(raw, "access_cfg_file")
|
|
raw["user_ocid"] = "ocid1..."
|
|
raw["tenancy_ocid"] = "ocid1..."
|
|
raw["fingerprint"] = "00:00..."
|
|
raw["key_file"] = keyFile.Name()
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
|
|
if errs != nil {
|
|
t.Fatalf("err: %+v", errs)
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("TenancyReadFromAccessCfgFile", func(t *testing.T) {
|
|
raw := testConfig(cfgFile)
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
}
|
|
|
|
tenancy, err := c.configProvider.TenancyOCID()
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error getting tenancy ocid: %v", err)
|
|
}
|
|
|
|
expected := "ocid1.tenancy.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
if tenancy != expected {
|
|
t.Errorf("Expected tenancy: %s, got %s.", expected, tenancy)
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("RegionNotDefaultedToPHXWhenSetInOCISettings", func(t *testing.T) {
|
|
raw := testConfig(cfgFile)
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
}
|
|
|
|
region, err := c.configProvider.Region()
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error getting region: %v", err)
|
|
}
|
|
|
|
expected := "us-ashburn-1"
|
|
if region != expected {
|
|
t.Errorf("Expected region: %s, got %s.", expected, region)
|
|
}
|
|
|
|
})
|
|
|
|
// Test the correct errors are produced when required template keys are
|
|
// omitted.
|
|
requiredKeys := []string{"availability_domain", "base_image_ocid", "shape", "subnet_ocid"}
|
|
for _, k := range requiredKeys {
|
|
t.Run(k+"_required", func(t *testing.T) {
|
|
raw := testConfig(cfgFile)
|
|
delete(raw, k)
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
|
|
if !strings.Contains(errs.Error(), k) {
|
|
t.Errorf("Expected '%s' to contain '%s'", errs.Error(), k)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("ImageNameDefaultedIfEmpty", func(t *testing.T) {
|
|
raw := testConfig(cfgFile)
|
|
delete(raw, "image_name")
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
}
|
|
|
|
if !strings.Contains(c.ImageName, "packer-") {
|
|
t.Errorf("got default ImageName %q, want image name 'packer-{{timestamp}}'", c.ImageName)
|
|
}
|
|
})
|
|
|
|
t.Run("user_ocid_overridden", func(t *testing.T) {
|
|
expected := "override"
|
|
raw := testConfig(cfgFile)
|
|
raw["user_ocid"] = expected
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
}
|
|
|
|
user, _ := c.configProvider.UserOCID()
|
|
if user != expected {
|
|
t.Errorf("Expected ConfigProvider.UserOCID: %s, got %s", expected, user)
|
|
}
|
|
})
|
|
|
|
t.Run("tenancy_ocid_overidden", func(t *testing.T) {
|
|
expected := "override"
|
|
raw := testConfig(cfgFile)
|
|
raw["tenancy_ocid"] = expected
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
}
|
|
|
|
tenancy, _ := c.configProvider.TenancyOCID()
|
|
if tenancy != expected {
|
|
t.Errorf("Expected ConfigProvider.TenancyOCID: %s, got %s", expected, tenancy)
|
|
}
|
|
})
|
|
|
|
t.Run("region_overidden", func(t *testing.T) {
|
|
expected := "override"
|
|
raw := testConfig(cfgFile)
|
|
raw["region"] = expected
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
}
|
|
|
|
region, _ := c.configProvider.Region()
|
|
if region != expected {
|
|
t.Errorf("Expected ConfigProvider.Region: %s, got %s", expected, region)
|
|
}
|
|
})
|
|
|
|
t.Run("fingerprint_overidden", func(t *testing.T) {
|
|
expected := "override"
|
|
raw := testConfig(cfgFile)
|
|
raw["fingerprint"] = expected
|
|
|
|
var c Config
|
|
errs := c.Prepare(raw)
|
|
if errs != nil {
|
|
t.Fatalf("Unexpected error in configuration: %+v", errs)
|
|
}
|
|
|
|
fingerprint, _ := c.configProvider.KeyFingerprint()
|
|
if fingerprint != expected {
|
|
t.Errorf("Expected ConfigProvider.KeyFingerprint: %s, got %s", expected, fingerprint)
|
|
}
|
|
})
|
|
}
|
|
|
|
// BaseTestConfig creates the base (DEFAULT) config including a temporary key
|
|
// file.
|
|
// NOTE: Caller is responsible for removing temporary key file.
|
|
func baseTestConfigWithTmpKeyFile() (*ini.File, *os.File, error) {
|
|
keyFile, err := generateRSAKeyFile()
|
|
if err != nil {
|
|
return nil, keyFile, err
|
|
}
|
|
// Build ini
|
|
cfg := ini.Empty()
|
|
section, _ := cfg.NewSection("DEFAULT")
|
|
section.NewKey("region", "us-ashburn-1")
|
|
section.NewKey("tenancy", "ocid1.tenancy.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
section.NewKey("user", "ocid1.user.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
section.NewKey("fingerprint", "70:04:5z:b3:19:ab:90:75:a4:1f:50:d4:c7:c3:33:20")
|
|
section.NewKey("key_file", keyFile.Name())
|
|
|
|
return cfg, keyFile, nil
|
|
}
|
|
|
|
// WriteTestConfig writes a ini.File to a temporary file for use in unit tests.
|
|
// NOTE: Caller is responsible for removing temporary file.
|
|
func writeTestConfig(cfg *ini.File) (*os.File, error) {
|
|
confFile, err := ioutil.TempFile("", "config_file")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, err := confFile.Write([]byte("[DEFAULT]\n")); err != nil {
|
|
os.Remove(confFile.Name())
|
|
return nil, err
|
|
}
|
|
|
|
if _, err := cfg.WriteTo(confFile); err != nil {
|
|
os.Remove(confFile.Name())
|
|
return nil, err
|
|
}
|
|
return confFile, nil
|
|
}
|
|
|
|
// generateRSAKeyFile generates an RSA key file for use in unit tests.
|
|
// NOTE: The caller is responsible for deleting the temporary file.
|
|
func generateRSAKeyFile() (*os.File, error) {
|
|
// Create temporary file for the key
|
|
f, err := ioutil.TempFile("", "key")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Generate key
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2014)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// ASN.1 DER encoded form
|
|
privDer := x509.MarshalPKCS1PrivateKey(priv)
|
|
privBlk := pem.Block{
|
|
Type: "RSA PRIVATE KEY",
|
|
Headers: nil,
|
|
Bytes: privDer,
|
|
}
|
|
|
|
// Write the key out
|
|
if _, err := f.Write(pem.EncodeToMemory(&privBlk)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return f, nil
|
|
}
|