github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/acctest/random_test.go (about) 1 package acctest 2 3 import ( 4 "regexp" 5 "testing" 6 ) 7 8 func TestRandIpAddress(t *testing.T) { 9 testCases := []struct { 10 s string 11 expected *regexp.Regexp 12 expectedErr string 13 }{ 14 { 15 s: "1.1.1.1/32", 16 expected: regexp.MustCompile(`^1\.1\.1\.1$`), 17 }, 18 { 19 s: "10.0.0.0/8", 20 expected: regexp.MustCompile(`^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$`), 21 }, 22 { 23 s: "0.0.0.0/0", 24 expectedErr: "CIDR range is too large: 32", 25 }, 26 { 27 s: "449d:e5f1:14b1:ddf3:8525:7e9e:4a0d:4a82/128", 28 expected: regexp.MustCompile(`^449d:e5f1:14b1:ddf3:8525:7e9e:4a0d:4a82$`), 29 }, 30 { 31 s: "2001:db8::/112", 32 expected: regexp.MustCompile(`^2001:db8::[[:xdigit:]]{1,4}$`), 33 }, 34 { 35 s: "2001:db8::/64", 36 expectedErr: "CIDR range is too large: 64", 37 }, 38 { 39 s: "abcdefg", 40 expectedErr: "invalid CIDR address: abcdefg", 41 }, 42 } 43 44 for i, tc := range testCases { 45 v, err := RandIpAddress(tc.s) 46 if err != nil { 47 msg := err.Error() 48 if tc.expectedErr == "" { 49 t.Fatalf("expected test case %d to succeed but got error %q, ", i, msg) 50 } 51 if msg != tc.expectedErr { 52 t.Fatalf("expected test case %d to fail with %q but got %q", i, tc.expectedErr, msg) 53 } 54 } else if !tc.expected.MatchString(v) { 55 t.Fatalf("expected test case %d to return %q but got %q", i, tc.expected, v) 56 } 57 } 58 }