github.com/keshavdv/terraform@v0.7.0-rc2.0.20160711232630-d69256dcb425/builtin/providers/aws/resource_aws_efs_mount_target_test.go (about)

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