2014-09-27 01:03:39 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2020-04-01 01:48:37 +02:00
|
|
|
"context"
|
2019-05-24 00:21:52 +02:00
|
|
|
"encoding/json"
|
2017-05-25 02:35:46 +02:00
|
|
|
"fmt"
|
2017-02-16 00:44:53 +01:00
|
|
|
"io/ioutil"
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
"log"
|
2014-09-27 01:30:49 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-01-19 05:50:45 +01:00
|
|
|
"strings"
|
2014-09-27 01:03:39 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-11-09 23:26:01 +01:00
|
|
|
"github.com/mitchellh/cli"
|
2018-03-28 00:31:05 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2018-11-09 23:26:01 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2020-03-31 23:02:40 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/getproviders"
|
|
|
|
"github.com/hashicorp/terraform/internal/providercache"
|
2018-11-09 23:26:01 +01:00
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-12-18 16:37:15 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-09-27 01:03:39 +02:00
|
|
|
)
|
|
|
|
|
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) {
|
2020-03-10 21:32:22 +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)()
|
|
|
|
|
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) {
|
2020-03-10 21:32:22 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-07-29 00:23:29 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:18:00 +01:00
|
|
|
if _, err := os.Stat(DefaultStateFilename); err == nil {
|
|
|
|
// This should never happen; it indicates a bug in another test
|
|
|
|
// is causing a terraform.tfstate to get left behind in our directory
|
|
|
|
// here, which can interfere with our init process in a way that
|
|
|
|
// isn't relevant to this test.
|
2019-01-29 23:16:41 +01:00
|
|
|
fullPath, _ := filepath.Abs(DefaultStateFilename)
|
|
|
|
t.Fatalf("some other test has left terraform.tfstate behind:\n%s", fullPath)
|
2018-11-10 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 00:23:29 +02:00
|
|
|
args := []string{
|
|
|
|
"-from-module=" + testFixturePath("init"),
|
2020-03-10 21:32:22 +01:00
|
|
|
td,
|
2017-07-29 00:23:29 +02:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-03-10 21:32:22 +01:00
|
|
|
if _, err := os.Stat(filepath.Join(td, "hello.tf")); err != nil {
|
2017-07-29 00:23:29 +02:00
|
|
|
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)
|
|
|
|
}
|
2020-03-10 21:32:22 +01:00
|
|
|
defer os.RemoveAll(dir)
|
2017-07-29 00:23:29 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
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)()
|
|
|
|
|
|
|
|
{
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
log.Printf("[TRACE] TestInit_backendUnset: beginning first init")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2017-02-16 00:44:53 +01:00
|
|
|
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())
|
|
|
|
}
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
log.Printf("[TRACE] TestInit_backendUnset: first init complete")
|
|
|
|
t.Logf("First run output:\n%s", ui.OutputWriter.String())
|
|
|
|
t.Logf("First run errors:\n%s", ui.ErrorWriter.String())
|
2017-02-16 00:44:53 +01:00
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(DefaultDataDir, DefaultStateFilename)); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
log.Printf("[TRACE] TestInit_backendUnset: beginning second init")
|
|
|
|
|
2017-02-16 00:44:53 +01:00
|
|
|
// Unset
|
|
|
|
if err := ioutil.WriteFile("main.tf", []byte(""), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-16 00:44:53 +01:00
|
|
|
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())
|
|
|
|
}
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
log.Printf("[TRACE] TestInit_backendUnset: second init complete")
|
|
|
|
t.Logf("Second run output:\n%s", ui.OutputWriter.String())
|
|
|
|
t.Logf("Second run errors:\n%s", ui.ErrorWriter.String())
|
2017-02-16 00:44:53 +01:00
|
|
|
|
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)()
|
|
|
|
|
2020-06-26 18:49:31 +02:00
|
|
|
t.Run("good-config-file", func(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
args := []string{"-backend-config", "input.config"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2020-06-26 18:49:31 +02:00
|
|
|
// Read our saved backend config and verify we have our settings
|
|
|
|
state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want {
|
|
|
|
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
|
|
|
|
}
|
|
|
|
})
|
2017-03-16 19:47:59 +01:00
|
|
|
|
2020-06-26 18:49:31 +02:00
|
|
|
// the backend config file must be a set of key-value pairs and not a full backend {} block
|
|
|
|
t.Run("invalid-config-file", func(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
args := []string{"-backend-config", "backend.config"}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("expected error, got success\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "Invalid backend configuration file") {
|
|
|
|
t.Fatalf("wrong error: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
})
|
2017-03-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-18 23:56:05 +02:00
|
|
|
func TestInit_backendConfigFilePowershellConfusion(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{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// SUBTLE: when using -flag=value with Powershell, unquoted values are
|
|
|
|
// broken into separate arguments. This results in the init command
|
|
|
|
// interpreting the flags as an empty backend-config setting (which is
|
|
|
|
// semantically valid!) followed by a custom configuration path.
|
|
|
|
//
|
|
|
|
// Adding the "=" here forces this codepath to be checked, and it should
|
|
|
|
// result in an early exit with a diagnostic that the provided
|
|
|
|
// configuration file is not a diretory.
|
|
|
|
args := []string{"-backend-config=", "./input.config"}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
|
|
|
if got, want := output, `Module directory ./input.config does not exist`; !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("wrong output\ngot:\n%s\n\nwant: message containing %q", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:21:52 +02:00
|
|
|
func TestInit_backendConfigKVReInit(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{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-backend-config", "path=test"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c = &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// a second init should require no changes, nor should it change the backend.
|
|
|
|
args = []string{"-input=false"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the backend is configured how we expect
|
|
|
|
configState := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
|
|
|
cfg := map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(configState.Backend.ConfigRaw, &cfg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cfg["path"] != "test" {
|
|
|
|
t.Fatalf(`expected backend path="test", got path="%v"`, cfg["path"])
|
|
|
|
}
|
2019-05-29 19:58:04 +02:00
|
|
|
|
|
|
|
// override the -backend-config options by settings
|
|
|
|
args = []string{"-input=false", "-backend-config", ""}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the backend is configured how we expect
|
|
|
|
configState = testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
|
|
|
cfg = map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(configState.Backend.ConfigRaw, &cfg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cfg["path"] != nil {
|
|
|
|
t.Fatalf(`expected backend path="<nil>", got path="%v"`, cfg["path"])
|
|
|
|
}
|
2019-05-24 00:21:52 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 20:51:18 +02:00
|
|
|
func TestInit_backendConfigKVReInitWithConfigDiff(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{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-input=false"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c = &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// a second init with identical config should require no changes, nor
|
|
|
|
// should it change the backend.
|
|
|
|
args = []string{"-input=false", "-backend-config", "path=foo"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the backend is configured how we expect
|
|
|
|
configState := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
|
|
|
|
cfg := map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(configState.Backend.ConfigRaw, &cfg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cfg["path"] != "foo" {
|
|
|
|
t.Fatalf(`expected backend path="foo", got path="%v"`, cfg["foo"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 14:08:28 +02:00
|
|
|
func TestInit_backendCli_no_config_block(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-backend-config", "path=test"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("got exit status %d; want 0\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
errMsg := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errMsg, "Warning: Missing backend configuration") {
|
|
|
|
t.Fatal("expected missing backend block warning, got", errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-19 01:06:49 +01:00
|
|
|
if state.Backend.Hash != uint64(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-12-19 01:06:49 +01:00
|
|
|
if state.Backend.Hash != uint64(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)
|
|
|
|
}
|
|
|
|
|
2018-11-09 02:26:15 +01:00
|
|
|
// We need a fresh InitCommand here because the old one now has our configuration
|
|
|
|
// file cached inside it, so it won't re-read the modification we just made.
|
|
|
|
c = &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-29 21:51:24 +02:00
|
|
|
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))
|
2018-11-09 02:26:15 +01:00
|
|
|
if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"foo","workspace_dir":null}`; got != want {
|
|
|
|
t.Errorf("wrong config after moving to arg\ngot: %s\nwant: %s", got, want)
|
|
|
|
}
|
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
|
2018-11-09 23:26:01 +01:00
|
|
|
fooState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetOutputValue(
|
|
|
|
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
cty.StringVal("foo"),
|
|
|
|
false, // not sensitive
|
|
|
|
)
|
|
|
|
})
|
|
|
|
if err := statemgr.NewFilesystem("foo").WriteState(fooState); err != nil {
|
2017-12-18 16:37:15 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-09 23:26:01 +01:00
|
|
|
barState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetOutputValue(
|
|
|
|
addrs.OutputValue{Name: "bar"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
cty.StringVal("bar"),
|
|
|
|
false, // not sensitive
|
|
|
|
)
|
|
|
|
})
|
|
|
|
if err := statemgr.NewFilesystem("bar").WriteState(barState); err != nil {
|
2017-12-18 16:37:15 +01:00
|
|
|
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
|
2019-05-24 17:31:04 +02:00
|
|
|
args = []string{"-backend-config=path=baz"}
|
2017-12-18 16:37:15 +01:00
|
|
|
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)
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, 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"},
|
|
|
|
})
|
|
|
|
defer close()
|
2017-06-13 03:22:47 +02:00
|
|
|
m := Meta{
|
2017-12-14 21:46:43 +01:00
|
|
|
testingOverrides: overrides,
|
2017-06-13 03:22:47 +02:00
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2020-03-31 23:02:40 +02:00
|
|
|
Meta: m,
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that we got the providers for our config
|
2020-03-31 23:02:40 +02:00
|
|
|
exactPath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/hashicorp/exact/1.2.3/%s", getproviders.CurrentPlatform)
|
2017-05-04 20:03:57 +02:00
|
|
|
if _, err := os.Stat(exactPath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'exact' not downloaded")
|
|
|
|
}
|
2020-03-31 23:02:40 +02:00
|
|
|
greaterThanPath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/hashicorp/greater-than/2.3.4/%s", getproviders.CurrentPlatform)
|
2017-05-04 20:03:57 +02:00
|
|
|
if _, err := os.Stat(greaterThanPath); os.IsNotExist(err) {
|
2020-03-20 18:59:59 +01:00
|
|
|
t.Fatal("provider 'greater-than' not downloaded")
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
2020-03-31 23:02:40 +02:00
|
|
|
betweenPath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/hashicorp/between/2.3.4/%s", getproviders.CurrentPlatform)
|
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"
|
2020-08-11 17:43:01 +02:00
|
|
|
testStateFileDefault(t, s)
|
2017-12-14 21:46:43 +01:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m.Ui = ui
|
|
|
|
c := &InitCommand{
|
2020-03-31 23:02:40 +02:00
|
|
|
Meta: m,
|
2017-12-14 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(nil); code == 0 {
|
|
|
|
t.Fatal("expected error, got:", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
errMsg := ui.ErrorWriter.String()
|
2018-11-10 00:06:00 +01:00
|
|
|
if !strings.Contains(errMsg, "which is newer than current") {
|
2017-12-14 21:46:43 +01:00
|
|
|
t.Fatal("unexpected error:", errMsg)
|
|
|
|
}
|
|
|
|
})
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:38:01 +02:00
|
|
|
func TestInit_getProviderSource(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-provider-source"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
overrides := metaOverridesForProvider(testProvider())
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
// looking for an exact version
|
|
|
|
"acme/alpha": []string{"1.2.3"},
|
|
|
|
// config doesn't specify versions for other providers
|
|
|
|
"registry.example.com/acme/beta": []string{"1.0.0"},
|
|
|
|
"gamma": []string{"2.0.0"},
|
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: overrides,
|
|
|
|
Ui: ui,
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backend=false", // should be possible to install plugins without backend init
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that we got the providers for our config
|
|
|
|
exactPath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/acme/alpha/1.2.3/%s", getproviders.CurrentPlatform)
|
|
|
|
if _, err := os.Stat(exactPath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'alpha' not downloaded")
|
|
|
|
}
|
|
|
|
greaterThanPath := fmt.Sprintf(".terraform/plugins/registry.example.com/acme/beta/1.0.0/%s", getproviders.CurrentPlatform)
|
|
|
|
if _, err := os.Stat(greaterThanPath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'beta' not downloaded")
|
|
|
|
}
|
|
|
|
betweenPath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/hashicorp/gamma/2.0.0/%s", getproviders.CurrentPlatform)
|
|
|
|
if _, err := os.Stat(betweenPath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'gamma' not downloaded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:48:45 +02:00
|
|
|
func TestInit_getProviderInvalidPackage(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-provider-invalid-package"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
overrides := metaOverridesForProvider(testProvider())
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
|
|
|
|
// create a provider source which allows installing an invalid package
|
|
|
|
addr := addrs.MustParseProviderSourceString("invalid/package")
|
|
|
|
version := getproviders.MustParseVersion("1.0.0")
|
|
|
|
meta, close, err := getproviders.FakeInstallablePackageMeta(
|
|
|
|
addr,
|
|
|
|
version,
|
|
|
|
getproviders.VersionList{getproviders.MustParseVersion("5.0")},
|
|
|
|
getproviders.CurrentPlatform,
|
|
|
|
"terraform-package", // should be "terraform-provider-package"
|
|
|
|
)
|
|
|
|
defer close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to prepare fake package for %s %s: %s", addr.ForDisplay(), version, err)
|
|
|
|
}
|
|
|
|
providerSource := getproviders.NewMockSource([]getproviders.PackageMeta{meta}, nil)
|
|
|
|
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: overrides,
|
|
|
|
Ui: ui,
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backend=false", // should be possible to install plugins without backend init
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalid provider should be installed
|
|
|
|
packagePath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/invalid/package/1.0.0/%s/terraform-package", getproviders.CurrentPlatform)
|
|
|
|
if _, err := os.Stat(packagePath); os.IsNotExist(err) {
|
|
|
|
t.Fatal("provider 'invalid/package' not downloaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
wantErrors := []string{
|
|
|
|
"Failed to validate installed provider",
|
|
|
|
"could not find executable file starting with terraform-provider-package",
|
|
|
|
}
|
|
|
|
got := ui.ErrorWriter.String()
|
|
|
|
for _, wantError := range wantErrors {
|
|
|
|
if !strings.Contains(got, wantError) {
|
|
|
|
t.Fatalf("missing error:\nwant: %q\n got: %q", wantError, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 21:24:35 +02:00
|
|
|
func TestInit_getProviderDetectedLegacy(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-get-provider-detected-legacy"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// We need to construct a multisource with a mock source and a registry
|
|
|
|
// source: the mock source will return ErrRegistryProviderNotKnown for an
|
|
|
|
// unknown provider, and the registry source will allow us to look up the
|
|
|
|
// appropriate namespace if possible.
|
|
|
|
providerSource, psClose := newMockProviderSource(t, map[string][]string{
|
|
|
|
"hashicorp/foo": []string{"1.2.3"},
|
|
|
|
"terraform-providers/baz": []string{"2.3.4"}, // this will not be installed
|
|
|
|
})
|
|
|
|
defer psClose()
|
|
|
|
registrySource, rsClose := testRegistrySource(t)
|
|
|
|
defer rsClose()
|
|
|
|
multiSource := getproviders.MultiSource{
|
|
|
|
{Source: providerSource},
|
|
|
|
{Source: registrySource},
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
Ui: ui,
|
|
|
|
ProviderSource: multiSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backend=false", // should be possible to install plugins without backend init
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error, got output: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// foo should be installed
|
|
|
|
fooPath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/hashicorp/foo/1.2.3/%s", getproviders.CurrentPlatform)
|
|
|
|
if _, err := os.Stat(fooPath); os.IsNotExist(err) {
|
|
|
|
t.Error("provider 'foo' not installed")
|
|
|
|
}
|
|
|
|
// baz should not be installed
|
|
|
|
bazPath := fmt.Sprintf(".terraform/plugins/registry.terraform.io/terraform-providers/baz/2.3.4/%s", getproviders.CurrentPlatform)
|
|
|
|
if _, err := os.Stat(bazPath); !os.IsNotExist(err) {
|
|
|
|
t.Error("provider 'baz' installed, but should not be")
|
|
|
|
}
|
|
|
|
|
|
|
|
// error output is the main focus of this test
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
2020-06-09 19:33:07 +02:00
|
|
|
errors := []string{
|
|
|
|
"Error while installing hashicorp/frob:",
|
|
|
|
"Could not find required providers, but found possible alternatives",
|
|
|
|
"hashicorp/baz -> terraform-providers/baz",
|
|
|
|
"terraform 0.13upgrade .",
|
|
|
|
"terraform 0.13upgrade child",
|
|
|
|
"The following remote modules must also be upgraded",
|
|
|
|
"- module.dicerolls at acme/bar/random",
|
|
|
|
}
|
|
|
|
for _, want := range errors {
|
|
|
|
if !strings.Contains(errOutput, want) {
|
|
|
|
t.Fatalf("expected error %q: %s", want, errOutput)
|
|
|
|
}
|
2020-05-25 21:24:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:54:53 +01:00
|
|
|
func TestInit_providerSource(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
configDirName := "init-required-providers"
|
|
|
|
copy.CopyDir(testFixturePath(configDirName), filepath.Join(td, configDirName))
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-04-01 02:31:03 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
2020-04-16 21:54:33 +02:00
|
|
|
"test": []string{"1.2.3", "1.2.4"},
|
|
|
|
"test-beta": []string{"1.2.4"},
|
|
|
|
"source": []string{"1.2.2", "1.2.3", "1.2.1"},
|
2020-04-01 02:31:03 +02:00
|
|
|
})
|
2020-03-31 23:02:40 +02:00
|
|
|
defer close()
|
|
|
|
|
2020-01-10 17:54:53 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2020-01-10 17:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2020-03-31 23:02:40 +02:00
|
|
|
Meta: m,
|
2020-01-10 17:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{configDirName}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
if strings.Contains(ui.OutputWriter.String(), "Terraform has initialized, but configuration upgrades may be needed") {
|
|
|
|
t.Fatalf("unexpected \"configuration upgrade\" warning in output")
|
|
|
|
}
|
2020-04-01 02:31:03 +02:00
|
|
|
|
|
|
|
cacheDir := m.providerLocalCacheDir()
|
|
|
|
gotPackages := cacheDir.AllAvailablePackages()
|
|
|
|
wantPackages := map[addrs.Provider][]providercache.CachedProvider{
|
|
|
|
addrs.NewDefaultProvider("test"): {
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("test", "1.2.3", false),
|
2020-04-01 02:31:03 +02:00
|
|
|
},
|
|
|
|
},
|
2020-04-16 21:54:33 +02:00
|
|
|
addrs.NewDefaultProvider("test-beta"): {
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test-beta"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("test-beta", "1.2.4", false),
|
2020-04-16 21:54:33 +02:00
|
|
|
},
|
|
|
|
},
|
2020-04-01 02:31:03 +02:00
|
|
|
addrs.NewDefaultProvider("source"): {
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("source"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("source", "1.2.3", false),
|
2020-04-01 02:31:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantPackages, gotPackages); diff != "" {
|
|
|
|
t.Errorf("wrong cache directory contents after upgrade\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
inst := m.providerInstaller()
|
|
|
|
gotSelected, err := inst.SelectedPackages()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get selected packages from installer: %s", err)
|
|
|
|
}
|
|
|
|
wantSelected := map[addrs.Provider]*providercache.CachedProvider{
|
2020-04-16 21:54:33 +02:00
|
|
|
addrs.NewDefaultProvider("test-beta"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test-beta"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("test-beta", "1.2.4", false),
|
2020-04-16 21:54:33 +02:00
|
|
|
},
|
2020-04-01 02:31:03 +02:00
|
|
|
addrs.NewDefaultProvider("test"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("test", "1.2.3", false),
|
2020-04-01 02:31:03 +02:00
|
|
|
},
|
|
|
|
addrs.NewDefaultProvider("source"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("source"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("source", "1.2.3", false),
|
2020-04-01 02:31:03 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantSelected, gotSelected); diff != "" {
|
|
|
|
t.Errorf("wrong version selections after upgrade\n%s", diff)
|
|
|
|
}
|
|
|
|
|
internal: Verify provider signatures on install
Providers installed from the registry are accompanied by a list of
checksums (the "SHA256SUMS" file), which is cryptographically signed to
allow package authentication. The process of verifying this has multiple
steps:
- First we must verify that the SHA256 hash of the package archive
matches the expected hash. This could be done for local installations
too, in the future.
- Next we ensure that the expected hash returned as part of the registry
API response matches an entry in the checksum list.
- Finally we verify the cryptographic signature of the checksum list,
using the public keys provided by the registry.
Each of these steps is implemented as a separate PackageAuthentication
type. The local archive installation mechanism uses only the archive
checksum authenticator, and the HTTP installation uses all three in the
order given.
The package authentication system now also returns a result value, which
is used by command/init to display the result of the authentication
process.
There are three tiers of signature, each of which is presented
differently to the user:
- Signatures from the embedded HashiCorp public key indicate that the
provider is officially supported by HashiCorp;
- If the signing key is not from HashiCorp, it may have an associated
trust signature, which indicates that the provider is from one of
HashiCorp's trusted partners;
- Otherwise, if the signature is valid, this is a community provider.
2020-04-08 22:22:07 +02:00
|
|
|
outputStr := ui.OutputWriter.String()
|
|
|
|
if want := "Installed hashicorp/test v1.2.3 (verified checksum)"; !strings.Contains(outputStr, want) {
|
|
|
|
t.Fatalf("unexpected output: %s\nexpected to include %q", outputStr, want)
|
|
|
|
}
|
2020-01-10 17:54:53 +01:00
|
|
|
}
|
|
|
|
|
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)()
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, 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"},
|
2020-04-01 01:48:37 +02:00
|
|
|
// config specifies > 1.0.0 , < 3.0.0
|
2020-03-31 23:02:40 +02:00
|
|
|
"between": []string{"3.4.5", "2.3.4", "1.2.3"},
|
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2017-06-13 03:32:42 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-06-13 03:32:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:48:37 +02:00
|
|
|
installFakeProviderPackages(t, &m, map[string][]string{
|
|
|
|
"exact": []string{"0.0.1"},
|
|
|
|
"greater-than": []string{"2.3.3"},
|
|
|
|
})
|
2017-06-13 03:32:42 +02:00
|
|
|
|
|
|
|
c := &InitCommand{
|
2020-03-31 23:02:40 +02:00
|
|
|
Meta: m,
|
2017-06-13 03:32:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-upgrade=true",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("command did not complete successfully:\n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:48:37 +02:00
|
|
|
cacheDir := m.providerLocalCacheDir()
|
|
|
|
gotPackages := cacheDir.AllAvailablePackages()
|
|
|
|
wantPackages := map[addrs.Provider][]providercache.CachedProvider{
|
|
|
|
// "between" wasn't previously installed at all, so we installed
|
|
|
|
// the newest available version that matched the version constraints.
|
|
|
|
addrs.NewDefaultProvider("between"): {
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("between"),
|
|
|
|
Version: getproviders.MustParseVersion("2.3.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("between", "2.3.4", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// The existing version of "exact" did not match the version constraints,
|
|
|
|
// so we installed what the configuration selected as well.
|
|
|
|
addrs.NewDefaultProvider("exact"): {
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("exact"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("exact", "1.2.3", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
// Previous version is still there, but not selected
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("exact"),
|
|
|
|
Version: getproviders.MustParseVersion("0.0.1"),
|
|
|
|
PackageDir: expectedPackageInstallPath("exact", "0.0.1", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// The existing version of "greater-than" _did_ match the constraints,
|
|
|
|
// but a newer version was available and the user specified
|
|
|
|
// -upgrade and so we upgraded it anyway.
|
|
|
|
addrs.NewDefaultProvider("greater-than"): {
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("greater-than"),
|
|
|
|
Version: getproviders.MustParseVersion("2.3.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("greater-than", "2.3.4", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
// Previous version is still there, but not selected
|
|
|
|
{
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("greater-than"),
|
|
|
|
Version: getproviders.MustParseVersion("2.3.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("greater-than", "2.3.3", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-03-31 23:02:40 +02:00
|
|
|
if diff := cmp.Diff(wantPackages, gotPackages); diff != "" {
|
|
|
|
t.Errorf("wrong cache directory contents after upgrade\n%s", diff)
|
2017-06-13 03:32:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 01:48:37 +02:00
|
|
|
inst := m.providerInstaller()
|
|
|
|
gotSelected, err := inst.SelectedPackages()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get selected packages from installer: %s", err)
|
|
|
|
}
|
|
|
|
wantSelected := map[addrs.Provider]*providercache.CachedProvider{
|
|
|
|
addrs.NewDefaultProvider("between"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("between"),
|
|
|
|
Version: getproviders.MustParseVersion("2.3.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("between", "2.3.4", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
addrs.NewDefaultProvider("exact"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("exact"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("exact", "1.2.3", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
addrs.NewDefaultProvider("greater-than"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("greater-than"),
|
|
|
|
Version: getproviders.MustParseVersion("2.3.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("greater-than", "2.3.4", false),
|
2020-04-01 01:48:37 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantSelected, gotSelected); diff != "" {
|
|
|
|
t.Errorf("wrong version selections after upgrade\n%s", diff)
|
|
|
|
}
|
|
|
|
|
2017-06-13 03:32:42 +02:00
|
|
|
}
|
|
|
|
|
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)()
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, 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"},
|
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2017-06-13 03:22:47 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2020-03-31 23:02:40 +02:00
|
|
|
Meta: m,
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code == 0 {
|
internal: Verify provider signatures on install
Providers installed from the registry are accompanied by a list of
checksums (the "SHA256SUMS" file), which is cryptographically signed to
allow package authentication. The process of verifying this has multiple
steps:
- First we must verify that the SHA256 hash of the package archive
matches the expected hash. This could be done for local installations
too, in the future.
- Next we ensure that the expected hash returned as part of the registry
API response matches an entry in the checksum list.
- Finally we verify the cryptographic signature of the checksum list,
using the public keys provided by the registry.
Each of these steps is implemented as a separate PackageAuthentication
type. The local archive installation mechanism uses only the archive
checksum authenticator, and the HTTP installation uses all three in the
order given.
The package authentication system now also returns a result value, which
is used by command/init to display the result of the authentication
process.
There are three tiers of signature, each of which is presented
differently to the user:
- Signatures from the embedded HashiCorp public key indicate that the
provider is officially supported by HashiCorp;
- If the signing key is not from HashiCorp, it may have an associated
trust signature, which indicates that the provider is from one of
HashiCorp's trusted partners;
- Otherwise, if the signature is valid, this is a community provider.
2020-04-08 22:22:07 +02:00
|
|
|
t.Fatalf("expected error, got output: \n%s", ui.OutputWriter.String())
|
2017-05-04 20:03:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 19:51:26 +02:00
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "no available releases match") {
|
2017-05-04 20:03:57 +02:00
|
|
|
t.Fatalf("unexpected error output: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:08:39 +01:00
|
|
|
func TestInit_checkRequiredVersion(t *testing.T) {
|
2017-08-28 20:25:16 +02:00
|
|
|
// 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
|
|
|
}
|
2020-08-18 15:18:00 +02:00
|
|
|
errStr := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errStr, `required_version = "~> 0.9.0"`) {
|
|
|
|
t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr)
|
|
|
|
}
|
|
|
|
if strings.Contains(errStr, `required_version = ">= 0.13.0"`) {
|
|
|
|
t.Fatalf("output should not point to met version constraint, but is:\n\n%s", errStr)
|
|
|
|
}
|
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)()
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
"test": []string{"1.2.3"},
|
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
|
2017-06-13 03:22:47 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-05-25 02:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2020-03-31 23:02:40 +02:00
|
|
|
Meta: m,
|
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())
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
selectionsFile := ".terraform/plugins/selections.json"
|
|
|
|
buf, err := ioutil.ReadFile(selectionsFile)
|
2017-05-25 02:35:46 +02:00
|
|
|
if err != nil {
|
2020-03-31 23:02:40 +02:00
|
|
|
t.Fatalf("failed to read provider selections file %s: %s", selectionsFile, err)
|
2017-05-25 02:35:46 +02:00
|
|
|
}
|
2020-03-31 23:02:40 +02:00
|
|
|
// The hash in here is for the fake package that newMockProviderSource produces
|
|
|
|
// (so it'll change if newMockProviderSource starts producing different contents)
|
2017-05-25 02:35:46 +02:00
|
|
|
wantLockFile := strings.TrimSpace(`
|
|
|
|
{
|
2020-03-31 23:02:40 +02:00
|
|
|
"registry.terraform.io/hashicorp/test": {
|
2020-04-03 19:50:08 +02:00
|
|
|
"hash": "h1:wlbEC2mChQZ2hhgUhl6SeVLPP7fMqOFUZAQhQ9GIIno=",
|
2020-03-31 23:02:40 +02:00
|
|
|
"version": "1.2.3"
|
|
|
|
}
|
2017-05-25 02:35:46 +02:00
|
|
|
}
|
|
|
|
`)
|
|
|
|
if string(buf) != wantLockFile {
|
2020-03-31 23:02:40 +02:00
|
|
|
t.Errorf("wrong provider selections file contents\ngot: %s\nwant: %s", buf, wantLockFile)
|
2017-05-25 02:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
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)
|
2020-01-13 21:10:00 +01:00
|
|
|
defer os.RemoveAll(td)
|
2017-12-21 17:21:07 +01:00
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
// An empty provider source
|
|
|
|
providerSource, close := newMockProviderSource(t, nil)
|
|
|
|
defer close()
|
|
|
|
|
2017-12-21 17:21:07 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-12-21 17:21:07 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource, // still empty
|
2017-12-21 17:21:07 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)()
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
// An empty provider source
|
|
|
|
providerSource, close := newMockProviderSource(t, nil)
|
|
|
|
defer close()
|
|
|
|
|
2017-06-15 21:23:16 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
2020-03-31 23:02:40 +02:00
|
|
|
Meta: m,
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// make our vendor paths
|
|
|
|
pluginPath := []string{"a", "b", "c"}
|
|
|
|
for _, p := range pluginPath {
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:46:38 +02:00
|
|
|
// We'll put some providers in our plugin dirs. To do this, we'll pretend
|
|
|
|
// for a moment that they are provider cache directories just because that
|
|
|
|
// allows us to lean on our existing test helper functions to do this.
|
|
|
|
for i, def := range [][]string{
|
|
|
|
[]string{"exact", "1.2.3"},
|
|
|
|
[]string{"greater-than", "2.3.4"},
|
|
|
|
[]string{"between", "2.3.4"},
|
2017-06-15 21:23:16 +02:00
|
|
|
} {
|
2020-04-01 19:46:38 +02:00
|
|
|
name, version := def[0], def[1]
|
|
|
|
dir := providercache.NewDir(pluginPath[i])
|
|
|
|
installFakeProviderPackagesElsewhere(t, dir, map[string][]string{
|
|
|
|
name: []string{version},
|
|
|
|
})
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-04-01 19:46:38 +02:00
|
|
|
|
|
|
|
inst := m.providerInstaller()
|
|
|
|
gotSelected, err := inst.SelectedPackages()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get selected packages from installer: %s", err)
|
|
|
|
}
|
|
|
|
wantSelected := map[addrs.Provider]*providercache.CachedProvider{
|
|
|
|
addrs.NewDefaultProvider("between"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("between"),
|
|
|
|
Version: getproviders.MustParseVersion("2.3.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("between", "2.3.4", false),
|
2020-04-01 19:46:38 +02:00
|
|
|
},
|
|
|
|
addrs.NewDefaultProvider("exact"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("exact"),
|
|
|
|
Version: getproviders.MustParseVersion("1.2.3"),
|
|
|
|
PackageDir: expectedPackageInstallPath("exact", "1.2.3", false),
|
2020-04-01 19:46:38 +02:00
|
|
|
},
|
|
|
|
addrs.NewDefaultProvider("greater-than"): {
|
2020-07-07 20:36:04 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("greater-than"),
|
|
|
|
Version: getproviders.MustParseVersion("2.3.4"),
|
|
|
|
PackageDir: expectedPackageInstallPath("greater-than", "2.3.4", false),
|
2020-04-01 19:46:38 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantSelected, gotSelected); diff != "" {
|
|
|
|
t.Errorf("wrong version selections after upgrade\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -plugin-dir overrides the normal provider source, so it should not have
|
|
|
|
// seen any calls at all.
|
|
|
|
if calls := providerSource.CallLog(); len(calls) > 0 {
|
|
|
|
t.Errorf("unexpected provider source calls (want none)\n%s", spew.Sdump(calls))
|
|
|
|
}
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)()
|
|
|
|
|
2020-04-01 19:46:38 +02:00
|
|
|
// Our provider source has a suitable package for "between" available,
|
|
|
|
// but we should ignore it because -plugin-dir is set and thus this
|
|
|
|
// source is temporarily overridden during install.
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
"between": []string{"2.3.4"},
|
|
|
|
})
|
2020-03-31 23:02:40 +02:00
|
|
|
defer close()
|
|
|
|
|
2020-04-01 19:46:38 +02:00
|
|
|
ui := cli.NewMockUi()
|
2017-06-15 21:23:16 +02:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
// make our vendor paths
|
|
|
|
pluginPath := []string{"a", "b"}
|
|
|
|
for _, p := range pluginPath {
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:46:38 +02:00
|
|
|
// We'll put some providers in our plugin dirs. To do this, we'll pretend
|
|
|
|
// for a moment that they are provider cache directories just because that
|
|
|
|
// allows us to lean on our existing test helper functions to do this.
|
|
|
|
for i, def := range [][]string{
|
|
|
|
[]string{"exact", "1.2.3"},
|
|
|
|
[]string{"greater-than", "2.3.4"},
|
2017-06-15 21:23:16 +02:00
|
|
|
} {
|
2020-04-01 19:46:38 +02:00
|
|
|
name, version := def[0], def[1]
|
|
|
|
dir := providercache.NewDir(pluginPath[i])
|
|
|
|
installFakeProviderPackagesElsewhere(t, dir, map[string][]string{
|
|
|
|
name: []string{version},
|
|
|
|
})
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-plugin-dir", "a",
|
|
|
|
"-plugin-dir", "b",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
// should have been an error
|
2020-04-01 19:46:38 +02:00
|
|
|
t.Fatalf("succeeded; want error\nstdout:\n%s\nstderr\n%s", ui.OutputWriter, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The error output should mention the "between" provider but should not
|
|
|
|
// mention either the "exact" or "greater-than" provider, because the
|
|
|
|
// latter two are available via the -plugin-dir directories.
|
|
|
|
errStr := ui.ErrorWriter.String()
|
|
|
|
if subStr := "hashicorp/between"; !strings.Contains(errStr, subStr) {
|
|
|
|
t.Errorf("error output should mention the 'between' provider\nwant substr: %s\ngot:\n%s", subStr, errStr)
|
|
|
|
}
|
|
|
|
if subStr := "hashicorp/exact"; strings.Contains(errStr, subStr) {
|
|
|
|
t.Errorf("error output should not mention the 'exact' provider\ndo not want substr: %s\ngot:\n%s", subStr, errStr)
|
|
|
|
}
|
|
|
|
if subStr := "hashicorp/greater-than"; strings.Contains(errStr, subStr) {
|
|
|
|
t.Errorf("error output should not mention the 'greater-than' provider\ndo not want substr: %s\ngot:\n%s", subStr, errStr)
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
2020-03-31 23:02:40 +02:00
|
|
|
|
|
|
|
if calls := providerSource.CallLog(); len(calls) > 0 {
|
|
|
|
t.Errorf("unexpected provider source calls (want none)\n%s", spew.Sdump(calls))
|
|
|
|
}
|
2017-06-15 21:23:16 +02:00
|
|
|
}
|
2018-01-05 17:51:09 +01:00
|
|
|
|
|
|
|
// Verify that plugin-dir doesn't prevent discovery of internal providers
|
2020-04-02 01:44:50 +02:00
|
|
|
func TestInit_pluginDirWithBuiltIn(t *testing.T) {
|
2018-01-05 17:51:09 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-internal"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
// An empty provider source
|
|
|
|
providerSource, close := newMockProviderSource(t, nil)
|
|
|
|
defer close()
|
|
|
|
|
2020-04-02 01:44:50 +02:00
|
|
|
ui := cli.NewMockUi()
|
2018-01-05 17:51:09 +01:00
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2020-03-31 23:02:40 +02:00
|
|
|
ProviderSource: providerSource,
|
2018-01-05 17:51:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-plugin-dir", "./"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("error: %s", ui.ErrorWriter)
|
|
|
|
}
|
2020-04-02 01:44:50 +02:00
|
|
|
|
|
|
|
outputStr := ui.OutputWriter.String()
|
|
|
|
if subStr := "terraform.io/builtin/terraform is built in to Terraform"; !strings.Contains(outputStr, subStr) {
|
|
|
|
t.Errorf("output should mention the terraform provider\nwant substr: %s\ngot:\n%s", subStr, outputStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInit_invalidBuiltInProviders(t *testing.T) {
|
|
|
|
// This test fixture includes two invalid provider dependencies:
|
|
|
|
// - an implied dependency on terraform.io/builtin/terraform with an
|
|
|
|
// explicit version number, which is not allowed because it's builtin.
|
|
|
|
// - an explicit dependency on terraform.io/builtin/nonexist, which does
|
|
|
|
// not exist at all.
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("init-internal-invalid"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// An empty provider source
|
|
|
|
providerSource, close := newMockProviderSource(t, nil)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(nil); code == 0 {
|
|
|
|
t.Fatalf("succeeded, but was expecting error\nstdout:\n%s\nstderr:\n%s", ui.OutputWriter, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
errStr := ui.ErrorWriter.String()
|
|
|
|
if subStr := "Cannot use terraform.io/builtin/terraform: built-in"; !strings.Contains(errStr, subStr) {
|
|
|
|
t.Errorf("error output should mention the terraform provider\nwant substr: %s\ngot:\n%s", subStr, errStr)
|
|
|
|
}
|
|
|
|
if subStr := "Cannot use terraform.io/builtin/nonexist: this Terraform release"; !strings.Contains(errStr, subStr) {
|
|
|
|
t.Errorf("error output should mention the 'nonexist' provider\nwant substr: %s\ngot:\n%s", subStr, errStr)
|
|
|
|
}
|
2018-01-05 17:51:09 +01:00
|
|
|
}
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
|
2020-03-31 23:02:40 +02:00
|
|
|
// newMockProviderSource is a helper to succinctly construct a mock provider
|
|
|
|
// source that contains a set of packages matching the given provider versions
|
|
|
|
// that are available for installation (from temporary local files).
|
|
|
|
//
|
|
|
|
// The caller must call the returned close callback once the source is no
|
|
|
|
// longer needed, at which point it will clean up all of the temporary files
|
|
|
|
// and the packages in the source will no longer be available for installation.
|
|
|
|
//
|
2020-05-25 22:38:01 +02:00
|
|
|
// Provider addresses must be valid source strings, and passing only the
|
|
|
|
// provider name will be interpreted as a "default" provider under
|
|
|
|
// registry.terraform.io/hashicorp. If you need more control over the
|
|
|
|
// provider addresses, pass a full provider source string.
|
2020-03-31 23:02:40 +02:00
|
|
|
//
|
|
|
|
// This function also registers providers as belonging to the current platform,
|
|
|
|
// to ensure that they will be available to a provider installer operating in
|
|
|
|
// its default configuration.
|
|
|
|
//
|
|
|
|
// In case of any errors while constructing the source, this function will
|
|
|
|
// abort the current test using the given testing.T. Therefore a caller can
|
|
|
|
// assume that if this function returns then the result is valid and ready
|
|
|
|
// to use.
|
|
|
|
func newMockProviderSource(t *testing.T, availableProviderVersions map[string][]string) (source *getproviders.MockSource, close func()) {
|
|
|
|
t.Helper()
|
|
|
|
var packages []getproviders.PackageMeta
|
|
|
|
var closes []func()
|
|
|
|
close = func() {
|
|
|
|
for _, f := range closes {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 22:38:01 +02:00
|
|
|
for source, versions := range availableProviderVersions {
|
|
|
|
addr := addrs.MustParseProviderSourceString(source)
|
2020-03-31 23:02:40 +02:00
|
|
|
for _, versionStr := range versions {
|
|
|
|
version, err := getproviders.ParseVersion(versionStr)
|
|
|
|
if err != nil {
|
|
|
|
close()
|
2020-05-25 22:38:01 +02:00
|
|
|
t.Fatalf("failed to parse %q as a version number for %q: %s", versionStr, addr.ForDisplay(), err)
|
2020-03-31 23:02:40 +02:00
|
|
|
}
|
2020-07-07 20:41:45 +02:00
|
|
|
meta, close, err := getproviders.FakeInstallablePackageMeta(addr, version, getproviders.VersionList{getproviders.MustParseVersion("5.0")}, getproviders.CurrentPlatform, "")
|
2020-03-31 23:02:40 +02:00
|
|
|
if err != nil {
|
|
|
|
close()
|
2020-05-25 22:38:01 +02:00
|
|
|
t.Fatalf("failed to prepare fake package for %s %s: %s", addr.ForDisplay(), versionStr, err)
|
2020-03-31 23:02:40 +02:00
|
|
|
}
|
|
|
|
closes = append(closes, close)
|
|
|
|
packages = append(packages, meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:49:48 +02:00
|
|
|
return getproviders.NewMockSource(packages, nil), close
|
2020-03-31 23:02:40 +02:00
|
|
|
}
|
2020-04-01 01:48:37 +02:00
|
|
|
|
2020-04-01 19:46:38 +02:00
|
|
|
// installFakeProviderPackages installs a fake package for the given provider
|
2020-04-01 01:48:37 +02:00
|
|
|
// names (interpreted as a "default" provider address) and versions into the
|
|
|
|
// local plugin cache for the given "meta".
|
|
|
|
//
|
|
|
|
// Any test using this must be using testChdir or some similar mechanism to
|
|
|
|
// make sure that it isn't writing directly into a test fixture or source
|
|
|
|
// directory within the codebase.
|
|
|
|
//
|
|
|
|
// If a requested package cannot be installed for some reason, this function
|
|
|
|
// will abort the test using the given testing.T. Therefore if this function
|
|
|
|
// returns the caller can assume that the requested providers have been
|
|
|
|
// installed.
|
|
|
|
func installFakeProviderPackages(t *testing.T, meta *Meta, providerVersions map[string][]string) {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-04-01 19:46:38 +02:00
|
|
|
cacheDir := meta.providerLocalCacheDir()
|
|
|
|
installFakeProviderPackagesElsewhere(t, cacheDir, providerVersions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// installFakeProviderPackagesElsewhere is a variant of installFakeProviderPackages
|
|
|
|
// that will install packages into the given provider cache directory, rather
|
|
|
|
// than forcing the use of the local cache of the current "Meta".
|
|
|
|
func installFakeProviderPackagesElsewhere(t *testing.T, cacheDir *providercache.Dir, providerVersions map[string][]string) {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-04-01 01:48:37 +02:00
|
|
|
// It can be hard to spot the mistake of forgetting to run testChdir before
|
|
|
|
// modifying the working directory, so we'll use a simple heuristic here
|
|
|
|
// to try to detect that mistake and make a noisy error about it instead.
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err == nil {
|
|
|
|
wd = filepath.Clean(wd)
|
|
|
|
// If the directory we're in is named "command" or if we're under a
|
|
|
|
// directory named "testdata" then we'll assume a mistake and generate
|
|
|
|
// an error. This will cause the test to fail but won't block it from
|
|
|
|
// running.
|
|
|
|
if filepath.Base(wd) == "command" || filepath.Base(wd) == "testdata" || strings.Contains(filepath.ToSlash(wd), "/testdata/") {
|
|
|
|
t.Errorf("installFakeProviderPackage may be used only by tests that switch to a temporary working directory, e.g. using testChdir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, versions := range providerVersions {
|
|
|
|
addr := addrs.NewDefaultProvider(name)
|
|
|
|
for _, versionStr := range versions {
|
|
|
|
version, err := getproviders.ParseVersion(versionStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse %q as a version number for %q: %s", versionStr, name, err)
|
|
|
|
}
|
2020-07-07 20:41:45 +02:00
|
|
|
meta, close, err := getproviders.FakeInstallablePackageMeta(addr, version, getproviders.VersionList{getproviders.MustParseVersion("5.0")}, getproviders.CurrentPlatform, "")
|
2020-04-01 01:48:37 +02:00
|
|
|
// We're going to install all these fake packages before we return,
|
|
|
|
// so we don't need to preserve them afterwards.
|
|
|
|
defer close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to prepare fake package for %s %s: %s", name, versionStr, err)
|
|
|
|
}
|
internal: Verify provider signatures on install
Providers installed from the registry are accompanied by a list of
checksums (the "SHA256SUMS" file), which is cryptographically signed to
allow package authentication. The process of verifying this has multiple
steps:
- First we must verify that the SHA256 hash of the package archive
matches the expected hash. This could be done for local installations
too, in the future.
- Next we ensure that the expected hash returned as part of the registry
API response matches an entry in the checksum list.
- Finally we verify the cryptographic signature of the checksum list,
using the public keys provided by the registry.
Each of these steps is implemented as a separate PackageAuthentication
type. The local archive installation mechanism uses only the archive
checksum authenticator, and the HTTP installation uses all three in the
order given.
The package authentication system now also returns a result value, which
is used by command/init to display the result of the authentication
process.
There are three tiers of signature, each of which is presented
differently to the user:
- Signatures from the embedded HashiCorp public key indicate that the
provider is officially supported by HashiCorp;
- If the signing key is not from HashiCorp, it may have an associated
trust signature, which indicates that the provider is from one of
HashiCorp's trusted partners;
- Otherwise, if the signature is valid, this is a community provider.
2020-04-08 22:22:07 +02:00
|
|
|
_, err = cacheDir.InstallPackage(context.Background(), meta)
|
2020-04-01 01:48:37 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to install fake package for %s %s: %s", name, versionStr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 02:31:03 +02:00
|
|
|
|
|
|
|
// expectedPackageInstallPath is a companion to installFakeProviderPackages
|
|
|
|
// that returns the path where the provider with the given name and version
|
|
|
|
// would be installed and, relatedly, where the installer will expect to
|
|
|
|
// find an already-installed version.
|
|
|
|
//
|
|
|
|
// Just as with installFakeProviderPackages, this function is a shortcut helper
|
|
|
|
// for "default-namespaced" providers as we commonly use in tests. If you need
|
|
|
|
// more control over the provider addresses, use functions of the underlying
|
|
|
|
// getproviders and providercache packages instead.
|
|
|
|
//
|
|
|
|
// The result always uses forward slashes, even on Windows, for consistency
|
|
|
|
// with how the getproviders and providercache packages build paths.
|
|
|
|
func expectedPackageInstallPath(name, version string, exe bool) string {
|
|
|
|
platform := getproviders.CurrentPlatform
|
|
|
|
baseDir := ".terraform/plugins"
|
|
|
|
if exe {
|
|
|
|
p := fmt.Sprintf("registry.terraform.io/hashicorp/%s/%s/%s/terraform-provider-%s_%s", name, version, platform, name, version)
|
|
|
|
if platform.OS == "windows" {
|
|
|
|
p += ".exe"
|
|
|
|
}
|
|
|
|
return filepath.ToSlash(filepath.Join(baseDir, p))
|
|
|
|
}
|
|
|
|
return filepath.ToSlash(filepath.Join(
|
|
|
|
baseDir, fmt.Sprintf("registry.terraform.io/hashicorp/%s/%s/%s", name, version, platform),
|
|
|
|
))
|
|
|
|
}
|