github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/service/efs"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSEFSFileSystem(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckEfsFileSystemDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSEFSFileSystemConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckEfsFileSystem(
    25  						"aws_efs_file_system.foo",
    26  					),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccAWSEFSFileSystemConfigWithTags,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckEfsFileSystem(
    33  						"aws_efs_file_system.foo-with-tags",
    34  					),
    35  					testAccCheckEfsFileSystemTags(
    36  						"aws_efs_file_system.foo-with-tags",
    37  						map[string]string{
    38  							"Name":    "foo-efs",
    39  							"Another": "tag",
    40  						},
    41  					),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func testAccCheckEfsFileSystemDestroy(s *terraform.State) error {
    49  	if len(s.RootModule().Resources) > 0 {
    50  		return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources)
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccCheckEfsFileSystem(resourceID string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[resourceID]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", resourceID)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No ID is set")
    65  		}
    66  
    67  		fs, ok := s.RootModule().Resources[resourceID]
    68  		if !ok {
    69  			return fmt.Errorf("Not found: %s", resourceID)
    70  		}
    71  
    72  		conn := testAccProvider.Meta().(*AWSClient).efsconn
    73  		_, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
    74  			FileSystemId: aws.String(fs.Primary.ID),
    75  		})
    76  
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccCheckEfsFileSystemTags(resourceID string, expectedTags map[string]string) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		rs, ok := s.RootModule().Resources[resourceID]
    88  		if !ok {
    89  			return fmt.Errorf("Not found: %s", resourceID)
    90  		}
    91  
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No ID is set")
    94  		}
    95  
    96  		fs, ok := s.RootModule().Resources[resourceID]
    97  		if !ok {
    98  			return fmt.Errorf("Not found: %s", resourceID)
    99  		}
   100  
   101  		conn := testAccProvider.Meta().(*AWSClient).efsconn
   102  		resp, err := conn.DescribeTags(&efs.DescribeTagsInput{
   103  			FileSystemId: aws.String(fs.Primary.ID),
   104  		})
   105  
   106  		if !reflect.DeepEqual(expectedTags, tagsToMapEFS(resp.Tags)) {
   107  			return fmt.Errorf("Tags mismatch.\nExpected: %#v\nGiven: %#v",
   108  				expectedTags, resp.Tags)
   109  		}
   110  
   111  		if err != nil {
   112  			return err
   113  		}
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  const testAccAWSEFSFileSystemConfig = `
   120  resource "aws_efs_file_system" "foo" {
   121  	reference_name = "radeksimko"
   122  }
   123  `
   124  
   125  const testAccAWSEFSFileSystemConfigWithTags = `
   126  resource "aws_efs_file_system" "foo-with-tags" {
   127  	reference_name = "yada_yada"
   128  	tags {
   129  		Name = "foo-efs"
   130  		Another = "tag"
   131  	}
   132  }
   133  `