github.com/ticketmaster/terraform@v0.10.0-beta2.0.20170711045249-a12daf5aba4f/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 TestValidationIntAtLeast(t *testing.T) { 40 runTestCases(t, []testCase{ 41 { 42 val: 1, 43 f: IntAtLeast(1), 44 }, 45 { 46 val: 1, 47 f: IntAtLeast(0), 48 }, 49 { 50 val: 1, 51 f: IntAtLeast(2), 52 expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\), got 1"), 53 }, 54 { 55 val: "1", 56 f: IntAtLeast(2), 57 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"), 58 }, 59 }) 60 } 61 62 func TestValidationIntAtMost(t *testing.T) { 63 runTestCases(t, []testCase{ 64 { 65 val: 1, 66 f: IntAtMost(1), 67 }, 68 { 69 val: 1, 70 f: IntAtMost(2), 71 }, 72 { 73 val: 1, 74 f: IntAtMost(0), 75 expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(0\\), got 1"), 76 }, 77 { 78 val: "1", 79 f: IntAtMost(0), 80 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"), 81 }, 82 }) 83 } 84 85 func TestValidationStringInSlice(t *testing.T) { 86 runTestCases(t, []testCase{ 87 { 88 val: "ValidValue", 89 f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false), 90 }, 91 // ignore case 92 { 93 val: "VALIDVALUE", 94 f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, true), 95 }, 96 { 97 val: "VALIDVALUE", 98 f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false), 99 expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got VALIDVALUE"), 100 }, 101 { 102 val: "InvalidValue", 103 f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false), 104 expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got InvalidValue"), 105 }, 106 { 107 val: 1, 108 f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false), 109 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"), 110 }, 111 }) 112 } 113 114 func TestValidateJsonString(t *testing.T) { 115 type testCases struct { 116 Value string 117 ErrCount int 118 } 119 120 invalidCases := []testCases{ 121 { 122 Value: `{0:"1"}`, 123 ErrCount: 1, 124 }, 125 { 126 Value: `{'abc':1}`, 127 ErrCount: 1, 128 }, 129 { 130 Value: `{"def":}`, 131 ErrCount: 1, 132 }, 133 { 134 Value: `{"xyz":[}}`, 135 ErrCount: 1, 136 }, 137 } 138 139 for _, tc := range invalidCases { 140 _, errors := ValidateJsonString(tc.Value, "json") 141 if len(errors) != tc.ErrCount { 142 t.Fatalf("Expected %q to trigger a validation error.", tc.Value) 143 } 144 } 145 146 validCases := []testCases{ 147 { 148 Value: ``, 149 ErrCount: 0, 150 }, 151 { 152 Value: `{}`, 153 ErrCount: 0, 154 }, 155 { 156 Value: `{"abc":["1","2"]}`, 157 ErrCount: 0, 158 }, 159 } 160 161 for _, tc := range validCases { 162 _, errors := ValidateJsonString(tc.Value, "json") 163 if len(errors) != tc.ErrCount { 164 t.Fatalf("Expected %q not to trigger a validation error.", tc.Value) 165 } 166 } 167 } 168 169 func runTestCases(t *testing.T, cases []testCase) { 170 matchErr := func(errs []error, r *regexp.Regexp) bool { 171 // err must match one provided 172 for _, err := range errs { 173 if r.MatchString(err.Error()) { 174 return true 175 } 176 } 177 178 return false 179 } 180 181 for i, tc := range cases { 182 _, errs := tc.f(tc.val, "test_property") 183 184 if len(errs) == 0 && tc.expectedErr == nil { 185 continue 186 } 187 188 if !matchErr(errs, tc.expectedErr) { 189 t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs) 190 } 191 } 192 }