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