2014-06-18 23:36:44 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-01-18 22:50:45 -06:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2016-08-01 16:16:22 -05:00
|
|
|
"flag"
|
2017-02-03 14:32:40 -06:00
|
|
|
"fmt"
|
2016-11-30 13:36:54 -06:00
|
|
|
"io"
|
2014-06-27 16:43:23 -05:00
|
|
|
"io/ioutil"
|
2016-08-01 16:16:22 -05:00
|
|
|
"log"
|
2017-01-18 22:50:45 -06:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-06-27 16:43:23 -05:00
|
|
|
"os"
|
2017-02-03 14:32:40 -06:00
|
|
|
"os/exec"
|
2014-06-18 23:36:44 -05:00
|
|
|
"path/filepath"
|
2015-02-26 12:29:23 -06:00
|
|
|
"strings"
|
2017-02-03 14:32:40 -06:00
|
|
|
"syscall"
|
2014-06-27 16:43:23 -05:00
|
|
|
"testing"
|
2014-06-18 23:36:44 -05:00
|
|
|
|
2018-07-04 10:24:49 -05:00
|
|
|
backendInit "github.com/hashicorp/terraform/backend/init"
|
2014-09-24 17:48:46 -05:00
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2016-08-01 16:16:22 -05:00
|
|
|
"github.com/hashicorp/terraform/helper/logging"
|
2014-06-18 23:36:44 -05:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is the directory where our test fixtures are.
|
2014-07-11 23:03:56 -05:00
|
|
|
var fixtureDir = "./test-fixtures"
|
|
|
|
|
2018-03-28 12:08:38 -05:00
|
|
|
// a top level temp directory which will be cleaned after all tests
|
|
|
|
var testingDir string
|
|
|
|
|
2014-07-11 23:03:56 -05:00
|
|
|
func init() {
|
2014-09-29 13:24:16 -05:00
|
|
|
test = true
|
|
|
|
|
2018-07-04 10:24:49 -05:00
|
|
|
// Initialize the backends
|
|
|
|
backendInit.Init(nil)
|
|
|
|
|
2014-07-11 23:03:56 -05:00
|
|
|
// Expand the fixture dir on init because we change the working
|
|
|
|
// directory in some tests.
|
|
|
|
var err error
|
|
|
|
fixtureDir, err = filepath.Abs(fixtureDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-28 12:08:38 -05:00
|
|
|
|
|
|
|
testingDir, err = ioutil.TempDir(testingDir, "tf")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-11 23:03:56 -05:00
|
|
|
}
|
2014-06-18 23:36:44 -05:00
|
|
|
|
2016-08-01 16:16:22 -05:00
|
|
|
func TestMain(m *testing.M) {
|
2018-03-28 12:08:38 -05:00
|
|
|
defer os.RemoveAll(testingDir)
|
|
|
|
|
2016-08-01 16:16:22 -05:00
|
|
|
flag.Parse()
|
|
|
|
if testing.Verbose() {
|
|
|
|
// if we're verbose, use the logging requested by TF_LOG
|
|
|
|
logging.SetOutput()
|
|
|
|
} else {
|
|
|
|
// otherwise silence all logs
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:48:46 -05:00
|
|
|
func tempDir(t *testing.T) string {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-28 12:08:38 -05:00
|
|
|
dir, err := ioutil.TempDir(testingDir, "tf")
|
2014-09-24 17:48:46 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2014-06-18 23:36:44 -05:00
|
|
|
func testFixturePath(name string) string {
|
2014-07-11 22:38:03 -05:00
|
|
|
return filepath.Join(fixtureDir, name)
|
2014-06-18 23:36:44 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:05:58 -05:00
|
|
|
func metaOverridesForProvider(p terraform.ResourceProvider) *testingOverrides {
|
|
|
|
return &testingOverrides{
|
2017-04-21 19:40:46 -05:00
|
|
|
ProviderResolver: terraform.ResourceProviderResolverFixed(
|
|
|
|
map[string]terraform.ResourceProviderFactory{
|
|
|
|
"test": func() (terraform.ResourceProvider, error) {
|
|
|
|
return p, nil
|
|
|
|
},
|
2014-06-18 23:36:44 -05:00
|
|
|
},
|
2017-04-21 19:40:46 -05:00
|
|
|
),
|
2014-06-18 23:36:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:05:58 -05:00
|
|
|
func metaOverridesForProviderAndProvisioner(p terraform.ResourceProvider, pr terraform.ResourceProvisioner) *testingOverrides {
|
|
|
|
return &testingOverrides{
|
2017-04-21 19:40:46 -05:00
|
|
|
ProviderResolver: terraform.ResourceProviderResolverFixed(
|
|
|
|
map[string]terraform.ResourceProviderFactory{
|
|
|
|
"test": func() (terraform.ResourceProvider, error) {
|
|
|
|
return p, nil
|
|
|
|
},
|
2015-05-08 02:01:21 -05:00
|
|
|
},
|
2017-04-21 19:40:46 -05:00
|
|
|
),
|
2015-05-08 02:01:21 -05:00
|
|
|
Provisioners: map[string]terraform.ResourceProvisionerFactory{
|
|
|
|
"shell": func() (terraform.ResourceProvisioner, error) {
|
|
|
|
return pr, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:48:46 -05:00
|
|
|
func testModule(t *testing.T, name string) *module.Tree {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2014-09-24 17:48:46 -05:00
|
|
|
mod, err := module.NewTreeModule("", filepath.Join(fixtureDir, name))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-07-05 14:28:29 -05:00
|
|
|
s := module.NewStorage(tempDir(t), nil)
|
2017-10-27 14:16:58 -05:00
|
|
|
s.Mode = module.GetModeGet
|
2017-10-27 11:58:24 -05:00
|
|
|
if err := mod.Load(s); err != nil {
|
2014-09-24 17:48:46 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2016-10-28 19:51:05 -05:00
|
|
|
// testPlan returns a non-nil noop plan.
|
|
|
|
func testPlan(t *testing.T) *terraform.Plan {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2016-10-28 19:51:05 -05:00
|
|
|
state := terraform.NewState()
|
|
|
|
state.RootModule().Outputs["foo"] = &terraform.OutputState{
|
|
|
|
Type: "string",
|
|
|
|
Value: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
return &terraform.Plan{
|
|
|
|
Module: testModule(t, "apply"),
|
|
|
|
State: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 16:43:23 -05:00
|
|
|
func testPlanFile(t *testing.T, plan *terraform.Plan) string {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2014-06-27 16:43:23 -05:00
|
|
|
path := testTempFile(t)
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := terraform.WritePlan(plan, f); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2014-07-01 11:12:05 -05:00
|
|
|
func testReadPlan(t *testing.T, path string) *terraform.Plan {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2014-07-01 11:12:05 -05:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
p, err := terraform.ReadPlan(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2014-09-17 13:15:07 -05:00
|
|
|
// testState returns a test State structure that we use for a lot of tests.
|
|
|
|
func testState() *terraform.State {
|
2016-08-10 14:47:25 -05:00
|
|
|
state := &terraform.State{
|
2014-09-17 13:15:07 -05:00
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
&terraform.ModuleState{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Resources: map[string]*terraform.ResourceState{
|
|
|
|
"test_instance.foo": &terraform.ResourceState{
|
|
|
|
Type: "test_instance",
|
|
|
|
Primary: &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-06-22 09:06:41 -05:00
|
|
|
Outputs: map[string]*terraform.OutputState{},
|
2014-09-17 13:15:07 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-08-10 14:47:25 -05:00
|
|
|
state.Init()
|
2017-07-05 16:47:05 -05:00
|
|
|
return state
|
2014-09-17 13:15:07 -05:00
|
|
|
}
|
|
|
|
|
2014-06-27 16:43:23 -05:00
|
|
|
func testStateFile(t *testing.T, s *terraform.State) string {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2014-06-27 16:43:23 -05:00
|
|
|
path := testTempFile(t)
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := terraform.WriteState(s, f); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
// testStateFileDefault writes the state out to the default statefile
|
|
|
|
// in the cwd. Use `testCwd` to change into a temp cwd.
|
|
|
|
func testStateFileDefault(t *testing.T, s *terraform.State) string {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
f, err := os.Create(DefaultStateFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := terraform.WriteState(s, f); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefaultStateFilename
|
|
|
|
}
|
|
|
|
|
2015-03-05 16:55:15 -06:00
|
|
|
// testStateFileRemote writes the state out to the remote statefile
|
|
|
|
// in the cwd. Use `testCwd` to change into a temp cwd.
|
|
|
|
func testStateFileRemote(t *testing.T, s *terraform.State) string {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2015-03-05 16:55:15 -06:00
|
|
|
path := filepath.Join(DefaultDataDir, DefaultStateFilename)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := terraform.WriteState(s, f); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// testStateRead reads the state from a file
|
|
|
|
func testStateRead(t *testing.T, path string) *terraform.State {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-07-05 16:59:42 -05:00
|
|
|
defer f.Close()
|
2015-02-26 12:29:23 -06:00
|
|
|
|
|
|
|
newState, err := terraform.ReadState(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
return newState
|
|
|
|
}
|
|
|
|
|
|
|
|
// testStateOutput tests that the state at the given path contains
|
|
|
|
// the expected state string.
|
|
|
|
func testStateOutput(t *testing.T, path string, expected string) {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
newState := testStateRead(t, path)
|
2015-02-26 12:29:23 -06:00
|
|
|
actual := strings.TrimSpace(newState.String())
|
|
|
|
expected = strings.TrimSpace(expected)
|
|
|
|
if actual != expected {
|
2016-03-08 14:37:34 -06:00
|
|
|
t.Fatalf("expected:\n%s\nactual:\n%s", expected, actual)
|
2015-02-26 12:29:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 23:36:44 -05:00
|
|
|
func testProvider() *terraform.MockResourceProvider {
|
|
|
|
p := new(terraform.MockResourceProvider)
|
2014-09-17 18:33:24 -05:00
|
|
|
p.DiffReturn = &terraform.InstanceDiff{}
|
2014-06-20 14:51:42 -05:00
|
|
|
p.RefreshFn = func(
|
2014-09-17 13:15:07 -05:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState) (*terraform.InstanceState, error) {
|
2014-06-20 14:51:42 -05:00
|
|
|
return s, nil
|
|
|
|
}
|
2014-06-18 23:36:44 -05:00
|
|
|
p.ResourcesReturn = []terraform.ResourceType{
|
|
|
|
terraform.ResourceType{
|
|
|
|
Name: "test_instance",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
2014-06-27 16:43:23 -05:00
|
|
|
|
|
|
|
func testTempFile(t *testing.T) string {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2016-05-10 19:03:58 -05:00
|
|
|
return filepath.Join(testTempDir(t), "state.tfstate")
|
2014-06-27 16:43:23 -05:00
|
|
|
}
|
2014-08-05 11:32:01 -05:00
|
|
|
|
|
|
|
func testTempDir(t *testing.T) string {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-28 12:08:38 -05:00
|
|
|
d, err := ioutil.TempDir(testingDir, "tf")
|
2014-08-05 11:32:01 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
2014-10-08 14:08:35 -05:00
|
|
|
|
2016-08-24 00:11:21 -05:00
|
|
|
// testRename renames the path to new and returns a function to defer to
|
|
|
|
// revert the rename.
|
|
|
|
func testRename(t *testing.T, base, path, new string) func() {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2016-08-24 00:11:21 -05:00
|
|
|
if base != "" {
|
|
|
|
path = filepath.Join(base, path)
|
|
|
|
new = filepath.Join(base, new)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(path, new); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Just re-rename and ignore the return value
|
|
|
|
testRename(t, "", new, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testChdir changes the directory and returns a function to defer to
|
|
|
|
// revert the old cwd.
|
|
|
|
func testChdir(t *testing.T, new string) func() {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2016-08-24 00:11:21 -05:00
|
|
|
old, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Chdir(new); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Re-run the function ignoring the defer result
|
|
|
|
testChdir(t, old)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:29:23 -06:00
|
|
|
// testCwd is used to change the current working directory
|
2014-10-08 14:08:35 -05:00
|
|
|
// into a test directory that should be remoted after
|
|
|
|
func testCwd(t *testing.T) (string, string) {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-28 12:08:38 -05:00
|
|
|
tmp, err := ioutil.TempDir(testingDir, "tf")
|
2014-10-08 14:08:35 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 12:49:31 -06:00
|
|
|
|
2014-10-08 14:08:35 -05:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 12:49:31 -06:00
|
|
|
|
2014-10-09 16:45:08 -05:00
|
|
|
if err := os.Chdir(tmp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 12:49:31 -06:00
|
|
|
|
2014-10-08 14:08:35 -05:00
|
|
|
return tmp, cwd
|
|
|
|
}
|
|
|
|
|
2014-10-09 16:45:08 -05:00
|
|
|
// testFixCwd is used to as a defer to testDir
|
|
|
|
func testFixCwd(t *testing.T, tmp, cwd string) {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2014-10-09 16:45:08 -05:00
|
|
|
if err := os.Chdir(cwd); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 12:49:31 -06:00
|
|
|
|
2014-10-09 16:45:08 -05:00
|
|
|
if err := os.RemoveAll(tmp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-10-08 14:08:35 -05:00
|
|
|
}
|
2016-11-30 13:36:54 -06:00
|
|
|
|
|
|
|
// testStdinPipe changes os.Stdin to be a pipe that sends the data from
|
|
|
|
// the reader before closing the pipe.
|
|
|
|
//
|
|
|
|
// The returned function should be deferred to properly clean up and restore
|
|
|
|
// the original stdin.
|
|
|
|
func testStdinPipe(t *testing.T, src io.Reader) func() {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2016-11-30 13:36:54 -06:00
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify stdin to point to our new pipe
|
|
|
|
old := os.Stdin
|
|
|
|
os.Stdin = r
|
|
|
|
|
|
|
|
// Copy the data from the reader to the pipe
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
io.Copy(w, src)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Close our read end
|
|
|
|
r.Close()
|
|
|
|
|
|
|
|
// Reset stdin
|
|
|
|
os.Stdin = old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify os.Stdout to write to the given buffer. Note that this is generally
|
|
|
|
// not useful since the commands are configured to write to a cli.Ui, not
|
|
|
|
// Stdout directly. Commands like `console` though use the raw stdout.
|
|
|
|
func testStdoutCapture(t *testing.T, dst io.Writer) func() {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2016-11-30 13:36:54 -06:00
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify stdout
|
|
|
|
old := os.Stdout
|
|
|
|
os.Stdout = w
|
|
|
|
|
|
|
|
// Copy
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
defer r.Close()
|
|
|
|
io.Copy(dst, r)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Close the writer end of the pipe
|
|
|
|
w.Sync()
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
// Reset stdout
|
|
|
|
os.Stdout = old
|
|
|
|
|
|
|
|
// Wait for the data copy to complete to avoid a race reading data
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 22:50:45 -06:00
|
|
|
|
|
|
|
// testInteractiveInput configures tests so that the answers given are sent
|
|
|
|
// in order to interactive prompts. The returned function must be called
|
|
|
|
// in a defer to clean up.
|
|
|
|
func testInteractiveInput(t *testing.T, answers []string) func() {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Disable test mode so input is called
|
|
|
|
test = false
|
|
|
|
|
|
|
|
// Setup reader/writers
|
|
|
|
testInputResponse = answers
|
|
|
|
defaultInputReader = bytes.NewBufferString("")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
// Return the cleanup
|
|
|
|
return func() {
|
|
|
|
test = true
|
|
|
|
testInputResponse = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 12:59:17 -06:00
|
|
|
// testInputMap configures tests so that the given answers are returned
|
|
|
|
// for calls to Input when the right question is asked. The key is the
|
|
|
|
// question "Id" that is used.
|
|
|
|
func testInputMap(t *testing.T, answers map[string]string) func() {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2017-03-01 12:59:17 -06:00
|
|
|
// Disable test mode so input is called
|
|
|
|
test = false
|
|
|
|
|
|
|
|
// Setup reader/writers
|
|
|
|
defaultInputReader = bytes.NewBufferString("")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
// Setup answers
|
|
|
|
testInputResponse = nil
|
|
|
|
testInputResponseMap = answers
|
|
|
|
|
|
|
|
// Return the cleanup
|
|
|
|
return func() {
|
|
|
|
test = true
|
|
|
|
testInputResponseMap = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// testBackendState is used to make a test HTTP server to test a configured
|
|
|
|
// backend. This returns the complete state that can be saved. Use
|
|
|
|
// `testStateFileRemote` to write the returned state.
|
|
|
|
func testBackendState(t *testing.T, s *terraform.State, c int) (*terraform.State, *httptest.Server) {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
var b64md5 string
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
cb := func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method == "PUT" {
|
|
|
|
resp.WriteHeader(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Header().Set("Content-MD5", b64md5)
|
|
|
|
resp.Write(buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a state was given, make sure we calculate the proper b64md5
|
|
|
|
if s != nil {
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
if err := enc.Encode(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
md5 := md5.Sum(buf.Bytes())
|
|
|
|
b64md5 = base64.StdEncoding.EncodeToString(md5[:16])
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(cb))
|
|
|
|
|
|
|
|
state := terraform.NewState()
|
|
|
|
state.Backend = &terraform.BackendState{
|
2018-03-27 17:31:05 -05:00
|
|
|
Type: "http",
|
|
|
|
ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)),
|
|
|
|
Hash: 2529831861221416334,
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return state, srv
|
|
|
|
}
|
|
|
|
|
|
|
|
// testRemoteState is used to make a test HTTP server to return a given
|
|
|
|
// state file that can be used for testing legacy remote state.
|
|
|
|
func testRemoteState(t *testing.T, s *terraform.State, c int) (*terraform.RemoteState, *httptest.Server) {
|
2017-08-25 18:23:47 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
var b64md5 string
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
cb := func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method == "PUT" {
|
|
|
|
resp.WriteHeader(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Header().Set("Content-MD5", b64md5)
|
|
|
|
resp.Write(buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(cb))
|
|
|
|
remote := &terraform.RemoteState{
|
|
|
|
Type: "http",
|
|
|
|
Config: map[string]string{"address": srv.URL},
|
|
|
|
}
|
|
|
|
|
|
|
|
if s != nil {
|
|
|
|
// Set the remote data
|
|
|
|
s.Remote = remote
|
|
|
|
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
if err := enc.Encode(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
md5 := md5.Sum(buf.Bytes())
|
|
|
|
b64md5 = base64.StdEncoding.EncodeToString(md5[:16])
|
|
|
|
}
|
|
|
|
|
|
|
|
return remote, srv
|
|
|
|
}
|
2017-02-03 14:32:40 -06:00
|
|
|
|
|
|
|
// testlockState calls a separate process to the lock the state file at path.
|
|
|
|
// deferFunc should be called in the caller to properly unlock the file.
|
2017-02-06 12:26:03 -06:00
|
|
|
// Since many tests change the working durectory, the sourcedir argument must be
|
|
|
|
// supplied to locate the statelocker.go source.
|
|
|
|
func testLockState(sourceDir, path string) (func(), error) {
|
2017-02-03 14:56:19 -06:00
|
|
|
// build and run the binary ourselves so we can quickly terminate it for cleanup
|
2018-03-28 12:08:38 -05:00
|
|
|
buildDir, err := ioutil.TempDir(testingDir, "locker")
|
2017-02-03 14:56:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cleanFunc := func() {
|
|
|
|
os.RemoveAll(buildDir)
|
|
|
|
}
|
|
|
|
|
2017-02-06 12:26:03 -06:00
|
|
|
source := filepath.Join(sourceDir, "statelocker.go")
|
2017-02-03 14:56:19 -06:00
|
|
|
lockBin := filepath.Join(buildDir, "statelocker")
|
2017-02-06 12:26:03 -06:00
|
|
|
|
|
|
|
out, err := exec.Command("go", "build", "-o", lockBin, source).CombinedOutput()
|
2017-02-03 14:56:19 -06:00
|
|
|
if err != nil {
|
|
|
|
cleanFunc()
|
|
|
|
return nil, fmt.Errorf("%s %s", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
locker := exec.Command(lockBin, path)
|
2017-02-03 14:32:40 -06:00
|
|
|
pr, pw, err := os.Pipe()
|
|
|
|
if err != nil {
|
2017-02-03 14:56:19 -06:00
|
|
|
cleanFunc()
|
2017-02-03 14:32:40 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer pr.Close()
|
|
|
|
defer pw.Close()
|
|
|
|
locker.Stderr = pw
|
|
|
|
locker.Stdout = pw
|
|
|
|
|
|
|
|
if err := locker.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deferFunc := func() {
|
2017-02-03 14:56:19 -06:00
|
|
|
cleanFunc()
|
2017-02-03 14:32:40 -06:00
|
|
|
locker.Process.Signal(syscall.SIGTERM)
|
|
|
|
locker.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the process to lock
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
n, err := pr.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return deferFunc, fmt.Errorf("read from statelocker returned: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 10:53:19 -06:00
|
|
|
output := string(buf[:n])
|
|
|
|
if !strings.HasPrefix(output, "LOCKID") {
|
2017-02-03 14:56:19 -06:00
|
|
|
return deferFunc, fmt.Errorf("statelocker wrote: %s", string(buf[:n]))
|
2017-02-03 14:32:40 -06:00
|
|
|
}
|
|
|
|
return deferFunc, nil
|
|
|
|
}
|