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

     1  package spotinst
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/spotinst/spotinst-sdk-go/spotinst"
    10  )
    11  
    12  func TestAccSpotinstGroup_Basic(t *testing.T) {
    13  	var group spotinst.AwsGroup
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckSpotinstGroupDestroy,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: testAccCheckSpotinstGroupConfigBasic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckSpotinstGroupExists("spotinst_aws_group.foo", &group),
    23  					testAccCheckSpotinstGroupAttributes(&group),
    24  					resource.TestCheckResourceAttr("spotinst_aws_group.foo", "name", "terraform"),
    25  					resource.TestCheckResourceAttr("spotinst_aws_group.foo", "description", "terraform"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccSpotinstGroup_Updated(t *testing.T) {
    33  	var group spotinst.AwsGroup
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckSpotinstGroupDestroy,
    38  		Steps: []resource.TestStep{
    39  			{
    40  				Config: testAccCheckSpotinstGroupConfigBasic,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckSpotinstGroupExists("spotinst_aws_group.foo", &group),
    43  					testAccCheckSpotinstGroupAttributes(&group),
    44  					resource.TestCheckResourceAttr("spotinst_aws_group.foo", "name", "terraform"),
    45  					resource.TestCheckResourceAttr("spotinst_aws_group.foo", "description", "terraform"),
    46  				),
    47  			},
    48  			{
    49  				Config: testAccCheckSpotinstGroupConfigNewValue,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckSpotinstGroupExists("spotinst_aws_group.foo", &group),
    52  					testAccCheckSpotinstGroupAttributesUpdated(&group),
    53  					resource.TestCheckResourceAttr("spotinst_aws_group.foo", "name", "terraform_updated"),
    54  					resource.TestCheckResourceAttr("spotinst_aws_group.foo", "description", "terraform_updated"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testAccCheckSpotinstGroupDestroy(s *terraform.State) error {
    62  	client := testAccProvider.Meta().(*spotinst.Client)
    63  	for _, rs := range s.RootModule().Resources {
    64  		if rs.Type != "spotinst_aws_group" {
    65  			continue
    66  		}
    67  		input := &spotinst.ReadAwsGroupInput{ID: spotinst.String(rs.Primary.ID)}
    68  		resp, err := client.AwsGroupService.Read(input)
    69  		if err == nil && resp != nil && resp.Group != nil {
    70  			return fmt.Errorf("Group still exists")
    71  		}
    72  	}
    73  	return nil
    74  }
    75  
    76  func testAccCheckSpotinstGroupAttributes(group *spotinst.AwsGroup) resource.TestCheckFunc {
    77  	return func(s *terraform.State) error {
    78  		if spotinst.StringValue(group.Name) != "terraform" {
    79  			return fmt.Errorf("Bad content: %v", group.Name)
    80  		}
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccCheckSpotinstGroupAttributesUpdated(group *spotinst.AwsGroup) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		if spotinst.StringValue(group.Name) != "terraform_updated" {
    88  			return fmt.Errorf("Bad content: %v", group.Name)
    89  		}
    90  		return nil
    91  	}
    92  }
    93  
    94  func testAccCheckSpotinstGroupExists(n string, group *spotinst.AwsGroup) resource.TestCheckFunc {
    95  	return func(s *terraform.State) error {
    96  		rs, ok := s.RootModule().Resources[n]
    97  		if !ok {
    98  			return fmt.Errorf("Not found: %s", n)
    99  		}
   100  		if rs.Primary.ID == "" {
   101  			return fmt.Errorf("No resource ID is set")
   102  		}
   103  		client := testAccProvider.Meta().(*spotinst.Client)
   104  		input := &spotinst.ReadAwsGroupInput{ID: spotinst.String(rs.Primary.ID)}
   105  		resp, err := client.AwsGroupService.Read(input)
   106  		if err != nil {
   107  			return err
   108  		}
   109  		if spotinst.StringValue(resp.Group.Name) != rs.Primary.Attributes["name"] {
   110  			return fmt.Errorf("Group not found: %+v,\n %+v\n", resp.Group, rs.Primary.Attributes)
   111  		}
   112  		*group = *resp.Group
   113  		return nil
   114  	}
   115  }
   116  
   117  const testAccCheckSpotinstGroupConfigBasic = `
   118  resource "spotinst_aws_group" "foo" {
   119  	name = "terraform"
   120  	description = "terraform"
   121  	product = "Linux/UNIX"
   122  
   123  	capacity {
   124  		target = 0
   125  		minimum = 0
   126  		maximum = 5
   127  	}
   128  
   129  	strategy {
   130  		risk = 100
   131  	}
   132  
   133  	instance_types {
   134  		ondemand = "c3.large"
   135  		spot = ["c3.large", "m4.xlarge"]
   136  	}
   137  
   138  	availability_zone {
   139  		name = "us-west-2b"
   140  	}
   141  
   142  	launch_specification {
   143  		monitoring = false
   144  		image_id = "ami-f0091d91"
   145  		key_pair = "east"
   146  		security_group_ids = ["default"]
   147  	}
   148  
   149  	scaling_up_policy {
   150          policy_name = "Scaling Policy 1"
   151          metric_name = "CPUUtilization"
   152          statistic = "average"
   153          unit = "percent"
   154          threshold = 80
   155          adjustment = 1
   156          namespace = "AWS/EC2"
   157          operator = "gte"
   158          period = 300
   159          evaluation_periods = 2
   160          cooldown = 300
   161          dimensions {
   162              env = "prod"
   163          }
   164      }
   165  }`
   166  
   167  const testAccCheckSpotinstGroupConfigNewValue = `
   168  resource "spotinst_aws_group" "foo" {
   169  	name = "terraform_updated"
   170  	description = "terraform_updated"
   171  	product = "Linux/UNIX"
   172  
   173  	capacity {
   174  		target = 0
   175  		minimum = 0
   176  		maximum = 5
   177  	}
   178  
   179  	strategy {
   180  		risk = 100
   181  	}
   182  
   183  	instance_types {
   184  		ondemand = "c3.large"
   185  		spot = ["c3.large", "m4.xlarge"]
   186  	}
   187  
   188  	availability_zone {
   189  		name = "us-west-2b"
   190  	}
   191  
   192  	launch_specification {
   193  		monitoring = false
   194  		image_id = "ami-f0091d91"
   195  		key_pair = "east"
   196  		security_group_ids = ["default"]
   197  	}
   198  
   199  	scaling_up_policy {
   200          policy_name = "Scaling Policy 2"
   201          metric_name = "CPUUtilization"
   202          statistic = "average"
   203          unit = "percent"
   204          threshold = 80
   205          adjustment = 1
   206          namespace = "AWS/EC2"
   207          operator = "gte"
   208          period = 300
   209          evaluation_periods = 2
   210          cooldown = 300
   211          dimensions {
   212              env = "dev"
   213          }
   214      }
   215  }`