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

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/denverdino/aliyungo/common"
     6  	"github.com/denverdino/aliyungo/ess"
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"log"
    10  	"testing"
    11  )
    12  
    13  func TestAccAlicloudEssScalingGroup_basic(t *testing.T) {
    14  	var sg ess.ScalingGroupItemType
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  
    21  		// module name
    22  		IDRefreshName: "alicloud_ess_scaling_group.foo",
    23  
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckEssScalingGroupDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccEssScalingGroupConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckEssScalingGroupExists(
    31  						"alicloud_ess_scaling_group.foo", &sg),
    32  					resource.TestCheckResourceAttr(
    33  						"alicloud_ess_scaling_group.foo",
    34  						"min_size",
    35  						"1"),
    36  					resource.TestCheckResourceAttr(
    37  						"alicloud_ess_scaling_group.foo",
    38  						"max_size",
    39  						"1"),
    40  					resource.TestCheckResourceAttr(
    41  						"alicloud_ess_scaling_group.foo",
    42  						"scaling_group_name",
    43  						"foo"),
    44  					resource.TestCheckResourceAttr(
    45  						"alicloud_ess_scaling_group.foo",
    46  						"removal_policies.#",
    47  						"2",
    48  					),
    49  				),
    50  			},
    51  		},
    52  	})
    53  
    54  }
    55  
    56  func TestAccAlicloudEssScalingGroup_update(t *testing.T) {
    57  	var sg ess.ScalingGroupItemType
    58  
    59  	resource.Test(t, resource.TestCase{
    60  		PreCheck: func() {
    61  			testAccPreCheck(t)
    62  		},
    63  
    64  		// module name
    65  		IDRefreshName: "alicloud_ess_scaling_group.foo",
    66  
    67  		Providers:    testAccProviders,
    68  		CheckDestroy: testAccCheckEssScalingGroupDestroy,
    69  		Steps: []resource.TestStep{
    70  			resource.TestStep{
    71  				Config: testAccEssScalingGroup,
    72  				Check: resource.ComposeTestCheckFunc(
    73  					testAccCheckEssScalingGroupExists(
    74  						"alicloud_ess_scaling_group.foo", &sg),
    75  					resource.TestCheckResourceAttr(
    76  						"alicloud_ess_scaling_group.foo",
    77  						"min_size",
    78  						"1"),
    79  					resource.TestCheckResourceAttr(
    80  						"alicloud_ess_scaling_group.foo",
    81  						"max_size",
    82  						"1"),
    83  					resource.TestCheckResourceAttr(
    84  						"alicloud_ess_scaling_group.foo",
    85  						"scaling_group_name",
    86  						"foo"),
    87  					resource.TestCheckResourceAttr(
    88  						"alicloud_ess_scaling_group.foo",
    89  						"removal_policies.#",
    90  						"2",
    91  					),
    92  				),
    93  			},
    94  
    95  			resource.TestStep{
    96  				Config: testAccEssScalingGroup_update,
    97  				Check: resource.ComposeTestCheckFunc(
    98  					testAccCheckEssScalingGroupExists(
    99  						"alicloud_ess_scaling_group.foo", &sg),
   100  					resource.TestCheckResourceAttr(
   101  						"alicloud_ess_scaling_group.foo",
   102  						"min_size",
   103  						"2"),
   104  					resource.TestCheckResourceAttr(
   105  						"alicloud_ess_scaling_group.foo",
   106  						"max_size",
   107  						"2"),
   108  					resource.TestCheckResourceAttr(
   109  						"alicloud_ess_scaling_group.foo",
   110  						"scaling_group_name",
   111  						"update"),
   112  					resource.TestCheckResourceAttr(
   113  						"alicloud_ess_scaling_group.foo",
   114  						"removal_policies.#",
   115  						"1",
   116  					),
   117  				),
   118  			},
   119  		},
   120  	})
   121  
   122  }
   123  
   124  func SkipTestAccAlicloudEssScalingGroup_vpc(t *testing.T) {
   125  	var sg ess.ScalingGroupItemType
   126  
   127  	resource.Test(t, resource.TestCase{
   128  		PreCheck: func() {
   129  			testAccPreCheck(t)
   130  		},
   131  
   132  		// module name
   133  		IDRefreshName: "alicloud_ess_scaling_group.foo",
   134  
   135  		Providers:    testAccProviders,
   136  		CheckDestroy: testAccCheckEssScalingGroupDestroy,
   137  		Steps: []resource.TestStep{
   138  			resource.TestStep{
   139  				Config: testAccEssScalingGroup_vpc,
   140  				Check: resource.ComposeTestCheckFunc(
   141  					testAccCheckEssScalingGroupExists(
   142  						"alicloud_ess_scaling_group.foo", &sg),
   143  					resource.TestCheckResourceAttr(
   144  						"alicloud_ess_scaling_group.foo",
   145  						"min_size",
   146  						"1"),
   147  					resource.TestCheckResourceAttr(
   148  						"alicloud_ess_scaling_group.foo",
   149  						"max_size",
   150  						"1"),
   151  					resource.TestCheckResourceAttr(
   152  						"alicloud_ess_scaling_group.foo",
   153  						"scaling_group_name",
   154  						"foo"),
   155  					resource.TestCheckResourceAttr(
   156  						"alicloud_ess_scaling_group.foo",
   157  						"removal_policies.#",
   158  						"2",
   159  					),
   160  				),
   161  			},
   162  		},
   163  	})
   164  
   165  }
   166  
   167  func testAccCheckEssScalingGroupExists(n string, d *ess.ScalingGroupItemType) resource.TestCheckFunc {
   168  	return func(s *terraform.State) error {
   169  		rs, ok := s.RootModule().Resources[n]
   170  		if !ok {
   171  			return fmt.Errorf("Not found: %s", n)
   172  		}
   173  
   174  		if rs.Primary.ID == "" {
   175  			return fmt.Errorf("No ESS Scaling Group ID is set")
   176  		}
   177  
   178  		client := testAccProvider.Meta().(*AliyunClient)
   179  		attr, err := client.DescribeScalingGroupById(rs.Primary.ID)
   180  		log.Printf("[DEBUG] check scaling group %s attribute %#v", rs.Primary.ID, attr)
   181  
   182  		if err != nil {
   183  			return err
   184  		}
   185  
   186  		if attr == nil {
   187  			return fmt.Errorf("Scaling Group not found")
   188  		}
   189  
   190  		*d = *attr
   191  		return nil
   192  	}
   193  }
   194  
   195  func testAccCheckEssScalingGroupDestroy(s *terraform.State) error {
   196  	client := testAccProvider.Meta().(*AliyunClient)
   197  
   198  	for _, rs := range s.RootModule().Resources {
   199  		if rs.Type != "alicloud_ess_scaling_group" {
   200  			continue
   201  		}
   202  
   203  		ins, err := client.DescribeScalingGroupById(rs.Primary.ID)
   204  
   205  		if ins != nil {
   206  			return fmt.Errorf("Error ESS scaling group still exist")
   207  		}
   208  
   209  		// Verify the error is what we want
   210  		if err != nil {
   211  			// Verify the error is what we want
   212  			e, _ := err.(*common.Error)
   213  			if e.ErrorResponse.Code == InstanceNotfound {
   214  				continue
   215  			}
   216  			return err
   217  		}
   218  	}
   219  
   220  	return nil
   221  }
   222  
   223  const testAccEssScalingGroupConfig = `
   224  resource "alicloud_ess_scaling_group" "foo" {
   225  	min_size = 1
   226  	max_size = 1
   227  	scaling_group_name = "foo"
   228  	removal_policies = ["OldestInstance", "NewestInstance"]
   229  }
   230  `
   231  
   232  const testAccEssScalingGroup = `
   233  resource "alicloud_ess_scaling_group" "foo" {
   234  	min_size = 1
   235  	max_size = 1
   236  	scaling_group_name = "foo"
   237  	removal_policies = ["OldestInstance", "NewestInstance"]
   238  }
   239  `
   240  
   241  const testAccEssScalingGroup_update = `
   242  resource "alicloud_ess_scaling_group" "foo" {
   243  	min_size = 2
   244  	max_size = 2
   245  	scaling_group_name = "update"
   246  	removal_policies = ["OldestInstance"]
   247  }
   248  `
   249  const testAccEssScalingGroup_vpc = `
   250  data "alicloud_images" "ecs_image" {
   251    most_recent = true
   252    name_regex =  "^centos_6\\w{1,5}[64].*"
   253  }
   254  
   255  data "alicloud_zones" "default" {
   256  	"available_disk_category"= "cloud_efficiency"
   257  	"available_resource_creation"= "VSwitch"
   258  }
   259  
   260  resource "alicloud_vpc" "foo" {
   261    	name = "tf_test_foo"
   262    	cidr_block = "172.16.0.0/12"
   263  }
   264  
   265  resource "alicloud_vswitch" "foo" {
   266    	vpc_id = "${alicloud_vpc.foo.id}"
   267    	cidr_block = "172.16.0.0/21"
   268    	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   269  }
   270  
   271  resource "alicloud_security_group" "tf_test_foo" {
   272  	description = "foo"
   273  	vpc_id = "${alicloud_vpc.foo.id}"
   274  }
   275  
   276  resource "alicloud_ess_scaling_group" "foo" {
   277  	min_size = 1
   278  	max_size = 1
   279  	scaling_group_name = "foo"
   280  	default_cooldown = 20
   281  	vswitch_id = "${alicloud_vswitch.foo.id}"
   282  	removal_policies = ["OldestInstance", "NewestInstance"]
   283  }
   284  
   285  resource "alicloud_ess_scaling_configuration" "foo" {
   286  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   287  	enable = true
   288  
   289  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   290  	instance_type = "ecs.n1.medium"
   291  	io_optimized = "optimized"
   292  	system_disk_category = "cloud_efficiency"
   293  	internet_charge_type = "PayByTraffic"
   294  	internet_max_bandwidth_out = 10
   295  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   296  }
   297  `