175 lines
3.5 KiB
Go
175 lines
3.5 KiB
Go
package config
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// This is the directory where our test fixtures are.
|
|
const fixtureDir = "./test-fixtures"
|
|
|
|
func TestConfigValidate(t *testing.T) {
|
|
c := testConfig(t, "validate-good")
|
|
if err := c.Validate(); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
func TestConfigValidate_badMultiResource(t *testing.T) {
|
|
c := testConfig(t, "validate-bad-multi-resource")
|
|
if err := c.Validate(); err == nil {
|
|
t.Fatal("should not be valid")
|
|
}
|
|
}
|
|
|
|
func TestConfigValidate_dupResource(t *testing.T) {
|
|
c := testConfig(t, "validate-dup-resource")
|
|
if err := c.Validate(); err == nil {
|
|
t.Fatal("should not be valid")
|
|
}
|
|
}
|
|
|
|
func TestConfigValidate_outputBadField(t *testing.T) {
|
|
c := testConfig(t, "validate-output-bad-field")
|
|
if err := c.Validate(); err == nil {
|
|
t.Fatal("should not be valid")
|
|
}
|
|
}
|
|
|
|
func TestConfigValidate_unknownThing(t *testing.T) {
|
|
c := testConfig(t, "validate-unknownthing")
|
|
if err := c.Validate(); err == nil {
|
|
t.Fatal("should not be valid")
|
|
}
|
|
}
|
|
|
|
func TestConfigValidate_unknownResourceVar(t *testing.T) {
|
|
c := testConfig(t, "validate-unknown-resource-var")
|
|
if err := c.Validate(); err == nil {
|
|
t.Fatal("should not be valid")
|
|
}
|
|
}
|
|
|
|
func TestConfigValidate_unknownResourceVar_output(t *testing.T) {
|
|
c := testConfig(t, "validate-unknown-resource-var-output")
|
|
if err := c.Validate(); err == nil {
|
|
t.Fatal("should not be valid")
|
|
}
|
|
}
|
|
|
|
func TestConfigValidate_unknownVar(t *testing.T) {
|
|
c := testConfig(t, "validate-unknownvar")
|
|
if err := c.Validate(); err == nil {
|
|
t.Fatal("should not be valid")
|
|
}
|
|
}
|
|
|
|
func TestNewResourceVariable(t *testing.T) {
|
|
v, err := NewResourceVariable("foo.bar.baz")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if v.Type != "foo" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
if v.Name != "bar" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
if v.Field != "baz" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
if v.Multi {
|
|
t.Fatal("should not be multi")
|
|
}
|
|
|
|
if v.FullKey() != "foo.bar.baz" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
}
|
|
|
|
func TestResourceVariable_Multi(t *testing.T) {
|
|
v, err := NewResourceVariable("foo.bar.*.baz")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if v.Type != "foo" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
if v.Name != "bar" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
if v.Field != "baz" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
if !v.Multi {
|
|
t.Fatal("should be multi")
|
|
}
|
|
}
|
|
|
|
func TestResourceVariable_MultiIndex(t *testing.T) {
|
|
cases := []struct {
|
|
Input string
|
|
Index int
|
|
Field string
|
|
}{
|
|
{"foo.bar.*.baz", -1, "baz"},
|
|
{"foo.bar.0.baz", 0, "baz"},
|
|
{"foo.bar.5.baz", 5, "baz"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
v, err := NewResourceVariable(tc.Input)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if !v.Multi {
|
|
t.Fatalf("should be multi: %s", tc.Input)
|
|
}
|
|
if v.Index != tc.Index {
|
|
t.Fatalf("bad: %d\n\n%s", v.Index, tc.Input)
|
|
}
|
|
if v.Field != tc.Field {
|
|
t.Fatalf("bad: %s\n\n%s", v.Field, tc.Input)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewUserVariable(t *testing.T) {
|
|
v, err := NewUserVariable("var.bar")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if v.Name != "bar" {
|
|
t.Fatalf("bad: %#v", v.Name)
|
|
}
|
|
if v.FullKey() != "var.bar" {
|
|
t.Fatalf("bad: %#v", v)
|
|
}
|
|
}
|
|
|
|
func TestProviderConfigName(t *testing.T) {
|
|
pcs := []*ProviderConfig{
|
|
&ProviderConfig{Name: "aw"},
|
|
&ProviderConfig{Name: "aws"},
|
|
&ProviderConfig{Name: "a"},
|
|
&ProviderConfig{Name: "gce_"},
|
|
}
|
|
|
|
n := ProviderConfigName("aws_instance", pcs)
|
|
if n != "aws" {
|
|
t.Fatalf("bad: %s", n)
|
|
}
|
|
}
|
|
|
|
func testConfig(t *testing.T, name string) *Config {
|
|
c, err := Load(filepath.Join(fixtureDir, name, "main.tf"))
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
return c
|
|
}
|