github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  	conn := testAccProvider.Meta().(*AWSClient).glacierconn
   186  
   187  	for _, rs := range s.RootModule().Resources {
   188  		if rs.Type != "aws_glacier_vault" {
   189  			continue
   190  		}
   191  
   192  		input := &glacier.DescribeVaultInput{
   193  			VaultName: aws.String(rs.Primary.ID),
   194  		}
   195  		if _, err := conn.DescribeVault(input); err != nil {
   196  			// Verify the error is what we want
   197  			if ae, ok := err.(awserr.Error); ok && ae.Code() == "ResourceNotFoundException" {
   198  				continue
   199  			}
   200  
   201  			return err
   202  		}
   203  		return fmt.Errorf("still exists")
   204  	}
   205  	return nil
   206  }
   207  
   208  const testAccGlacierVault_basic = `
   209  resource "aws_glacier_vault" "test" {
   210    name = "my_test_vault"
   211  }
   212  `
   213  
   214  const testAccGlacierVault_full = `
   215  resource "aws_sns_topic" "aws_sns_topic" {
   216    name = "glacier-sns-topic"
   217  }
   218  
   219  resource "aws_glacier_vault" "full" {
   220    name = "my_test_vault"
   221    notification {
   222    	sns_topic = "${aws_sns_topic.aws_sns_topic.arn}"
   223    	events = ["ArchiveRetrievalCompleted","InventoryRetrievalCompleted"]
   224    }
   225    tags {
   226      Test="Test1"
   227    }
   228  }
   229  `
   230  
   231  const testAccGlacierVault_withoutNotification = `
   232  resource "aws_sns_topic" "aws_sns_topic" {
   233    name = "glacier-sns-topic"
   234  }
   235  
   236  resource "aws_glacier_vault" "full" {
   237    name = "my_test_vault"
   238    tags {
   239      Test="Test1"
   240    }
   241  }
   242  `