2016-09-03 22:54:34 +02:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-01-28 20:09:24 +01:00
|
|
|
"fmt"
|
2018-12-12 22:08:28 +01:00
|
|
|
"regexp"
|
2016-09-03 22:54:34 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDataSource_dataSourceCount(t *testing.T) {
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: func(s *terraform.State) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
data "test_data_source" "test" {
|
|
|
|
count = 3
|
|
|
|
input = "count-${count.index}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "test_resource" "foo" {
|
|
|
|
required = "yep"
|
|
|
|
required_map = {
|
|
|
|
key = "value"
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:17:27 +02:00
|
|
|
list = "${data.test_data_source.test.*.output}"
|
2016-09-03 22:54:34 +02:00
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: func(s *terraform.State) error {
|
|
|
|
res, hasRes := s.RootModule().Resources["test_resource.foo"]
|
|
|
|
if !hasRes {
|
|
|
|
return errors.New("No test_resource.foo in state")
|
|
|
|
}
|
|
|
|
if res.Primary.Attributes["list.#"] != "3" {
|
|
|
|
return errors.New("Wrong list.#, expected 3")
|
|
|
|
}
|
|
|
|
if res.Primary.Attributes["list.0"] != "count-0" {
|
|
|
|
return errors.New("Wrong list.0, expected count-0")
|
|
|
|
}
|
|
|
|
if res.Primary.Attributes["list.1"] != "count-1" {
|
|
|
|
return errors.New("Wrong list.0, expected count-1")
|
|
|
|
}
|
|
|
|
if res.Primary.Attributes["list.2"] != "count-2" {
|
|
|
|
return errors.New("Wrong list.0, expected count-2")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-01-28 20:09:24 +01:00
|
|
|
|
|
|
|
// Test that the output of a data source can be used as the value for
|
|
|
|
// a "count" in a real resource. This would fail with "count cannot be computed"
|
|
|
|
// at some point.
|
|
|
|
func TestDataSource_valueAsResourceCount(t *testing.T) {
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: func(s *terraform.State) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
data "test_data_source" "test" {
|
|
|
|
input = "4"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "test_resource" "foo" {
|
|
|
|
count = "${data.test_data_source.test.output}"
|
|
|
|
|
|
|
|
required = "yep"
|
|
|
|
required_map = {
|
|
|
|
key = "value"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: func(s *terraform.State) error {
|
|
|
|
count := 0
|
|
|
|
for k, _ := range s.RootModule().Resources {
|
|
|
|
if strings.HasPrefix(k, "test_resource.foo.") {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count != 4 {
|
|
|
|
return fmt.Errorf("bad count: %d", count)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-04-20 01:56:54 +02:00
|
|
|
|
2017-04-20 07:23:52 +02:00
|
|
|
// TestDataSource_dataSourceCountGrandChild tests that a grandchild data source
|
|
|
|
// that is based off of count works, ie: dependency chain foo -> bar -> baz.
|
|
|
|
// This was failing because CountBoundaryTransformer is being run during apply
|
|
|
|
// instead of plan, which meant that it wasn't firing after data sources were
|
|
|
|
// potentially changing state and causing diff/interpolation issues.
|
2017-04-20 01:56:54 +02:00
|
|
|
//
|
|
|
|
// This happens after the initial apply, after state is saved.
|
|
|
|
func TestDataSource_dataSourceCountGrandChild(t *testing.T) {
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: func(s *terraform.State) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: dataSourceCountGrandChildConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: dataSourceCountGrandChildConfig,
|
|
|
|
Check: func(s *terraform.State) error {
|
|
|
|
for _, v := range []string{"foo", "bar", "baz"} {
|
|
|
|
count := 0
|
|
|
|
for k := range s.RootModule().Resources {
|
|
|
|
if strings.HasPrefix(k, fmt.Sprintf("data.test_data_source.%s.", v)) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count != 2 {
|
|
|
|
return fmt.Errorf("bad count for data.test_data_source.%s: %d", v, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const dataSourceCountGrandChildConfig = `
|
|
|
|
data "test_data_source" "foo" {
|
|
|
|
count = 2
|
|
|
|
input = "one"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "bar" {
|
|
|
|
count = "${length(data.test_data_source.foo.*.id)}"
|
|
|
|
input = "${data.test_data_source.foo.*.output[count.index]}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "baz" {
|
|
|
|
count = "${length(data.test_data_source.bar.*.id)}"
|
|
|
|
input = "${data.test_data_source.bar.*.output[count.index]}"
|
|
|
|
}
|
|
|
|
`
|
2018-11-28 21:41:25 +01:00
|
|
|
|
|
|
|
func TestDataSource_nilComputedValues(t *testing.T) {
|
|
|
|
check := func(s *terraform.State) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Check: check,
|
|
|
|
Config: `
|
|
|
|
variable "index" {
|
|
|
|
default = "d"
|
|
|
|
}
|
|
|
|
|
|
|
|
locals {
|
|
|
|
name = {
|
|
|
|
a = "something"
|
|
|
|
b = "else"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "x" {
|
|
|
|
input = "${lookup(local.name, var.index, local.name["a"])}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "y" {
|
|
|
|
input = data.test_data_source.x.nil == "something" ? "something" : "else"
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-12-12 22:08:28 +01:00
|
|
|
|
|
|
|
// referencing test_data_source.one.output_map["a"] should produce an error when
|
2018-12-20 21:28:23 +01:00
|
|
|
// there's a count.
|
2018-12-12 22:08:28 +01:00
|
|
|
func TestDataSource_indexedCountOfOne(t *testing.T) {
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
data "test_data_source" "one" {
|
|
|
|
count = 1
|
|
|
|
input_map = {
|
|
|
|
"a" = "b"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "two" {
|
|
|
|
input_map = {
|
|
|
|
"x" = data.test_data_source.one.output_map["a"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
2018-12-20 21:28:23 +01:00
|
|
|
ExpectError: regexp.MustCompile("Because data.test_data_source.one has \"count\" set, its attributes must be accessed on specific instances"),
|
2018-12-12 22:08:28 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that we can destroy when a data source references something with a
|
|
|
|
// count of 1.
|
|
|
|
func TestDataSource_countRefDestroyError(t *testing.T) {
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
data "test_data_source" "one" {
|
|
|
|
count = 1
|
|
|
|
input = "a"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "two" {
|
|
|
|
input = data.test_data_source.one[0].output
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-05-03 22:23:32 +02:00
|
|
|
|
|
|
|
func TestDataSource_planUpdate(t *testing.T) {
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource" "a" {
|
|
|
|
required = "first"
|
|
|
|
required_map = {
|
|
|
|
key = "1"
|
|
|
|
}
|
|
|
|
optional_force_new = "first"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "a" {
|
|
|
|
input = "${test_resource.a.computed_from_required}"
|
|
|
|
}
|
|
|
|
|
|
|
|
output "out" {
|
|
|
|
value = "${data.test_data_source.a.output}"
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: strings.TrimSpace(`
|
|
|
|
resource "test_resource" "a" {
|
|
|
|
required = "second"
|
|
|
|
required_map = {
|
|
|
|
key = "1"
|
|
|
|
}
|
|
|
|
optional_force_new = "second"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "test_data_source" "a" {
|
|
|
|
input = "${test_resource.a.computed_from_required}"
|
|
|
|
}
|
|
|
|
|
|
|
|
output "out" {
|
|
|
|
value = "${data.test_data_source.a.output}"
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr("data.test_data_source.a", "output", "second"),
|
|
|
|
resource.TestCheckOutput("out", "second"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|