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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/ec2"
    13  )
    14  
    15  func TestAccAWSPlacementGroup_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSPlacementGroupDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSPlacementGroupConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSPlacementGroupExists("aws_placement_group.pg"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckAWSPlacementGroupDestroy(s *terraform.State) error {
    32  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "aws_placement_group" {
    36  			continue
    37  		}
    38  
    39  		_, err := conn.DescribePlacementGroups(&ec2.DescribePlacementGroupsInput{
    40  			GroupNames: []*string{aws.String(rs.Primary.Attributes["name"])},
    41  		})
    42  		if err != nil {
    43  			// Verify the error is what we want
    44  			if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidPlacementGroup.Unknown" {
    45  				continue
    46  			}
    47  			return err
    48  		}
    49  
    50  		return fmt.Errorf("still exists")
    51  	}
    52  	return nil
    53  }
    54  
    55  func testAccCheckAWSPlacementGroupExists(n string) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[n]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", n)
    60  		}
    61  
    62  		if rs.Primary.ID == "" {
    63  			return fmt.Errorf("No Placement Group ID is set")
    64  		}
    65  
    66  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    67  		_, err := conn.DescribePlacementGroups(&ec2.DescribePlacementGroupsInput{
    68  			GroupNames: []*string{aws.String(rs.Primary.ID)},
    69  		})
    70  
    71  		if err != nil {
    72  			return fmt.Errorf("Placement Group error: %v", err)
    73  		}
    74  		return nil
    75  	}
    76  }
    77  
    78  func testAccCheckAWSDestroyPlacementGroup(n string) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		rs, ok := s.RootModule().Resources[n]
    81  		if !ok {
    82  			return fmt.Errorf("Not found: %s", n)
    83  		}
    84  
    85  		if rs.Primary.ID == "" {
    86  			return fmt.Errorf("No Placement Group ID is set")
    87  		}
    88  
    89  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    90  		_, err := conn.DeletePlacementGroup(&ec2.DeletePlacementGroupInput{
    91  			GroupName: aws.String(rs.Primary.ID),
    92  		})
    93  
    94  		if err != nil {
    95  			return fmt.Errorf("Error destroying Placement Group (%s): %s", rs.Primary.ID, err)
    96  		}
    97  		return nil
    98  	}
    99  }
   100  
   101  var testAccAWSPlacementGroupConfig = `
   102  resource "aws_placement_group" "pg" {
   103  	name = "tf-test-pg"
   104  	strategy = "cluster"
   105  }
   106  `