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