2014-07-08 19:02:02 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2014-07-08 20:14:07 +02:00
|
|
|
"fmt"
|
2014-07-08 19:02:02 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidator(t *testing.T) {
|
|
|
|
v := &Validator{
|
|
|
|
Required: []string{"foo"},
|
|
|
|
Optional: []string{"bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var c *terraform.ResourceConfig
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
})
|
2014-07-08 20:14:07 +02:00
|
|
|
testValid(v, c)
|
|
|
|
|
|
|
|
// Valid + optional
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
})
|
|
|
|
testValid(v, c)
|
2014-07-08 19:02:02 +02:00
|
|
|
|
|
|
|
// Missing required
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"bar": "baz",
|
|
|
|
})
|
2014-07-08 20:14:07 +02:00
|
|
|
testInvalid(v, c)
|
2014-07-08 19:02:02 +02:00
|
|
|
|
|
|
|
// Unknown key
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"what": "what",
|
|
|
|
})
|
2014-07-08 20:14:07 +02:00
|
|
|
testInvalid(v, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidator_complex(t *testing.T) {
|
|
|
|
v := &Validator{
|
|
|
|
Required: []string{
|
|
|
|
"foo",
|
|
|
|
"nested.*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var c *terraform.ResourceConfig
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"nested": []map[string]interface{}{
|
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
testValid(v, c)
|
|
|
|
|
|
|
|
// Not a nested structure
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"nested": "baa",
|
|
|
|
})
|
|
|
|
testInvalid(v, c)
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testConfig(
|
|
|
|
t *testing.T,
|
|
|
|
c map[string]interface{}) *terraform.ResourceConfig {
|
|
|
|
r, err := config.NewRawConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return terraform.NewResourceConfig(r)
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:14:07 +02:00
|
|
|
func testInvalid(v *Validator, c *terraform.ResourceConfig) {
|
2014-07-08 19:02:02 +02:00
|
|
|
ws, es := v.Validate(c)
|
|
|
|
if len(ws) > 0 {
|
2014-07-08 20:14:07 +02:00
|
|
|
panic(fmt.Sprintf("bad: %#v", ws))
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
if len(es) == 0 {
|
2014-07-08 20:14:07 +02:00
|
|
|
panic(fmt.Sprintf("bad: %#v", es))
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:14:07 +02:00
|
|
|
func testValid(v *Validator, c *terraform.ResourceConfig) {
|
2014-07-08 19:02:02 +02:00
|
|
|
ws, es := v.Validate(c)
|
|
|
|
if len(ws) > 0 {
|
2014-07-08 20:14:07 +02:00
|
|
|
panic(fmt.Sprintf("bad: %#v", ws))
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
if len(es) > 0 {
|
2014-07-08 20:14:07 +02:00
|
|
|
panic(fmt.Sprintf("bad: %#v", es))
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
}
|