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