github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_ami_ids_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/satori/uuid" 9 ) 10 11 func TestAccDataSourceAwsAmiIds_basic(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 Steps: []resource.TestStep{ 16 { 17 Config: testAccDataSourceAwsAmiIdsConfig_basic, 18 Check: resource.ComposeTestCheckFunc( 19 testAccCheckAwsAmiDataSourceID("data.aws_ami_ids.ubuntu"), 20 ), 21 }, 22 }, 23 }) 24 } 25 26 func TestAccDataSourceAwsAmiIds_sorted(t *testing.T) { 27 uuid := uuid.NewV4().String() 28 29 resource.Test(t, resource.TestCase{ 30 PreCheck: func() { testAccPreCheck(t) }, 31 Providers: testAccProviders, 32 Steps: []resource.TestStep{ 33 { 34 Config: testAccDataSourceAwsAmiIdsConfig_sorted1(uuid), 35 Check: resource.ComposeTestCheckFunc( 36 resource.TestCheckResourceAttrSet("aws_ami_from_instance.a", "id"), 37 resource.TestCheckResourceAttrSet("aws_ami_from_instance.b", "id"), 38 ), 39 }, 40 { 41 Config: testAccDataSourceAwsAmiIdsConfig_sorted2(uuid), 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckAwsEbsSnapshotDataSourceID("data.aws_ami_ids.test"), 44 resource.TestCheckResourceAttr("data.aws_ami_ids.test", "ids.#", "2"), 45 resource.TestCheckResourceAttrPair( 46 "data.aws_ami_ids.test", "ids.0", 47 "aws_ami_from_instance.b", "id"), 48 resource.TestCheckResourceAttrPair( 49 "data.aws_ami_ids.test", "ids.1", 50 "aws_ami_from_instance.a", "id"), 51 ), 52 }, 53 }, 54 }) 55 } 56 57 func TestAccDataSourceAwsAmiIds_empty(t *testing.T) { 58 resource.Test(t, resource.TestCase{ 59 PreCheck: func() { testAccPreCheck(t) }, 60 Providers: testAccProviders, 61 Steps: []resource.TestStep{ 62 { 63 Config: testAccDataSourceAwsAmiIdsConfig_empty, 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckAwsAmiDataSourceID("data.aws_ami_ids.empty"), 66 resource.TestCheckResourceAttr("data.aws_ami_ids.empty", "ids.#", "0"), 67 ), 68 }, 69 }, 70 }) 71 } 72 73 const testAccDataSourceAwsAmiIdsConfig_basic = ` 74 data "aws_ami_ids" "ubuntu" { 75 owners = ["099720109477"] 76 77 filter { 78 name = "name" 79 values = ["ubuntu/images/ubuntu-*-*-amd64-server-*"] 80 } 81 } 82 ` 83 84 func testAccDataSourceAwsAmiIdsConfig_sorted1(uuid string) string { 85 return fmt.Sprintf(` 86 resource "aws_instance" "test" { 87 ami = "ami-efd0428f" 88 instance_type = "m3.medium" 89 90 count = 2 91 } 92 93 resource "aws_ami_from_instance" "a" { 94 name = "tf-test-%s-a" 95 source_instance_id = "${aws_instance.test.*.id[0]}" 96 snapshot_without_reboot = true 97 } 98 99 resource "aws_ami_from_instance" "b" { 100 name = "tf-test-%s-b" 101 source_instance_id = "${aws_instance.test.*.id[1]}" 102 snapshot_without_reboot = true 103 104 // We want to ensure that 'aws_ami_from_instance.a.creation_date' is less 105 // than 'aws_ami_from_instance.b.creation_date' so that we can ensure that 106 // the images are being sorted correctly. 107 depends_on = ["aws_ami_from_instance.a"] 108 } 109 `, uuid, uuid) 110 } 111 112 func testAccDataSourceAwsAmiIdsConfig_sorted2(uuid string) string { 113 return testAccDataSourceAwsAmiIdsConfig_sorted1(uuid) + fmt.Sprintf(` 114 data "aws_ami_ids" "test" { 115 owners = ["self"] 116 name_regex = "^tf-test-%s-" 117 } 118 `, uuid) 119 } 120 121 const testAccDataSourceAwsAmiIdsConfig_empty = ` 122 data "aws_ami_ids" "empty" { 123 filter { 124 name = "name" 125 values = [] 126 } 127 } 128 `