opentofu/builtin/provisioners/local-exec/resource_provisioner_test.go
James Bardin ff2936bb3f Fix cancellation when spawning a subprocess
If the shell spawns a subprocess which doesn't close the output file
descriptors, the exec.Cmd will block on Wait() (see
golang.org/issue/18874). Use an os.Pipe to provide the command with a
real file descriptor so the exec package doesn't need to do the copy
manually. This in turn may block our own reading goroutine, but we can
select on that and leave it for cleanup later.
2017-02-01 12:01:29 -05:00

109 lines
2.1 KiB
Go

package localexec
import (
"io/ioutil"
"log"
"os"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
)
func TestResourceProvider_Apply(t *testing.T) {
defer os.Remove("test_out")
c := testConfig(t, map[string]interface{}{
"command": "echo foo > test_out",
})
output := new(terraform.MockUIOutput)
p := Provisioner()
if err := p.Apply(output, nil, c); err != nil {
t.Fatalf("err: %v", err)
}
// Check the file
raw, err := ioutil.ReadFile("test_out")
if err != nil {
t.Fatalf("err: %v", err)
}
actual := strings.TrimSpace(string(raw))
expected := "foo"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceProvider_stop(t *testing.T) {
c := testConfig(t, map[string]interface{}{
// bash/zsh/ksh will exec a single command in the same process. This
// makes certain there's a subprocess in the shell.
"command": "sleep 30; sleep 30",
})
output := new(terraform.MockUIOutput)
p := Provisioner()
var err error
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
err = p.Apply(output, nil, c)
}()
select {
case <-doneCh:
t.Fatal("should not finish quickly")
case <-time.After(50 * time.Millisecond):
}
// Stop it
p.Stop()
select {
case <-doneCh:
case <-time.After(500 * time.Millisecond):
log.Fatal("should finish")
}
}
func TestResourceProvider_Validate_good(t *testing.T) {
c := testConfig(t, map[string]interface{}{
"command": "echo foo",
})
p := Provisioner()
warn, errs := p.Validate(c)
if len(warn) > 0 {
t.Fatalf("Warnings: %v", warn)
}
if len(errs) > 0 {
t.Fatalf("Errors: %v", errs)
}
}
func TestResourceProvider_Validate_missing(t *testing.T) {
c := testConfig(t, map[string]interface{}{})
p := Provisioner()
warn, errs := p.Validate(c)
if len(warn) > 0 {
t.Fatalf("Warnings: %v", warn)
}
if len(errs) == 0 {
t.Fatalf("Should have errors")
}
}
func testConfig(
t *testing.T,
c map[string]interface{}) *terraform.ResourceConfig {
r, err := config.NewRawConfig(c)
if err != nil {
t.Fatalf("bad: %s", err)
}
return terraform.NewResourceConfig(r)
}