github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ami_launch_permission_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	r "github.com/hashicorp/terraform/helper/resource"
     6  	"github.com/hashicorp/terraform/terraform"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  func TestAccAWSAMILaunchPermission_Basic(t *testing.T) {
    12  	image_id := ""
    13  	account_id := os.Getenv("AWS_ACCOUNT_ID")
    14  
    15  	r.Test(t, r.TestCase{
    16  		PreCheck: func() {
    17  			testAccPreCheck(t)
    18  			if os.Getenv("AWS_ACCOUNT_ID") == "" {
    19  				t.Fatal("AWS_ACCOUNT_ID must be set")
    20  			}
    21  		},
    22  		Providers: testAccProviders,
    23  		Steps: []r.TestStep{
    24  			// Scaffold everything
    25  			r.TestStep{
    26  				Config: testAccAWSAMILaunchPermissionConfig(account_id, true),
    27  				Check: r.ComposeTestCheckFunc(
    28  					testCheckResourceGetAttr("aws_ami_copy.test", "id", &image_id),
    29  					testAccAWSAMILaunchPermissionExists(account_id, &image_id),
    30  				),
    31  			},
    32  			// Drop just launch permission to test destruction
    33  			r.TestStep{
    34  				Config: testAccAWSAMILaunchPermissionConfig(account_id, false),
    35  				Check: r.ComposeTestCheckFunc(
    36  					testAccAWSAMILaunchPermissionDestroyed(account_id, &image_id),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func testCheckResourceGetAttr(name, key string, value *string) r.TestCheckFunc {
    44  	return func(s *terraform.State) error {
    45  		ms := s.RootModule()
    46  		rs, ok := ms.Resources[name]
    47  		if !ok {
    48  			return fmt.Errorf("Not found: %s", name)
    49  		}
    50  
    51  		is := rs.Primary
    52  		if is == nil {
    53  			return fmt.Errorf("No primary instance: %s", name)
    54  		}
    55  
    56  		*value = is.Attributes[key]
    57  		return nil
    58  	}
    59  }
    60  
    61  func testAccAWSAMILaunchPermissionExists(account_id string, image_id *string) r.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    64  		if has, err := hasLaunchPermission(conn, *image_id, account_id); err != nil {
    65  			return err
    66  		} else if !has {
    67  			return fmt.Errorf("launch permission does not exist for '%s' on '%s'", account_id, *image_id)
    68  		}
    69  		return nil
    70  	}
    71  }
    72  
    73  func testAccAWSAMILaunchPermissionDestroyed(account_id string, image_id *string) r.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    76  		if has, err := hasLaunchPermission(conn, *image_id, account_id); err != nil {
    77  			return err
    78  		} else if has {
    79  			return fmt.Errorf("launch permission still exists for '%s' on '%s'", account_id, *image_id)
    80  		}
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccAWSAMILaunchPermissionConfig(account_id string, includeLaunchPermission bool) string {
    86  	base := `
    87  resource "aws_ami_copy" "test" {
    88    name = "launch-permission-test"
    89    description = "Launch Permission Test Copy"
    90    source_ami_id = "ami-7172b611"
    91    source_ami_region = "us-west-2"
    92  }
    93  `
    94  
    95  	if !includeLaunchPermission {
    96  		return base
    97  	}
    98  
    99  	return base + fmt.Sprintf(`
   100  resource "aws_ami_launch_permission" "self-test" {
   101      image_id   = "${aws_ami_copy.test.id}"
   102      account_id = "%s"
   103  }
   104  `, account_id)
   105  }