github.com/sylr/terraform@v0.11.12-beta1/helper/config/validator_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestValidator(t *testing.T) {
    12  	v := &Validator{
    13  		Required: []string{"foo"},
    14  		Optional: []string{"bar"},
    15  	}
    16  
    17  	var c *terraform.ResourceConfig
    18  
    19  	// Valid
    20  	c = testConfig(t, map[string]interface{}{
    21  		"foo": "bar",
    22  	})
    23  	testValid(v, c)
    24  
    25  	// Valid + optional
    26  	c = testConfig(t, map[string]interface{}{
    27  		"foo": "bar",
    28  		"bar": "baz",
    29  	})
    30  	testValid(v, c)
    31  
    32  	// Missing required
    33  	c = testConfig(t, map[string]interface{}{
    34  		"bar": "baz",
    35  	})
    36  	testInvalid(v, c)
    37  
    38  	// Unknown key
    39  	c = testConfig(t, map[string]interface{}{
    40  		"foo":  "bar",
    41  		"what": "what",
    42  	})
    43  	testInvalid(v, c)
    44  }
    45  
    46  func TestValidator_array(t *testing.T) {
    47  	v := &Validator{
    48  		Required: []string{
    49  			"foo",
    50  			"nested.*",
    51  		},
    52  	}
    53  
    54  	var c *terraform.ResourceConfig
    55  
    56  	// Valid
    57  	c = testConfig(t, map[string]interface{}{
    58  		"foo":    "bar",
    59  		"nested": []interface{}{"foo", "bar"},
    60  	})
    61  	testValid(v, c)
    62  
    63  	// Not a nested structure
    64  	c = testConfig(t, map[string]interface{}{
    65  		"foo":    "bar",
    66  		"nested": "baa",
    67  	})
    68  	testInvalid(v, c)
    69  }
    70  
    71  func TestValidator_complex(t *testing.T) {
    72  	v := &Validator{
    73  		Required: []string{
    74  			"foo",
    75  			"nested.*",
    76  		},
    77  	}
    78  
    79  	var c *terraform.ResourceConfig
    80  
    81  	// Valid
    82  	c = testConfig(t, map[string]interface{}{
    83  		"foo": "bar",
    84  		"nested": []map[string]interface{}{
    85  			map[string]interface{}{"foo": "bar"},
    86  		},
    87  	})
    88  	testValid(v, c)
    89  
    90  	// Not a nested structure
    91  	c = testConfig(t, map[string]interface{}{
    92  		"foo":    "bar",
    93  		"nested": "baa",
    94  	})
    95  	testInvalid(v, c)
    96  }
    97  
    98  func TestValidator_complexNested(t *testing.T) {
    99  	v := &Validator{
   100  		Required: []string{
   101  			"ingress.*",
   102  			"ingress.*.from_port",
   103  		},
   104  
   105  		Optional: []string{
   106  			"ingress.*.cidr_blocks.*",
   107  		},
   108  	}
   109  
   110  	var c *terraform.ResourceConfig
   111  
   112  	// Valid
   113  	c = testConfig(t, map[string]interface{}{
   114  		"ingress": []map[string]interface{}{
   115  			map[string]interface{}{
   116  				"from_port": "80",
   117  			},
   118  		},
   119  	})
   120  	testValid(v, c)
   121  
   122  	// Valid
   123  	c = testConfig(t, map[string]interface{}{
   124  		"ingress": []map[string]interface{}{
   125  			map[string]interface{}{
   126  				"from_port":   "80",
   127  				"cidr_blocks": []interface{}{"foo"},
   128  			},
   129  		},
   130  	})
   131  	testValid(v, c)
   132  }
   133  
   134  func TestValidator_complexDeepRequired(t *testing.T) {
   135  	v := &Validator{
   136  		Required: []string{
   137  			"foo",
   138  			"nested.*.foo",
   139  		},
   140  	}
   141  
   142  	var c *terraform.ResourceConfig
   143  
   144  	// Valid
   145  	c = testConfig(t, map[string]interface{}{
   146  		"foo": "bar",
   147  		"nested": []map[string]interface{}{
   148  			map[string]interface{}{"foo": "bar"},
   149  		},
   150  	})
   151  	testValid(v, c)
   152  
   153  	// Valid
   154  	c = testConfig(t, map[string]interface{}{
   155  		"foo": "bar",
   156  	})
   157  	testInvalid(v, c)
   158  
   159  	// Not a nested structure
   160  	c = testConfig(t, map[string]interface{}{
   161  		"foo":    "bar",
   162  		"nested": "baa",
   163  	})
   164  	testInvalid(v, c)
   165  }
   166  
   167  func testConfig(
   168  	t *testing.T,
   169  	c map[string]interface{}) *terraform.ResourceConfig {
   170  	r, err := config.NewRawConfig(c)
   171  	if err != nil {
   172  		t.Fatalf("bad: %s", err)
   173  	}
   174  
   175  	return terraform.NewResourceConfig(r)
   176  }
   177  
   178  func testInvalid(v *Validator, c *terraform.ResourceConfig) {
   179  	ws, es := v.Validate(c)
   180  	if len(ws) > 0 {
   181  		panic(fmt.Sprintf("bad: %#v", ws))
   182  	}
   183  	if len(es) == 0 {
   184  		panic(fmt.Sprintf("bad: %#v", es))
   185  	}
   186  }
   187  
   188  func testValid(v *Validator, c *terraform.ResourceConfig) {
   189  	ws, es := v.Validate(c)
   190  	if len(ws) > 0 {
   191  		panic(fmt.Sprintf("bad: %#v", ws))
   192  	}
   193  	if len(es) > 0 {
   194  		estrs := make([]string, len(es))
   195  		for i, e := range es {
   196  			estrs[i] = e.Error()
   197  		}
   198  		panic(fmt.Sprintf("bad: %#v", estrs))
   199  	}
   200  }