mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-14 17:44:26 -06:00
terraform: test for various taint cases
This commit is contained in:
parent
fc84b3a788
commit
341be226f4
@ -4693,6 +4693,132 @@ func TestContext2Apply_taint(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_taintDep(t *testing.T) {
|
||||
m := testModule(t, "apply-taint-dep")
|
||||
p := testProvider("aws")
|
||||
p.ApplyFn = testApplyFn
|
||||
p.DiffFn = testDiffFn
|
||||
s := &State{
|
||||
Modules: []*ModuleState{
|
||||
&ModuleState{
|
||||
Path: rootModulePath,
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.foo": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Tainted: []*InstanceState{
|
||||
&InstanceState{
|
||||
ID: "baz",
|
||||
Attributes: map[string]string{
|
||||
"num": "2",
|
||||
"type": "aws_instance",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"aws_instance.bar": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Primary: &InstanceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"foo": "baz",
|
||||
"num": "2",
|
||||
"type": "aws_instance",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
State: s,
|
||||
})
|
||||
|
||||
if p, err := ctx.Plan(nil); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
} else {
|
||||
t.Logf("plan: %s", p)
|
||||
}
|
||||
|
||||
state, err := ctx.Apply()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(state.String())
|
||||
expected := strings.TrimSpace(testTerraformApplyTaintDepStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_taintDepRequiresNew(t *testing.T) {
|
||||
m := testModule(t, "apply-taint-dep-requires-new")
|
||||
p := testProvider("aws")
|
||||
p.ApplyFn = testApplyFn
|
||||
p.DiffFn = testDiffFn
|
||||
s := &State{
|
||||
Modules: []*ModuleState{
|
||||
&ModuleState{
|
||||
Path: rootModulePath,
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.foo": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Tainted: []*InstanceState{
|
||||
&InstanceState{
|
||||
ID: "baz",
|
||||
Attributes: map[string]string{
|
||||
"num": "2",
|
||||
"type": "aws_instance",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"aws_instance.bar": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Primary: &InstanceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"foo": "baz",
|
||||
"num": "2",
|
||||
"type": "aws_instance",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
State: s,
|
||||
})
|
||||
|
||||
if p, err := ctx.Plan(nil); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
} else {
|
||||
t.Logf("plan: %s", p)
|
||||
}
|
||||
|
||||
state, err := ctx.Apply()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(state.String())
|
||||
expected := strings.TrimSpace(testTerraformApplyTaintDepRequireNewStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_unknownAttribute(t *testing.T) {
|
||||
m := testModule(t, "apply-unknown")
|
||||
p := testProvider("aws")
|
||||
@ -4945,7 +5071,13 @@ func testApplyFn(
|
||||
}
|
||||
|
||||
result := &InstanceState{
|
||||
ID: id,
|
||||
ID: id,
|
||||
Attributes: make(map[string]string),
|
||||
}
|
||||
|
||||
// Copy all the prior attributes
|
||||
for k, v := range s.Attributes {
|
||||
result.Attributes[k] = v
|
||||
}
|
||||
|
||||
if d != nil {
|
||||
|
@ -433,6 +433,36 @@ aws_instance.bar:
|
||||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyTaintDepStr = `
|
||||
aws_instance.bar:
|
||||
ID = bar
|
||||
foo = foo
|
||||
num = 2
|
||||
type = aws_instance
|
||||
|
||||
Dependencies:
|
||||
aws_instance.foo
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
num = 2
|
||||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyTaintDepRequireNewStr = `
|
||||
aws_instance.bar:
|
||||
ID = foo
|
||||
foo = foo
|
||||
require_new = yes
|
||||
type = aws_instance
|
||||
|
||||
Dependencies:
|
||||
aws_instance.foo
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
num = 2
|
||||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyOutputStr = `
|
||||
aws_instance.bar:
|
||||
ID = foo
|
||||
|
10
terraform/test-fixtures/apply-taint-dep-requires-new/main.tf
Normal file
10
terraform/test-fixtures/apply-taint-dep-requires-new/main.tf
Normal file
@ -0,0 +1,10 @@
|
||||
resource "aws_instance" "foo" {
|
||||
id = "foo"
|
||||
num = "2"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
id = "bar"
|
||||
foo = "${aws_instance.foo.id}"
|
||||
require_new = "yes"
|
||||
}
|
10
terraform/test-fixtures/apply-taint-dep/main.tf
Normal file
10
terraform/test-fixtures/apply-taint-dep/main.tf
Normal file
@ -0,0 +1,10 @@
|
||||
resource "aws_instance" "foo" {
|
||||
id = "foo"
|
||||
num = "2"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
id = "bar"
|
||||
num = "2"
|
||||
foo = "${aws_instance.foo.id}"
|
||||
}
|
Loading…
Reference in New Issue
Block a user