github.com/lymingtonprecision/terraform@v0.9.9-0.20170613092852-62acef9611a9/builtin/providers/test/resource_data_dep_test.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 // TestResourceDataDep_alignedCountScaleOut tests to make sure interpolation 12 // works (namely without index errors) when a data source and a resource share 13 // the same count variable during scale-out with an existing state. 14 func TestResourceDataDep_alignedCountScaleOut(t *testing.T) { 15 resource.UnitTest(t, resource.TestCase{ 16 Providers: testAccProviders, 17 CheckDestroy: func(s *terraform.State) error { 18 return nil 19 }, 20 Steps: []resource.TestStep{ 21 { 22 Config: testResourceDataDepConfig(2), 23 }, 24 { 25 Config: testResourceDataDepConfig(4), 26 Check: resource.TestCheckOutput("out", "value_from_api,value_from_api,value_from_api,value_from_api"), 27 }, 28 }, 29 }) 30 } 31 32 // TestResourceDataDep_alignedCountScaleIn tests to make sure interpolation 33 // works (namely without index errors) when a data source and a resource share 34 // the same count variable during scale-in with an existing state. 35 func TestResourceDataDep_alignedCountScaleIn(t *testing.T) { 36 resource.UnitTest(t, resource.TestCase{ 37 Providers: testAccProviders, 38 CheckDestroy: func(s *terraform.State) error { 39 return nil 40 }, 41 Steps: []resource.TestStep{ 42 { 43 Config: testResourceDataDepConfig(4), 44 }, 45 { 46 Config: testResourceDataDepConfig(2), 47 Check: resource.TestCheckOutput("out", "value_from_api,value_from_api"), 48 }, 49 }, 50 }) 51 } 52 53 // TestDataResourceDep_alignedCountScaleOut functions like 54 // TestResourceDataDep_alignedCountScaleOut, but with the dependencies swapped 55 // (resource now depends on data source, a pretty regular use case, but 56 // included here to check for regressions). 57 func TestDataResourceDep_alignedCountScaleOut(t *testing.T) { 58 resource.UnitTest(t, resource.TestCase{ 59 Providers: testAccProviders, 60 CheckDestroy: func(s *terraform.State) error { 61 return nil 62 }, 63 Steps: []resource.TestStep{ 64 { 65 Config: testDataResourceDepConfig(2), 66 }, 67 { 68 Config: testDataResourceDepConfig(4), 69 Check: resource.TestCheckOutput("out", "test,test,test,test"), 70 }, 71 }, 72 }) 73 } 74 75 // TestDataResourceDep_alignedCountScaleIn functions like 76 // TestResourceDataDep_alignedCountScaleIn, but with the dependencies swapped 77 // (resource now depends on data source, a pretty regular use case, but 78 // included here to check for regressions). 79 func TestDataResourceDep_alignedCountScaleIn(t *testing.T) { 80 resource.UnitTest(t, resource.TestCase{ 81 Providers: testAccProviders, 82 CheckDestroy: func(s *terraform.State) error { 83 return nil 84 }, 85 Steps: []resource.TestStep{ 86 { 87 Config: testDataResourceDepConfig(4), 88 }, 89 { 90 Config: testDataResourceDepConfig(2), 91 Check: resource.TestCheckOutput("out", "test,test"), 92 }, 93 }, 94 }) 95 } 96 97 // TestResourceResourceDep_alignedCountScaleOut functions like 98 // TestResourceDataDep_alignedCountScaleOut, but with a resource-to-resource 99 // dependency instead, a pretty regular use case, but included here to check 100 // for regressions. 101 func TestResourceResourceDep_alignedCountScaleOut(t *testing.T) { 102 resource.UnitTest(t, resource.TestCase{ 103 Providers: testAccProviders, 104 CheckDestroy: func(s *terraform.State) error { 105 return nil 106 }, 107 Steps: []resource.TestStep{ 108 { 109 Config: testResourceResourceDepConfig(2), 110 }, 111 { 112 Config: testResourceResourceDepConfig(4), 113 Check: resource.TestCheckOutput("out", "test,test,test,test"), 114 }, 115 }, 116 }) 117 } 118 119 // TestResourceResourceDep_alignedCountScaleIn functions like 120 // TestResourceDataDep_alignedCountScaleIn, but with a resource-to-resource 121 // dependency instead, a pretty regular use case, but included here to check 122 // for regressions. 123 func TestResourceResourceDep_alignedCountScaleIn(t *testing.T) { 124 resource.UnitTest(t, resource.TestCase{ 125 Providers: testAccProviders, 126 CheckDestroy: func(s *terraform.State) error { 127 return nil 128 }, 129 Steps: []resource.TestStep{ 130 { 131 Config: testResourceResourceDepConfig(4), 132 }, 133 { 134 Config: testResourceResourceDepConfig(2), 135 Check: resource.TestCheckOutput("out", "test,test"), 136 }, 137 }, 138 }) 139 } 140 141 func testResourceDataDepConfig(count int) string { 142 return fmt.Sprintf(` 143 variable count { 144 default = "%d" 145 } 146 147 resource "test_resource" "foo" { 148 count = "${var.count}" 149 required = "yes" 150 151 required_map = { 152 "foo" = "bar" 153 } 154 } 155 156 data "test_data_source" "bar" { 157 count = "${var.count}" 158 input = "${test_resource.foo.*.computed_read_only[count.index]}" 159 } 160 161 output "out" { 162 value = "${join(",", data.test_data_source.bar.*.output)}" 163 } 164 `, count) 165 } 166 167 func testDataResourceDepConfig(count int) string { 168 return fmt.Sprintf(` 169 variable count { 170 default = "%d" 171 } 172 173 data "test_data_source" "foo" { 174 count = "${var.count}" 175 input = "test" 176 } 177 178 resource "test_resource" "bar" { 179 count = "${var.count}" 180 required = "yes" 181 optional = "${data.test_data_source.foo.*.output[count.index]}" 182 183 required_map = { 184 "foo" = "bar" 185 } 186 } 187 188 output "out" { 189 value = "${join(",", test_resource.bar.*.optional)}" 190 } 191 `, count) 192 } 193 194 func testResourceResourceDepConfig(count int) string { 195 return fmt.Sprintf(` 196 variable count { 197 default = "%d" 198 } 199 200 resource "test_resource" "foo" { 201 count = "${var.count}" 202 required = "yes" 203 optional = "test" 204 205 required_map = { 206 "foo" = "bar" 207 } 208 } 209 210 resource "test_resource" "bar" { 211 count = "${var.count}" 212 required = "yes" 213 optional = "${test_resource.foo.*.optional[count.index]}" 214 215 required_map = { 216 "foo" = "bar" 217 } 218 } 219 220 output "out" { 221 value = "${join(",", test_resource.bar.*.optional)}" 222 } 223 `, count) 224 }