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"
|
2014-07-14 23:28:37 +02:00
|
|
|
"io"
|
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"
|
|
|
|
|
2018-03-20 19:24:01 +01:00
|
|
|
"github.com/hashicorp/terraform/communicator"
|
|
|
|
"github.com/hashicorp/terraform/communicator/remote"
|
2014-07-10 22:30:44 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2016-12-23 02:05:45 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-10 22:30:44 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2016-11-29 21:00:26 +01:00
|
|
|
func TestResourceProvisioner_impl(t *testing.T) {
|
|
|
|
var _ terraform.ResourceProvisioner = Provisioner()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner(t *testing.T) {
|
|
|
|
if err := Provisioner().(*schema.Provisioner).InternalValidate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:30:44 +02:00
|
|
|
func TestResourceProvider_Validate_good(t *testing.T) {
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
2014-07-14 21:47:04 +02:00
|
|
|
"inline": "echo foo",
|
2014-07-10 22:30:44 +02:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
|
|
|
warn, errs := Provisioner().Validate(c)
|
2014-07-10 22:30:44 +02:00
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("Warnings: %v", warn)
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("Errors: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_Validate_bad(t *testing.T) {
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"invalid": "nope",
|
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
|
|
|
warn, errs := Provisioner().Validate(c)
|
2014-07-10 22:30:44 +02:00
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("Warnings: %v", warn)
|
|
|
|
}
|
|
|
|
if len(errs) == 0 {
|
|
|
|
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) {
|
|
|
|
conf := map[string]interface{}{
|
2014-10-10 06:29:21 +02:00
|
|
|
"inline": []interface{}{
|
2014-07-14 21:47:04 +02:00
|
|
|
"cd /tmp",
|
|
|
|
"wget http://foobar",
|
|
|
|
"exit 0",
|
|
|
|
},
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2017-05-19 20:42:14 +02:00
|
|
|
|
|
|
|
out, err := generateScripts(
|
|
|
|
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, conf),
|
|
|
|
)
|
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) {
|
|
|
|
p := Provisioner().(*schema.Provisioner)
|
|
|
|
conf := map[string]interface{}{
|
|
|
|
"inline": []interface{}{""},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := generateScripts(schema.TestResourceDataRaw(
|
|
|
|
t, p.Schema, conf))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "Error parsing") {
|
|
|
|
t.Fatalf("expected parsing error, got: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 23:28:37 +02:00
|
|
|
func TestResourceProvider_CollectScripts_inline(t *testing.T) {
|
2016-12-23 02:05:45 +01:00
|
|
|
conf := map[string]interface{}{
|
2014-10-10 06:29:21 +02:00
|
|
|
"inline": []interface{}{
|
2014-07-14 23:28:37 +02:00
|
|
|
"cd /tmp",
|
|
|
|
"wget http://foobar",
|
|
|
|
"exit 0",
|
|
|
|
},
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
scripts, err := collectScripts(
|
|
|
|
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, 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) {
|
2016-12-23 02:05:45 +01:00
|
|
|
conf := map[string]interface{}{
|
2019-06-30 09:38:36 +02:00
|
|
|
"script": "testdata/script1.sh",
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
scripts, err := collectScripts(
|
|
|
|
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, 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) {
|
2016-12-23 02:05:45 +01:00
|
|
|
conf := map[string]interface{}{
|
2014-07-14 23:28:37 +02:00
|
|
|
"scripts": []interface{}{
|
2019-06-30 09:38:36 +02:00
|
|
|
"testdata/script1.sh",
|
|
|
|
"testdata/script1.sh",
|
|
|
|
"testdata/script1.sh",
|
2014-07-14 23:28:37 +02:00
|
|
|
},
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
scripts, err := collectScripts(
|
|
|
|
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, 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) {
|
|
|
|
p := Provisioner().(*schema.Provisioner)
|
|
|
|
conf := map[string]interface{}{
|
|
|
|
"scripts": []interface{}{""},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := collectScripts(schema.TestResourceDataRaw(
|
|
|
|
t, p.Schema, conf))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "Error parsing") {
|
|
|
|
t.Fatalf("Expected parsing error, got: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:24:01 +01:00
|
|
|
func TestProvisionerTimeout(t *testing.T) {
|
|
|
|
o := new(terraform.MockUIOutput)
|
|
|
|
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"
|
|
|
|
|
|
|
|
p := Provisioner().(*schema.Provisioner)
|
|
|
|
conf := map[string]interface{}{
|
|
|
|
"inline": []interface{}{"echo hello"},
|
|
|
|
}
|
|
|
|
|
|
|
|
scripts, err := collectScripts(schema.TestResourceDataRaw(
|
|
|
|
t, p.Schema, conf))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
if err := runScripts(ctx, o, c, scripts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-disconnected:
|
|
|
|
t.Fatal("communicator disconnected before command completed")
|
|
|
|
case <-completed:
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
|
2014-07-10 22:30:44 +02:00
|
|
|
r, err := config.NewRawConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return terraform.NewResourceConfig(r)
|
|
|
|
}
|