2014-07-10 22:30:44 +02:00
|
|
|
package remoteexec
|
|
|
|
|
|
|
|
import (
|
2014-07-14 23:28:37 +02:00
|
|
|
"bytes"
|
2018-03-20 19:24:01 +01:00
|
|
|
"context"
|
2021-04-20 18:31:32 +02:00
|
|
|
"fmt"
|
2014-07-14 23:28:37 +02:00
|
|
|
"io"
|
2020-11-25 20:06:32 +01:00
|
|
|
"log"
|
2014-07-10 22:30:44 +02:00
|
|
|
"testing"
|
2018-03-20 19:24:01 +01:00
|
|
|
"time"
|
2014-07-10 22:30:44 +02:00
|
|
|
|
2017-05-01 22:48:42 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-05-17 19:00:12 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/communicator"
|
|
|
|
"github.com/hashicorp/terraform/internal/communicator/remote"
|
2021-05-17 19:51:48 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/provisioners"
|
2020-11-25 20:06:32 +01:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-07-10 22:30:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceProvider_Validate_good(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
c := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"inline": cty.ListVal([]cty.Value{cty.StringVal("echo foo")}),
|
2014-07-10 22:30:44 +02:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: c,
|
|
|
|
})
|
|
|
|
if len(resp.Diagnostics) > 0 {
|
|
|
|
t.Fatal(resp.Diagnostics.ErrWithWarnings())
|
2014-07-10 22:30:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_Validate_bad(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
c := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"invalid": cty.StringVal("nope"),
|
2014-07-10 22:30:44 +02:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: c,
|
|
|
|
})
|
|
|
|
if !resp.Diagnostics.HasErrors() {
|
2014-07-10 22:30:44 +02:00
|
|
|
t.Fatalf("Should have errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 00:50:48 +02:00
|
|
|
var expectedScriptOut = `cd /tmp
|
|
|
|
wget http://foobar
|
|
|
|
exit 0
|
|
|
|
`
|
|
|
|
|
2016-12-23 02:05:45 +01:00
|
|
|
func TestResourceProvider_generateScript(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
inline := cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("cd /tmp"),
|
|
|
|
cty.StringVal("wget http://foobar"),
|
|
|
|
cty.StringVal("exit 0"),
|
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
out, err := generateScripts(inline)
|
2014-07-14 21:47:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:51:51 +01:00
|
|
|
if len(out) != 1 {
|
|
|
|
t.Fatal("expected 1 out")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out[0] != expectedScriptOut {
|
2014-07-14 21:47:04 +02:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 22:48:42 +02:00
|
|
|
func TestResourceProvider_generateScriptEmptyInline(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
inline := cty.ListVal([]cty.Value{cty.StringVal("")})
|
2017-05-01 22:48:42 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
_, err := generateScripts(inline)
|
2017-05-01 22:48:42 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got none")
|
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
if !strings.Contains(err.Error(), "empty string") {
|
|
|
|
t.Fatalf("expected empty string error, got: %s", err)
|
2017-05-01 22:48:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 23:28:37 +02:00
|
|
|
func TestResourceProvider_CollectScripts_inline(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
conf := map[string]cty.Value{
|
|
|
|
"inline": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("cd /tmp"),
|
|
|
|
cty.StringVal("wget http://foobar"),
|
|
|
|
cty.StringVal("exit 0"),
|
|
|
|
}),
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
scripts, err := collectScripts(cty.ObjectVal(conf))
|
2014-07-14 23:28:37 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:51:51 +01:00
|
|
|
if len(scripts) != 1 {
|
2014-07-14 23:28:37 +02:00
|
|
|
t.Fatalf("bad: %v", scripts)
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:51:51 +01:00
|
|
|
var out bytes.Buffer
|
|
|
|
_, err = io.Copy(&out, scripts[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2017-02-07 01:51:51 +01:00
|
|
|
if out.String() != expectedScriptOut {
|
|
|
|
t.Fatalf("bad: %v", out.String())
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_CollectScripts_script(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
p := New()
|
|
|
|
schema := p.GetSchema().Provisioner
|
|
|
|
|
|
|
|
conf, err := schema.CoerceValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"scripts": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("testdata/script1.sh"),
|
|
|
|
}),
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
scripts, err := collectScripts(conf)
|
2014-07-14 23:28:37 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(scripts) != 1 {
|
|
|
|
t.Fatalf("bad: %v", scripts)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
_, err = io.Copy(&out, scripts[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-10 00:50:48 +02:00
|
|
|
if out.String() != expectedScriptOut {
|
|
|
|
t.Fatalf("bad: %v", out.String())
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_CollectScripts_scripts(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
p := New()
|
|
|
|
schema := p.GetSchema().Provisioner
|
|
|
|
|
|
|
|
conf, err := schema.CoerceValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"scripts": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("testdata/script1.sh"),
|
|
|
|
cty.StringVal("testdata/script1.sh"),
|
|
|
|
cty.StringVal("testdata/script1.sh"),
|
|
|
|
}),
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
scripts, err := collectScripts(conf)
|
2014-07-14 23:28:37 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(scripts) != 3 {
|
|
|
|
t.Fatalf("bad: %v", scripts)
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range scripts {
|
|
|
|
var out bytes.Buffer
|
|
|
|
_, err = io.Copy(&out, scripts[idx])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-10 00:50:48 +02:00
|
|
|
if out.String() != expectedScriptOut {
|
|
|
|
t.Fatalf("bad: %v", out.String())
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 22:48:42 +02:00
|
|
|
func TestResourceProvider_CollectScripts_scriptsEmpty(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
p := New()
|
|
|
|
schema := p.GetSchema().Provisioner
|
2017-05-01 22:48:42 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
conf, err := schema.CoerceValue(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"scripts": cty.ListVal([]cty.Value{cty.StringVal("")}),
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-01 22:48:42 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
_, err = collectScripts(conf)
|
2017-05-01 22:48:42 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
if !strings.Contains(err.Error(), "empty string") {
|
|
|
|
t.Fatalf("Expected empty string error, got: %s", err)
|
2017-05-01 22:48:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:24:01 +01:00
|
|
|
func TestProvisionerTimeout(t *testing.T) {
|
2020-11-25 20:06:32 +01:00
|
|
|
o := cli.NewMockUi()
|
2018-03-20 19:24:01 +01:00
|
|
|
c := new(communicator.MockCommunicator)
|
|
|
|
|
|
|
|
disconnected := make(chan struct{})
|
|
|
|
c.DisconnectFunc = func() error {
|
|
|
|
close(disconnected)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
completed := make(chan struct{})
|
|
|
|
c.CommandFunc = func(cmd *remote.Cmd) error {
|
|
|
|
defer close(completed)
|
|
|
|
cmd.Init()
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
cmd.SetExitStatus(0, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.ConnTimeout = time.Second
|
|
|
|
c.UploadScripts = map[string]string{"hello": "echo hello"}
|
|
|
|
c.RemoteScriptPath = "hello"
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
conf := map[string]cty.Value{
|
|
|
|
"inline": cty.ListVal([]cty.Value{cty.StringVal("echo hello")}),
|
2018-03-20 19:24:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
scripts, err := collectScripts(cty.ObjectVal(conf))
|
2018-03-20 19:24:01 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
2020-12-01 22:55:50 +01:00
|
|
|
var runErr error
|
2018-03-20 19:24:01 +01:00
|
|
|
go func() {
|
|
|
|
defer close(done)
|
2020-12-01 22:55:50 +01:00
|
|
|
runErr = runScripts(ctx, o, c, scripts)
|
2018-03-20 19:24:01 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-disconnected:
|
|
|
|
t.Fatal("communicator disconnected before command completed")
|
|
|
|
case <-completed:
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
2020-12-01 22:55:50 +01:00
|
|
|
if runErr != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-10 22:30:44 +02:00
|
|
|
}
|
2021-01-08 18:36:45 +01:00
|
|
|
|
|
|
|
// Validate that Stop can Close can be called even when not provisioning.
|
|
|
|
func TestResourceProvisioner_StopClose(t *testing.T) {
|
|
|
|
p := New()
|
|
|
|
p.Stop()
|
|
|
|
p.Close()
|
|
|
|
}
|
2021-02-24 19:25:58 +01:00
|
|
|
|
|
|
|
func TestResourceProvisioner_connectionRequired(t *testing.T) {
|
|
|
|
p := New()
|
|
|
|
resp := p.ProvisionResource(provisioners.ProvisionResourceRequest{})
|
|
|
|
if !resp.Diagnostics.HasErrors() {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
got := resp.Diagnostics.Err().Error()
|
2021-05-19 17:10:05 +02:00
|
|
|
if !strings.Contains(got, "Missing connection") {
|
|
|
|
t.Fatalf("expected 'Missing connection' error: got %q", got)
|
2021-02-24 19:25:58 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-20 18:31:32 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|