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