github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/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("^us-west-2a.[^.]+.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("^us-west-2a.[^.]+.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("^us-west-2b.[^.]+.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("non-existent-1c", 95 "fs-123456ab", "non-existent-1") 96 97 expected := "non-existent-1c.fs-123456ab.efs.non-existent-1.amazonaws.com" 98 if actual != expected { 99 t.Fatalf("Expected EFS mount target DNS name to be %s, got %s", 100 expected, actual) 101 } 102 } 103 104 func TestResourceAWSEFSMountTarget_hasEmptyMountTargets(t *testing.T) { 105 mto := &efs.DescribeMountTargetsOutput{ 106 MountTargets: []*efs.MountTargetDescription{}, 107 } 108 109 var actual bool 110 111 actual = hasEmptyMountTargets(mto) 112 if !actual { 113 t.Fatalf("Expected return value to be true, got %t", actual) 114 } 115 116 // Add an empty mount target. 117 mto.MountTargets = append(mto.MountTargets, &efs.MountTargetDescription{}) 118 119 actual = hasEmptyMountTargets(mto) 120 if actual { 121 t.Fatalf("Expected return value to be false, got %t", actual) 122 } 123 124 } 125 126 func testAccCheckEfsMountTargetDestroy(s *terraform.State) error { 127 conn := testAccProvider.Meta().(*AWSClient).efsconn 128 for _, rs := range s.RootModule().Resources { 129 if rs.Type != "aws_efs_mount_target" { 130 continue 131 } 132 133 resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{ 134 MountTargetId: aws.String(rs.Primary.ID), 135 }) 136 if err != nil { 137 if efsErr, ok := err.(awserr.Error); ok && efsErr.Code() == "MountTargetNotFound" { 138 // gone 139 return nil 140 } 141 return fmt.Errorf("Error describing EFS Mount in tests: %s", err) 142 } 143 if len(resp.MountTargets) > 0 { 144 return fmt.Errorf("EFS Mount target %q still exists", rs.Primary.ID) 145 } 146 } 147 148 return nil 149 } 150 151 func testAccCheckEfsMountTarget(resourceID string, mount *efs.MountTargetDescription) resource.TestCheckFunc { 152 return func(s *terraform.State) error { 153 rs, ok := s.RootModule().Resources[resourceID] 154 if !ok { 155 return fmt.Errorf("Not found: %s", resourceID) 156 } 157 158 if rs.Primary.ID == "" { 159 return fmt.Errorf("No ID is set") 160 } 161 162 fs, ok := s.RootModule().Resources[resourceID] 163 if !ok { 164 return fmt.Errorf("Not found: %s", resourceID) 165 } 166 167 conn := testAccProvider.Meta().(*AWSClient).efsconn 168 mt, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{ 169 MountTargetId: aws.String(fs.Primary.ID), 170 }) 171 if err != nil { 172 return err 173 } 174 175 if *mt.MountTargets[0].MountTargetId != fs.Primary.ID { 176 return fmt.Errorf("Mount target ID mismatch: %q != %q", 177 *mt.MountTargets[0].MountTargetId, fs.Primary.ID) 178 } 179 180 *mount = *mt.MountTargets[0] 181 182 return nil 183 } 184 } 185 186 func testAccAWSEFSMountTargetDisappears(mount *efs.MountTargetDescription) resource.TestCheckFunc { 187 return func(s *terraform.State) error { 188 conn := testAccProvider.Meta().(*AWSClient).efsconn 189 190 _, err := conn.DeleteMountTarget(&efs.DeleteMountTargetInput{ 191 MountTargetId: mount.MountTargetId, 192 }) 193 194 if err != nil { 195 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "MountTargetNotFound" { 196 return nil 197 } 198 return err 199 } 200 201 return resource.Retry(3*time.Minute, func() *resource.RetryError { 202 resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{ 203 MountTargetId: mount.MountTargetId, 204 }) 205 if err != nil { 206 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "MountTargetNotFound" { 207 return nil 208 } 209 return resource.NonRetryableError( 210 fmt.Errorf("Error reading EFS mount target: %s", err)) 211 } 212 if resp.MountTargets == nil || len(resp.MountTargets) < 1 { 213 return nil 214 } 215 if *resp.MountTargets[0].LifeCycleState == "deleted" { 216 return nil 217 } 218 return resource.RetryableError(fmt.Errorf( 219 "Waiting for EFS mount target: %s", *mount.MountTargetId)) 220 }) 221 } 222 223 } 224 225 func testAccAWSEFSMountTargetConfig(ct string) string { 226 return fmt.Sprintf(` 227 resource "aws_efs_file_system" "foo" { 228 creation_token = "%s" 229 } 230 231 resource "aws_efs_mount_target" "alpha" { 232 file_system_id = "${aws_efs_file_system.foo.id}" 233 subnet_id = "${aws_subnet.alpha.id}" 234 } 235 236 resource "aws_vpc" "foo" { 237 cidr_block = "10.0.0.0/16" 238 } 239 240 resource "aws_subnet" "alpha" { 241 vpc_id = "${aws_vpc.foo.id}" 242 availability_zone = "us-west-2a" 243 cidr_block = "10.0.1.0/24" 244 } 245 `, ct) 246 } 247 248 func testAccAWSEFSMountTargetConfigModified(ct string) string { 249 return fmt.Sprintf(` 250 resource "aws_efs_file_system" "foo" { 251 creation_token = "%s" 252 } 253 254 resource "aws_efs_mount_target" "alpha" { 255 file_system_id = "${aws_efs_file_system.foo.id}" 256 subnet_id = "${aws_subnet.alpha.id}" 257 } 258 259 resource "aws_efs_mount_target" "beta" { 260 file_system_id = "${aws_efs_file_system.foo.id}" 261 subnet_id = "${aws_subnet.beta.id}" 262 } 263 264 resource "aws_vpc" "foo" { 265 cidr_block = "10.0.0.0/16" 266 } 267 268 resource "aws_subnet" "alpha" { 269 vpc_id = "${aws_vpc.foo.id}" 270 availability_zone = "us-west-2a" 271 cidr_block = "10.0.1.0/24" 272 } 273 274 resource "aws_subnet" "beta" { 275 vpc_id = "${aws_vpc.foo.id}" 276 availability_zone = "us-west-2b" 277 cidr_block = "10.0.2.0/24" 278 } 279 `, ct) 280 }