2017-07-08 03:54:29 +02:00
|
|
|
package e2etest
|
|
|
|
|
|
|
|
import (
|
2017-08-16 15:48:51 +02:00
|
|
|
"path/filepath"
|
2017-07-08 03:54:29 +02:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2021-05-17 18:48:22 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/e2e"
|
2021-05-17 21:33:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-02 00:45:12 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-07-08 03:54:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// The tests in this file are for the "primary workflow", which includes
|
|
|
|
// variants of the following sequence, with different details:
|
|
|
|
// terraform init
|
|
|
|
// terraform plan
|
|
|
|
// terraform apply
|
|
|
|
// terraform destroy
|
|
|
|
|
|
|
|
func TestPrimarySeparatePlan(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// This test reaches out to releases.hashicorp.com to download the
|
|
|
|
// template and null providers, so it can only run if network access is
|
|
|
|
// allowed.
|
|
|
|
skipIfCannotAccessNetwork(t)
|
|
|
|
|
2019-06-30 09:38:36 +02:00
|
|
|
fixturePath := filepath.Join("testdata", "full-workflow-null")
|
2017-08-16 15:48:51 +02:00
|
|
|
tf := e2e.NewBinary(terraformBin, fixturePath)
|
2017-07-08 03:54:29 +02:00
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
//// INIT
|
|
|
|
stdout, stderr, err := tf.Run("init")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected init error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we actually downloaded the plugins, rather than picking up
|
|
|
|
// copies that might be already installed globally on the system.
|
2020-04-03 22:59:42 +02:00
|
|
|
if !strings.Contains(stdout, "Installing hashicorp/template v") {
|
2017-07-08 03:54:29 +02:00
|
|
|
t.Errorf("template provider download message is missing from init output:\n%s", stdout)
|
|
|
|
t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)")
|
|
|
|
}
|
2020-04-03 22:59:42 +02:00
|
|
|
if !strings.Contains(stdout, "Installing hashicorp/null v") {
|
2017-07-08 03:54:29 +02:00
|
|
|
t.Errorf("null provider download message is missing from init output:\n%s", stdout)
|
|
|
|
t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)")
|
|
|
|
}
|
|
|
|
|
|
|
|
//// PLAN
|
|
|
|
stdout, stderr, err = tf.Run("plan", "-out=tfplan")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected plan error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout, "1 to add, 0 to change, 0 to destroy") {
|
|
|
|
t.Errorf("incorrect plan tally; want 1 to add:\n%s", stdout)
|
2017-09-19 20:51:00 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 03:29:39 +01:00
|
|
|
if !strings.Contains(stdout, "Saved the plan to: tfplan") {
|
|
|
|
t.Errorf("missing \"Saved the plan to...\" message in plan output\n%s", stdout)
|
2017-09-19 20:51:00 +02:00
|
|
|
}
|
|
|
|
if !strings.Contains(stdout, "terraform apply \"tfplan\"") {
|
|
|
|
t.Errorf("missing next-step instruction in plan output\n%s", stdout)
|
2017-07-08 03:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
plan, err := tf.Plan("tfplan")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read plan file: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-04-29 19:03:25 +02:00
|
|
|
diffResources := plan.Changes.Resources
|
2020-09-22 03:30:20 +02:00
|
|
|
if len(diffResources) != 1 {
|
2020-09-16 22:25:51 +02:00
|
|
|
t.Errorf("incorrect number of resources in plan")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]plans.Action{
|
2020-09-22 03:30:20 +02:00
|
|
|
"null_resource.test": plans.Create,
|
2020-09-16 22:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range diffResources {
|
|
|
|
expectedAction, ok := expected[r.Addr.String()]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected change for %q", r.Addr)
|
|
|
|
}
|
|
|
|
if r.Action != expectedAction {
|
|
|
|
t.Fatalf("unexpected action %q for %q", r.Action, r.Addr)
|
|
|
|
}
|
2017-07-08 03:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//// APPLY
|
|
|
|
stdout, stderr, err = tf.Run("apply", "tfplan")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout, "Resources: 1 added, 0 changed, 0 destroyed") {
|
|
|
|
t.Errorf("incorrect apply tally; want 1 added:\n%s", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := tf.LocalState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read state file: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-04-29 19:03:25 +02:00
|
|
|
stateResources := state.RootModule().Resources
|
2017-07-08 03:54:29 +02:00
|
|
|
var gotResources []string
|
2020-12-01 15:15:36 +01:00
|
|
|
for n := range stateResources {
|
2017-07-08 03:54:29 +02:00
|
|
|
gotResources = append(gotResources, n)
|
|
|
|
}
|
|
|
|
sort.Strings(gotResources)
|
|
|
|
|
|
|
|
wantResources := []string{
|
|
|
|
"data.template_file.test",
|
|
|
|
"null_resource.test",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(gotResources, wantResources) {
|
|
|
|
t.Errorf("wrong resources in state\ngot: %#v\nwant: %#v", gotResources, wantResources)
|
|
|
|
}
|
|
|
|
|
|
|
|
//// DESTROY
|
2018-02-01 01:14:42 +01:00
|
|
|
stdout, stderr, err = tf.Run("destroy", "-auto-approve")
|
2017-07-08 03:54:29 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected destroy error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
2017-09-19 20:25:31 +02:00
|
|
|
if !strings.Contains(stdout, "Resources: 1 destroyed") {
|
|
|
|
t.Errorf("incorrect destroy tally; want 1 destroyed:\n%s", stdout)
|
2017-07-08 03:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state, err = tf.LocalState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read state file after destroy: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateResources = state.RootModule().Resources
|
|
|
|
if len(stateResources) != 0 {
|
|
|
|
t.Errorf("wrong resources in state after destroy; want none, but still have:%s", spew.Sdump(stateResources))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-02 00:45:12 +02:00
|
|
|
|
|
|
|
func TestPrimaryChdirOption(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// This test case does not include any provider dependencies, so it's
|
|
|
|
// safe to run it even when network access is disallowed.
|
|
|
|
|
|
|
|
fixturePath := filepath.Join("testdata", "chdir-option")
|
|
|
|
tf := e2e.NewBinary(terraformBin, fixturePath)
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
//// INIT
|
2020-12-01 18:37:08 +01:00
|
|
|
_, stderr, err := tf.Run("-chdir=subdir", "init")
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-02 00:45:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected init error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
//// PLAN
|
2020-12-01 18:37:08 +01:00
|
|
|
stdout, stderr, err := tf.Run("-chdir=subdir", "plan", "-out=tfplan")
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-02 00:45:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected plan error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
if want := "You can apply this plan to save these new output values"; !strings.Contains(stdout, want) {
|
|
|
|
t.Errorf("missing expected message for an outputs-only plan\ngot:\n%s\n\nwant substring: %s", stdout, want)
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-02 00:45:12 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 03:29:39 +01:00
|
|
|
if !strings.Contains(stdout, "Saved the plan to: tfplan") {
|
|
|
|
t.Errorf("missing \"Saved the plan to...\" message in plan output\n%s", stdout)
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-02 00:45:12 +02:00
|
|
|
}
|
|
|
|
if !strings.Contains(stdout, "terraform apply \"tfplan\"") {
|
|
|
|
t.Errorf("missing next-step instruction in plan output\n%s", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The saved plan is in the subdirectory because -chdir switched there
|
|
|
|
plan, err := tf.Plan("subdir/tfplan")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read plan file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
diffResources := plan.Changes.Resources
|
|
|
|
if len(diffResources) != 0 {
|
|
|
|
t.Errorf("incorrect diff in plan; want no resource changes, but have:\n%s", spew.Sdump(diffResources))
|
|
|
|
}
|
|
|
|
|
|
|
|
//// APPLY
|
|
|
|
stdout, stderr, err = tf.Run("-chdir=subdir", "apply", "tfplan")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout, "Resources: 0 added, 0 changed, 0 destroyed") {
|
|
|
|
t.Errorf("incorrect apply tally; want 0 added:\n%s", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The state file is in subdir because -chdir changed the current working directory.
|
|
|
|
state, err := tf.StateFromFile("subdir/terraform.tfstate")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read state file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotOutput := state.RootModule().OutputValues["cwd"]
|
|
|
|
wantOutputValue := cty.StringVal(tf.Path()) // path.cwd returns the original path, because path.root is how we get the overridden path
|
|
|
|
if gotOutput == nil || !wantOutputValue.RawEquals(gotOutput.Value) {
|
|
|
|
t.Errorf("incorrect value for cwd output\ngot: %#v\nwant Value: %#v", gotOutput, wantOutputValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotOutput = state.RootModule().OutputValues["root"]
|
|
|
|
wantOutputValue = cty.StringVal(tf.Path("subdir")) // path.root is a relative path, but the text fixture uses abspath on it.
|
|
|
|
if gotOutput == nil || !wantOutputValue.RawEquals(gotOutput.Value) {
|
|
|
|
t.Errorf("incorrect value for root output\ngot: %#v\nwant Value: %#v", gotOutput, wantOutputValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(state.RootModule().Resources) != 0 {
|
|
|
|
t.Errorf("unexpected resources in state")
|
|
|
|
}
|
|
|
|
|
|
|
|
//// DESTROY
|
|
|
|
stdout, stderr, err = tf.Run("-chdir=subdir", "destroy", "-auto-approve")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected destroy error: %s\nstderr:\n%s", err, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout, "Resources: 0 destroyed") {
|
|
|
|
t.Errorf("incorrect destroy tally; want 0 destroyed:\n%s", stdout)
|
|
|
|
}
|
|
|
|
}
|