2014-05-23 01:56:28 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2014-07-01 19:28:42 +02:00
|
|
|
"sort"
|
2014-05-23 01:56:28 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-05-24 00:42:29 +02:00
|
|
|
func TestLoad_badType(t *testing.T) {
|
|
|
|
_, err := Load(filepath.Join(fixtureDir, "bad_type.tf.nope"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
func TestLoadBasic(t *testing.T) {
|
|
|
|
c, err := Load(filepath.Join(fixtureDir, "basic.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := variablesStr(c.Variables)
|
|
|
|
if actual != strings.TrimSpace(basicVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
actual = providerConfigsStr(c.ProviderConfigs)
|
|
|
|
if actual != strings.TrimSpace(basicProvidersStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
actual = resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(basicResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:11:57 +02:00
|
|
|
func TestLoadBasic_import(t *testing.T) {
|
|
|
|
c, err := Load(filepath.Join(fixtureDir, "import.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := variablesStr(c.Variables)
|
|
|
|
if actual != strings.TrimSpace(importVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
actual = providerConfigsStr(c.ProviderConfigs)
|
|
|
|
if actual != strings.TrimSpace(importProvidersStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:11:57 +02:00
|
|
|
actual = resourcesStr(c.Resources)
|
2014-05-24 01:25:54 +02:00
|
|
|
if actual != strings.TrimSpace(importResourcesStr) {
|
2014-05-24 00:11:57 +02:00
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:55:51 +02:00
|
|
|
func TestLoad_variables(t *testing.T) {
|
|
|
|
c, err := Load(filepath.Join(fixtureDir, "variables.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := variablesStr(c.Variables)
|
|
|
|
if actual != strings.TrimSpace(variablesVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.Variables["foo"].Required() {
|
|
|
|
t.Fatal("foo should be required")
|
|
|
|
}
|
|
|
|
if c.Variables["bar"].Required() {
|
|
|
|
t.Fatal("bar should not be required")
|
|
|
|
}
|
|
|
|
if c.Variables["baz"].Required() {
|
|
|
|
t.Fatal("baz should not be required")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
// This helper turns a provider configs field into a deterministic
|
|
|
|
// string value for comparison in tests.
|
|
|
|
func providerConfigsStr(pcs map[string]*ProviderConfig) string {
|
|
|
|
result := ""
|
2014-07-01 19:28:42 +02:00
|
|
|
|
|
|
|
ns := make([]string, 0, len(pcs))
|
|
|
|
for n, _ := range pcs {
|
|
|
|
ns = append(ns, n)
|
|
|
|
}
|
|
|
|
sort.Strings(ns)
|
|
|
|
|
|
|
|
for _, n := range ns {
|
|
|
|
pc := pcs[n]
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
result += fmt.Sprintf("%s\n", n)
|
|
|
|
|
2014-07-01 19:28:42 +02:00
|
|
|
keys := make([]string, 0, len(pc.RawConfig.Raw))
|
2014-06-13 02:40:59 +02:00
|
|
|
for k, _ := range pc.RawConfig.Raw {
|
2014-07-01 19:28:42 +02:00
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
2014-05-26 03:05:18 +02:00
|
|
|
result += fmt.Sprintf(" %s\n", k)
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:40:59 +02:00
|
|
|
if len(pc.RawConfig.Variables) > 0 {
|
2014-05-26 03:05:18 +02:00
|
|
|
result += fmt.Sprintf(" vars\n")
|
2014-06-13 02:40:59 +02:00
|
|
|
for _, rawV := range pc.RawConfig.Variables {
|
2014-05-26 03:05:18 +02:00
|
|
|
kind := "unknown"
|
|
|
|
str := rawV.FullKey()
|
|
|
|
|
|
|
|
switch rawV.(type) {
|
|
|
|
case *ResourceVariable:
|
|
|
|
kind = "resource"
|
|
|
|
case *UserVariable:
|
|
|
|
kind = "user"
|
|
|
|
}
|
|
|
|
|
|
|
|
result += fmt.Sprintf(" %s: %s\n", kind, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(result)
|
|
|
|
}
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
// This helper turns a resources field into a deterministic
|
|
|
|
// string value for comparison in tests.
|
2014-05-24 22:57:51 +02:00
|
|
|
func resourcesStr(rs []*Resource) string {
|
2014-05-23 01:56:28 +02:00
|
|
|
result := ""
|
|
|
|
for _, r := range rs {
|
|
|
|
result += fmt.Sprintf(
|
|
|
|
"%s[%s]\n",
|
|
|
|
r.Type,
|
|
|
|
r.Name)
|
|
|
|
|
2014-07-01 19:28:42 +02:00
|
|
|
ks := make([]string, 0, len(r.RawConfig.Raw))
|
2014-06-13 02:40:59 +02:00
|
|
|
for k, _ := range r.RawConfig.Raw {
|
2014-07-01 19:28:42 +02:00
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
for _, k := range ks {
|
2014-05-23 01:56:28 +02:00
|
|
|
result += fmt.Sprintf(" %s\n", k)
|
|
|
|
}
|
2014-05-24 06:58:06 +02:00
|
|
|
|
2014-06-13 02:40:59 +02:00
|
|
|
if len(r.RawConfig.Variables) > 0 {
|
2014-05-24 06:58:06 +02:00
|
|
|
result += fmt.Sprintf(" vars\n")
|
2014-07-02 19:05:39 +02:00
|
|
|
|
|
|
|
ks := make([]string, 0, len(r.RawConfig.Variables))
|
|
|
|
for k, _ := range r.RawConfig.Variables {
|
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
for _, k := range ks {
|
|
|
|
rawV := r.RawConfig.Variables[k]
|
2014-05-24 06:58:06 +02:00
|
|
|
kind := "unknown"
|
|
|
|
str := rawV.FullKey()
|
|
|
|
|
|
|
|
switch rawV.(type) {
|
|
|
|
case *ResourceVariable:
|
|
|
|
kind = "resource"
|
|
|
|
case *UserVariable:
|
|
|
|
kind = "user"
|
|
|
|
}
|
|
|
|
|
|
|
|
result += fmt.Sprintf(" %s: %s\n", kind, str)
|
|
|
|
}
|
|
|
|
}
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This helper turns a variables field into a deterministic
|
|
|
|
// string value for comparison in tests.
|
2014-06-04 00:55:51 +02:00
|
|
|
func variablesStr(vs map[string]*Variable) string {
|
2014-05-23 01:56:28 +02:00
|
|
|
result := ""
|
2014-07-01 19:28:42 +02:00
|
|
|
ks := make([]string, 0, len(vs))
|
|
|
|
for k, _ := range vs {
|
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
for _, k := range ks {
|
|
|
|
v := vs[k]
|
|
|
|
|
2014-05-24 01:09:41 +02:00
|
|
|
if v.Default == "" {
|
|
|
|
v.Default = "<>"
|
|
|
|
}
|
|
|
|
if v.Description == "" {
|
|
|
|
v.Description = "<>"
|
|
|
|
}
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
result += fmt.Sprintf(
|
|
|
|
"%s\n %s\n %s\n",
|
|
|
|
k,
|
|
|
|
v.Default,
|
|
|
|
v.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(result)
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
const basicProvidersStr = `
|
|
|
|
aws
|
|
|
|
access_key
|
|
|
|
secret_key
|
|
|
|
do
|
|
|
|
api_key
|
|
|
|
vars
|
|
|
|
user: var.foo
|
|
|
|
`
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
const basicResourcesStr = `
|
|
|
|
aws_security_group[firewall]
|
|
|
|
aws_instance[web]
|
|
|
|
ami
|
2014-07-02 18:11:31 +02:00
|
|
|
network_interface
|
2014-05-23 01:56:28 +02:00
|
|
|
security_groups
|
2014-05-24 06:58:06 +02:00
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.foo
|
2014-07-02 19:05:39 +02:00
|
|
|
user: var.foo
|
2014-05-23 01:56:28 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const basicVariablesStr = `
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
bar
|
|
|
|
`
|
2014-05-24 00:11:57 +02:00
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
const importProvidersStr = `
|
|
|
|
aws
|
|
|
|
foo
|
|
|
|
`
|
|
|
|
|
2014-05-24 01:25:54 +02:00
|
|
|
const importResourcesStr = `
|
|
|
|
aws_security_group[db]
|
|
|
|
aws_security_group[web]
|
|
|
|
`
|
|
|
|
|
2014-05-24 00:11:57 +02:00
|
|
|
const importVariablesStr = `
|
2014-05-24 01:09:41 +02:00
|
|
|
bar
|
|
|
|
<>
|
|
|
|
<>
|
2014-05-24 00:11:57 +02:00
|
|
|
foo
|
|
|
|
bar
|
|
|
|
bar
|
|
|
|
`
|
2014-06-04 00:55:51 +02:00
|
|
|
|
|
|
|
const variablesVariablesStr = `
|
|
|
|
bar
|
|
|
|
<>
|
|
|
|
<>
|
|
|
|
baz
|
|
|
|
foo
|
|
|
|
<>
|
2014-07-01 19:28:42 +02:00
|
|
|
foo
|
|
|
|
<>
|
|
|
|
<>
|
2014-06-04 00:55:51 +02:00
|
|
|
`
|