mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
cb5c2567b3
fixes #1810
186 lines
3.6 KiB
Go
186 lines
3.6 KiB
Go
package command
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestShow(t *testing.T) {
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"bad",
|
|
"bad",
|
|
}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_noArgs(t *testing.T) {
|
|
// Create the default state
|
|
td, err := ioutil.TempDir("", "tf")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
statePath := filepath.Join(td, DefaultStateFilename)
|
|
|
|
f, err := os.Create(statePath)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
err = terraform.WriteState(testState(), f)
|
|
f.Close()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
// Change to the temporary directory
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer os.Chdir(cwd)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_noArgsNoState(t *testing.T) {
|
|
// Create the default state
|
|
td, err := ioutil.TempDir("", "tf")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
statePath := filepath.Join(td, DefaultStateFilename)
|
|
|
|
// Change to the temporary directory
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer os.Chdir(cwd)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_plan(t *testing.T) {
|
|
planPath := testPlanFile(t, &terraform.Plan{
|
|
Module: new(module.Tree),
|
|
})
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
planPath,
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_noArgsRemoteState(t *testing.T) {
|
|
tmp, cwd := testCwd(t)
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
// Pretend like we have a local cache of remote state
|
|
remoteStatePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
|
|
if err := os.MkdirAll(filepath.Dir(remoteStatePath), 0755); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
f, err := os.Create(remoteStatePath)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
err = terraform.WriteState(testState(), f)
|
|
f.Close()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
}
|
|
|
|
expected := "test_instance.foo"
|
|
actual := ui.OutputWriter.String()
|
|
if !strings.Contains(actual, expected) {
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestShow_state(t *testing.T) {
|
|
originalState := testState()
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
statePath,
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
}
|
|
}
|