github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  	rInt := acctest.RandInt()
    86  	resource.Test(t, resource.TestCase{
    87  		PreCheck:     func() { testAccPreCheck(t) },
    88  		Providers:    testAccProviders,
    89  		CheckDestroy: testAccCheckEfsFileSystemDestroy,
    90  		Steps: []resource.TestStep{
    91  			{
    92  				Config: testAccAWSEFSFileSystemConfig,
    93  				Check: resource.ComposeTestCheckFunc(
    94  					resource.TestCheckResourceAttr(
    95  						"aws_efs_file_system.foo",
    96  						"performance_mode",
    97  						"generalPurpose"),
    98  					testAccCheckEfsFileSystem(
    99  						"aws_efs_file_system.foo",
   100  					),
   101  					testAccCheckEfsFileSystemPerformanceMode(
   102  						"aws_efs_file_system.foo",
   103  						"generalPurpose",
   104  					),
   105  				),
   106  			},
   107  			{
   108  				Config: testAccAWSEFSFileSystemConfigWithTags(rInt),
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testAccCheckEfsFileSystem(
   111  						"aws_efs_file_system.foo-with-tags",
   112  					),
   113  					testAccCheckEfsFileSystemPerformanceMode(
   114  						"aws_efs_file_system.foo-with-tags",
   115  						"generalPurpose",
   116  					),
   117  					testAccCheckEfsFileSystemTags(
   118  						"aws_efs_file_system.foo-with-tags",
   119  						map[string]string{
   120  							"Name":    fmt.Sprintf("foo-efs-%d", rInt),
   121  							"Another": "tag",
   122  						},
   123  					),
   124  				),
   125  			},
   126  			{
   127  				Config: testAccAWSEFSFileSystemConfigWithPerformanceMode,
   128  				Check: resource.ComposeTestCheckFunc(
   129  					testAccCheckEfsFileSystem(
   130  						"aws_efs_file_system.foo-with-performance-mode",
   131  					),
   132  					testAccCheckEfsCreationToken(
   133  						"aws_efs_file_system.foo-with-performance-mode",
   134  						"supercalifragilisticexpialidocious",
   135  					),
   136  					testAccCheckEfsFileSystemPerformanceMode(
   137  						"aws_efs_file_system.foo-with-performance-mode",
   138  						"maxIO",
   139  					),
   140  				),
   141  			},
   142  		},
   143  	})
   144  }
   145  
   146  func TestAccAWSEFSFileSystem_pagedTags(t *testing.T) {
   147  	rInt := acctest.RandInt()
   148  	resource.Test(t, resource.TestCase{
   149  		PreCheck:     func() { testAccPreCheck(t) },
   150  		Providers:    testAccProviders,
   151  		CheckDestroy: testAccCheckEfsFileSystemDestroy,
   152  		Steps: []resource.TestStep{
   153  			{
   154  				Config: testAccAWSEFSFileSystemConfigPagedTags(rInt),
   155  				Check: resource.ComposeTestCheckFunc(
   156  					resource.TestCheckResourceAttr(
   157  						"aws_efs_file_system.foo",
   158  						"tags.%",
   159  						"11"),
   160  					//testAccCheckEfsFileSystem(
   161  					//	"aws_efs_file_system.foo",
   162  					//),
   163  					//testAccCheckEfsFileSystemPerformanceMode(
   164  					//	"aws_efs_file_system.foo",
   165  					//	"generalPurpose",
   166  					//),
   167  				),
   168  			},
   169  		},
   170  	})
   171  }
   172  
   173  func testAccCheckEfsFileSystemDestroy(s *terraform.State) error {
   174  	conn := testAccProvider.Meta().(*AWSClient).efsconn
   175  	for _, rs := range s.RootModule().Resources {
   176  		if rs.Type != "aws_efs_file_system" {
   177  			continue
   178  		}
   179  
   180  		resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   181  			FileSystemId: aws.String(rs.Primary.ID),
   182  		})
   183  		if err != nil {
   184  			if efsErr, ok := err.(awserr.Error); ok && efsErr.Code() == "FileSystemNotFound" {
   185  				// gone
   186  				return nil
   187  			}
   188  			return fmt.Errorf("Error describing EFS in tests: %s", err)
   189  		}
   190  		if len(resp.FileSystems) > 0 {
   191  			return fmt.Errorf("EFS file system %q still exists", rs.Primary.ID)
   192  		}
   193  	}
   194  
   195  	return nil
   196  }
   197  
   198  func testAccCheckEfsFileSystem(resourceID string) resource.TestCheckFunc {
   199  	return func(s *terraform.State) error {
   200  		rs, ok := s.RootModule().Resources[resourceID]
   201  		if !ok {
   202  			return fmt.Errorf("Not found: %s", resourceID)
   203  		}
   204  
   205  		if rs.Primary.ID == "" {
   206  			return fmt.Errorf("No ID is set")
   207  		}
   208  
   209  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   210  		_, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   211  			FileSystemId: aws.String(rs.Primary.ID),
   212  		})
   213  
   214  		if err != nil {
   215  			return err
   216  		}
   217  
   218  		return nil
   219  	}
   220  }
   221  
   222  func testAccCheckEfsCreationToken(resourceID string, expectedToken string) resource.TestCheckFunc {
   223  	return func(s *terraform.State) error {
   224  		rs, ok := s.RootModule().Resources[resourceID]
   225  		if !ok {
   226  			return fmt.Errorf("Not found: %s", resourceID)
   227  		}
   228  
   229  		if rs.Primary.ID == "" {
   230  			return fmt.Errorf("No ID is set")
   231  		}
   232  
   233  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   234  		resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   235  			FileSystemId: aws.String(rs.Primary.ID),
   236  		})
   237  
   238  		fs := resp.FileSystems[0]
   239  		if *fs.CreationToken != expectedToken {
   240  			return fmt.Errorf("Creation Token mismatch.\nExpected: %s\nGiven: %v",
   241  				expectedToken, *fs.CreationToken)
   242  		}
   243  
   244  		if err != nil {
   245  			return err
   246  		}
   247  
   248  		return nil
   249  	}
   250  }
   251  
   252  func testAccCheckEfsFileSystemTags(resourceID string, expectedTags map[string]string) resource.TestCheckFunc {
   253  	return func(s *terraform.State) error {
   254  		rs, ok := s.RootModule().Resources[resourceID]
   255  		if !ok {
   256  			return fmt.Errorf("Not found: %s", resourceID)
   257  		}
   258  
   259  		if rs.Primary.ID == "" {
   260  			return fmt.Errorf("No ID is set")
   261  		}
   262  
   263  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   264  		resp, err := conn.DescribeTags(&efs.DescribeTagsInput{
   265  			FileSystemId: aws.String(rs.Primary.ID),
   266  		})
   267  
   268  		if !reflect.DeepEqual(expectedTags, tagsToMapEFS(resp.Tags)) {
   269  			return fmt.Errorf("Tags mismatch.\nExpected: %#v\nGiven: %#v",
   270  				expectedTags, resp.Tags)
   271  		}
   272  
   273  		if err != nil {
   274  			return err
   275  		}
   276  
   277  		return nil
   278  	}
   279  }
   280  
   281  func testAccCheckEfsFileSystemPerformanceMode(resourceID string, expectedMode string) resource.TestCheckFunc {
   282  	return func(s *terraform.State) error {
   283  		rs, ok := s.RootModule().Resources[resourceID]
   284  		if !ok {
   285  			return fmt.Errorf("Not found: %s", resourceID)
   286  		}
   287  
   288  		if rs.Primary.ID == "" {
   289  			return fmt.Errorf("No ID is set")
   290  		}
   291  
   292  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   293  		resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   294  			FileSystemId: aws.String(rs.Primary.ID),
   295  		})
   296  
   297  		fs := resp.FileSystems[0]
   298  		if *fs.PerformanceMode != expectedMode {
   299  			return fmt.Errorf("Performance Mode mismatch.\nExpected: %s\nGiven: %v",
   300  				expectedMode, *fs.PerformanceMode)
   301  		}
   302  
   303  		if err != nil {
   304  			return err
   305  		}
   306  
   307  		return nil
   308  	}
   309  }
   310  
   311  const testAccAWSEFSFileSystemConfig = `
   312  resource "aws_efs_file_system" "foo" {
   313  	creation_token = "radeksimko"
   314  }
   315  `
   316  
   317  func testAccAWSEFSFileSystemConfigPagedTags(rInt int) string {
   318  	return fmt.Sprintf(`
   319  	resource "aws_efs_file_system" "foo" {
   320  		tags {
   321  			Name = "foo-efs-%d"
   322  			Another = "tag"
   323  			Test = "yes"
   324  			User = "root"
   325  			Page = "1"
   326  			Environment = "prod"
   327  			CostCenter = "terraform"
   328  			AcceptanceTest = "PagedTags"
   329  			CreationToken = "radek"
   330  			PerfMode = "max"
   331  			Region = "us-west-2"
   332  		}
   333  	}
   334  	`, rInt)
   335  }
   336  
   337  func testAccAWSEFSFileSystemConfigWithTags(rInt int) string {
   338  	return fmt.Sprintf(`
   339  	resource "aws_efs_file_system" "foo-with-tags" {
   340  		tags {
   341  			Name = "foo-efs-%d"
   342  			Another = "tag"
   343  		}
   344  	}
   345  	`, rInt)
   346  }
   347  
   348  const testAccAWSEFSFileSystemConfigWithPerformanceMode = `
   349  resource "aws_efs_file_system" "foo-with-performance-mode" {
   350  	creation_token = "supercalifragilisticexpialidocious"
   351  	performance_mode = "maxIO"
   352  }
   353  `