github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_ami_launch_permission_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	r "github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSAMILaunchPermission_Basic(t *testing.T) {
    15  	imageID := ""
    16  	accountID := os.Getenv("AWS_ACCOUNT_ID")
    17  
    18  	r.Test(t, r.TestCase{
    19  		PreCheck: func() {
    20  			testAccPreCheck(t)
    21  			if os.Getenv("AWS_ACCOUNT_ID") == "" {
    22  				t.Fatal("AWS_ACCOUNT_ID must be set")
    23  			}
    24  		},
    25  		Providers: testAccProviders,
    26  		Steps: []r.TestStep{
    27  			// Scaffold everything
    28  			r.TestStep{
    29  				Config: testAccAWSAMILaunchPermissionConfig(accountID, true),
    30  				Check: r.ComposeTestCheckFunc(
    31  					testCheckResourceGetAttr("aws_ami_copy.test", "id", &imageID),
    32  					testAccAWSAMILaunchPermissionExists(accountID, &imageID),
    33  				),
    34  			},
    35  			// Drop just launch permission to test destruction
    36  			r.TestStep{
    37  				Config: testAccAWSAMILaunchPermissionConfig(accountID, false),
    38  				Check: r.ComposeTestCheckFunc(
    39  					testAccAWSAMILaunchPermissionDestroyed(accountID, &imageID),
    40  				),
    41  			},
    42  			// Re-add everything so we can test when AMI disappears
    43  			r.TestStep{
    44  				Config: testAccAWSAMILaunchPermissionConfig(accountID, true),
    45  				Check: r.ComposeTestCheckFunc(
    46  					testCheckResourceGetAttr("aws_ami_copy.test", "id", &imageID),
    47  					testAccAWSAMILaunchPermissionExists(accountID, &imageID),
    48  				),
    49  			},
    50  			// Here we delete the AMI to verify the follow-on refresh after this step
    51  			// should not error.
    52  			r.TestStep{
    53  				Config: testAccAWSAMILaunchPermissionConfig(accountID, true),
    54  				Check: r.ComposeTestCheckFunc(
    55  					testAccAWSAMIDisappears(&imageID),
    56  				),
    57  				ExpectNonEmptyPlan: true,
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testCheckResourceGetAttr(name, key string, value *string) r.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		ms := s.RootModule()
    66  		rs, ok := ms.Resources[name]
    67  		if !ok {
    68  			return fmt.Errorf("Not found: %s", name)
    69  		}
    70  
    71  		is := rs.Primary
    72  		if is == nil {
    73  			return fmt.Errorf("No primary instance: %s", name)
    74  		}
    75  
    76  		*value = is.Attributes[key]
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccAWSAMILaunchPermissionExists(accountID string, imageID *string) r.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    84  		if has, err := hasLaunchPermission(conn, *imageID, accountID); err != nil {
    85  			return err
    86  		} else if !has {
    87  			return fmt.Errorf("launch permission does not exist for '%s' on '%s'", accountID, *imageID)
    88  		}
    89  		return nil
    90  	}
    91  }
    92  
    93  func testAccAWSAMILaunchPermissionDestroyed(accountID string, imageID *string) r.TestCheckFunc {
    94  	return func(s *terraform.State) error {
    95  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    96  		if has, err := hasLaunchPermission(conn, *imageID, accountID); err != nil {
    97  			return err
    98  		} else if has {
    99  			return fmt.Errorf("launch permission still exists for '%s' on '%s'", accountID, *imageID)
   100  		}
   101  		return nil
   102  	}
   103  }
   104  
   105  // testAccAWSAMIDisappears is technically a "test check function" but really it
   106  // exists to perform a side effect of deleting an AMI out from under a resource
   107  // so we can test that Terraform will react properly
   108  func testAccAWSAMIDisappears(imageID *string) r.TestCheckFunc {
   109  	return func(s *terraform.State) error {
   110  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   111  		req := &ec2.DeregisterImageInput{
   112  			ImageId: aws.String(*imageID),
   113  		}
   114  
   115  		_, err := conn.DeregisterImage(req)
   116  		if err != nil {
   117  			return err
   118  		}
   119  
   120  		if err := resourceAwsAmiWaitForDestroy(*imageID, conn); err != nil {
   121  			return err
   122  		}
   123  		return nil
   124  	}
   125  }
   126  
   127  func testAccAWSAMILaunchPermissionConfig(accountID string, includeLaunchPermission bool) string {
   128  	base := `
   129  resource "aws_ami_copy" "test" {
   130    name = "launch-permission-test"
   131    description = "Launch Permission Test Copy"
   132    source_ami_id = "ami-7172b611"
   133    source_ami_region = "us-west-2"
   134  }
   135  `
   136  
   137  	if !includeLaunchPermission {
   138  		return base
   139  	}
   140  
   141  	return base + fmt.Sprintf(`
   142  resource "aws_ami_launch_permission" "self-test" {
   143      image_id   = "${aws_ami_copy.test.id}"
   144      account_id = "%s"
   145  }
   146  `, accountID)
   147  }