github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/helper/validation/validation_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  type testCase struct {
    11  	val         interface{}
    12  	f           schema.SchemaValidateFunc
    13  	expectedErr *regexp.Regexp
    14  }
    15  
    16  func TestValidationIntBetween(t *testing.T) {
    17  	runTestCases(t, []testCase{
    18  		{
    19  			val: 1,
    20  			f:   IntBetween(1, 1),
    21  		},
    22  		{
    23  			val: 1,
    24  			f:   IntBetween(0, 2),
    25  		},
    26  		{
    27  			val:         1,
    28  			f:           IntBetween(2, 3),
    29  			expectedErr: regexp.MustCompile("expected [\\w]+ to be in the range \\(2 - 3\\), got 1"),
    30  		},
    31  		{
    32  			val:         "1",
    33  			f:           IntBetween(2, 3),
    34  			expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"),
    35  		},
    36  	})
    37  }
    38  
    39  func TestValidationStringInSlice(t *testing.T) {
    40  	runTestCases(t, []testCase{
    41  		{
    42  			val: "ValidValue",
    43  			f:   StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
    44  		},
    45  		// ignore case
    46  		{
    47  			val: "VALIDVALUE",
    48  			f:   StringInSlice([]string{"ValidValue", "AnotherValidValue"}, true),
    49  		},
    50  		{
    51  			val:         "VALIDVALUE",
    52  			f:           StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
    53  			expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got VALIDVALUE"),
    54  		},
    55  		{
    56  			val:         "InvalidValue",
    57  			f:           StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
    58  			expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got InvalidValue"),
    59  		},
    60  		{
    61  			val:         1,
    62  			f:           StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
    63  			expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"),
    64  		},
    65  	})
    66  }
    67  
    68  func TestValidateJsonString(t *testing.T) {
    69  	type testCases struct {
    70  		Value    string
    71  		ErrCount int
    72  	}
    73  
    74  	invalidCases := []testCases{
    75  		{
    76  			Value:    `{0:"1"}`,
    77  			ErrCount: 1,
    78  		},
    79  		{
    80  			Value:    `{'abc':1}`,
    81  			ErrCount: 1,
    82  		},
    83  		{
    84  			Value:    `{"def":}`,
    85  			ErrCount: 1,
    86  		},
    87  		{
    88  			Value:    `{"xyz":[}}`,
    89  			ErrCount: 1,
    90  		},
    91  	}
    92  
    93  	for _, tc := range invalidCases {
    94  		_, errors := ValidateJsonString(tc.Value, "json")
    95  		if len(errors) != tc.ErrCount {
    96  			t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
    97  		}
    98  	}
    99  
   100  	validCases := []testCases{
   101  		{
   102  			Value:    ``,
   103  			ErrCount: 0,
   104  		},
   105  		{
   106  			Value:    `{}`,
   107  			ErrCount: 0,
   108  		},
   109  		{
   110  			Value:    `{"abc":["1","2"]}`,
   111  			ErrCount: 0,
   112  		},
   113  	}
   114  
   115  	for _, tc := range validCases {
   116  		_, errors := ValidateJsonString(tc.Value, "json")
   117  		if len(errors) != tc.ErrCount {
   118  			t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
   119  		}
   120  	}
   121  }
   122  
   123  func runTestCases(t *testing.T, cases []testCase) {
   124  	matchErr := func(errs []error, r *regexp.Regexp) bool {
   125  		// err must match one provided
   126  		for _, err := range errs {
   127  			if r.MatchString(err.Error()) {
   128  				return true
   129  			}
   130  		}
   131  
   132  		return false
   133  	}
   134  
   135  	for i, tc := range cases {
   136  		_, errs := tc.f(tc.val, "test_property")
   137  
   138  		if len(errs) == 0 && tc.expectedErr == nil {
   139  			continue
   140  		}
   141  
   142  		if !matchErr(errs, tc.expectedErr) {
   143  			t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
   144  		}
   145  	}
   146  }