terraform: more self tests
This commit is contained in:
parent
0e59acc2c9
commit
dd7bc5db0c
|
@ -3812,6 +3812,66 @@ func TestContext2Apply_provisionerSelfRef(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext2Apply_provisionerMultiSelfRef(t *testing.T) {
|
||||||
|
var lock sync.Mutex
|
||||||
|
commands := make([]string, 0, 5)
|
||||||
|
|
||||||
|
m := testModule(t, "apply-provisioner-multi-self-ref")
|
||||||
|
p := testProvider("aws")
|
||||||
|
pr := testProvisioner()
|
||||||
|
p.ApplyFn = testApplyFn
|
||||||
|
p.DiffFn = testDiffFn
|
||||||
|
pr.ApplyFn = func(rs *InstanceState, c *ResourceConfig) error {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
val, ok := c.Config["command"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("bad value for command: %v %#v", val, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
commands = append(commands, val.(string))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testContext2(t, &ContextOpts{
|
||||||
|
Module: m,
|
||||||
|
Providers: map[string]ResourceProviderFactory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
Provisioners: map[string]ResourceProvisionerFactory{
|
||||||
|
"shell": testProvisionerFuncFixed(pr),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if _, err := ctx.Plan(nil); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
state, err := ctx.Apply()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.TrimSpace(state.String())
|
||||||
|
expected := strings.TrimSpace(testTerraformApplyProvisionerMultiSelfRefStr)
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("bad: \n%s", actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify apply was invoked
|
||||||
|
if !pr.ApplyCalled {
|
||||||
|
t.Fatalf("provisioner not invoked")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify our result
|
||||||
|
sort.Strings(commands)
|
||||||
|
expectedCommands := []string{"number 0", "number 1", "number 2"}
|
||||||
|
if !reflect.DeepEqual(commands, expectedCommands) {
|
||||||
|
t.Fatalf("bad: %#v", commands)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Provisioner should NOT run on a diff, only create
|
// Provisioner should NOT run on a diff, only create
|
||||||
func TestContext2Apply_Provisioner_Diff(t *testing.T) {
|
func TestContext2Apply_Provisioner_Diff(t *testing.T) {
|
||||||
m := testModule(t, "apply-provisioner-diff")
|
m := testModule(t, "apply-provisioner-diff")
|
||||||
|
|
|
@ -366,6 +366,21 @@ aws_instance.foo:
|
||||||
type = aws_instance
|
type = aws_instance
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const testTerraformApplyProvisionerMultiSelfRefStr = `
|
||||||
|
aws_instance.foo.0:
|
||||||
|
ID = foo
|
||||||
|
foo = number 0
|
||||||
|
type = aws_instance
|
||||||
|
aws_instance.foo.1:
|
||||||
|
ID = foo
|
||||||
|
foo = number 1
|
||||||
|
type = aws_instance
|
||||||
|
aws_instance.foo.2:
|
||||||
|
ID = foo
|
||||||
|
foo = number 2
|
||||||
|
type = aws_instance
|
||||||
|
`
|
||||||
|
|
||||||
const testTerraformApplyProvisionerDiffStr = `
|
const testTerraformApplyProvisionerDiffStr = `
|
||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = foo
|
ID = foo
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
count = 3
|
||||||
|
foo = "number ${count.index}"
|
||||||
|
|
||||||
|
provisioner "shell" {
|
||||||
|
command = "${self.foo}"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue