github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/aws/resource_aws_ssm_association_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ssm"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSSSMAssociation_basic(t *testing.T) {
    16  	name := acctest.RandString(10)
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSSSMAssociationDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: testAccAWSSSMAssociationBasicConfig(name),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSSSMAssociation_withTargets(t *testing.T) {
    33  	name := acctest.RandString(10)
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckAWSSSMAssociationDestroy,
    38  		Steps: []resource.TestStep{
    39  			{
    40  				Config: testAccAWSSSMAssociationBasicConfigWithTargets(name),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckAWSSSMAssociationExists(n string) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		rs, ok := s.RootModule().Resources[n]
    52  		if !ok {
    53  			return fmt.Errorf("Not found: %s", n)
    54  		}
    55  
    56  		if rs.Primary.ID == "" {
    57  			return fmt.Errorf("No SSM Assosciation ID is set")
    58  		}
    59  
    60  		conn := testAccProvider.Meta().(*AWSClient).ssmconn
    61  
    62  		_, err := conn.DescribeAssociation(&ssm.DescribeAssociationInput{
    63  			AssociationId: aws.String(rs.Primary.Attributes["association_id"]),
    64  		})
    65  
    66  		if err != nil {
    67  			if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "AssociationDoesNotExist" {
    68  				return nil
    69  			}
    70  			return err
    71  		}
    72  
    73  		return nil
    74  	}
    75  }
    76  
    77  func testAccCheckAWSSSMAssociationDestroy(s *terraform.State) error {
    78  	conn := testAccProvider.Meta().(*AWSClient).ssmconn
    79  
    80  	for _, rs := range s.RootModule().Resources {
    81  		if rs.Type != "aws_ssm_association" {
    82  			continue
    83  		}
    84  
    85  		out, err := conn.DescribeAssociation(&ssm.DescribeAssociationInput{
    86  			AssociationId: aws.String(rs.Primary.Attributes["association_id"]),
    87  		})
    88  
    89  		if err != nil {
    90  			if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "AssociationDoesNotExist" {
    91  				return nil
    92  			}
    93  			return err
    94  		}
    95  
    96  		if out != nil {
    97  			return fmt.Errorf("Expected AWS SSM Association to be gone, but was still found")
    98  		}
    99  	}
   100  
   101  	return fmt.Errorf("Default error in SSM Association Test")
   102  }
   103  
   104  func testAccAWSSSMAssociationBasicConfigWithTargets(rName string) string {
   105  	return fmt.Sprintf(`
   106  resource "aws_ssm_document" "foo_document" {
   107    name = "test_document_association-%s",
   108    document_type = "Command"
   109    content = <<DOC
   110    {
   111      "schemaVersion": "1.2",
   112      "description": "Check ip configuration of a Linux instance.",
   113      "parameters": {
   114  
   115      },
   116      "runtimeConfig": {
   117        "aws:runShellScript": {
   118          "properties": [
   119            {
   120              "id": "0.aws:runShellScript",
   121              "runCommand": ["ifconfig"]
   122            }
   123          ]
   124        }
   125      }
   126    }
   127  DOC
   128  }
   129  
   130  resource "aws_ssm_association" "foo" {
   131    name = "${aws_ssm_document.foo_document.name}",
   132    targets {
   133      key = "tag:Name"
   134      values = ["acceptanceTest"]
   135    }
   136  }`, rName)
   137  }
   138  
   139  func testAccAWSSSMAssociationBasicConfig(rName string) string {
   140  	return fmt.Sprintf(`
   141  resource "aws_security_group" "tf_test_foo" {
   142    name = "tf_test_foo-%s"
   143    description = "foo"
   144    ingress {
   145      protocol = "icmp"
   146      from_port = -1
   147      to_port = -1
   148      cidr_blocks = ["0.0.0.0/0"]
   149    }
   150  }
   151  
   152  resource "aws_instance" "foo" {
   153    ami = "ami-4fccb37f"
   154    availability_zone = "us-west-2a"
   155    instance_type = "m1.small"
   156    security_groups = ["${aws_security_group.tf_test_foo.name}"]
   157  }
   158  
   159  resource "aws_ssm_document" "foo_document" {
   160    name    = "test_document_association-%s",
   161  	document_type = "Command"
   162    content = <<DOC
   163    {
   164      "schemaVersion": "1.2",
   165      "description": "Check ip configuration of a Linux instance.",
   166      "parameters": {
   167  
   168      },
   169      "runtimeConfig": {
   170        "aws:runShellScript": {
   171          "properties": [
   172            {
   173              "id": "0.aws:runShellScript",
   174              "runCommand": ["ifconfig"]
   175            }
   176          ]
   177        }
   178      }
   179    }
   180  DOC
   181  }
   182  
   183  resource "aws_ssm_association" "foo" {
   184    name        = "test_document_association-%s",
   185    instance_id = "${aws_instance.foo.id}"
   186  }
   187  `, rName, rName, rName)
   188  }