2015-06-02 22:19:50 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-01-25 22:45:58 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-02 22:19:50 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/efs"
|
|
|
|
|
2016-07-28 12:19:39 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-06-02 22:19:50 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2016-09-03 12:40:24 +02:00
|
|
|
func TestResourceAWSEFSFileSystem_validateReferenceName(t *testing.T) {
|
2016-07-28 12:19:39 +02:00
|
|
|
var value string
|
|
|
|
var errors []error
|
|
|
|
|
|
|
|
value = acctest.RandString(128)
|
|
|
|
_, errors = validateReferenceName(value, "reference_name")
|
|
|
|
if len(errors) == 0 {
|
|
|
|
t.Fatalf("Expected to trigger a validation error")
|
|
|
|
}
|
|
|
|
|
|
|
|
value = acctest.RandString(32)
|
|
|
|
_, errors = validateReferenceName(value, "reference_name")
|
|
|
|
if len(errors) != 0 {
|
2016-09-03 12:40:24 +02:00
|
|
|
t.Fatalf("Expected not to trigger a validation error")
|
2016-07-28 12:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-03 12:40:24 +02:00
|
|
|
func TestResourceAWSEFSFileSystem_validatePerformanceModeType(t *testing.T) {
|
|
|
|
_, errors := validatePerformanceModeType("incorrect", "performance_mode")
|
|
|
|
if len(errors) == 0 {
|
|
|
|
t.Fatalf("Expected to trigger a validation error")
|
2016-07-28 12:19:39 +02:00
|
|
|
}
|
|
|
|
|
2016-09-03 12:40:24 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
Value string
|
|
|
|
ErrCount int
|
|
|
|
}{
|
2016-07-28 12:19:39 +02:00
|
|
|
{
|
|
|
|
Value: "generalPurpose",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "maxIO",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-03 12:40:24 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
_, errors := validatePerformanceModeType(tc.Value, "performance_mode")
|
2016-07-28 12:19:39 +02:00
|
|
|
if len(errors) != tc.ErrCount {
|
|
|
|
t.Fatalf("Expected not to trigger a validation error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-03 12:40:24 +02:00
|
|
|
func TestResourceAWSEFSFileSystem_hasEmptyFileSystems(t *testing.T) {
|
|
|
|
fs := &efs.DescribeFileSystemsOutput{
|
|
|
|
FileSystems: []*efs.FileSystemDescription{},
|
|
|
|
}
|
|
|
|
|
|
|
|
var actual bool
|
|
|
|
|
|
|
|
actual = hasEmptyFileSystems(fs)
|
|
|
|
if !actual {
|
|
|
|
t.Fatalf("Expected return value to be true, got %t", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an empty file system.
|
|
|
|
fs.FileSystems = append(fs.FileSystems, &efs.FileSystemDescription{})
|
|
|
|
|
|
|
|
actual = hasEmptyFileSystems(fs)
|
|
|
|
if actual {
|
|
|
|
t.Fatalf("Expected return value to be false, got %t", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-25 22:45:58 +01:00
|
|
|
func TestAccAWSEFSFileSystem_basic(t *testing.T) {
|
2015-06-02 22:19:50 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckEfsFileSystemDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-01 23:09:13 +01:00
|
|
|
{
|
2015-06-02 22:19:50 +02:00
|
|
|
Config: testAccAWSEFSFileSystemConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-10-05 18:14:12 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_efs_file_system.foo",
|
|
|
|
"performance_mode",
|
|
|
|
"generalPurpose"),
|
2015-06-02 22:19:50 +02:00
|
|
|
testAccCheckEfsFileSystem(
|
|
|
|
"aws_efs_file_system.foo",
|
|
|
|
),
|
2016-07-28 12:19:39 +02:00
|
|
|
testAccCheckEfsFileSystemPerformanceMode(
|
|
|
|
"aws_efs_file_system.foo",
|
|
|
|
"generalPurpose",
|
|
|
|
),
|
2015-06-02 22:19:50 +02:00
|
|
|
),
|
|
|
|
},
|
2017-03-01 23:09:13 +01:00
|
|
|
{
|
2015-06-02 22:19:50 +02:00
|
|
|
Config: testAccAWSEFSFileSystemConfigWithTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckEfsFileSystem(
|
|
|
|
"aws_efs_file_system.foo-with-tags",
|
|
|
|
),
|
2016-07-28 12:19:39 +02:00
|
|
|
testAccCheckEfsFileSystemPerformanceMode(
|
|
|
|
"aws_efs_file_system.foo-with-tags",
|
|
|
|
"generalPurpose",
|
|
|
|
),
|
2015-06-02 22:19:50 +02:00
|
|
|
testAccCheckEfsFileSystemTags(
|
|
|
|
"aws_efs_file_system.foo-with-tags",
|
|
|
|
map[string]string{
|
|
|
|
"Name": "foo-efs",
|
|
|
|
"Another": "tag",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2017-03-01 23:09:13 +01:00
|
|
|
{
|
2016-07-28 12:19:39 +02:00
|
|
|
Config: testAccAWSEFSFileSystemConfigWithPerformanceMode,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckEfsFileSystem(
|
|
|
|
"aws_efs_file_system.foo-with-performance-mode",
|
|
|
|
),
|
|
|
|
testAccCheckEfsCreationToken(
|
|
|
|
"aws_efs_file_system.foo-with-performance-mode",
|
|
|
|
"supercalifragilisticexpialidocious",
|
|
|
|
),
|
|
|
|
testAccCheckEfsFileSystemPerformanceMode(
|
|
|
|
"aws_efs_file_system.foo-with-performance-mode",
|
|
|
|
"maxIO",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2015-06-02 22:19:50 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:09:13 +01:00
|
|
|
func TestAccAWSEFSFileSystem_pagedTags(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckEfsFileSystemDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAWSEFSFileSystemConfigPagedTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_efs_file_system.foo",
|
|
|
|
"tags.%",
|
|
|
|
"11"),
|
|
|
|
//testAccCheckEfsFileSystem(
|
|
|
|
// "aws_efs_file_system.foo",
|
|
|
|
//),
|
|
|
|
//testAccCheckEfsFileSystemPerformanceMode(
|
|
|
|
// "aws_efs_file_system.foo",
|
|
|
|
// "generalPurpose",
|
|
|
|
//),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-02 22:19:50 +02:00
|
|
|
func testAccCheckEfsFileSystemDestroy(s *terraform.State) error {
|
2016-01-25 22:45:58 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).efsconn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_efs_file_system" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
|
|
|
|
FileSystemId: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if efsErr, ok := err.(awserr.Error); ok && efsErr.Code() == "FileSystemNotFound" {
|
|
|
|
// gone
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error describing EFS in tests: %s", err)
|
|
|
|
}
|
|
|
|
if len(resp.FileSystems) > 0 {
|
|
|
|
return fmt.Errorf("EFS file system %q still exists", rs.Primary.ID)
|
|
|
|
}
|
2015-06-02 22:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckEfsFileSystem(resourceID string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[resourceID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", resourceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).efsconn
|
|
|
|
_, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
|
2016-07-28 12:19:39 +02:00
|
|
|
FileSystemId: aws.String(rs.Primary.ID),
|
2015-06-02 22:19:50 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:19:39 +02:00
|
|
|
func testAccCheckEfsCreationToken(resourceID string, expectedToken string) resource.TestCheckFunc {
|
2015-06-02 22:19:50 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[resourceID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", resourceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:19:39 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).efsconn
|
|
|
|
resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
|
|
|
|
FileSystemId: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
fs := resp.FileSystems[0]
|
|
|
|
if *fs.CreationToken != expectedToken {
|
|
|
|
return fmt.Errorf("Creation Token mismatch.\nExpected: %s\nGiven: %v",
|
|
|
|
expectedToken, *fs.CreationToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckEfsFileSystemTags(resourceID string, expectedTags map[string]string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[resourceID]
|
2015-06-02 22:19:50 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", resourceID)
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:19:39 +02:00
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2015-06-02 22:19:50 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).efsconn
|
|
|
|
resp, err := conn.DescribeTags(&efs.DescribeTagsInput{
|
2016-07-28 12:19:39 +02:00
|
|
|
FileSystemId: aws.String(rs.Primary.ID),
|
2015-06-02 22:19:50 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedTags, tagsToMapEFS(resp.Tags)) {
|
|
|
|
return fmt.Errorf("Tags mismatch.\nExpected: %#v\nGiven: %#v",
|
|
|
|
expectedTags, resp.Tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 12:19:39 +02:00
|
|
|
func testAccCheckEfsFileSystemPerformanceMode(resourceID string, expectedMode string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[resourceID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", resourceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).efsconn
|
|
|
|
resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
|
|
|
|
FileSystemId: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
fs := resp.FileSystems[0]
|
|
|
|
if *fs.PerformanceMode != expectedMode {
|
|
|
|
return fmt.Errorf("Performance Mode mismatch.\nExpected: %s\nGiven: %v",
|
|
|
|
expectedMode, *fs.PerformanceMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-02 22:19:50 +02:00
|
|
|
const testAccAWSEFSFileSystemConfig = `
|
|
|
|
resource "aws_efs_file_system" "foo" {
|
2016-10-05 18:14:12 +02:00
|
|
|
creation_token = "radeksimko"
|
2015-06-02 22:19:50 +02:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2017-03-01 23:09:13 +01:00
|
|
|
const testAccAWSEFSFileSystemConfigPagedTags = `
|
|
|
|
resource "aws_efs_file_system" "foo" {
|
|
|
|
creation_token = "radeksimko"
|
|
|
|
tags {
|
|
|
|
Name = "foo-efs"
|
|
|
|
Another = "tag"
|
|
|
|
Test = "yes"
|
|
|
|
User = "root"
|
|
|
|
Page = "1"
|
|
|
|
Environment = "prod"
|
|
|
|
CostCenter = "terraform"
|
|
|
|
AcceptanceTest = "PagedTags"
|
|
|
|
CreationToken = "radek"
|
|
|
|
PerfMode = "max"
|
|
|
|
Region = "us-west-2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-06-02 22:19:50 +02:00
|
|
|
const testAccAWSEFSFileSystemConfigWithTags = `
|
|
|
|
resource "aws_efs_file_system" "foo-with-tags" {
|
2016-10-05 18:14:12 +02:00
|
|
|
creation_token = "yada_yada"
|
2015-06-02 22:19:50 +02:00
|
|
|
tags {
|
|
|
|
Name = "foo-efs"
|
|
|
|
Another = "tag"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2016-07-28 12:19:39 +02:00
|
|
|
|
|
|
|
const testAccAWSEFSFileSystemConfigWithPerformanceMode = `
|
|
|
|
resource "aws_efs_file_system" "foo-with-performance-mode" {
|
|
|
|
creation_token = "supercalifragilisticexpialidocious"
|
|
|
|
performance_mode = "maxIO"
|
|
|
|
}
|
|
|
|
`
|