github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_efs_file_system_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/efs"
    11  
    12  	"github.com/hashicorp/terraform/helper/acctest"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/terraform"
    15  )
    16  
    17  func TestResourceAWSEFSFileSystem_validateReferenceName(t *testing.T) {
    18  	var value string
    19  	var errors []error
    20  
    21  	value = acctest.RandString(128)
    22  	_, errors = validateReferenceName(value, "reference_name")
    23  	if len(errors) == 0 {
    24  		t.Fatalf("Expected to trigger a validation error")
    25  	}
    26  
    27  	value = acctest.RandString(32)
    28  	_, errors = validateReferenceName(value, "reference_name")
    29  	if len(errors) != 0 {
    30  		t.Fatalf("Expected not to trigger a validation error")
    31  	}
    32  }
    33  
    34  func TestResourceAWSEFSFileSystem_validatePerformanceModeType(t *testing.T) {
    35  	_, errors := validatePerformanceModeType("incorrect", "performance_mode")
    36  	if len(errors) == 0 {
    37  		t.Fatalf("Expected to trigger a validation error")
    38  	}
    39  
    40  	var testCases = []struct {
    41  		Value    string
    42  		ErrCount int
    43  	}{
    44  		{
    45  			Value:    "generalPurpose",
    46  			ErrCount: 0,
    47  		},
    48  		{
    49  			Value:    "maxIO",
    50  			ErrCount: 0,
    51  		},
    52  	}
    53  
    54  	for _, tc := range testCases {
    55  		_, errors := validatePerformanceModeType(tc.Value, "performance_mode")
    56  		if len(errors) != tc.ErrCount {
    57  			t.Fatalf("Expected not to trigger a validation error")
    58  		}
    59  	}
    60  }
    61  
    62  func TestResourceAWSEFSFileSystem_hasEmptyFileSystems(t *testing.T) {
    63  	fs := &efs.DescribeFileSystemsOutput{
    64  		FileSystems: []*efs.FileSystemDescription{},
    65  	}
    66  
    67  	var actual bool
    68  
    69  	actual = hasEmptyFileSystems(fs)
    70  	if !actual {
    71  		t.Fatalf("Expected return value to be true, got %t", actual)
    72  	}
    73  
    74  	// Add an empty file system.
    75  	fs.FileSystems = append(fs.FileSystems, &efs.FileSystemDescription{})
    76  
    77  	actual = hasEmptyFileSystems(fs)
    78  	if actual {
    79  		t.Fatalf("Expected return value to be false, got %t", actual)
    80  	}
    81  
    82  }
    83  
    84  func TestAccAWSEFSFileSystem_basic(t *testing.T) {
    85  	resource.Test(t, resource.TestCase{
    86  		PreCheck:     func() { testAccPreCheck(t) },
    87  		Providers:    testAccProviders,
    88  		CheckDestroy: testAccCheckEfsFileSystemDestroy,
    89  		Steps: []resource.TestStep{
    90  			resource.TestStep{
    91  				Config: testAccAWSEFSFileSystemConfig,
    92  				Check: resource.ComposeTestCheckFunc(
    93  					resource.TestCheckResourceAttr(
    94  						"aws_efs_file_system.foo",
    95  						"performance_mode",
    96  						"generalPurpose"),
    97  					testAccCheckEfsFileSystem(
    98  						"aws_efs_file_system.foo",
    99  					),
   100  					testAccCheckEfsFileSystemPerformanceMode(
   101  						"aws_efs_file_system.foo",
   102  						"generalPurpose",
   103  					),
   104  				),
   105  			},
   106  			resource.TestStep{
   107  				Config: testAccAWSEFSFileSystemConfigWithTags,
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testAccCheckEfsFileSystem(
   110  						"aws_efs_file_system.foo-with-tags",
   111  					),
   112  					testAccCheckEfsFileSystemPerformanceMode(
   113  						"aws_efs_file_system.foo-with-tags",
   114  						"generalPurpose",
   115  					),
   116  					testAccCheckEfsFileSystemTags(
   117  						"aws_efs_file_system.foo-with-tags",
   118  						map[string]string{
   119  							"Name":    "foo-efs",
   120  							"Another": "tag",
   121  						},
   122  					),
   123  				),
   124  			},
   125  			resource.TestStep{
   126  				Config: testAccAWSEFSFileSystemConfigWithPerformanceMode,
   127  				Check: resource.ComposeTestCheckFunc(
   128  					testAccCheckEfsFileSystem(
   129  						"aws_efs_file_system.foo-with-performance-mode",
   130  					),
   131  					testAccCheckEfsCreationToken(
   132  						"aws_efs_file_system.foo-with-performance-mode",
   133  						"supercalifragilisticexpialidocious",
   134  					),
   135  					testAccCheckEfsFileSystemPerformanceMode(
   136  						"aws_efs_file_system.foo-with-performance-mode",
   137  						"maxIO",
   138  					),
   139  				),
   140  			},
   141  		},
   142  	})
   143  }
   144  
   145  func testAccCheckEfsFileSystemDestroy(s *terraform.State) error {
   146  	conn := testAccProvider.Meta().(*AWSClient).efsconn
   147  	for _, rs := range s.RootModule().Resources {
   148  		if rs.Type != "aws_efs_file_system" {
   149  			continue
   150  		}
   151  
   152  		resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   153  			FileSystemId: aws.String(rs.Primary.ID),
   154  		})
   155  		if err != nil {
   156  			if efsErr, ok := err.(awserr.Error); ok && efsErr.Code() == "FileSystemNotFound" {
   157  				// gone
   158  				return nil
   159  			}
   160  			return fmt.Errorf("Error describing EFS in tests: %s", err)
   161  		}
   162  		if len(resp.FileSystems) > 0 {
   163  			return fmt.Errorf("EFS file system %q still exists", rs.Primary.ID)
   164  		}
   165  	}
   166  
   167  	return nil
   168  }
   169  
   170  func testAccCheckEfsFileSystem(resourceID string) resource.TestCheckFunc {
   171  	return func(s *terraform.State) error {
   172  		rs, ok := s.RootModule().Resources[resourceID]
   173  		if !ok {
   174  			return fmt.Errorf("Not found: %s", resourceID)
   175  		}
   176  
   177  		if rs.Primary.ID == "" {
   178  			return fmt.Errorf("No ID is set")
   179  		}
   180  
   181  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   182  		_, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   183  			FileSystemId: aws.String(rs.Primary.ID),
   184  		})
   185  
   186  		if err != nil {
   187  			return err
   188  		}
   189  
   190  		return nil
   191  	}
   192  }
   193  
   194  func testAccCheckEfsCreationToken(resourceID string, expectedToken string) resource.TestCheckFunc {
   195  	return func(s *terraform.State) error {
   196  		rs, ok := s.RootModule().Resources[resourceID]
   197  		if !ok {
   198  			return fmt.Errorf("Not found: %s", resourceID)
   199  		}
   200  
   201  		if rs.Primary.ID == "" {
   202  			return fmt.Errorf("No ID is set")
   203  		}
   204  
   205  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   206  		resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   207  			FileSystemId: aws.String(rs.Primary.ID),
   208  		})
   209  
   210  		fs := resp.FileSystems[0]
   211  		if *fs.CreationToken != expectedToken {
   212  			return fmt.Errorf("Creation Token mismatch.\nExpected: %s\nGiven: %v",
   213  				expectedToken, *fs.CreationToken)
   214  		}
   215  
   216  		if err != nil {
   217  			return err
   218  		}
   219  
   220  		return nil
   221  	}
   222  }
   223  
   224  func testAccCheckEfsFileSystemTags(resourceID string, expectedTags map[string]string) resource.TestCheckFunc {
   225  	return func(s *terraform.State) error {
   226  		rs, ok := s.RootModule().Resources[resourceID]
   227  		if !ok {
   228  			return fmt.Errorf("Not found: %s", resourceID)
   229  		}
   230  
   231  		if rs.Primary.ID == "" {
   232  			return fmt.Errorf("No ID is set")
   233  		}
   234  
   235  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   236  		resp, err := conn.DescribeTags(&efs.DescribeTagsInput{
   237  			FileSystemId: aws.String(rs.Primary.ID),
   238  		})
   239  
   240  		if !reflect.DeepEqual(expectedTags, tagsToMapEFS(resp.Tags)) {
   241  			return fmt.Errorf("Tags mismatch.\nExpected: %#v\nGiven: %#v",
   242  				expectedTags, resp.Tags)
   243  		}
   244  
   245  		if err != nil {
   246  			return err
   247  		}
   248  
   249  		return nil
   250  	}
   251  }
   252  
   253  func testAccCheckEfsFileSystemPerformanceMode(resourceID string, expectedMode string) resource.TestCheckFunc {
   254  	return func(s *terraform.State) error {
   255  		rs, ok := s.RootModule().Resources[resourceID]
   256  		if !ok {
   257  			return fmt.Errorf("Not found: %s", resourceID)
   258  		}
   259  
   260  		if rs.Primary.ID == "" {
   261  			return fmt.Errorf("No ID is set")
   262  		}
   263  
   264  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   265  		resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   266  			FileSystemId: aws.String(rs.Primary.ID),
   267  		})
   268  
   269  		fs := resp.FileSystems[0]
   270  		if *fs.PerformanceMode != expectedMode {
   271  			return fmt.Errorf("Performance Mode mismatch.\nExpected: %s\nGiven: %v",
   272  				expectedMode, *fs.PerformanceMode)
   273  		}
   274  
   275  		if err != nil {
   276  			return err
   277  		}
   278  
   279  		return nil
   280  	}
   281  }
   282  
   283  const testAccAWSEFSFileSystemConfig = `
   284  resource "aws_efs_file_system" "foo" {
   285  	creation_token = "radeksimko"
   286  }
   287  `
   288  
   289  const testAccAWSEFSFileSystemConfigWithTags = `
   290  resource "aws_efs_file_system" "foo-with-tags" {
   291  	creation_token = "yada_yada"
   292  	tags {
   293  		Name = "foo-efs"
   294  		Another = "tag"
   295  	}
   296  }
   297  `
   298  
   299  const testAccAWSEFSFileSystemConfigWithPerformanceMode = `
   300  resource "aws_efs_file_system" "foo-with-performance-mode" {
   301  	creation_token = "supercalifragilisticexpialidocious"
   302  	performance_mode = "maxIO"
   303  }
   304  `