github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_efs_mount_target_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/efs"
    12  
    13  	"github.com/hashicorp/terraform/helper/acctest"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/terraform"
    16  )
    17  
    18  func TestAccAWSEFSMountTarget_basic(t *testing.T) {
    19  	var mount efs.MountTargetDescription
    20  	ct := fmt.Sprintf("createtoken-%d", acctest.RandInt())
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckEfsMountTargetDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccAWSEFSMountTargetConfig(ct),
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckEfsMountTarget(
    31  						"aws_efs_mount_target.alpha",
    32  						&mount,
    33  					),
    34  					resource.TestMatchResourceAttr(
    35  						"aws_efs_mount_target.alpha",
    36  						"dns_name",
    37  						regexp.MustCompile("^[^.]+.efs.us-west-2.amazonaws.com$"),
    38  					),
    39  				),
    40  			},
    41  			resource.TestStep{
    42  				Config: testAccAWSEFSMountTargetConfigModified(ct),
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckEfsMountTarget(
    45  						"aws_efs_mount_target.alpha",
    46  						&mount,
    47  					),
    48  					resource.TestMatchResourceAttr(
    49  						"aws_efs_mount_target.alpha",
    50  						"dns_name",
    51  						regexp.MustCompile("^[^.]+.efs.us-west-2.amazonaws.com$"),
    52  					),
    53  					testAccCheckEfsMountTarget(
    54  						"aws_efs_mount_target.beta",
    55  						&mount,
    56  					),
    57  					resource.TestMatchResourceAttr(
    58  						"aws_efs_mount_target.beta",
    59  						"dns_name",
    60  						regexp.MustCompile("^[^.]+.efs.us-west-2.amazonaws.com$"),
    61  					),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func TestAccAWSEFSMountTarget_disappears(t *testing.T) {
    69  	var mount efs.MountTargetDescription
    70  
    71  	ct := fmt.Sprintf("createtoken-%d", acctest.RandInt())
    72  
    73  	resource.Test(t, resource.TestCase{
    74  		PreCheck:     func() { testAccPreCheck(t) },
    75  		Providers:    testAccProviders,
    76  		CheckDestroy: testAccCheckVpnGatewayDestroy,
    77  		Steps: []resource.TestStep{
    78  			resource.TestStep{
    79  				Config: testAccAWSEFSMountTargetConfig(ct),
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testAccCheckEfsMountTarget(
    82  						"aws_efs_mount_target.alpha",
    83  						&mount,
    84  					),
    85  					testAccAWSEFSMountTargetDisappears(&mount),
    86  				),
    87  				ExpectNonEmptyPlan: true,
    88  			},
    89  		},
    90  	})
    91  }
    92  
    93  func TestResourceAWSEFSMountTarget_mountTargetDnsName(t *testing.T) {
    94  	actual := resourceAwsEfsMountTargetDnsName("fs-123456ab", "non-existent-1")
    95  
    96  	expected := "fs-123456ab.efs.non-existent-1.amazonaws.com"
    97  	if actual != expected {
    98  		t.Fatalf("Expected EFS mount target DNS name to be %s, got %s",
    99  			expected, actual)
   100  	}
   101  }
   102  
   103  func TestResourceAWSEFSMountTarget_hasEmptyMountTargets(t *testing.T) {
   104  	mto := &efs.DescribeMountTargetsOutput{
   105  		MountTargets: []*efs.MountTargetDescription{},
   106  	}
   107  
   108  	var actual bool
   109  
   110  	actual = hasEmptyMountTargets(mto)
   111  	if !actual {
   112  		t.Fatalf("Expected return value to be true, got %t", actual)
   113  	}
   114  
   115  	// Add an empty mount target.
   116  	mto.MountTargets = append(mto.MountTargets, &efs.MountTargetDescription{})
   117  
   118  	actual = hasEmptyMountTargets(mto)
   119  	if actual {
   120  		t.Fatalf("Expected return value to be false, got %t", actual)
   121  	}
   122  
   123  }
   124  
   125  func testAccCheckEfsMountTargetDestroy(s *terraform.State) error {
   126  	conn := testAccProvider.Meta().(*AWSClient).efsconn
   127  	for _, rs := range s.RootModule().Resources {
   128  		if rs.Type != "aws_efs_mount_target" {
   129  			continue
   130  		}
   131  
   132  		resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
   133  			MountTargetId: aws.String(rs.Primary.ID),
   134  		})
   135  		if err != nil {
   136  			if efsErr, ok := err.(awserr.Error); ok && efsErr.Code() == "MountTargetNotFound" {
   137  				// gone
   138  				return nil
   139  			}
   140  			return fmt.Errorf("Error describing EFS Mount in tests: %s", err)
   141  		}
   142  		if len(resp.MountTargets) > 0 {
   143  			return fmt.Errorf("EFS Mount target %q still exists", rs.Primary.ID)
   144  		}
   145  	}
   146  
   147  	return nil
   148  }
   149  
   150  func testAccCheckEfsMountTarget(resourceID string, mount *efs.MountTargetDescription) resource.TestCheckFunc {
   151  	return func(s *terraform.State) error {
   152  		rs, ok := s.RootModule().Resources[resourceID]
   153  		if !ok {
   154  			return fmt.Errorf("Not found: %s", resourceID)
   155  		}
   156  
   157  		if rs.Primary.ID == "" {
   158  			return fmt.Errorf("No ID is set")
   159  		}
   160  
   161  		fs, ok := s.RootModule().Resources[resourceID]
   162  		if !ok {
   163  			return fmt.Errorf("Not found: %s", resourceID)
   164  		}
   165  
   166  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   167  		mt, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
   168  			MountTargetId: aws.String(fs.Primary.ID),
   169  		})
   170  		if err != nil {
   171  			return err
   172  		}
   173  
   174  		if *mt.MountTargets[0].MountTargetId != fs.Primary.ID {
   175  			return fmt.Errorf("Mount target ID mismatch: %q != %q",
   176  				*mt.MountTargets[0].MountTargetId, fs.Primary.ID)
   177  		}
   178  
   179  		*mount = *mt.MountTargets[0]
   180  
   181  		return nil
   182  	}
   183  }
   184  
   185  func testAccAWSEFSMountTargetDisappears(mount *efs.MountTargetDescription) resource.TestCheckFunc {
   186  	return func(s *terraform.State) error {
   187  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   188  
   189  		_, err := conn.DeleteMountTarget(&efs.DeleteMountTargetInput{
   190  			MountTargetId: mount.MountTargetId,
   191  		})
   192  
   193  		if err != nil {
   194  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "MountTargetNotFound" {
   195  				return nil
   196  			}
   197  			return err
   198  		}
   199  
   200  		return resource.Retry(3*time.Minute, func() *resource.RetryError {
   201  			resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
   202  				MountTargetId: mount.MountTargetId,
   203  			})
   204  			if err != nil {
   205  				if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "MountTargetNotFound" {
   206  					return nil
   207  				}
   208  				return resource.NonRetryableError(
   209  					fmt.Errorf("Error reading EFS mount target: %s", err))
   210  			}
   211  			if resp.MountTargets == nil || len(resp.MountTargets) < 1 {
   212  				return nil
   213  			}
   214  			if *resp.MountTargets[0].LifeCycleState == "deleted" {
   215  				return nil
   216  			}
   217  			return resource.RetryableError(fmt.Errorf(
   218  				"Waiting for EFS mount target: %s", *mount.MountTargetId))
   219  		})
   220  	}
   221  
   222  }
   223  
   224  func testAccAWSEFSMountTargetConfig(ct string) string {
   225  	return fmt.Sprintf(`
   226  resource "aws_efs_file_system" "foo" {
   227  	creation_token = "%s"
   228  }
   229  
   230  resource "aws_efs_mount_target" "alpha" {
   231  	file_system_id = "${aws_efs_file_system.foo.id}"
   232  	subnet_id = "${aws_subnet.alpha.id}"
   233  }
   234  
   235  resource "aws_vpc" "foo" {
   236  	cidr_block = "10.0.0.0/16"
   237  }
   238  
   239  resource "aws_subnet" "alpha" {
   240  	vpc_id = "${aws_vpc.foo.id}"
   241  	availability_zone = "us-west-2a"
   242  	cidr_block = "10.0.1.0/24"
   243  }
   244  `, ct)
   245  }
   246  
   247  func testAccAWSEFSMountTargetConfigModified(ct string) string {
   248  	return fmt.Sprintf(`
   249  resource "aws_efs_file_system" "foo" {
   250  	creation_token = "%s"
   251  }
   252  
   253  resource "aws_efs_mount_target" "alpha" {
   254  	file_system_id = "${aws_efs_file_system.foo.id}"
   255  	subnet_id = "${aws_subnet.alpha.id}"
   256  }
   257  
   258  resource "aws_efs_mount_target" "beta" {
   259  	file_system_id = "${aws_efs_file_system.foo.id}"
   260  	subnet_id = "${aws_subnet.beta.id}"
   261  }
   262  
   263  resource "aws_vpc" "foo" {
   264  	cidr_block = "10.0.0.0/16"
   265  }
   266  
   267  resource "aws_subnet" "alpha" {
   268  	vpc_id = "${aws_vpc.foo.id}"
   269  	availability_zone = "us-west-2a"
   270  	cidr_block = "10.0.1.0/24"
   271  }
   272  
   273  resource "aws_subnet" "beta" {
   274  	vpc_id = "${aws_vpc.foo.id}"
   275  	availability_zone = "us-west-2b"
   276  	cidr_block = "10.0.2.0/24"
   277  }
   278  `, ct)
   279  }