github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/service/ec2"
    12  )
    13  
    14  func TestAccAWSPlacementGroup_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSPlacementGroupDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSPlacementGroupConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSPlacementGroupExists("aws_placement_group.pg"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckAWSPlacementGroupDestroy(s *terraform.State) error {
    31  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    32  
    33  	for _, rs := range s.RootModule().Resources {
    34  		if rs.Type != "aws_placement_group" {
    35  			continue
    36  		}
    37  		_, err := conn.DeletePlacementGroup(&ec2.DeletePlacementGroupInput{
    38  			GroupName: aws.String(rs.Primary.ID),
    39  		})
    40  		if err != nil {
    41  			return err
    42  		}
    43  	}
    44  	return nil
    45  }
    46  
    47  func testAccCheckAWSPlacementGroupExists(n string) resource.TestCheckFunc {
    48  	return func(s *terraform.State) error {
    49  		rs, ok := s.RootModule().Resources[n]
    50  		if !ok {
    51  			return fmt.Errorf("Not found: %s", n)
    52  		}
    53  
    54  		if rs.Primary.ID == "" {
    55  			return fmt.Errorf("No Placement Group ID is set")
    56  		}
    57  
    58  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    59  		_, err := conn.DescribePlacementGroups(&ec2.DescribePlacementGroupsInput{
    60  			GroupNames: []*string{aws.String(rs.Primary.ID)},
    61  		})
    62  
    63  		if err != nil {
    64  			return fmt.Errorf("Placement Group error: %v", err)
    65  		}
    66  		return nil
    67  	}
    68  }
    69  
    70  func testAccCheckAWSDestroyPlacementGroup(n string) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		rs, ok := s.RootModule().Resources[n]
    73  		if !ok {
    74  			return fmt.Errorf("Not found: %s", n)
    75  		}
    76  
    77  		if rs.Primary.ID == "" {
    78  			return fmt.Errorf("No Placement Group ID is set")
    79  		}
    80  
    81  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    82  		_, err := conn.DeletePlacementGroup(&ec2.DeletePlacementGroupInput{
    83  			GroupName: aws.String(rs.Primary.ID),
    84  		})
    85  
    86  		if err != nil {
    87  			return fmt.Errorf("Error destroying Placement Group (%s): %s", rs.Primary.ID, err)
    88  		}
    89  		return nil
    90  	}
    91  }
    92  
    93  var testAccAWSPlacementGroupConfig = `
    94  resource "aws_placement_group" "pg" {
    95  	name = "tf-test-pg"
    96  	strategy = "cluster"
    97  }
    98  `