mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #28457 from hashicorp/jbardin/provisioner-null-checks
additional null checks in provisioners
This commit is contained in:
commit
d15f7394a1
@ -82,10 +82,12 @@ func (p *provisioner) ProvisionResource(req provisioners.ProvisionResourceReques
|
|||||||
|
|
||||||
if !envVal.IsNull() {
|
if !envVal.IsNull() {
|
||||||
for k, v := range envVal.AsValueMap() {
|
for k, v := range envVal.AsValueMap() {
|
||||||
|
if !v.IsNull() {
|
||||||
entry := fmt.Sprintf("%s=%s", k, v.AsString())
|
entry := fmt.Sprintf("%s=%s", k, v.AsString())
|
||||||
env = append(env, entry)
|
env = append(env, entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Execute the command using a shell
|
// Execute the command using a shell
|
||||||
intrVal := req.Config.GetAttr("interpreter")
|
intrVal := req.Config.GetAttr("interpreter")
|
||||||
@ -93,8 +95,10 @@ func (p *provisioner) ProvisionResource(req provisioners.ProvisionResourceReques
|
|||||||
var cmdargs []string
|
var cmdargs []string
|
||||||
if !intrVal.IsNull() && intrVal.LengthInt() > 0 {
|
if !intrVal.IsNull() && intrVal.LengthInt() > 0 {
|
||||||
for _, v := range intrVal.AsValueSlice() {
|
for _, v := range intrVal.AsValueSlice() {
|
||||||
|
if !v.IsNull() {
|
||||||
cmdargs = append(cmdargs, v.AsString())
|
cmdargs = append(cmdargs, v.AsString())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
cmdargs = []string{"cmd", "/C"}
|
cmdargs = []string{"cmd", "/C"}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package localexec
|
package localexec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -204,3 +205,48 @@ func TestResourceProvisioner_StopClose(t *testing.T) {
|
|||||||
p.Stop()
|
p.Stop()
|
||||||
p.Close()
|
p.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResourceProvisioner_nullsInOptionals(t *testing.T) {
|
||||||
|
output := cli.NewMockUi()
|
||||||
|
p := New()
|
||||||
|
schema := p.GetSchema().Provisioner
|
||||||
|
|
||||||
|
for i, cfg := range []cty.Value{
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"command": cty.StringVal("echo OK"),
|
||||||
|
"environment": cty.MapVal(map[string]cty.Value{
|
||||||
|
"FOO": cty.NullVal(cty.String),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"command": cty.StringVal("echo OK"),
|
||||||
|
"environment": cty.NullVal(cty.Map(cty.String)),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"command": cty.StringVal("echo OK"),
|
||||||
|
"interpreter": cty.ListVal([]cty.Value{cty.NullVal(cty.String)}),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"command": cty.StringVal("echo OK"),
|
||||||
|
"interpreter": cty.NullVal(cty.List(cty.String)),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"command": cty.StringVal("echo OK"),
|
||||||
|
"working_dir": cty.NullVal(cty.String),
|
||||||
|
}),
|
||||||
|
} {
|
||||||
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||||
|
|
||||||
|
cfg, err := schema.CoerceValue(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// verifying there are no panics
|
||||||
|
p.ProvisionResource(provisioners.ProvisionResourceRequest{
|
||||||
|
Config: cfg,
|
||||||
|
UIOutput: output,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -128,6 +128,10 @@ func (p *provisioner) Close() error {
|
|||||||
func generateScripts(inline cty.Value) ([]string, error) {
|
func generateScripts(inline cty.Value) ([]string, error) {
|
||||||
var lines []string
|
var lines []string
|
||||||
for _, l := range inline.AsValueSlice() {
|
for _, l := range inline.AsValueSlice() {
|
||||||
|
if l.IsNull() {
|
||||||
|
return nil, errors.New("invalid null string in 'scripts'")
|
||||||
|
}
|
||||||
|
|
||||||
s := l.AsString()
|
s := l.AsString()
|
||||||
if s == "" {
|
if s == "" {
|
||||||
return nil, errors.New("invalid empty string in 'scripts'")
|
return nil, errors.New("invalid empty string in 'scripts'")
|
||||||
@ -169,11 +173,14 @@ func collectScripts(v cty.Value) ([]io.ReadCloser, error) {
|
|||||||
|
|
||||||
if scriptList := v.GetAttr("scripts"); !scriptList.IsNull() {
|
if scriptList := v.GetAttr("scripts"); !scriptList.IsNull() {
|
||||||
for _, script := range scriptList.AsValueSlice() {
|
for _, script := range scriptList.AsValueSlice() {
|
||||||
|
if script.IsNull() {
|
||||||
|
return nil, errors.New("invalid null string in 'script'")
|
||||||
|
}
|
||||||
s := script.AsString()
|
s := script.AsString()
|
||||||
if s == "" {
|
if s == "" {
|
||||||
return nil, errors.New("invalid empty string in 'script'")
|
return nil, errors.New("invalid empty string in 'script'")
|
||||||
}
|
}
|
||||||
scripts = append(scripts, script.AsString())
|
scripts = append(scripts, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package remoteexec
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
@ -274,3 +275,46 @@ func TestResourceProvisioner_connectionRequired(t *testing.T) {
|
|||||||
t.Fatalf("expected 'missing connection' error: got %q", got)
|
t.Fatalf("expected 'missing connection' error: got %q", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResourceProvisioner_nullsInOptionals(t *testing.T) {
|
||||||
|
output := cli.NewMockUi()
|
||||||
|
p := New()
|
||||||
|
schema := p.GetSchema().Provisioner
|
||||||
|
|
||||||
|
for i, cfg := range []cty.Value{
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"script": cty.StringVal("echo"),
|
||||||
|
"inline": cty.NullVal(cty.List(cty.String)),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"inline": cty.ListVal([]cty.Value{
|
||||||
|
cty.NullVal(cty.String),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"script": cty.NullVal(cty.String),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"scripts": cty.NullVal(cty.List(cty.String)),
|
||||||
|
}),
|
||||||
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
|
"scripts": cty.ListVal([]cty.Value{
|
||||||
|
cty.NullVal(cty.String),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
} {
|
||||||
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||||
|
|
||||||
|
cfg, err := schema.CoerceValue(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// verifying there are no panics
|
||||||
|
p.ProvisionResource(provisioners.ProvisionResourceRequest{
|
||||||
|
Config: cfg,
|
||||||
|
UIOutput: output,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user