2014-09-27 01:03:39 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-05-25 02:35:46 +02:00
|
|
|
"fmt"
|
2017-02-16 00:44:53 +01:00
|
|
|
"io/ioutil"
|
2014-09-27 01:30:49 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-13 03:32:42 +02:00
|
|
|
"reflect"
|
2017-05-25 02:35:46 +02:00
|
|
|
"runtime"
|
2017-06-13 03:32:42 +02:00
|
|
|
"sort"
|
2017-01-19 05:50:45 +01:00
|
|
|
"strings"
|
2014-09-27 01:03:39 +02:00
|
|
|
"testing"
|
|
|
|
|
2018-03-28 00:31:05 +02:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2017-12-14 21:46:43 +01:00
|
|
|
"github.com/hashicorp/terraform/backend/local"
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2017-06-09 17:20:27 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
2017-12-18 16:37:15 +01:00
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-09-27 01:03:39 +02:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
func TestInit_empty(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-09-27 01:03:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
2014-09-27 01:03:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
func TestInit_multipleArgs(t *testing.T) {
|
2014-09-27 01:03:39 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2014-09-27 01:03:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
args := []string{
|
|
|
|
"bad",
|
|
|
|
"bad",
|
|
|
|
}
|
2014-09-27 01:03:39 +02:00
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 21:19:39 +01:00
|
|
|
|
2017-07-29 00:23:29 +02:00
|
|
|
func TestInit_fromModule_explicitDest(t *testing.T) {
|
|
|
|
dir := tempDir(t)
|
2018-10-15 01:35:59 +02:00
|
|
|
err := os.Mkdir(dir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-29 00:23:29 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-from-module=" + testFixturePath("init"),
|
|
|
|
dir,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInit_fromModule_cwdDest(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, os.ModePerm)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-from-module=" + testFixturePath("init"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(td, "hello.tf")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/hashicorp/terraform/issues/518
|
|
|
|
func TestInit_fromModule_dstInSrc(t *testing.T) {
|
|
|
|
dir := tempDir(t)
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to the temporary directory
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(dir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
2018-10-15 01:35:59 +02:00
|
|
|
if err := os.Mkdir("foo", os.ModePerm); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-07-29 00:23:29 +02:00
|
|
|
if _, err := os.Create("issue518.tf"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-from-module=.",
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
func TestInit_get(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check output
|
|
|
|
output := ui.OutputWriter.String()
|
2018-10-15 01:35:59 +02:00
|
|
|
if !strings.Contains(output, "foo in foo") {
|
|
|
|
t.Fatalf("doesn't look like we installed module 'foo': %s", output)
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 23:14:40 +02:00
|
|
|
func TestInit_getUpgradeModules(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
// copy.CopyDir(testFixturePath("init-get"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-get=true",
|
|
|
|
"-get-plugins=false",
|
|
|
|
"-upgrade",
|
|
|
|
testFixturePath("init-get"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("command did not complete successfully:\n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check output
|
|
|
|
output := ui.OutputWriter.String()
|
2018-10-15 01:35:59 +02:00
|
|
|
if !strings.Contains(output, "Upgrading modules...") {
|
2017-06-12 23:14:40 +02:00
|
|
|
t.Fatalf("doesn't look like get upgrade: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
func TestInit_backend(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(DefaultDataDir, DefaultStateFilename)); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:44:53 +01:00
|
|
|
func TestInit_backendUnset(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
{
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-02-16 00:44:53 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(DefaultDataDir, DefaultStateFilename)); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Unset
|
|
|
|
if err := ioutil.WriteFile("main.tf", []byte(""), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-02-16 00:44:53 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:05:51 +01:00
|
|
|
args := []string{"-force-copy"}
|
2017-02-16 00:44:53 +01:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
s := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2017-02-16 00:44:53 +01:00
|
|
|
if !s.Backend.Empty() {
|
|
|
|
t.Fatal("should not have backend config")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
func TestInit_backendConfigFile(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend-config-file"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-backend-config", "input.config"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2017-03-16 19:47:59 +01:00
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read our saved backend config and verify we have our settings
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2018-10-15 01:35:59 +02:00
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want {
|
2018-03-28 00:31:05 +02:00
|
|
|
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
|
2017-03-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInit_backendConfigFileChange(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend-config-file-change"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// Ask input
|
|
|
|
defer testInputMap(t, map[string]string{
|
|
|
|
"backend-migrate-to-new": "no",
|
|
|
|
})()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-03-16 19:47:59 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-backend-config", "input.config"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2017-03-17 18:22:48 +01:00
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read our saved backend config and verify we have our settings
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2018-10-15 01:35:59 +02:00
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want {
|
2018-03-28 00:31:05 +02:00
|
|
|
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
|
2017-03-17 18:22:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInit_backendConfigKV(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend-config-kv"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-03-17 18:22:48 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-backend-config", "path=hello"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2017-01-19 05:50:45 +01:00
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read our saved backend config and verify we have our settings
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2018-10-15 01:35:59 +02:00
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want {
|
2018-03-28 00:31:05 +02:00
|
|
|
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 22:13:11 +02:00
|
|
|
func TestInit_targetSubdir(t *testing.T) {
|
2017-01-19 05:50:45 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-06-02 22:13:11 +02:00
|
|
|
// copy the source into a subdir
|
|
|
|
copy.CopyDir(testFixturePath("init-backend"), filepath.Join(td, "source"))
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-06-02 22:13:11 +02:00
|
|
|
"source",
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-06-14 21:22:30 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(td, DefaultDataDir, DefaultStateFilename)); err != nil {
|
2017-06-02 22:13:11 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a data directory should not have been added to out working dir
|
2017-06-14 21:22:30 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(td, "source", DefaultDataDir)); !os.IsNotExist(err) {
|
2017-01-19 05:50:45 +01:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 18:50:20 +02:00
|
|
|
func TestInit_backendReinitWithExtra(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend-empty"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
m := testMetaBackend(t, nil)
|
|
|
|
opts := &BackendOpts{
|
2018-03-28 00:31:05 +02:00
|
|
|
ConfigOverride: configs.SynthBody("synth", map[string]cty.Value{
|
|
|
|
"path": cty.StringVal("hello"),
|
|
|
|
}),
|
|
|
|
Init: true,
|
2017-03-29 18:50:20 +02:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:31:05 +02:00
|
|
|
_, cHash, err := m.backendConfig(opts)
|
2017-03-29 18:50:20 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-03-29 18:50:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-backend-config", "path=hello"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read our saved backend config and verify we have our settings
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2018-10-15 01:35:59 +02:00
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want {
|
2018-03-28 00:31:05 +02:00
|
|
|
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
|
2017-03-29 18:50:20 +02:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:31:05 +02:00
|
|
|
if state.Backend.Hash != cHash {
|
2017-03-29 18:50:20 +02:00
|
|
|
t.Fatal("mismatched state and config backend hashes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// init again and make sure nothing changes
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
state = testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2018-10-15 01:35:59 +02:00
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want {
|
2018-03-28 00:31:05 +02:00
|
|
|
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
|
2017-03-29 18:50:20 +02:00
|
|
|
}
|
2018-03-28 00:31:05 +02:00
|
|
|
if state.Backend.Hash != cHash {
|
2017-03-29 18:50:20 +02:00
|
|
|
t.Fatal("mismatched state and config backend hashes")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 21:51:24 +02:00
|
|
|
// move option from config to -backend-config args
|
|
|
|
func TestInit_backendReinitConfigToExtra(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-03-29 21:51:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run([]string{"-input=false"}); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read our saved backend config and verify we have our settings
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2018-10-15 01:35:59 +02:00
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"foo","workspace_dir":null}`; got != want {
|
2018-03-28 00:31:05 +02:00
|
|
|
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
|
2017-03-29 21:51:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
backendHash := state.Backend.Hash
|
|
|
|
|
|
|
|
// init again but remove the path option from the config
|
|
|
|
cfg := "terraform {\n backend \"local\" {}\n}\n"
|
|
|
|
if err := ioutil.WriteFile("main.tf", []byte(cfg), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-input=false", "-backend-config=path=foo"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
state = testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
2017-03-29 21:51:24 +02:00
|
|
|
|
|
|
|
if state.Backend.Hash == backendHash {
|
|
|
|
t.Fatal("state.Backend.Hash was not updated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 22:45:25 +02:00
|
|
|
// make sure inputFalse stops execution on migrate
|
|
|
|
func TestInit_inputFalse(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-backend"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2017-03-29 22:45:25 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-input=false", "-backend-config=path=foo"}
|
|
|
|
if code := c.Run([]string{"-input=false"}); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-12-18 16:37:15 +01:00
|
|
|
// write different states for foo and bar
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Lineage = "foo"
|
|
|
|
if err := (&state.LocalState{Path: "foo"}).WriteState(s); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s.Lineage = "bar"
|
|
|
|
if err := (&state.LocalState{Path: "bar"}).WriteState(s); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c = &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-29 22:45:25 +02:00
|
|
|
args = []string{"-input=false", "-backend-config=path=bar"}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("init should have failed", ui.OutputWriter)
|
|
|
|
}
|
2017-12-18 16:37:15 +01:00
|
|
|
|
|
|
|
errMsg := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errMsg, "input disabled") {
|
|
|
|
t.Fatal("expected input disabled error, got", errMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c = &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// A missing input=false should abort rather than loop infinitely
|
|
|
|
args = []string{"-backend-config=path=bar"}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("init should have failed", ui.OutputWriter)
|
|
|
|
}
|
2017-03-29 22:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 20:03:57 +02:00
|
|
|
func TestInit_getProvider(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-providers"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-12-14 21:46:43 +01:00
|
|
|
overrides := metaOverridesForProvider(testProvider())
|
2017-06-13 03:22:47 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
2017-12-14 21:46:43 +01:00
|
|
|
testingOverrides: overrides,
|
2017-06-13 03:22:47 +02:00
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
installer := &mockProviderInstaller{
|
2017-05-04 20:03:57 +02:00
|
|
|
Providers: map[string][]string{
|
|
|
|
// looking for an exact version
|
|
|
|
"exact": []string{"1.2.3"},
|
|
|
|
// config requires >= 2.3.3
|
|
|
|
"greater_than": []string{"2.3.4", "2.3.3", "2.3.0"},
|
|
|
|
// config specifies
|
|
|
|
"between": []string{"3.4.5", "2.3.4", "1.2.3"},
|
|
|
|
},
|
2017-06-13 03:22:47 +02:00
|
|
|
|
|
|
|
Dir: m.pluginDir(),
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2017-06-13 03:22:47 +02:00
|
|
|
Meta: m,
|
|
|
|
providerInstaller: installer,
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
2017-06-21 19:32:13 +02:00
|
|
|
args := []string{
|
|
|
|
"-backend=false", // should be possible to install plugins without backend init
|
|
|
|
}
|
2017-05-04 20:03:57 +02:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-06-20 20:25:41 +02:00
|
|
|
if !installer.PurgeUnusedCalled {
|
|
|
|
t.Errorf("init didn't purge providers, but should have")
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:03:57 +02:00
|
|
|
// check that we got the providers for our config
|
2017-06-13 03:22:47 +02:00
|
|
|
exactPath := filepath.Join(c.pluginDir(), installer.FileName("exact", "1.2.3"))
|
2017-05-04 20:03:57 +02:00
|
|
|
if _, err := os.Stat(exactPath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'exact' not downloaded")
|
|
|
|
}
|
2017-06-13 03:22:47 +02:00
|
|
|
greaterThanPath := filepath.Join(c.pluginDir(), installer.FileName("greater_than", "2.3.4"))
|
2017-05-04 20:03:57 +02:00
|
|
|
if _, err := os.Stat(greaterThanPath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'greater_than' not downloaded")
|
|
|
|
}
|
2017-06-13 03:22:47 +02:00
|
|
|
betweenPath := filepath.Join(c.pluginDir(), installer.FileName("between", "2.3.4"))
|
2017-05-04 20:03:57 +02:00
|
|
|
if _, err := os.Stat(betweenPath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'between' not downloaded")
|
|
|
|
}
|
2017-12-14 21:46:43 +01:00
|
|
|
|
|
|
|
t.Run("future-state", func(t *testing.T) {
|
|
|
|
// getting providers should fail if a state from a newer version of
|
|
|
|
// terraform exists, since InitCommand.getProviders needs to inspect that
|
|
|
|
// state.
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.TFVersion = "100.1.0"
|
|
|
|
local := &state.LocalState{
|
|
|
|
Path: local.DefaultStateFilename,
|
|
|
|
}
|
|
|
|
if err := local.WriteState(s); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m.Ui = ui
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
providerInstaller: installer,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(nil); code == 0 {
|
|
|
|
t.Fatal("expected error, got:", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
errMsg := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errMsg, "future Terraform version") {
|
|
|
|
t.Fatal("unexpected error:", errMsg)
|
|
|
|
}
|
|
|
|
})
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
2017-06-15 16:12:00 +02:00
|
|
|
// make sure we can locate providers in various paths
|
|
|
|
func TestInit_findVendoredProviders(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
|
|
|
|
configDirName := "init-get-providers"
|
|
|
|
copy.CopyDir(testFixturePath(configDirName), filepath.Join(td, configDirName))
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
providerInstaller: &mockProviderInstaller{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// make our plugin paths
|
|
|
|
if err := os.MkdirAll(c.pluginDir(), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-06-16 20:09:47 +02:00
|
|
|
if err := os.MkdirAll(DefaultPluginVendorDir, 0755); err != nil {
|
2017-06-15 16:12:00 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add some dummy providers
|
|
|
|
// the auto plugin directory
|
|
|
|
exactPath := filepath.Join(c.pluginDir(), "terraform-provider-exact_v1.2.3_x4")
|
|
|
|
if err := ioutil.WriteFile(exactPath, []byte("test bin"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// the vendor path
|
2017-06-16 20:09:47 +02:00
|
|
|
greaterThanPath := filepath.Join(DefaultPluginVendorDir, "terraform-provider-greater_than_v2.3.4_x4")
|
2017-06-15 16:12:00 +02:00
|
|
|
if err := ioutil.WriteFile(greaterThanPath, []byte("test bin"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-06-15 21:23:16 +02:00
|
|
|
// Check the current directory too
|
2017-06-15 16:12:00 +02:00
|
|
|
betweenPath := filepath.Join(".", "terraform-provider-between_v2.3.4_x4")
|
|
|
|
if err := ioutil.WriteFile(betweenPath, []byte("test bin"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{configDirName}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:13:54 +02:00
|
|
|
// make sure we can locate providers defined in the legacy rc file
|
|
|
|
func TestInit_rcProviders(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
|
|
|
|
configDirName := "init-legacy-rc"
|
|
|
|
copy.CopyDir(testFixturePath(configDirName), filepath.Join(td, configDirName))
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
pluginDir := filepath.Join(td, "custom")
|
|
|
|
pluginPath := filepath.Join(pluginDir, "terraform-provider-legacy")
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
Ui: ui,
|
|
|
|
PluginOverrides: &PluginOverrides{
|
|
|
|
Providers: map[string]string{
|
|
|
|
"legacy": pluginPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
providerInstaller: &mockProviderInstaller{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// make our plugin paths
|
|
|
|
if err := os.MkdirAll(pluginDir, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(pluginPath, []byte("test bin"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{configDirName}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 03:32:42 +02:00
|
|
|
func TestInit_getUpgradePlugins(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-providers"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
installer := &mockProviderInstaller{
|
|
|
|
Providers: map[string][]string{
|
|
|
|
// looking for an exact version
|
|
|
|
"exact": []string{"1.2.3"},
|
|
|
|
// config requires >= 2.3.3
|
|
|
|
"greater_than": []string{"2.3.4", "2.3.3", "2.3.0"},
|
|
|
|
// config specifies
|
|
|
|
"between": []string{"3.4.5", "2.3.4", "1.2.3"},
|
|
|
|
},
|
|
|
|
|
|
|
|
Dir: m.pluginDir(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.MkdirAll(m.pluginDir(), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
exactUnwanted := filepath.Join(m.pluginDir(), installer.FileName("exact", "0.0.1"))
|
|
|
|
err = ioutil.WriteFile(exactUnwanted, []byte{}, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
greaterThanUnwanted := filepath.Join(m.pluginDir(), installer.FileName("greater_than", "2.3.3"))
|
|
|
|
err = ioutil.WriteFile(greaterThanUnwanted, []byte{}, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
betweenOverride := installer.FileName("between", "2.3.4") // intentionally directly in cwd, and should override auto-install
|
|
|
|
err = ioutil.WriteFile(betweenOverride, []byte{}, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
providerInstaller: installer,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-upgrade=true",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("command did not complete successfully:\n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(m.pluginDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !installer.PurgeUnusedCalled {
|
|
|
|
t.Errorf("init -upgrade didn't purge providers, but should have")
|
|
|
|
}
|
|
|
|
|
|
|
|
gotFilenames := make([]string, len(files))
|
|
|
|
for i, info := range files {
|
|
|
|
gotFilenames[i] = info.Name()
|
|
|
|
}
|
|
|
|
sort.Strings(gotFilenames)
|
|
|
|
|
|
|
|
wantFilenames := []string{
|
|
|
|
"lock.json",
|
|
|
|
|
|
|
|
// no "between" because the file in cwd overrides it
|
|
|
|
|
|
|
|
// The mock PurgeUnused doesn't actually purge anything, so the dir
|
|
|
|
// includes both our old and new versions.
|
|
|
|
"terraform-provider-exact_v0.0.1_x4",
|
|
|
|
"terraform-provider-exact_v1.2.3_x4",
|
|
|
|
"terraform-provider-greater_than_v2.3.3_x4",
|
|
|
|
"terraform-provider-greater_than_v2.3.4_x4",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(gotFilenames, wantFilenames) {
|
|
|
|
t.Errorf("wrong directory contents after upgrade\ngot: %#v\nwant: %#v", gotFilenames, wantFilenames)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:03:57 +02:00
|
|
|
func TestInit_getProviderMissing(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-providers"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-06-13 03:22:47 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
installer := &mockProviderInstaller{
|
2017-05-04 20:03:57 +02:00
|
|
|
Providers: map[string][]string{
|
|
|
|
// looking for exact version 1.2.3
|
|
|
|
"exact": []string{"1.2.4"},
|
|
|
|
// config requires >= 2.3.3
|
|
|
|
"greater_than": []string{"2.3.4", "2.3.3", "2.3.0"},
|
|
|
|
// config specifies
|
|
|
|
"between": []string{"3.4.5", "2.3.4", "1.2.3"},
|
|
|
|
},
|
2017-06-13 03:22:47 +02:00
|
|
|
|
|
|
|
Dir: m.pluginDir(),
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2017-06-13 03:22:47 +02:00
|
|
|
Meta: m,
|
|
|
|
providerInstaller: installer,
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expceted error, got output: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "no suitable version for provider") {
|
|
|
|
t.Fatalf("unexpected error output: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 17:20:27 +02:00
|
|
|
func TestInit_getProviderHaveLegacyVersion(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-providers-lock"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile("terraform-provider-test", []byte("provider bin"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// provider test has a version constraint in the config, which should
|
|
|
|
// trigger the getProvider error below.
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2017-06-13 03:22:47 +02:00
|
|
|
providerInstaller: callbackPluginInstaller(func(provider string, req discovery.Constraints) (discovery.PluginMeta, error) {
|
|
|
|
return discovery.PluginMeta{}, fmt.Errorf("EXPECTED PROVIDER ERROR %s", provider)
|
|
|
|
}),
|
2017-06-09 17:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expceted error, got output: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "EXPECTED PROVIDER ERROR test") {
|
|
|
|
t.Fatalf("unexpected error output: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:25:16 +02:00
|
|
|
func TestInit_getProviderCheckRequiredVersion(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-check-required-version"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-15 01:35:59 +02:00
|
|
|
ui := cli.NewMockUi()
|
2017-08-28 20:25:16 +02:00
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
2018-10-15 01:35:59 +02:00
|
|
|
t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
|
2017-08-28 20:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:35:46 +02:00
|
|
|
func TestInit_providerLockFile(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-provider-lock-file"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-06-13 03:22:47 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
installer := &mockProviderInstaller{
|
2017-05-25 02:35:46 +02:00
|
|
|
Providers: map[string][]string{
|
|
|
|
"test": []string{"1.2.3"},
|
|
|
|
},
|
2017-06-13 03:22:47 +02:00
|
|
|
|
|
|
|
Dir: m.pluginDir(),
|
2017-05-25 02:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2017-06-13 03:22:47 +02:00
|
|
|
Meta: m,
|
|
|
|
providerInstaller: installer,
|
2017-05-25 02:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
providersLockFile := fmt.Sprintf(
|
2017-06-09 22:28:51 +02:00
|
|
|
".terraform/plugins/%s_%s/lock.json",
|
2017-05-25 02:35:46 +02:00
|
|
|
runtime.GOOS, runtime.GOARCH,
|
|
|
|
)
|
|
|
|
buf, err := ioutil.ReadFile(providersLockFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read providers lock file %s: %s", providersLockFile, err)
|
|
|
|
}
|
|
|
|
// The hash in here is for the empty files that mockGetProvider produces
|
|
|
|
wantLockFile := strings.TrimSpace(`
|
|
|
|
{
|
|
|
|
"test": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
if string(buf) != wantLockFile {
|
|
|
|
t.Errorf("wrong provider lock file contents\ngot: %s\nwant: %s", buf, wantLockFile)
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 21:23:16 +02:00
|
|
|
|
2017-12-21 17:21:07 +01:00
|
|
|
func TestInit_pluginDirReset(t *testing.T) {
|
2018-03-28 19:08:38 +02:00
|
|
|
td := testTempDir(t)
|
2017-12-21 17:21:07 +01:00
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
providerInstaller: &mockProviderInstaller{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// make our vendor paths
|
|
|
|
pluginPath := []string{"a", "b", "c"}
|
|
|
|
for _, p := range pluginPath {
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run once and save the -plugin-dir
|
|
|
|
args := []string{"-plugin-dir", "a"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginDirs, err := c.loadPluginPath()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pluginDirs) != 1 || pluginDirs[0] != "a" {
|
|
|
|
t.Fatalf(`expected plugin dir ["a"], got %q`, pluginDirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c = &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
providerInstaller: &mockProviderInstaller{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we remove the plugin-dir record
|
2018-01-04 22:49:45 +01:00
|
|
|
args = []string{"-plugin-dir="}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2017-12-21 17:21:07 +01:00
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginDirs, err = c.loadPluginPath()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pluginDirs) != 0 {
|
|
|
|
t.Fatalf("expected no plugin dirs got %q", pluginDirs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 21:23:16 +02:00
|
|
|
// Test user-supplied -plugin-dir
|
|
|
|
func TestInit_pluginDirProviders(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-providers"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
providerInstaller: &mockProviderInstaller{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// make our vendor paths
|
|
|
|
pluginPath := []string{"a", "b", "c"}
|
|
|
|
for _, p := range pluginPath {
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add some dummy providers in our plugin dirs
|
|
|
|
for i, name := range []string{
|
|
|
|
"terraform-provider-exact_v1.2.3_x4",
|
|
|
|
"terraform-provider-greater_than_v2.3.4_x4",
|
|
|
|
"terraform-provider-between_v2.3.4_x4",
|
|
|
|
} {
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(pluginPath[i], name), []byte("test bin"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-plugin-dir", "a",
|
|
|
|
"-plugin-dir", "b",
|
|
|
|
"-plugin-dir", "c",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test user-supplied -plugin-dir doesn't allow auto-install
|
|
|
|
func TestInit_pluginDirProvidersDoesNotGet(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-providers"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
providerInstaller: callbackPluginInstaller(func(provider string, req discovery.Constraints) (discovery.PluginMeta, error) {
|
|
|
|
t.Fatalf("plugin installer should not have been called for %q", provider)
|
|
|
|
return discovery.PluginMeta{}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// make our vendor paths
|
|
|
|
pluginPath := []string{"a", "b"}
|
|
|
|
for _, p := range pluginPath {
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add some dummy providers in our plugin dirs
|
|
|
|
for i, name := range []string{
|
|
|
|
"terraform-provider-exact_v1.2.3_x4",
|
|
|
|
"terraform-provider-greater_than_v2.3.4_x4",
|
|
|
|
} {
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(pluginPath[i], name), []byte("test bin"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-plugin-dir", "a",
|
|
|
|
"-plugin-dir", "b",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
// should have been an error
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 17:51:09 +01:00
|
|
|
|
|
|
|
// Verify that plugin-dir doesn't prevent discovery of internal providers
|
|
|
|
func TestInit_pluginWithInternal(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-internal"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-plugin-dir", "./"}
|
|
|
|
//args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("error: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|