improve failing test

Correct the initial test state, and expand the test to cause a cycle
without the previous fix.
This commit is contained in:
James Bardin 2020-09-16 10:05:11 -04:00
parent 2ea921f915
commit 67fd32db7e
1 changed files with 44 additions and 12 deletions

View File

@ -11390,18 +11390,29 @@ variable "ct" {
resource "test_instance" "a" { resource "test_instance" "a" {
count = var.ct count = var.ct
}
resource "test_instance" "b" {
require_new = local.removable
lifecycle { lifecycle {
create_before_destroy = true create_before_destroy = true
} }
} }
resource "test_instance" "b" { resource "test_instance" "c" {
foo = join(".", test_instance.a[*].id) require_new = test_instance.b.id
lifecycle {
create_before_destroy = true
}
} }
output "out" { output "out" {
value = join(".", test_instance.a[*].id) value = join(".", test_instance.a[*].id)
} }
locals {
removable = join(".", test_instance.a[*].id)
}
`}) `})
state := states.NewState() state := states.NewState()
@ -11409,27 +11420,43 @@ output "out" {
root.SetResourceInstanceCurrent( root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_instance.a[0]").Resource, mustResourceInstanceAddr("test_instance.a[0]").Resource,
&states.ResourceInstanceObjectSrc{ &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"a0"}`), AttrsJSON: []byte(`{"id":"a0"}`),
Dependencies: []addrs.ConfigResource{mustResourceAddr("module.child.aws_instance.child")}, Dependencies: []addrs.ConfigResource{},
CreateBeforeDestroy: true,
}, },
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`), mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
) )
root.SetResourceInstanceCurrent( root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_instance.a[1]").Resource, mustResourceInstanceAddr("test_instance.a[1]").Resource,
&states.ResourceInstanceObjectSrc{ &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"a1"}`), AttrsJSON: []byte(`{"id":"a1"}`),
Dependencies: []addrs.ConfigResource{mustResourceAddr("module.child.aws_instance.child")}, Dependencies: []addrs.ConfigResource{},
CreateBeforeDestroy: true,
}, },
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`), mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
) )
root.SetResourceInstanceCurrent( root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_instance.b").Resource, mustResourceInstanceAddr("test_instance.b").Resource,
&states.ResourceInstanceObjectSrc{ &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"b", "foo":"old.old"}`), AttrsJSON: []byte(`{"id":"b", "require_new":"old.old"}`),
Dependencies: []addrs.ConfigResource{mustResourceAddr("test_instance.a")}, Dependencies: []addrs.ConfigResource{mustResourceAddr("test_instance.a")},
CreateBeforeDestroy: true,
},
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_instance.c").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"c", "require_new":"b"}`),
Dependencies: []addrs.ConfigResource{
mustResourceAddr("test_instance.a"),
mustResourceAddr("test_instance.b"),
},
CreateBeforeDestroy: true,
}, },
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`), mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
) )
@ -11443,7 +11470,7 @@ output "out" {
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
Variables: InputValues{ Variables: InputValues{
"ct": &InputValue{ "ct": &InputValue{
Value: cty.NumberIntVal(1), Value: cty.NumberIntVal(0),
SourceType: ValueFromCaller, SourceType: ValueFromCaller,
}, },
}, },
@ -11462,6 +11489,11 @@ output "out" {
// if resource b isn't going to apply correctly, we will get an error about // if resource b isn't going to apply correctly, we will get an error about
// an invalid plan value // an invalid plan value
state, diags = ctx.Apply() state, diags = ctx.Apply()
errMsg := diags.ErrWithWarnings().Error()
if strings.Contains(errMsg, "Cycle") {
t.Fatal("test should not produce a cycle:\n", errMsg)
}
if !diags.HasErrors() { if !diags.HasErrors() {
// FIXME: this test is correct, but needs to wait until we no longer // FIXME: this test is correct, but needs to wait until we no longer
// evaluate resourced that are pending destruction. // evaluate resourced that are pending destruction.