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)
|
|
|
|
}
|
|
|
|
|
2014-07-15 03:22:05 +02:00
|
|
|
func TestValidator_array(t *testing.T) {
|
|
|
|
v := &Validator{
|
|
|
|
Required: []string{
|
|
|
|
"foo",
|
|
|
|
"nested.*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var c *terraform.ResourceConfig
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
2014-10-10 06:29:21 +02:00
|
|
|
"nested": []interface{}{"foo", "bar"},
|
2014-07-15 03:22:05 +02:00
|
|
|
})
|
|
|
|
testValid(v, c)
|
|
|
|
|
|
|
|
// Not a nested structure
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"nested": "baa",
|
|
|
|
})
|
|
|
|
testInvalid(v, c)
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:14:07 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-07-17 00:07:46 +02:00
|
|
|
func TestValidator_complexNested(t *testing.T) {
|
|
|
|
v := &Validator{
|
|
|
|
Required: []string{
|
|
|
|
"ingress.*",
|
|
|
|
"ingress.*.from_port",
|
|
|
|
},
|
|
|
|
|
|
|
|
Optional: []string{
|
|
|
|
"ingress.*.cidr_blocks.*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var c *terraform.ResourceConfig
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"ingress": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"from_port": "80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
testValid(v, c)
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"ingress": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"from_port": "80",
|
2014-10-10 06:29:21 +02:00
|
|
|
"cidr_blocks": []interface{}{"foo"},
|
2014-07-17 00:07:46 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
testValid(v, c)
|
|
|
|
}
|
|
|
|
|
2014-07-11 02:05:40 +02:00
|
|
|
func TestValidator_complexDeepRequired(t *testing.T) {
|
|
|
|
v := &Validator{
|
|
|
|
Required: []string{
|
|
|
|
"foo",
|
|
|
|
"nested.*.foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
c = testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
})
|
2014-07-17 00:07:46 +02:00
|
|
|
testInvalid(v, c)
|
2014-07-11 02:05:40 +02:00
|
|
|
|
|
|
|
// 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-11 02:05:40 +02:00
|
|
|
estrs := make([]string, len(es))
|
|
|
|
for i, e := range es {
|
|
|
|
estrs[i] = e.Error()
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("bad: %#v", estrs))
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
}
|