github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/data_source_alicloud_zones_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/resource"
     6  	"github.com/hashicorp/terraform/terraform"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestAccAlicloudZonesDataSource_basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck: func() {
    14  			testAccPreCheck(t)
    15  		},
    16  		Providers: testAccProviders,
    17  		Steps: []resource.TestStep{
    18  			{
    19  				Config: testAccCheckAlicloudZonesDataSourceBasicConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func TestAccAlicloudZonesDataSource_filter(t *testing.T) {
    29  
    30  	resource.Test(t, resource.TestCase{
    31  		PreCheck: func() {
    32  			testAccPreCheck(t)
    33  		},
    34  		Providers: testAccProviders,
    35  		Steps: []resource.TestStep{
    36  			{
    37  				Config: testAccCheckAlicloudZonesDataSourceFilter,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
    40  					testCheckZoneLength("data.alicloud_zones.foo"),
    41  				),
    42  			},
    43  
    44  			resource.TestStep{
    45  				Config: testAccCheckAlicloudZonesDataSourceFilterIoOptimized,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
    48  					testCheckZoneLength("data.alicloud_zones.foo"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccAlicloudZonesDataSource_unitRegion(t *testing.T) {
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck: func() {
    58  			testAccPreCheck(t)
    59  		},
    60  		Providers: testAccProviders,
    61  		Steps: []resource.TestStep{
    62  			{
    63  				Config: testAccCheckAlicloudZonesDataSource_unitRegion,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  // the zone length changed occasionally
    73  // check by range to avoid test case failure
    74  func testCheckZoneLength(name string) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  		ms := s.RootModule()
    77  		rs, ok := ms.Resources[name]
    78  		if !ok {
    79  			return fmt.Errorf("Not found: %s", name)
    80  		}
    81  
    82  		is := rs.Primary
    83  		if is == nil {
    84  			return fmt.Errorf("No primary instance: %s", name)
    85  		}
    86  
    87  		i, err := strconv.Atoi(is.Attributes["zones.#"])
    88  
    89  		if err != nil {
    90  			return fmt.Errorf("convert zone length err: %#v", err)
    91  		}
    92  
    93  		if i <= 0 {
    94  			return fmt.Errorf("zone length expected greater than 0 got err: %d", i)
    95  		}
    96  
    97  		return nil
    98  	}
    99  }
   100  
   101  const testAccCheckAlicloudZonesDataSourceBasicConfig = `
   102  data "alicloud_zones" "foo" {
   103  }
   104  `
   105  
   106  const testAccCheckAlicloudZonesDataSourceFilter = `
   107  data "alicloud_zones" "foo" {
   108  	available_instance_type= "ecs.c2.xlarge"
   109  	available_resource_creation= "VSwitch"
   110  	available_disk_category= "cloud_efficiency"
   111  }
   112  `
   113  
   114  const testAccCheckAlicloudZonesDataSourceFilterIoOptimized = `
   115  data "alicloud_zones" "foo" {
   116  	available_instance_type= "ecs.c2.xlarge"
   117  	available_resource_creation= "IoOptimized"
   118  	available_disk_category= "cloud"
   119  }
   120  `
   121  
   122  const testAccCheckAlicloudZonesDataSource_unitRegion = `
   123  provider "alicloud" {
   124  	alias = "northeast"
   125  	region = "ap-northeast-1"
   126  }
   127  
   128  data "alicloud_zones" "foo" {
   129  	provider = "alicloud.northeast"
   130  	available_resource_creation= "VSwitch"
   131  }
   132  `