github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/aws/resource_aws_efs_mount_target_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/efs"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSEFSMountTarget_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckEfsMountTargetDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSEFSMountTargetConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckEfsMountTarget(
    25  						"aws_efs_mount_target.alpha",
    26  					),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccAWSEFSMountTargetConfigModified,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckEfsMountTarget(
    33  						"aws_efs_mount_target.alpha",
    34  					),
    35  					testAccCheckEfsMountTarget(
    36  						"aws_efs_mount_target.beta",
    37  					),
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func testAccCheckEfsMountTargetDestroy(s *terraform.State) error {
    45  	conn := testAccProvider.Meta().(*AWSClient).efsconn
    46  	for _, rs := range s.RootModule().Resources {
    47  		if rs.Type != "aws_efs_mount_target" {
    48  			continue
    49  		}
    50  
    51  		resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
    52  			MountTargetId: aws.String(rs.Primary.ID),
    53  		})
    54  		if err != nil {
    55  			if efsErr, ok := err.(awserr.Error); ok && efsErr.Code() == "MountTargetNotFound" {
    56  				// gone
    57  				return nil
    58  			}
    59  			return fmt.Errorf("Error describing EFS Mount in tests: %s", err)
    60  		}
    61  		if len(resp.MountTargets) > 0 {
    62  			return fmt.Errorf("EFS Mount target %q still exists", rs.Primary.ID)
    63  		}
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func testAccCheckEfsMountTarget(resourceID string) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  		rs, ok := s.RootModule().Resources[resourceID]
    72  		if !ok {
    73  			return fmt.Errorf("Not found: %s", resourceID)
    74  		}
    75  
    76  		if rs.Primary.ID == "" {
    77  			return fmt.Errorf("No ID is set")
    78  		}
    79  
    80  		fs, ok := s.RootModule().Resources[resourceID]
    81  		if !ok {
    82  			return fmt.Errorf("Not found: %s", resourceID)
    83  		}
    84  
    85  		conn := testAccProvider.Meta().(*AWSClient).efsconn
    86  		mt, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
    87  			MountTargetId: aws.String(fs.Primary.ID),
    88  		})
    89  		if err != nil {
    90  			return err
    91  		}
    92  
    93  		if *mt.MountTargets[0].MountTargetId != fs.Primary.ID {
    94  			return fmt.Errorf("Mount target ID mismatch: %q != %q",
    95  				*mt.MountTargets[0].MountTargetId, fs.Primary.ID)
    96  		}
    97  
    98  		return nil
    99  	}
   100  }
   101  
   102  const testAccAWSEFSMountTargetConfig = `
   103  resource "aws_efs_file_system" "foo" {
   104  	reference_name = "radeksimko"
   105  }
   106  
   107  resource "aws_efs_mount_target" "alpha" {
   108  	file_system_id = "${aws_efs_file_system.foo.id}"
   109  	subnet_id = "${aws_subnet.alpha.id}"
   110  }
   111  
   112  resource "aws_vpc" "foo" {
   113  	cidr_block = "10.0.0.0/16"
   114  }
   115  
   116  resource "aws_subnet" "alpha" {
   117  	vpc_id = "${aws_vpc.foo.id}"
   118  	availability_zone = "us-west-2a"
   119  	cidr_block = "10.0.1.0/24"
   120  }
   121  `
   122  
   123  const testAccAWSEFSMountTargetConfigModified = `
   124  resource "aws_efs_file_system" "foo" {
   125  	reference_name = "radeksimko"
   126  }
   127  
   128  resource "aws_efs_mount_target" "alpha" {
   129  	file_system_id = "${aws_efs_file_system.foo.id}"
   130  	subnet_id = "${aws_subnet.alpha.id}"
   131  }
   132  
   133  resource "aws_efs_mount_target" "beta" {
   134  	file_system_id = "${aws_efs_file_system.foo.id}"
   135  	subnet_id = "${aws_subnet.beta.id}"
   136  }
   137  
   138  resource "aws_vpc" "foo" {
   139  	cidr_block = "10.0.0.0/16"
   140  }
   141  
   142  resource "aws_subnet" "alpha" {
   143  	vpc_id = "${aws_vpc.foo.id}"
   144  	availability_zone = "us-west-2a"
   145  	cidr_block = "10.0.1.0/24"
   146  }
   147  
   148  resource "aws_subnet" "beta" {
   149  	vpc_id = "${aws_vpc.foo.id}"
   150  	availability_zone = "us-west-2b"
   151  	cidr_block = "10.0.2.0/24"
   152  }
   153  `