github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/helper/config/validator_test.go (about)

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