github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_sns_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestAccDataSourceAwsSnsTopic(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 Steps: []resource.TestStep{ 16 resource.TestStep{ 17 Config: testAccDataSourceAwsSnsTopicConfig, 18 Check: resource.ComposeTestCheckFunc( 19 testAccDataSourceAwsSnsTopicCheck("data.aws_sns_topic.by_name"), 20 ), 21 }, 22 }, 23 }) 24 } 25 26 func testAccDataSourceAwsSnsTopicCheck(name string) resource.TestCheckFunc { 27 return func(s *terraform.State) error { 28 rs, ok := s.RootModule().Resources[name] 29 if !ok { 30 return fmt.Errorf("root module has no resource called %s", name) 31 } 32 33 snsTopicRs, ok := s.RootModule().Resources["aws_sns_topic.tf_test"] 34 if !ok { 35 return fmt.Errorf("can't find aws_sns_topic.tf_test in state") 36 } 37 38 attr := rs.Primary.Attributes 39 40 if attr["name"] != snsTopicRs.Primary.Attributes["name"] { 41 return fmt.Errorf( 42 "name is %s; want %s", 43 attr["name"], 44 snsTopicRs.Primary.Attributes["name"], 45 ) 46 } 47 48 return nil 49 } 50 } 51 52 const testAccDataSourceAwsSnsTopicConfig = ` 53 provider "aws" { 54 region = "us-west-2" 55 } 56 57 resource "aws_sns_topic" "tf_wrong1" { 58 name = "wrong1" 59 } 60 resource "aws_sns_topic" "tf_test" { 61 name = "tf_test" 62 } 63 resource "aws_sns_topic" "tf_wrong2" { 64 name = "wrong2" 65 } 66 67 data "aws_sns_topic" "by_name" { 68 name = "${aws_sns_topic.tf_test.name}" 69 } 70 `