github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_ssm_patch_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws/awserr"
     8  	"github.com/aws/aws-sdk-go/service/ssm"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSSSMPatchGroup_basic(t *testing.T) {
    15  	name := acctest.RandString(10)
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSSSMPatchGroupDestroy,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccAWSSSMPatchGroupBasicConfig(name),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSSSMPatchGroupExists("aws_ssm_patch_group.patchgroup"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckAWSSSMPatchGroupExists(n string) resource.TestCheckFunc {
    32  	return func(s *terraform.State) error {
    33  		rs, ok := s.RootModule().Resources[n]
    34  		if !ok {
    35  			return fmt.Errorf("Not found: %s", n)
    36  		}
    37  
    38  		if rs.Primary.ID == "" {
    39  			return fmt.Errorf("No SSM Patch Baseline ID is set")
    40  		}
    41  
    42  		conn := testAccProvider.Meta().(*AWSClient).ssmconn
    43  
    44  		resp, err := conn.DescribePatchGroups(&ssm.DescribePatchGroupsInput{})
    45  		if err != nil {
    46  			return err
    47  		}
    48  
    49  		for _, i := range resp.Mappings {
    50  			if *i.BaselineIdentity.BaselineId == rs.Primary.Attributes["baseline_id"] && *i.PatchGroup == rs.Primary.ID {
    51  				return nil
    52  			}
    53  		}
    54  
    55  		return fmt.Errorf("No AWS SSM Patch Group found")
    56  	}
    57  }
    58  
    59  func testAccCheckAWSSSMPatchGroupDestroy(s *terraform.State) error {
    60  	conn := testAccProvider.Meta().(*AWSClient).ssmconn
    61  
    62  	for _, rs := range s.RootModule().Resources {
    63  		if rs.Type != "aws_ssm_patch_group" {
    64  			continue
    65  		}
    66  
    67  		resp, err := conn.DescribePatchGroups(&ssm.DescribePatchGroupsInput{})
    68  
    69  		if err != nil {
    70  			// Verify the error is what we want
    71  			if ae, ok := err.(awserr.Error); ok && ae.Code() == "DoesNotExistException" {
    72  				continue
    73  			}
    74  			return err
    75  		}
    76  
    77  		for _, i := range resp.Mappings {
    78  			if *i.BaselineIdentity.BaselineId == rs.Primary.Attributes["baseline_id"] && *i.PatchGroup == rs.Primary.ID {
    79  				return fmt.Errorf("Expected AWS SSM Patch Group to be gone, but was still found")
    80  			}
    81  		}
    82  
    83  		return nil
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func testAccAWSSSMPatchGroupBasicConfig(rName string) string {
    90  	return fmt.Sprintf(`
    91  
    92  resource "aws_ssm_patch_baseline" "foo" {
    93    name  = "patch-baseline-%s"
    94    approved_patches = ["KB123456"]
    95  }
    96  
    97  resource "aws_ssm_patch_group" "patchgroup" {
    98    baseline_id = "${aws_ssm_patch_baseline.foo.id}"
    99    patch_group = "patch-group"
   100  }
   101  
   102  `, rName)
   103  }