2017-09-12 11:30:39 -04:00
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-11 05:20:40 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2017-02-13 05:35:14 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
"github.com/go-ini/ini"
|
2017-02-13 05:35:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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...",
|
2017-02-13 07:37:27 -05:00
|
|
|
|
|
|
|
// Comm
|
2018-02-13 08:20:26 -05:00
|
|
|
"ssh_username": "opc",
|
|
|
|
"use_private_ip": false,
|
2018-07-02 04:48:08 -04:00
|
|
|
"metadata": map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
},
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig(t *testing.T) {
|
|
|
|
// Shared set-up and defered deletion
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
cfg, keyFile, err := baseTestConfigWithTmpKeyFile()
|
2017-02-13 05:35:14 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(keyFile.Name())
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
cfgFile, err := writeTestConfig(cfg)
|
2017-02-13 05:35:14 -05:00
|
|
|
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 {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error when creating temporary directory: %+v", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
_, errs := NewConfig(raw)
|
|
|
|
|
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoAccessConfig", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
delete(raw, "access_cfg_file")
|
|
|
|
|
|
|
|
_, errs := NewConfig(raw)
|
|
|
|
|
|
|
|
expectedErrors := []string{
|
2018-04-11 05:20:40 -04:00
|
|
|
"'user_ocid'", "'tenancy_ocid'", "'fingerprint'", "'key_file'",
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
2018-04-11 05:20:40 -04:00
|
|
|
|
|
|
|
s := errs.Error()
|
2017-02-13 05:35:14 -05:00
|
|
|
for _, expected := range expectedErrors {
|
|
|
|
if !strings.Contains(s, expected) {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Errorf("Expected %q to contain '%s'", s, expected)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
_, errs := NewConfig(raw)
|
|
|
|
|
|
|
|
if errs != nil {
|
2017-02-13 07:51:01 -05:00
|
|
|
t.Fatalf("err: %+v", errs)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TenancyReadFromAccessCfgFile", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
c, errs := NewConfig(raw)
|
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
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)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2017-09-29 05:51:31 -04:00
|
|
|
t.Run("RegionNotDefaultedToPHXWhenSetInOCISettings", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
c, errs := NewConfig(raw)
|
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
region, err := c.ConfigProvider.Region()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error getting region: %v", err)
|
2017-09-29 05:51:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := "us-ashburn-1"
|
2018-04-11 05:20:40 -04:00
|
|
|
if region != expected {
|
|
|
|
t.Errorf("Expected region: %s, got %s.", expected, region)
|
2017-09-29 05:51:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2017-02-13 05:35:14 -05:00
|
|
|
// Test the correct errors are produced when required template keys are
|
|
|
|
// omitted.
|
2017-08-02 05:51:08 -04:00
|
|
|
requiredKeys := []string{"availability_domain", "base_image_ocid", "shape", "subnet_ocid"}
|
2017-02-13 05:35:14 -05:00
|
|
|
for _, k := range requiredKeys {
|
|
|
|
t.Run(k+"_required", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
delete(raw, k)
|
|
|
|
|
|
|
|
_, errs := NewConfig(raw)
|
|
|
|
|
|
|
|
if !strings.Contains(errs.Error(), k) {
|
|
|
|
t.Errorf("Expected '%s' to contain '%s'", errs.Error(), k)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-02 05:51:08 -04:00
|
|
|
t.Run("ImageNameDefaultedIfEmpty", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
delete(raw, "image_name")
|
|
|
|
|
|
|
|
c, errs := NewConfig(raw)
|
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
2017-08-02 05:51:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(c.ImageName, "packer-") {
|
|
|
|
t.Errorf("got default ImageName %q, want image name 'packer-{{timestamp}}'", c.ImageName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Run("user_ocid_overridden", func(t *testing.T) {
|
|
|
|
expected := "override"
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
raw["user_ocid"] = expected
|
|
|
|
|
|
|
|
c, errs := NewConfig(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
|
|
|
|
|
|
|
|
c, errs := NewConfig(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
|
|
|
|
|
|
|
|
c, errs := NewConfig(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
|
|
|
|
|
|
|
|
c, errs := NewConfig(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
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
2018-04-11 05:20:40 -04:00
|
|
|
// 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
|
|
|
|
}
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
// 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
|
|
|
|
}
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
if _, err := confFile.Write([]byte("[DEFAULT]\n")); err != nil {
|
|
|
|
os.Remove(confFile.Name())
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
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
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
2018-04-11 05:20:40 -04:00
|
|
|
|
|
|
|
// 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
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|