github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_glacier_vault_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/glacier"
    11  
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSGlacierVault_basic(t *testing.T) {
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckGlacierVaultDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccGlacierVault_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckGlacierVaultExists("aws_glacier_vault.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSGlacierVault_full(t *testing.T) {
    33  	resource.Test(t, resource.TestCase{
    34  		PreCheck:     func() { testAccPreCheck(t) },
    35  		Providers:    testAccProviders,
    36  		CheckDestroy: testAccCheckGlacierVaultDestroy,
    37  		Steps: []resource.TestStep{
    38  			resource.TestStep{
    39  				Config: testAccGlacierVault_full,
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckGlacierVaultExists("aws_glacier_vault.full"),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func TestAccAWSGlacierVault_RemoveNotifications(t *testing.T) {
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccCheckGlacierVaultDestroy,
    53  		Steps: []resource.TestStep{
    54  			resource.TestStep{
    55  				Config: testAccGlacierVault_full,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckGlacierVaultExists("aws_glacier_vault.full"),
    58  				),
    59  			},
    60  			resource.TestStep{
    61  				Config: testAccGlacierVault_withoutNotification,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckGlacierVaultExists("aws_glacier_vault.full"),
    64  					testAccCheckVaultNotificationsMissing("aws_glacier_vault.full"),
    65  				),
    66  			},
    67  		},
    68  	})
    69  }
    70  
    71  func TestDiffGlacierVaultTags(t *testing.T) {
    72  	cases := []struct {
    73  		Old, New map[string]interface{}
    74  		Create   map[string]string
    75  		Remove   []string
    76  	}{
    77  		// Basic add/remove
    78  		{
    79  			Old: map[string]interface{}{
    80  				"foo": "bar",
    81  			},
    82  			New: map[string]interface{}{
    83  				"bar": "baz",
    84  			},
    85  			Create: map[string]string{
    86  				"bar": "baz",
    87  			},
    88  			Remove: []string{
    89  				"foo",
    90  			},
    91  		},
    92  
    93  		// Modify
    94  		{
    95  			Old: map[string]interface{}{
    96  				"foo": "bar",
    97  			},
    98  			New: map[string]interface{}{
    99  				"foo": "baz",
   100  			},
   101  			Create: map[string]string{
   102  				"foo": "baz",
   103  			},
   104  			Remove: []string{
   105  				"foo",
   106  			},
   107  		},
   108  	}
   109  
   110  	for i, tc := range cases {
   111  		c, r := diffGlacierVaultTags(mapGlacierVaultTags(tc.Old), mapGlacierVaultTags(tc.New))
   112  
   113  		if !reflect.DeepEqual(c, tc.Create) {
   114  			t.Fatalf("%d: bad create: %#v", i, c)
   115  		}
   116  		if !reflect.DeepEqual(r, tc.Remove) {
   117  			t.Fatalf("%d: bad remove: %#v", i, r)
   118  		}
   119  	}
   120  }
   121  
   122  func testAccCheckGlacierVaultExists(name string) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		rs, ok := s.RootModule().Resources[name]
   125  		if !ok {
   126  			return fmt.Errorf("Not found: %s", name)
   127  		}
   128  
   129  		if rs.Primary.ID == "" {
   130  			return fmt.Errorf("No ID is set")
   131  		}
   132  
   133  		glacierconn := testAccProvider.Meta().(*AWSClient).glacierconn
   134  		out, err := glacierconn.DescribeVault(&glacier.DescribeVaultInput{
   135  			VaultName: aws.String(rs.Primary.ID),
   136  		})
   137  
   138  		if err != nil {
   139  			return err
   140  		}
   141  
   142  		if out.VaultARN == nil {
   143  			return fmt.Errorf("No Glacier Vault Found")
   144  		}
   145  
   146  		if *out.VaultName != rs.Primary.ID {
   147  			return fmt.Errorf("Glacier Vault Mismatch - existing: %q, state: %q",
   148  				*out.VaultName, rs.Primary.ID)
   149  		}
   150  
   151  		return nil
   152  	}
   153  }
   154  
   155  func testAccCheckVaultNotificationsMissing(name string) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		rs, ok := s.RootModule().Resources[name]
   158  		if !ok {
   159  			return fmt.Errorf("Not found: %s", name)
   160  		}
   161  
   162  		if rs.Primary.ID == "" {
   163  			return fmt.Errorf("No ID is set")
   164  		}
   165  
   166  		glacierconn := testAccProvider.Meta().(*AWSClient).glacierconn
   167  		out, err := glacierconn.GetVaultNotifications(&glacier.GetVaultNotificationsInput{
   168  			VaultName: aws.String(rs.Primary.ID),
   169  		})
   170  
   171  		if awserr, ok := err.(awserr.Error); ok && awserr.Code() != "ResourceNotFoundException" {
   172  			return fmt.Errorf("Expected ResourceNotFoundException for Vault %s Notification Block but got %s", rs.Primary.ID, awserr.Code())
   173  		}
   174  
   175  		if out.VaultNotificationConfig != nil {
   176  			return fmt.Errorf("Vault Notification Block has been found for %s", rs.Primary.ID)
   177  		}
   178  
   179  		return nil
   180  	}
   181  
   182  }
   183  
   184  func testAccCheckGlacierVaultDestroy(s *terraform.State) error {
   185  	if len(s.RootModule().Resources) > 0 {
   186  		return fmt.Errorf("Expected all resources to be gone, but found: %#v",
   187  			s.RootModule().Resources)
   188  	}
   189  
   190  	return nil
   191  }
   192  
   193  const testAccGlacierVault_basic = `
   194  resource "aws_glacier_vault" "test" {
   195    name = "my_test_vault"
   196  }
   197  `
   198  
   199  const testAccGlacierVault_full = `
   200  resource "aws_sns_topic" "aws_sns_topic" {
   201    name = "glacier-sns-topic"
   202  }
   203  
   204  resource "aws_glacier_vault" "full" {
   205    name = "my_test_vault"
   206    notification {
   207    	sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   208    	events = ["ArchiveRetrievalCompleted","InventoryRetrievalCompleted"]
   209    }
   210    tags {
   211      Test="Test1"
   212    }
   213  }
   214  `
   215  
   216  const testAccGlacierVault_withoutNotification = `
   217  resource "aws_sns_topic" "aws_sns_topic" {
   218    name = "glacier-sns-topic"
   219  }
   220  
   221  resource "aws_glacier_vault" "full" {
   222    name = "my_test_vault"
   223    tags {
   224      Test="Test1"
   225    }
   226  }
   227  `