mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
fix provisioner tests
Add host where required in the test configs, and fix the mock to check for a null connection.
This commit is contained in:
parent
c552284157
commit
0b59f9cad2
@ -5675,11 +5675,8 @@ func TestContext2Apply_provisionerDestroyRefInvalid(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
if _, diags := ctx.Plan(); diags.HasErrors() {
|
||||
t.Fatalf("plan errors: %s", diags.Err())
|
||||
}
|
||||
|
||||
if _, diags := ctx.Apply(); diags == nil {
|
||||
// this was an apply test, but this is now caught in Validation
|
||||
if diags := ctx.Validate(); !diags.HasErrors() {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
@ -374,8 +374,9 @@ func TestEvalValidateProvisioner_valid(t *testing.T) {
|
||||
Config: hcl.EmptyBody(),
|
||||
},
|
||||
ConnConfig: &configs.Connection{
|
||||
//Type: "ssh",
|
||||
Config: hcl.EmptyBody(),
|
||||
Config: configs.SynthBody("", map[string]cty.Value{
|
||||
"host": cty.StringVal("foo"),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
@ -421,6 +422,7 @@ func TestEvalValidateProvisioner_warning(t *testing.T) {
|
||||
},
|
||||
ConnConfig: &configs.Connection{
|
||||
Config: configs.SynthBody("", map[string]cty.Value{
|
||||
"host": cty.StringVal("localhost"),
|
||||
"type": cty.StringVal("ssh"),
|
||||
}),
|
||||
},
|
||||
@ -442,7 +444,7 @@ func TestEvalValidateProvisioner_warning(t *testing.T) {
|
||||
var diags tfdiags.Diagnostics
|
||||
diags = diags.Append(err)
|
||||
if len(diags) != 1 {
|
||||
t.Fatalf("wrong number of diagsnostics in %#v; want one warning", diags)
|
||||
t.Fatalf("wrong number of diagnostics in %s; want one warning", diags.ErrWithWarnings())
|
||||
}
|
||||
|
||||
if got, want := diags[0].Description().Summary, mp.ValidateProvisionerConfigResponse.Diagnostics[0].Description().Summary; got != want {
|
||||
@ -492,7 +494,7 @@ func TestEvalValidateProvisioner_connectionInvalid(t *testing.T) {
|
||||
|
||||
var diags tfdiags.Diagnostics
|
||||
diags = diags.Append(err)
|
||||
if len(diags) != 2 {
|
||||
if len(diags) != 3 {
|
||||
t.Fatalf("wrong number of diagnostics; want two errors\n\n%s", diags.Err())
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
@ -79,21 +80,29 @@ func (p *MockProvisioner) ProvisionResource(r provisioners.ProvisionResourceRequ
|
||||
p.ProvisionResourceCalled = true
|
||||
p.ProvisionResourceRequest = r
|
||||
if p.ApplyFn != nil {
|
||||
if !r.Config.IsKnown() {
|
||||
panic(fmt.Sprintf("cannot provision with unknown value: %#v", r.Config))
|
||||
}
|
||||
|
||||
schema := p.getSchema()
|
||||
rc := NewResourceConfigShimmed(r.Config, schema.Provisioner)
|
||||
connVal := r.Connection
|
||||
connMap := map[string]string{}
|
||||
for it := connVal.ElementIterator(); it.Next(); {
|
||||
ak, av := it.Element()
|
||||
name := ak.AsString()
|
||||
|
||||
if !av.IsKnown() || av.IsNull() {
|
||||
continue
|
||||
if !connVal.IsNull() && connVal.IsKnown() {
|
||||
for it := connVal.ElementIterator(); it.Next(); {
|
||||
ak, av := it.Element()
|
||||
name := ak.AsString()
|
||||
|
||||
if !av.IsKnown() || av.IsNull() {
|
||||
continue
|
||||
}
|
||||
|
||||
av, _ = convert.Convert(av, cty.String)
|
||||
connMap[name] = av.AsString()
|
||||
}
|
||||
|
||||
av, _ = convert.Convert(av, cty.String)
|
||||
connMap[name] = av.AsString()
|
||||
}
|
||||
|
||||
// We no longer pass the full instance state to a provisioner, so we'll
|
||||
// construct a partial one that should be good enough for what existing
|
||||
// test mocks need.
|
||||
|
@ -12,12 +12,14 @@ resource "aws_instance" "foo" {
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
connection {
|
||||
host = "localhost"
|
||||
type = "telnet"
|
||||
}
|
||||
|
||||
provisioner "shell" {
|
||||
foo = "${aws_instance.foo.value}"
|
||||
connection {
|
||||
host = "localhost"
|
||||
type = "telnet"
|
||||
user = "superuser"
|
||||
port = 2222
|
||||
|
@ -25,6 +25,7 @@ resource "aws_load_balancer" "weblb" {
|
||||
provisioner "shell" {
|
||||
cmd = "add ${aws_instance.web.id}"
|
||||
connection {
|
||||
host = "localhost"
|
||||
type = "magic"
|
||||
user = "${aws_security_group.firewall.id}"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user