From e524d1eb95c22e5b37dade909935f550db8b2190 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 2 Feb 2018 17:24:28 -0800 Subject: [PATCH] configs: Simple test of loading valid configuration files This test is intended to be an easy-to-maintain catalog of good examples that we can use to catch certain parsing or decoding regressions easily. It's not a fully-comprehensive test since it doesn't check the result of decoding, instead just accepting any decode that completes without errors. However, an easy-to-maintain test like this is a good complement to some more specialized tests since we can easily collect good examples over time and just add them in here. --- configs/parser_config_test.go | 43 +++++++++++++++++++ configs/test-fixtures/valid-files/backend.tf | 10 +++++ .../test-fixtures/valid-files/data-sources.tf | 15 +++++++ configs/test-fixtures/valid-files/empty.tf | 0 .../test-fixtures/valid-files/empty.tf.json | 1 + configs/test-fixtures/valid-files/locals.tf | 16 +++++++ .../test-fixtures/valid-files/locals.tf.json | 10 +++++ .../test-fixtures/valid-files/module-calls.tf | 26 +++++++++++ configs/test-fixtures/valid-files/outputs.tf | 25 +++++++++++ .../valid-files/provider-configs.tf | 15 +++++++ .../valid-files/required-providers.tf | 7 +++ .../valid-files/required-version.tf | 4 ++ .../test-fixtures/valid-files/resources.tf | 42 ++++++++++++++++++ .../test-fixtures/valid-files/variables.tf | 24 +++++++++++ .../valid-files/variables.tf.json | 21 +++++++++ 15 files changed, 259 insertions(+) create mode 100644 configs/parser_config_test.go create mode 100644 configs/test-fixtures/valid-files/backend.tf create mode 100644 configs/test-fixtures/valid-files/data-sources.tf create mode 100644 configs/test-fixtures/valid-files/empty.tf create mode 100644 configs/test-fixtures/valid-files/empty.tf.json create mode 100644 configs/test-fixtures/valid-files/locals.tf create mode 100644 configs/test-fixtures/valid-files/locals.tf.json create mode 100644 configs/test-fixtures/valid-files/module-calls.tf create mode 100644 configs/test-fixtures/valid-files/outputs.tf create mode 100644 configs/test-fixtures/valid-files/provider-configs.tf create mode 100644 configs/test-fixtures/valid-files/required-providers.tf create mode 100644 configs/test-fixtures/valid-files/required-version.tf create mode 100644 configs/test-fixtures/valid-files/resources.tf create mode 100644 configs/test-fixtures/valid-files/variables.tf create mode 100644 configs/test-fixtures/valid-files/variables.tf.json diff --git a/configs/parser_config_test.go b/configs/parser_config_test.go new file mode 100644 index 0000000000..affc946267 --- /dev/null +++ b/configs/parser_config_test.go @@ -0,0 +1,43 @@ +package configs + +import ( + "io/ioutil" + "path/filepath" + "testing" +) + +// TestParseLoadConfigFileSuccess is a simple test that just verifies that +// a number of test configuration files (in test-fixtures/valid-files) can +// be parsed without raising any diagnostics. +// +// This test does not verify that reading these files produces the correct +// file element contents. More detailed assertions may be made on some subset +// of these configuration files in other tests. +func TestParserLoadConfigFileSuccess(t *testing.T) { + files, err := ioutil.ReadDir("test-fixtures/valid-files") + if err != nil { + t.Fatal(err) + } + + for _, info := range files { + name := info.Name() + t.Run(name, func(t *testing.T) { + src, err := ioutil.ReadFile(filepath.Join("test-fixtures/valid-files", name)) + if err != nil { + t.Fatal(err) + } + + parser := testParser(map[string]string{ + name: string(src), + }) + + _, diags := parser.LoadConfigFile(name) + if len(diags) != 0 { + t.Errorf("unexpected diagnostics") + for _, diag := range diags { + t.Logf("- %s", diag) + } + } + }) + } +} diff --git a/configs/test-fixtures/valid-files/backend.tf b/configs/test-fixtures/valid-files/backend.tf new file mode 100644 index 0000000000..bd8e0f669a --- /dev/null +++ b/configs/test-fixtures/valid-files/backend.tf @@ -0,0 +1,10 @@ + +terraform { + backend "example" { + foo = "bar" + + baz { + bar = "foo" + } + } +} diff --git a/configs/test-fixtures/valid-files/data-sources.tf b/configs/test-fixtures/valid-files/data-sources.tf new file mode 100644 index 0000000000..f14dffdac6 --- /dev/null +++ b/configs/test-fixtures/valid-files/data-sources.tf @@ -0,0 +1,15 @@ +data "http" "example1" { +} + +data "http" "example2" { + url = "http://example.com/" + + request_headers = { + "Accept" = "application/json" + } + + count = 5 + depends_on = [ + data.http.example1, + ] +} diff --git a/configs/test-fixtures/valid-files/empty.tf b/configs/test-fixtures/valid-files/empty.tf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/configs/test-fixtures/valid-files/empty.tf.json b/configs/test-fixtures/valid-files/empty.tf.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/configs/test-fixtures/valid-files/empty.tf.json @@ -0,0 +1 @@ +{} diff --git a/configs/test-fixtures/valid-files/locals.tf b/configs/test-fixtures/valid-files/locals.tf new file mode 100644 index 0000000000..f2ee4a7c4c --- /dev/null +++ b/configs/test-fixtures/valid-files/locals.tf @@ -0,0 +1,16 @@ + +locals { + # This block intentionally left blank +} + +locals { + foo = "foo" + bar = true +} + +locals { + baz = "oink" + dunno = "🤷" + rowing = "🚣‍♀️" + π = 3.14159265359 +} diff --git a/configs/test-fixtures/valid-files/locals.tf.json b/configs/test-fixtures/valid-files/locals.tf.json new file mode 100644 index 0000000000..525f69d6fd --- /dev/null +++ b/configs/test-fixtures/valid-files/locals.tf.json @@ -0,0 +1,10 @@ +{ + "locals": { + "foo": "foo", + "bar": true, + "baz": "oink", + "dunno": "🤷", + "rowing": "🚣‍♀️", + "π": 3.14159265359 + } +} diff --git a/configs/test-fixtures/valid-files/module-calls.tf b/configs/test-fixtures/valid-files/module-calls.tf new file mode 100644 index 0000000000..abd4235868 --- /dev/null +++ b/configs/test-fixtures/valid-files/module-calls.tf @@ -0,0 +1,26 @@ + +module "foo" { + source = "./foo" + # this block intentionally left (almost) blank +} + +module "bar" { + source = "hashicorp/bar/aws" + + boom = "🎆" + yes = true +} + +module "baz" { + source = "git::https://example.com/" + + a = 1 + + count = 12 + for_each = ["a", "b", "c"] + + depends_on = [ + module.bar, + ] +} + diff --git a/configs/test-fixtures/valid-files/outputs.tf b/configs/test-fixtures/valid-files/outputs.tf new file mode 100644 index 0000000000..7a8066686f --- /dev/null +++ b/configs/test-fixtures/valid-files/outputs.tf @@ -0,0 +1,25 @@ + +output "foo" { + value = "hello" +} + +output "bar" { + value = local.bar +} + +output "baz" { + value = "ssshhhhhhh" + sensitive = true +} + +output "cheeze_pizza" { + description = "Nothing special" + value = "🍕" +} + +output "π" { + value = 3.14159265359 + depends_on = [ + pizza.cheese, + ] +} diff --git a/configs/test-fixtures/valid-files/provider-configs.tf b/configs/test-fixtures/valid-files/provider-configs.tf new file mode 100644 index 0000000000..a07f08be88 --- /dev/null +++ b/configs/test-fixtures/valid-files/provider-configs.tf @@ -0,0 +1,15 @@ + +provider "foo" { +} + +provider "bar" { + version = ">= 1.0.2" + + other = 12 +} + +provider "bar" { + other = 13 + + alias = "bar" +} diff --git a/configs/test-fixtures/valid-files/required-providers.tf b/configs/test-fixtures/valid-files/required-providers.tf new file mode 100644 index 0000000000..271df4a57f --- /dev/null +++ b/configs/test-fixtures/valid-files/required-providers.tf @@ -0,0 +1,7 @@ + +terraform { + required_providers { + aws = "~> 1.0.0" + consul = "~> 1.2.0" + } +} diff --git a/configs/test-fixtures/valid-files/required-version.tf b/configs/test-fixtures/valid-files/required-version.tf new file mode 100644 index 0000000000..77c9a35706 --- /dev/null +++ b/configs/test-fixtures/valid-files/required-version.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = "~> 0.12.0" +} diff --git a/configs/test-fixtures/valid-files/resources.tf b/configs/test-fixtures/valid-files/resources.tf new file mode 100644 index 0000000000..53fb745335 --- /dev/null +++ b/configs/test-fixtures/valid-files/resources.tf @@ -0,0 +1,42 @@ +resource "aws_security_group" "firewall" { + lifecycle { + create_before_destroy = true + prevent_destroy = true + ignore_changes = [ + description, + ] + } + + connection { + host = "127.0.0.1" + } + + provisioner "local-exec" { + command = "echo hello" + + connection { + host = "10.1.2.1" + } + } + + provisioner "local-exec" { + command = "echo hello" + } +} + +resource "aws_instance" "web" { + ami = "ami-1234" + security_groups = [ + "foo", + "bar", + ] + + network_interface { + device_index = 0 + description = "Main network interface" + } + + depends_on = [ + aws_security_group.firewall, + ] +} diff --git a/configs/test-fixtures/valid-files/variables.tf b/configs/test-fixtures/valid-files/variables.tf new file mode 100644 index 0000000000..185d9de555 --- /dev/null +++ b/configs/test-fixtures/valid-files/variables.tf @@ -0,0 +1,24 @@ + +variable "foo" { +} + +variable "bar" { + default = "hello" +} + +variable "baz" { + type = list +} + +variable "bar-baz" { + default = [] + type = list +} + +variable "cheeze_pizza" { + description = "Nothing special" +} + +variable "π" { + default = 3.14159265359 +} diff --git a/configs/test-fixtures/valid-files/variables.tf.json b/configs/test-fixtures/valid-files/variables.tf.json new file mode 100644 index 0000000000..3c97d61786 --- /dev/null +++ b/configs/test-fixtures/valid-files/variables.tf.json @@ -0,0 +1,21 @@ +{ + "variable": { + "foo": {}, + "bar": { + "default": "hello" + }, + "baz": { + "type": "list" + }, + "bar-baz": { + "default": [], + "type": "list" + }, + "cheese_pizza": { + "description": "Nothing special" + }, + "π": { + "default": 3.14159265359 + } + } +}