github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/fastly/data_source_ip_ranges_test.go (about)

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"sort"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccFastlyIPRanges(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:  func() { testAccPreCheck(t) },
    17  		Providers: testAccProviders,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccFastlyIPRangesConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccFastlyIPRanges("data.fastly_ip_ranges.some"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccFastlyIPRanges(n string) resource.TestCheckFunc {
    30  	return func(s *terraform.State) error {
    31  
    32  		r := s.RootModule().Resources[n]
    33  		a := r.Primary.Attributes
    34  
    35  		var (
    36  			cidrBlockSize int
    37  			err           error
    38  		)
    39  
    40  		if cidrBlockSize, err = strconv.Atoi(a["cidr_blocks.#"]); err != nil {
    41  			return err
    42  		}
    43  
    44  		if cidrBlockSize < 10 {
    45  			return fmt.Errorf("cidr_blocks seem suspiciously low: %d", cidrBlockSize)
    46  		}
    47  
    48  		var cidrBlocks sort.StringSlice = make([]string, cidrBlockSize)
    49  
    50  		for i := range make([]string, cidrBlockSize) {
    51  
    52  			block := a[fmt.Sprintf("cidr_blocks.%d", i)]
    53  
    54  			if _, _, err := net.ParseCIDR(block); err != nil {
    55  				return fmt.Errorf("malformed CIDR block %s: %s", block, err)
    56  			}
    57  
    58  			cidrBlocks[i] = block
    59  
    60  		}
    61  
    62  		if !sort.IsSorted(cidrBlocks) {
    63  			return fmt.Errorf("unexpected order of cidr_blocks: %s", cidrBlocks)
    64  		}
    65  
    66  		return nil
    67  	}
    68  }
    69  
    70  const testAccFastlyIPRangesConfig = `
    71  data "fastly_ip_ranges" "some" {
    72  }
    73  `