github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_ip_ranges_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "net" 6 "regexp" 7 "sort" 8 "strconv" 9 "testing" 10 "time" 11 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSIPRanges(t *testing.T) { 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSIPRangesConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccAWSIPRanges("data.aws_ip_ranges.some"), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccAWSIPRanges(n string) resource.TestCheckFunc { 32 return func(s *terraform.State) error { 33 34 r := s.RootModule().Resources[n] 35 a := r.Primary.Attributes 36 37 var ( 38 cidrBlockSize int 39 createDate time.Time 40 err error 41 syncToken int 42 ) 43 44 if cidrBlockSize, err = strconv.Atoi(a["cidr_blocks.#"]); err != nil { 45 return err 46 } 47 48 if cidrBlockSize < 10 { 49 return fmt.Errorf("cidr_blocks for eu-west-1 seem suspiciously low: %d", cidrBlockSize) 50 } 51 52 if createDate, err = time.Parse("2006-01-02-15-04-05", a["create_date"]); err != nil { 53 return err 54 } 55 56 if syncToken, err = strconv.Atoi(a["sync_token"]); err != nil { 57 return err 58 } 59 60 if syncToken != int(createDate.Unix()) { 61 return fmt.Errorf("sync_token %d does not match create_date %s", syncToken, createDate) 62 } 63 64 var cidrBlocks sort.StringSlice = make([]string, cidrBlockSize) 65 66 for i := range make([]string, cidrBlockSize) { 67 68 block := a[fmt.Sprintf("cidr_blocks.%d", i)] 69 70 if _, _, err := net.ParseCIDR(block); err != nil { 71 return fmt.Errorf("malformed CIDR block %s: %s", block, err) 72 } 73 74 cidrBlocks[i] = block 75 76 } 77 78 if !sort.IsSorted(cidrBlocks) { 79 return fmt.Errorf("unexpected order of cidr_blocks: %s", cidrBlocks) 80 } 81 82 var ( 83 regionMember = regexp.MustCompile(`regions\.\d+`) 84 regions, services int 85 serviceMember = regexp.MustCompile(`services\.\d+`) 86 ) 87 88 for k, v := range a { 89 90 if regionMember.MatchString(k) { 91 92 if !(v == "eu-west-1" || v == "EU-central-1") { 93 return fmt.Errorf("unexpected region %s", v) 94 } 95 96 regions = regions + 1 97 98 } 99 100 if serviceMember.MatchString(k) { 101 102 if v != "EC2" { 103 return fmt.Errorf("unexpected service %s", v) 104 } 105 106 services = services + 1 107 } 108 109 } 110 111 if regions != 2 { 112 return fmt.Errorf("unexpected number of regions: %d", regions) 113 } 114 115 if services != 1 { 116 return fmt.Errorf("unexpected number of services: %d", services) 117 } 118 119 return nil 120 } 121 } 122 123 const testAccAWSIPRangesConfig = ` 124 data "aws_ip_ranges" "some" { 125 regions = [ "eu-west-1", "EU-central-1" ] 126 services = [ "EC2" ] 127 } 128 `