2014-05-23 01:56:28 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2015-06-23 16:15:26 +02:00
|
|
|
"io/ioutil"
|
2014-05-23 01:56:28 +02:00
|
|
|
"path/filepath"
|
2015-03-05 21:56:31 +01:00
|
|
|
"reflect"
|
2014-05-23 01:56:28 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-11-02 18:30:28 +01:00
|
|
|
func TestErrNoConfigsFound_impl(t *testing.T) {
|
|
|
|
var _ error = new(ErrNoConfigsFound)
|
|
|
|
}
|
|
|
|
|
2014-09-27 00:49:51 +02:00
|
|
|
func TestIsEmptyDir(t *testing.T) {
|
|
|
|
val, err := IsEmptyDir(fixtureDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if val {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:28:18 +02:00
|
|
|
func TestIsEmptyDir_noExist(t *testing.T) {
|
|
|
|
val, err := IsEmptyDir(filepath.Join(fixtureDir, "nopenopenope"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !val {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 00:49:51 +02:00
|
|
|
func TestIsEmptyDir_noConfigs(t *testing.T) {
|
|
|
|
val, err := IsEmptyDir(filepath.Join(fixtureDir, "dir-empty"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !val {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFile_badType(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "bad_type.tf.nope"))
|
2014-05-24 00:42:29 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 03:11:11 +01:00
|
|
|
func TestLoadFile_gitCrypt(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "git-crypt.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-01-19 20:28:45 +01:00
|
|
|
func TestLoadFile_lifecycleKeyCheck(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "lifecycle_cbd_typo.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-12-11 01:27:01 +01:00
|
|
|
func TestLoadFile_varInvalidKey(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "var-invalid-key.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 01:05:49 +01:00
|
|
|
func TestLoadFile_resourceArityMistake(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "resource-arity-mistake.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
expected := "Error loading test-fixtures/resource-arity-mistake.tf: position 2:10: resource must be followed by exactly two strings, a type and a name"
|
|
|
|
if err.Error() != expected {
|
|
|
|
t.Fatalf("expected:\n%s\ngot:\n%s", expected, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:52:05 +01:00
|
|
|
func TestLoadFile_resourceMultiLifecycle(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "resource-multi-lifecycle.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 01:20:00 +01:00
|
|
|
func TestLoadFile_dataSourceArityMistake(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "data-source-arity-mistake.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
expected := "Error loading test-fixtures/data-source-arity-mistake.tf: position 2:6: 'data' must be followed by exactly two strings: a type and a name"
|
|
|
|
if err.Error() != expected {
|
|
|
|
t.Fatalf("expected:\n%s\ngot:\n%s", expected, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:26:38 +01:00
|
|
|
func TestLoadFileWindowsLineEndings(t *testing.T) {
|
|
|
|
testFile := filepath.Join(fixtureDir, "windows-line-endings.tf")
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile(testFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(contents), "\r\n") {
|
|
|
|
t.Fatalf("Windows line endings test file %s contains no windows line endings - this may be an autocrlf related issue.", testFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := LoadFile(testFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(windowsHeredocResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 16:15:41 +01:00
|
|
|
func TestLoadFileHeredoc(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "heredoc.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := providerConfigsStr(c.ProviderConfigs)
|
|
|
|
if actual != strings.TrimSpace(heredocProvidersStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(heredocResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:31:33 +01:00
|
|
|
func TestLoadFileEscapedQuotes(t *testing.T) {
|
2016-06-20 18:06:03 +02:00
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "escapedquotes.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected syntax error as escaped quotes are no longer supported")
|
2015-11-19 16:31:33 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 19:28:31 +01:00
|
|
|
if !strings.Contains(err.Error(), "parse error") {
|
2016-06-20 18:06:03 +02:00
|
|
|
t.Fatalf("expected \"syntax error\", got: %s", err)
|
2015-11-19 16:31:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFileBasic(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "basic.tf"))
|
2014-05-23 01:56:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2014-09-15 04:35:38 +02:00
|
|
|
if c.Dir != "" {
|
2017-07-01 18:12:31 +02:00
|
|
|
t.Fatalf("wrong dir %#v; want %#v", c.Dir, "")
|
2014-09-15 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 01:20:33 +01:00
|
|
|
expectedTF := &Terraform{RequiredVersion: "foo"}
|
|
|
|
if !reflect.DeepEqual(c.Terraform, expectedTF) {
|
2017-07-01 18:12:31 +02:00
|
|
|
t.Fatalf("wrong terraform block %#v; want %#v", c.Terraform, expectedTF)
|
2016-11-13 01:20:33 +01:00
|
|
|
}
|
|
|
|
|
2015-03-05 21:56:31 +01:00
|
|
|
expectedAtlas := &AtlasConfig{Name: "mitchellh/foo"}
|
|
|
|
if !reflect.DeepEqual(c.Atlas, expectedAtlas) {
|
2017-07-01 18:12:31 +02:00
|
|
|
t.Fatalf("wrong atlas config %#v; want %#v", c.Atlas, expectedAtlas)
|
2015-03-05 21:56:31 +01:00
|
|
|
}
|
|
|
|
|
2014-05-23 01:56:28 +02:00
|
|
|
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-07-04 19:43:06 +02:00
|
|
|
|
2017-07-01 18:12:31 +02:00
|
|
|
if actual, want := localsStr(c.Locals), strings.TrimSpace(basicLocalsStr); actual != want {
|
|
|
|
t.Fatalf("wrong locals:\n%s\nwant:\n%s", actual, want)
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:43:06 +02:00
|
|
|
actual = outputsStr(c.Outputs)
|
|
|
|
if actual != strings.TrimSpace(basicOutputsStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFileBasic_empty(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "empty.tf"))
|
2014-09-15 18:41:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFileBasic_import(t *testing.T) {
|
2014-08-05 07:04:48 +02:00
|
|
|
// Skip because we disabled importing
|
|
|
|
t.Skip()
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "import.tf"))
|
2014-05-24 00:11:57 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFileBasic_json(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "basic.tf.json"))
|
2014-07-19 01:54:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2014-09-15 04:35:38 +02:00
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:56:31 +01:00
|
|
|
expectedAtlas := &AtlasConfig{Name: "mitchellh/foo"}
|
|
|
|
if !reflect.DeepEqual(c.Atlas, expectedAtlas) {
|
|
|
|
t.Fatalf("bad: %#v", c.Atlas)
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:54:52 +02:00
|
|
|
actual := variablesStr(c.Variables)
|
|
|
|
if actual != strings.TrimSpace(basicVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = providerConfigsStr(c.ProviderConfigs)
|
|
|
|
if actual != strings.TrimSpace(basicProvidersStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(basicResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:12:31 +02:00
|
|
|
if actual, want := localsStr(c.Locals), strings.TrimSpace(basicLocalsStr); actual != want {
|
|
|
|
t.Fatalf("wrong locals:\n%s\nwant:\n%s", actual, want)
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:54:52 +02:00
|
|
|
actual = outputsStr(c.Outputs)
|
|
|
|
if actual != strings.TrimSpace(basicOutputsStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFileBasic_modules(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "modules.tf"))
|
2014-09-12 00:58:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2014-09-15 04:35:38 +02:00
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
2014-09-12 00:58:30 +02:00
|
|
|
actual := modulesStr(c.Modules)
|
|
|
|
if actual != strings.TrimSpace(modulesModulesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 00:42:34 +02:00
|
|
|
func TestLoadFile_unnamedModule(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "module-unnamed.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad: expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
errorStr := err.Error()
|
|
|
|
if !strings.Contains(errorStr, `"module" must be followed`) {
|
|
|
|
t.Fatalf("bad: expected error has wrong text: %s", errorStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 02:46:34 +01:00
|
|
|
func TestLoadFile_outputDependsOn(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "output-depends-on.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := outputsStr(c.Outputs)
|
|
|
|
if actual != strings.TrimSpace(outputDependsOnStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:46:49 +01:00
|
|
|
func TestLoadFile_terraformBackend(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "terraform-backend.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2017-03-16 22:51:26 +01:00
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
actual := terraformStr(c.Terraform)
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
backend (s3)
|
|
|
|
foo`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadFile_terraformBackendJSON(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "terraform-backend.tf.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:04:19 +01:00
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
actual := terraformStr(c.Terraform)
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
backend (s3)
|
|
|
|
foo`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that the alternate, more obvious JSON format also decodes properly
|
|
|
|
func TestLoadFile_terraformBackendJSON2(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "terraform-backend-2.tf.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:46:49 +01:00
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
actual := terraformStr(c.Terraform)
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
backend (s3)
|
|
|
|
foo`)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadFile_terraformBackendMulti(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "terraform-backend-multi.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
errorStr := err.Error()
|
|
|
|
if !strings.Contains(errorStr, "only one 'backend'") {
|
|
|
|
t.Fatalf("bad: expected error has wrong text: %s", errorStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadJSONBasic(t *testing.T) {
|
|
|
|
raw, err := ioutil.ReadFile(filepath.Join(fixtureDir, "basic.tf.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := LoadJSON(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedAtlas := &AtlasConfig{Name: "mitchellh/foo"}
|
|
|
|
if !reflect.DeepEqual(c.Atlas, expectedAtlas) {
|
|
|
|
t.Fatalf("bad: %#v", c.Atlas)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := variablesStr(c.Variables)
|
|
|
|
if actual != strings.TrimSpace(basicVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = providerConfigsStr(c.ProviderConfigs)
|
|
|
|
if actual != strings.TrimSpace(basicProvidersStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(basicResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = outputsStr(c.Outputs)
|
|
|
|
if actual != strings.TrimSpace(basicOutputsStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 22:51:12 +02:00
|
|
|
func TestLoadJSONAmbiguous(t *testing.T) {
|
|
|
|
js := `
|
|
|
|
{
|
|
|
|
"variable": {
|
|
|
|
"first": {
|
|
|
|
"default": {
|
|
|
|
"key": "val"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"second": {
|
|
|
|
"description": "Described",
|
|
|
|
"default": {
|
|
|
|
"key": "val"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
c, err := LoadJSON([]byte(js))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Variables) != 2 {
|
|
|
|
t.Fatal("config should have 2 variables, found", len(c.Variables))
|
|
|
|
}
|
|
|
|
|
|
|
|
first := &Variable{
|
|
|
|
Name: "first",
|
|
|
|
Default: map[string]interface{}{"key": "val"},
|
|
|
|
}
|
|
|
|
second := &Variable{
|
|
|
|
Name: "second",
|
|
|
|
Description: "Described",
|
|
|
|
Default: map[string]interface{}{"key": "val"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(first, c.Variables[0]) {
|
|
|
|
t.Fatalf("\nexpected: %#v\ngot: %#v", first, c.Variables[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(second, c.Variables[1]) {
|
|
|
|
t.Fatalf("\nexpected: %#v\ngot: %#v", second, c.Variables[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:18:18 +02:00
|
|
|
func TestLoadFileBasic_jsonNoName(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "resource-no-name.tf.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(basicJsonNoNameResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFile_variables(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "variables.tf"))
|
2014-06-04 00:55:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2014-09-15 04:35:38 +02:00
|
|
|
if c.Dir != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:55:51 +02:00
|
|
|
actual := variablesStr(c.Variables)
|
|
|
|
if actual != strings.TrimSpace(variablesVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 05:15:09 +02:00
|
|
|
func TestLoadDir_basic(t *testing.T) {
|
2014-09-15 04:35:38 +02:00
|
|
|
dir := filepath.Join(fixtureDir, "dir-basic")
|
|
|
|
c, err := LoadDir(dir)
|
2014-07-12 05:15:09 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
2014-09-15 04:55:38 +02:00
|
|
|
dirAbs, err := filepath.Abs(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if c.Dir != dirAbs {
|
2014-09-15 04:35:38 +02:00
|
|
|
t.Fatalf("bad: %#v", c.Dir)
|
|
|
|
}
|
|
|
|
|
2014-07-12 05:15:09 +02:00
|
|
|
actual := variablesStr(c.Variables)
|
|
|
|
if actual != strings.TrimSpace(dirBasicVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = providerConfigsStr(c.ProviderConfigs)
|
|
|
|
if actual != strings.TrimSpace(dirBasicProvidersStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(dirBasicResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = outputsStr(c.Outputs)
|
|
|
|
if actual != strings.TrimSpace(dirBasicOutputsStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 17:34:43 +02:00
|
|
|
func TestLoadDir_file(t *testing.T) {
|
|
|
|
_, err := LoadDir(filepath.Join(fixtureDir, "variables.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:04:59 +02:00
|
|
|
func TestLoadDir_noConfigs(t *testing.T) {
|
|
|
|
_, err := LoadDir(filepath.Join(fixtureDir, "dir-empty"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:44:23 +02:00
|
|
|
func TestLoadDir_noMerge(t *testing.T) {
|
|
|
|
c, err := LoadDir(filepath.Join(fixtureDir, "dir-merge"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Validate(); err == nil {
|
|
|
|
t.Fatal("should not be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 02:52:46 +02:00
|
|
|
func TestLoadDir_override(t *testing.T) {
|
|
|
|
c, err := LoadDir(filepath.Join(fixtureDir, "dir-override"))
|
|
|
|
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(dirOverrideVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = providerConfigsStr(c.ProviderConfigs)
|
|
|
|
if actual != strings.TrimSpace(dirOverrideProvidersStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(dirOverrideResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = outputsStr(c.Outputs)
|
|
|
|
if actual != strings.TrimSpace(dirOverrideOutputsStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 22:48:21 +02:00
|
|
|
func TestLoadDir_overrideVar(t *testing.T) {
|
|
|
|
c, err := LoadDir(filepath.Join(fixtureDir, "dir-override-var"))
|
|
|
|
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(dirOverrideVarsVariablesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:33:16 +01:00
|
|
|
func TestLoadFile_mismatchedVariableTypes(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "variable-mismatched-type.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad: expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
errorStr := err.Error()
|
|
|
|
if !strings.Contains(errorStr, "'not_a_map' has a default value which is not of type 'string'") {
|
|
|
|
t.Fatalf("bad: expected error has wrong text: %s", errorStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadFile_badVariableTypes(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "bad-variable-type.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad: expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
errorStr := err.Error()
|
2017-04-20 21:41:33 +02:00
|
|
|
if !strings.Contains(errorStr, "'bad_type' type must be one of") {
|
2016-01-21 20:33:16 +01:00
|
|
|
t.Fatalf("bad: expected error has wrong text: %s", errorStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 22:59:16 +01:00
|
|
|
func TestLoadFile_variableNoName(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "variable-no-name.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad: expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
errorStr := err.Error()
|
2017-05-03 00:42:34 +02:00
|
|
|
if !strings.Contains(errorStr, `"variable" must be followed`) {
|
2016-11-02 22:59:16 +01:00
|
|
|
t.Fatalf("bad: expected error has wrong text: %s", errorStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFile_provisioners(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "provisioners.tf"))
|
2014-07-03 05:20:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(provisionerResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:10:21 +01:00
|
|
|
func TestLoadFile_provisionersDestroy(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "provisioners-destroy.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(provisionerDestroyResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 00:56:20 +01:00
|
|
|
func TestLoadFile_unnamedOutput(t *testing.T) {
|
|
|
|
_, err := LoadFile(filepath.Join(fixtureDir, "output-unnamed.tf"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad: expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
errorStr := err.Error()
|
2017-05-03 00:42:34 +02:00
|
|
|
if !strings.Contains(errorStr, `"output" must be followed`) {
|
2016-11-03 00:56:20 +01:00
|
|
|
t.Fatalf("bad: expected error has wrong text: %s", errorStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFile_connections(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "connection.tf"))
|
2014-07-11 20:37:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(connectionResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the connection info
|
|
|
|
r := c.Resources[0]
|
|
|
|
if r.Name != "web" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
p1 := r.Provisioners[0]
|
|
|
|
if p1.ConnInfo == nil || len(p1.ConnInfo.Raw) != 2 {
|
|
|
|
t.Fatalf("Bad: %#v", p1.ConnInfo)
|
|
|
|
}
|
|
|
|
if p1.ConnInfo.Raw["user"] != "nobody" {
|
|
|
|
t.Fatalf("Bad: %#v", p1.ConnInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
p2 := r.Provisioners[1]
|
|
|
|
if p2.ConnInfo == nil || len(p2.ConnInfo.Raw) != 2 {
|
|
|
|
t.Fatalf("Bad: %#v", p2.ConnInfo)
|
|
|
|
}
|
|
|
|
if p2.ConnInfo.Raw["user"] != "root" {
|
|
|
|
t.Fatalf("Bad: %#v", p2.ConnInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:15:26 +02:00
|
|
|
func TestLoadFile_createBeforeDestroy(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "create-before-destroy.tf"))
|
2014-09-22 22:31:20 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(createBeforeDestroyResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the flag value
|
|
|
|
r := c.Resources[0]
|
|
|
|
if r.Name != "web" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should enable create before destroy
|
2014-09-23 00:02:00 +02:00
|
|
|
if !r.Lifecycle.CreateBeforeDestroy {
|
2014-09-22 22:31:20 +02:00
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = c.Resources[1]
|
|
|
|
if r.Name != "bar" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not enable create before destroy
|
2014-09-23 00:02:00 +02:00
|
|
|
if r.Lifecycle.CreateBeforeDestroy {
|
2014-09-22 22:31:20 +02:00
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-09 10:02:28 +02:00
|
|
|
func TestLoadFile_ignoreChanges(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "ignore-changes.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
print(actual)
|
|
|
|
if actual != strings.TrimSpace(ignoreChangesResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the flag value
|
|
|
|
r := c.Resources[0]
|
|
|
|
if r.Name != "web" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should populate ignore changes
|
|
|
|
if len(r.Lifecycle.IgnoreChanges) == 0 {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = c.Resources[1]
|
|
|
|
if r.Name != "bar" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not populate ignore changes
|
|
|
|
if len(r.Lifecycle.IgnoreChanges) > 0 {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = c.Resources[2]
|
|
|
|
if r.Name != "baz" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not populate ignore changes
|
|
|
|
if len(r.Lifecycle.IgnoreChanges) > 0 {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 07:04:23 +02:00
|
|
|
func TestLoad_preventDestroyString(t *testing.T) {
|
2015-06-24 07:30:41 +02:00
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "prevent-destroy-string.tf"))
|
2015-06-08 07:04:23 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
if actual != strings.TrimSpace(createBeforeDestroyResourcesStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the flag value
|
|
|
|
r := c.Resources[0]
|
|
|
|
if r.Name != "web" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should enable create before destroy
|
|
|
|
if !r.Lifecycle.PreventDestroy {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = c.Resources[1]
|
|
|
|
if r.Name != "bar" && r.Type != "aws_instance" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not enable create before destroy
|
|
|
|
if r.Lifecycle.PreventDestroy {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 14:11:49 +01:00
|
|
|
func TestLoad_temporary_files(t *testing.T) {
|
|
|
|
_, err := LoadDir(filepath.Join(fixtureDir, "dir-temporary-files"))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to see an error stating no config files found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:06:30 +01:00
|
|
|
func TestLoad_hclAttributes(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "attributes.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
print(actual)
|
|
|
|
if actual != strings.TrimSpace(jsonAttributeStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := c.Resources[0]
|
|
|
|
if r.Name != "test" && r.Type != "cloudstack_firewall" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := r.RawConfig
|
|
|
|
if raw.Raw["ipaddress"] != "192.168.0.1" {
|
|
|
|
t.Fatalf("Bad: %s", raw.Raw["ipAddress"])
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := raw.Raw["rule"].([]map[string]interface{})[0]
|
|
|
|
if rule["protocol"] != "tcp" {
|
|
|
|
t.Fatalf("Bad: %s", rule["protocol"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule["source_cidr"] != "10.0.0.0/8" {
|
|
|
|
t.Fatalf("Bad: %s", rule["source_cidr"])
|
|
|
|
}
|
|
|
|
|
|
|
|
ports := rule["ports"].([]interface{})
|
|
|
|
|
|
|
|
if ports[0] != "80" {
|
|
|
|
t.Fatalf("Bad ports: %s", ports[0])
|
|
|
|
}
|
|
|
|
if ports[1] != "1000-2000" {
|
|
|
|
t.Fatalf("Bad ports: %s", ports[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoad_jsonAttributes(t *testing.T) {
|
|
|
|
c, err := LoadFile(filepath.Join(fixtureDir, "attributes.tf.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
t.Fatal("config should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resourcesStr(c.Resources)
|
|
|
|
print(actual)
|
|
|
|
if actual != strings.TrimSpace(jsonAttributeStr) {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := c.Resources[0]
|
|
|
|
if r.Name != "test" && r.Type != "cloudstack_firewall" {
|
|
|
|
t.Fatalf("Bad: %#v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := r.RawConfig
|
|
|
|
if raw.Raw["ipaddress"] != "192.168.0.1" {
|
|
|
|
t.Fatalf("Bad: %s", raw.Raw["ipAddress"])
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := raw.Raw["rule"].([]map[string]interface{})[0]
|
|
|
|
if rule["protocol"] != "tcp" {
|
|
|
|
t.Fatalf("Bad: %s", rule["protocol"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule["source_cidr"] != "10.0.0.0/8" {
|
|
|
|
t.Fatalf("Bad: %s", rule["source_cidr"])
|
|
|
|
}
|
|
|
|
|
|
|
|
ports := rule["ports"].([]interface{})
|
|
|
|
|
|
|
|
if ports[0] != "80" {
|
|
|
|
t.Fatalf("Bad ports: %s", ports[0])
|
|
|
|
}
|
|
|
|
if ports[1] != "1000-2000" {
|
|
|
|
t.Fatalf("Bad ports: %s", ports[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsonAttributeStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
cloudstack_firewall.test (x1)
|
2015-11-19 15:06:30 +01:00
|
|
|
ipaddress
|
|
|
|
rule
|
|
|
|
`
|
|
|
|
|
2015-12-01 19:26:38 +01:00
|
|
|
const windowsHeredocResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.test (x1)
|
2015-12-01 19:26:38 +01:00
|
|
|
user_data
|
|
|
|
`
|
|
|
|
|
2015-11-10 16:15:41 +01:00
|
|
|
const heredocProvidersStr = `
|
|
|
|
aws
|
|
|
|
access_key
|
|
|
|
secret_key
|
|
|
|
`
|
|
|
|
|
|
|
|
const heredocResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_iam_policy.policy (x1)
|
2015-11-10 16:15:41 +01:00
|
|
|
description
|
|
|
|
name
|
|
|
|
path
|
|
|
|
policy
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.heredocwithnumbers (x1)
|
2015-12-01 17:31:05 +01:00
|
|
|
ami
|
|
|
|
provisioners
|
|
|
|
local-exec
|
|
|
|
command
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.test (x1)
|
2015-11-25 19:50:16 +01:00
|
|
|
ami
|
|
|
|
provisioners
|
|
|
|
remote-exec
|
|
|
|
inline
|
2015-11-10 16:15:41 +01:00
|
|
|
`
|
|
|
|
|
2014-07-04 19:43:06 +02:00
|
|
|
const basicOutputsStr = `
|
|
|
|
web_ip
|
|
|
|
vars
|
|
|
|
resource: aws_instance.web.private_ip
|
|
|
|
`
|
|
|
|
|
2017-07-01 18:12:31 +02:00
|
|
|
const basicLocalsStr = `
|
|
|
|
literal
|
|
|
|
literal_list
|
|
|
|
literal_map
|
|
|
|
security_group_ids
|
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.*.id
|
|
|
|
web_ip
|
|
|
|
vars
|
|
|
|
resource: aws_instance.web.private_ip
|
|
|
|
`
|
|
|
|
|
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 = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.db (x1)
|
2014-07-23 17:38:43 +02:00
|
|
|
VPC
|
2014-07-08 23:57:47 +02:00
|
|
|
security_groups
|
2015-01-16 19:14:48 +01:00
|
|
|
provisioners
|
|
|
|
file
|
|
|
|
destination
|
|
|
|
source
|
2014-07-23 02:10:17 +02:00
|
|
|
dependsOn
|
|
|
|
aws_instance.web
|
2014-07-08 23:57:47 +02:00
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.*.id
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.web (x1)
|
2014-05-23 01:56:28 +02:00
|
|
|
ami
|
2014-07-02 18:11:31 +02:00
|
|
|
network_interface
|
2014-05-23 01:56:28 +02:00
|
|
|
security_groups
|
2015-01-16 19:14:48 +01:00
|
|
|
provisioners
|
|
|
|
file
|
|
|
|
destination
|
|
|
|
source
|
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
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_security_group.firewall (x5)
|
2016-01-17 01:20:00 +01:00
|
|
|
data.do.depends (x1)
|
|
|
|
dependsOn
|
|
|
|
data.do.simple
|
|
|
|
data.do.simple (x1)
|
|
|
|
foo
|
2014-05-23 01:56:28 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const basicVariablesStr = `
|
2016-01-21 20:33:16 +01:00
|
|
|
bar (required) (string)
|
|
|
|
<>
|
|
|
|
<>
|
|
|
|
baz (map)
|
|
|
|
map[key:value]
|
|
|
|
<>
|
2014-05-23 01:56:28 +02:00
|
|
|
foo
|
|
|
|
bar
|
|
|
|
bar
|
|
|
|
`
|
2014-05-24 00:11:57 +02:00
|
|
|
|
2016-08-26 02:18:18 +02:00
|
|
|
const basicJsonNoNameResourcesStr = `
|
|
|
|
aws_security_group.allow_external_http_https (x1)
|
|
|
|
tags
|
|
|
|
`
|
|
|
|
|
2014-07-12 05:15:09 +02:00
|
|
|
const dirBasicOutputsStr = `
|
|
|
|
web_ip
|
|
|
|
vars
|
|
|
|
resource: aws_instance.web.private_ip
|
|
|
|
`
|
|
|
|
|
|
|
|
const dirBasicProvidersStr = `
|
|
|
|
aws
|
|
|
|
access_key
|
|
|
|
secret_key
|
|
|
|
do
|
|
|
|
api_key
|
|
|
|
vars
|
|
|
|
user: var.foo
|
|
|
|
`
|
|
|
|
|
|
|
|
const dirBasicResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.db (x1)
|
2014-07-12 05:15:09 +02:00
|
|
|
security_groups
|
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.*.id
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.web (x1)
|
2014-07-12 05:15:09 +02:00
|
|
|
ami
|
|
|
|
network_interface
|
|
|
|
security_groups
|
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.foo
|
|
|
|
user: var.foo
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_security_group.firewall (x5)
|
2016-01-17 01:20:00 +01:00
|
|
|
data.do.depends (x1)
|
|
|
|
dependsOn
|
|
|
|
data.do.simple
|
|
|
|
data.do.simple (x1)
|
|
|
|
foo
|
2014-07-12 05:15:09 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const dirBasicVariablesStr = `
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
bar
|
|
|
|
`
|
|
|
|
|
2014-07-21 02:52:46 +02:00
|
|
|
const dirOverrideOutputsStr = `
|
|
|
|
web_ip
|
|
|
|
vars
|
|
|
|
resource: aws_instance.web.private_ip
|
|
|
|
`
|
|
|
|
|
|
|
|
const dirOverrideProvidersStr = `
|
|
|
|
aws
|
|
|
|
access_key
|
|
|
|
secret_key
|
|
|
|
do
|
|
|
|
api_key
|
|
|
|
vars
|
|
|
|
user: var.foo
|
|
|
|
`
|
|
|
|
|
|
|
|
const dirOverrideResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.db (x1)
|
2014-07-21 02:52:46 +02:00
|
|
|
ami
|
|
|
|
security_groups
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.web (x1)
|
2014-07-21 02:52:46 +02:00
|
|
|
ami
|
2014-07-21 02:56:02 +02:00
|
|
|
foo
|
2014-07-21 02:52:46 +02:00
|
|
|
network_interface
|
|
|
|
security_groups
|
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.foo
|
|
|
|
user: var.foo
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_security_group.firewall (x5)
|
2016-01-17 01:20:00 +01:00
|
|
|
data.do.depends (x1)
|
|
|
|
hello
|
|
|
|
dependsOn
|
|
|
|
data.do.simple
|
|
|
|
data.do.simple (x1)
|
|
|
|
foo
|
2014-07-21 02:52:46 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const dirOverrideVariablesStr = `
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
bar
|
|
|
|
`
|
|
|
|
|
2016-08-26 22:48:21 +02:00
|
|
|
const dirOverrideVarsVariablesStr = `
|
|
|
|
foo
|
|
|
|
baz
|
|
|
|
bar
|
|
|
|
`
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
const importProvidersStr = `
|
|
|
|
aws
|
2014-07-21 02:17:03 +02:00
|
|
|
bar
|
2014-05-26 03:05:18 +02:00
|
|
|
foo
|
|
|
|
`
|
|
|
|
|
2014-05-24 01:25:54 +02:00
|
|
|
const importResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_security_group.db (x1)
|
|
|
|
aws_security_group.web (x1)
|
2014-05-24 01:25:54 +02:00
|
|
|
`
|
|
|
|
|
2014-05-24 00:11:57 +02:00
|
|
|
const importVariablesStr = `
|
2014-07-19 02:48:30 +02:00
|
|
|
bar (required)
|
2014-05-24 01:09:41 +02:00
|
|
|
<>
|
|
|
|
<>
|
2014-05-24 00:11:57 +02:00
|
|
|
foo
|
|
|
|
bar
|
|
|
|
bar
|
|
|
|
`
|
2014-06-04 00:55:51 +02:00
|
|
|
|
2014-09-12 00:58:30 +02:00
|
|
|
const modulesModulesStr = `
|
2014-09-15 20:45:41 +02:00
|
|
|
bar
|
2014-09-12 00:58:30 +02:00
|
|
|
source = baz
|
|
|
|
memory
|
|
|
|
`
|
|
|
|
|
2014-07-03 05:20:13 +02:00
|
|
|
const provisionerResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.web (x1)
|
2014-07-03 05:20:13 +02:00
|
|
|
ami
|
|
|
|
security_groups
|
|
|
|
provisioners
|
|
|
|
shell
|
|
|
|
path
|
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.foo
|
|
|
|
user: var.foo
|
|
|
|
`
|
|
|
|
|
2017-01-20 03:10:21 +01:00
|
|
|
const provisionerDestroyResourcesStr = `
|
|
|
|
aws_instance.web (x1)
|
|
|
|
provisioners
|
|
|
|
shell
|
|
|
|
shell (destroy)
|
|
|
|
path
|
|
|
|
shell (destroy)
|
|
|
|
on_failure = continue
|
|
|
|
path
|
|
|
|
`
|
|
|
|
|
2014-07-11 20:37:04 +02:00
|
|
|
const connectionResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.web (x1)
|
2014-07-11 20:37:04 +02:00
|
|
|
ami
|
|
|
|
security_groups
|
|
|
|
provisioners
|
|
|
|
shell
|
|
|
|
path
|
|
|
|
shell
|
|
|
|
path
|
|
|
|
vars
|
|
|
|
resource: aws_security_group.firewall.foo
|
|
|
|
user: var.foo
|
|
|
|
`
|
|
|
|
|
2016-11-12 02:46:34 +01:00
|
|
|
const outputDependsOnStr = `
|
|
|
|
value
|
|
|
|
dependsOn
|
|
|
|
foo
|
|
|
|
`
|
|
|
|
|
2014-06-04 00:55:51 +02:00
|
|
|
const variablesVariablesStr = `
|
|
|
|
bar
|
|
|
|
<>
|
|
|
|
<>
|
|
|
|
baz
|
|
|
|
foo
|
|
|
|
<>
|
2014-07-19 02:48:30 +02:00
|
|
|
foo (required)
|
2014-07-01 19:28:42 +02:00
|
|
|
<>
|
|
|
|
<>
|
2014-06-04 00:55:51 +02:00
|
|
|
`
|
2014-09-22 22:31:20 +02:00
|
|
|
|
|
|
|
const createBeforeDestroyResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.bar (x1)
|
2014-09-22 22:31:20 +02:00
|
|
|
ami
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.web (x1)
|
2014-09-22 22:31:20 +02:00
|
|
|
ami
|
|
|
|
`
|
2015-08-09 10:02:28 +02:00
|
|
|
|
|
|
|
const ignoreChangesResourcesStr = `
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.bar (x1)
|
2015-08-09 10:02:28 +02:00
|
|
|
ami
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.baz (x1)
|
2015-08-09 10:02:28 +02:00
|
|
|
ami
|
2016-05-01 23:43:05 +02:00
|
|
|
aws_instance.web (x1)
|
2015-08-09 10:02:28 +02:00
|
|
|
ami
|
|
|
|
`
|