Fix: don't panic when both source and content of file provisioner are null (#1376)

Signed-off-by: Zejun Chen <tibazq@gmail.com>
This commit is contained in:
chenzj 2024-03-14 01:45:36 +08:00 committed by GitHub
parent 07a9185767
commit 8d2216d24b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View File

@ -149,7 +149,7 @@ func getSrc(v cty.Value) (string, bool, error) {
return expansion, false, err
default:
panic("source and content cannot both be null")
return "", false, errors.New("source and content cannot both be null")
}
}

View File

@ -121,3 +121,28 @@ func TestResourceProvisioner_connectionRequired(t *testing.T) {
t.Fatalf("expected 'Missing connection' error: got %q", got)
}
}
func TestResourceProvisioner_nullSrcVars(t *testing.T) {
conn := cty.ObjectVal(map[string]cty.Value{
"type": cty.StringVal(""),
"host": cty.StringVal("localhost"),
})
config := cty.ObjectVal(map[string]cty.Value{
"source": cty.NilVal,
"content": cty.NilVal,
"destination": cty.StringVal("/tmp/bar"),
})
p := New()
resp := p.ProvisionResource(provisioners.ProvisionResourceRequest{
Connection: conn,
Config: config,
})
if !resp.Diagnostics.HasErrors() {
t.Fatal("expected error")
}
got := resp.Diagnostics.Err().Error()
if !strings.Contains(got, "file provisioner error: source and content cannot both be null") {
t.Fatalf("file provisioner error: source and content cannot both be null' error: got %q", got)
}
}