2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-16 08:48:51 -05:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2022-04-08 11:34:16 -05:00
|
|
|
"testing"
|
2017-08-16 08:48:51 -05:00
|
|
|
|
2024-03-04 08:00:29 -06:00
|
|
|
"github.com/opentofu/opentofu/internal/encryption"
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/plans"
|
|
|
|
"github.com/opentofu/opentofu/internal/plans/planfile"
|
|
|
|
"github.com/opentofu/opentofu/internal/states"
|
|
|
|
"github.com/opentofu/opentofu/internal/states/statefile"
|
2017-08-16 08:48:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Type binary represents the combination of a compiled binary
|
|
|
|
// and a temporary working directory to run it in.
|
|
|
|
type binary struct {
|
|
|
|
binPath string
|
|
|
|
workDir string
|
2017-09-19 13:31:18 -05:00
|
|
|
env []string
|
2017-08-16 08:48:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBinary prepares a temporary directory containing the files from the
|
|
|
|
// given fixture and returns an instance of type binary that can run
|
|
|
|
// the generated binary in that directory.
|
|
|
|
//
|
|
|
|
// If the temporary directory cannot be created, a fixture of the given name
|
|
|
|
// cannot be found, or if an error occurs while _copying_ the fixture files,
|
|
|
|
// this function will panic. Tests should be written to assume that this
|
|
|
|
// function always succeeds.
|
2022-04-08 11:34:16 -05:00
|
|
|
func NewBinary(t *testing.T, binaryPath, workingDir string) *binary {
|
|
|
|
tmpDir, err := filepath.EvalSymlinks(t.TempDir())
|
2020-09-08 12:46:20 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-08-16 08:48:51 -05:00
|
|
|
// For our purposes here we do a very simplistic file copy that doesn't
|
|
|
|
// attempt to preserve file permissions, attributes, alternate data
|
|
|
|
// streams, etc. Since we only have to deal with our own fixtures in
|
2019-06-30 02:38:36 -05:00
|
|
|
// the testdata subdir, we know we don't need to deal with anything
|
2017-08-16 08:48:51 -05:00
|
|
|
// of this nature.
|
|
|
|
err = filepath.Walk(workingDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if path == workingDir {
|
|
|
|
// nothing to do at the root
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:03:25 -05:00
|
|
|
if filepath.Base(path) == ".exists" {
|
|
|
|
// We use this file just to let git know the "empty" fixture
|
|
|
|
// exists. It is not used by any test.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-16 08:48:51 -05:00
|
|
|
srcFn := path
|
|
|
|
|
|
|
|
path, err = filepath.Rel(workingDir, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dstFn := filepath.Join(tmpDir, path)
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return os.Mkdir(dstFn, os.ModePerm)
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := os.Open(srcFn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dst, err := os.OpenFile(dstFn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := src.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dst.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &binary{
|
|
|
|
binPath: binaryPath,
|
|
|
|
workDir: tmpDir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 13:31:18 -05:00
|
|
|
// AddEnv appends an entry to the environment variable table passed to any
|
|
|
|
// commands subsequently run.
|
|
|
|
func (b *binary) AddEnv(entry string) {
|
|
|
|
b.env = append(b.env, entry)
|
|
|
|
}
|
|
|
|
|
2023-09-26 12:09:27 -05:00
|
|
|
// Cmd returns an exec.Cmd pre-configured to run the generated OpenTofu
|
2017-08-16 08:48:51 -05:00
|
|
|
// binary with the given arguments in the temporary working directory.
|
|
|
|
//
|
|
|
|
// The returned object can be mutated by the caller to customize how the
|
|
|
|
// process will be run, before calling Run.
|
|
|
|
func (b *binary) Cmd(args ...string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(b.binPath, args...)
|
|
|
|
cmd.Dir = b.workDir
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
|
2017-09-19 13:31:18 -05:00
|
|
|
cmd.Env = append(cmd.Env, b.env...)
|
|
|
|
|
2017-08-16 08:48:51 -05:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-26 12:09:27 -05:00
|
|
|
// Run executes the generated OpenTofu binary with the given arguments
|
2017-08-16 08:48:51 -05:00
|
|
|
// and returns the bytes that it wrote to both stdout and stderr.
|
|
|
|
//
|
2023-09-26 12:09:27 -05:00
|
|
|
// This is a simple way to run OpenTofu for non-interactive commands
|
2017-08-16 08:48:51 -05:00
|
|
|
// that don't need any special environment variables. For more complex
|
|
|
|
// situations, use Cmd and customize the command before running it.
|
|
|
|
func (b *binary) Run(args ...string) (stdout, stderr string, err error) {
|
|
|
|
cmd := b.Cmd(args...)
|
|
|
|
cmd.Stdin = nil
|
|
|
|
cmd.Stdout = &bytes.Buffer{}
|
|
|
|
cmd.Stderr = &bytes.Buffer{}
|
|
|
|
err = cmd.Run()
|
|
|
|
stdout = cmd.Stdout.(*bytes.Buffer).String()
|
|
|
|
stderr = cmd.Stderr.(*bytes.Buffer).String()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns a file path within the temporary working directory by
|
|
|
|
// appending the given arguments as path segments.
|
|
|
|
func (b *binary) Path(parts ...string) string {
|
2022-11-21 11:13:47 -06:00
|
|
|
args := make([]string, 0, len(parts)+1)
|
|
|
|
args = append(args, b.workDir)
|
2017-08-16 08:48:51 -05:00
|
|
|
args = append(args, parts...)
|
|
|
|
return filepath.Join(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile is a helper for easily opening a file from the working directory
|
|
|
|
// for reading.
|
|
|
|
func (b *binary) OpenFile(path ...string) (*os.File, error) {
|
|
|
|
flatPath := b.Path(path...)
|
|
|
|
return os.Open(flatPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile is a helper for easily reading a whole file from the working
|
|
|
|
// directory.
|
|
|
|
func (b *binary) ReadFile(path ...string) ([]byte, error) {
|
|
|
|
flatPath := b.Path(path...)
|
2023-09-08 03:03:43 -05:00
|
|
|
return os.ReadFile(flatPath)
|
2017-08-16 08:48:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileExists is a helper for easily testing whether a particular file
|
|
|
|
// exists in the working directory.
|
|
|
|
func (b *binary) FileExists(path ...string) bool {
|
|
|
|
flatPath := b.Path(path...)
|
|
|
|
_, err := os.Stat(flatPath)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalState is a helper for easily reading the local backend's state file
|
|
|
|
// terraform.tfstate from the working directory.
|
2019-04-29 12:03:25 -05:00
|
|
|
func (b *binary) LocalState() (*states.State, error) {
|
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-01 17:45:12 -05:00
|
|
|
return b.StateFromFile("terraform.tfstate")
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateFromFile is a helper for easily reading a state snapshot from a file
|
|
|
|
// on disk relative to the working directory.
|
|
|
|
func (b *binary) StateFromFile(filename string) (*states.State, error) {
|
|
|
|
f, err := b.OpenFile(filename)
|
2017-08-16 08:48:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-04-29 12:03:25 -05:00
|
|
|
|
2024-03-04 08:25:14 -06:00
|
|
|
stateFile, err := statefile.Read(f, encryption.StateEncryptionDisabled())
|
2019-04-29 12:03:25 -05:00
|
|
|
if err != nil {
|
2023-09-18 07:53:49 -05:00
|
|
|
return nil, fmt.Errorf("Error reading statefile: %w", err)
|
2019-04-29 12:03:25 -05:00
|
|
|
}
|
|
|
|
return stateFile.State, nil
|
2017-08-16 08:48:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plan is a helper for easily reading a plan file from the working directory.
|
2019-04-29 12:03:25 -05:00
|
|
|
func (b *binary) Plan(path string) (*plans.Plan, error) {
|
|
|
|
path = b.Path(path)
|
2024-03-04 08:00:29 -06:00
|
|
|
pr, err := planfile.Open(path, encryption.PlanEncryptionDisabled())
|
2017-08-16 08:48:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 12:03:25 -05:00
|
|
|
plan, err := pr.ReadPlan()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return plan, nil
|
2017-08-16 08:48:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLocalState is a helper for easily writing to the file the local backend
|
|
|
|
// uses for state in the working directory. This does not go through the
|
|
|
|
// actual local backend code, so processing such as management of serials
|
|
|
|
// does not apply and the given state will simply be written verbatim.
|
2019-04-29 12:03:25 -05:00
|
|
|
func (b *binary) SetLocalState(state *states.State) error {
|
2017-08-16 08:48:51 -05:00
|
|
|
path := b.Path("terraform.tfstate")
|
|
|
|
f, err := os.OpenFile(path, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
2023-09-18 07:53:49 -05:00
|
|
|
return fmt.Errorf("failed to create temporary state file %s: %w", path, err)
|
2017-08-16 08:48:51 -05:00
|
|
|
}
|
2019-04-29 12:03:25 -05:00
|
|
|
defer f.Close()
|
2017-08-16 08:48:51 -05:00
|
|
|
|
2019-04-29 12:03:25 -05:00
|
|
|
sf := &statefile.File{
|
|
|
|
Serial: 0,
|
|
|
|
Lineage: "fake-for-testing",
|
|
|
|
State: state,
|
|
|
|
}
|
2024-03-04 08:25:14 -06:00
|
|
|
return statefile.Write(sf, f, encryption.StateEncryptionDisabled())
|
2017-08-16 08:48:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func GoBuild(pkgPath, tmpPrefix string) string {
|
2020-10-14 20:00:23 -05:00
|
|
|
dir, prefix := filepath.Split(tmpPrefix)
|
2023-09-08 03:03:43 -05:00
|
|
|
tmpFile, err := os.CreateTemp(dir, prefix)
|
2017-08-16 08:48:51 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tmpFilename := tmpFile.Name()
|
|
|
|
if err = tmpFile.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(
|
|
|
|
"go", "build",
|
|
|
|
"-o", tmpFilename,
|
|
|
|
pkgPath,
|
|
|
|
)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
// The go compiler will have already produced some error messages
|
|
|
|
// on stderr by the time we get here.
|
|
|
|
panic(fmt.Sprintf("failed to build executable: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpFilename
|
|
|
|
}
|
2020-04-21 16:09:29 -05:00
|
|
|
|
|
|
|
// WorkDir() returns the binary workdir
|
|
|
|
func (b *binary) WorkDir() string {
|
|
|
|
return b.workDir
|
|
|
|
}
|