opentofu/command/init_test.go

290 lines
5.7 KiB
Go
Raw Normal View History

2014-09-26 18:03:39 -05:00
package command
import (
2014-09-26 18:30:49 -05:00
"os"
"path/filepath"
2014-09-26 18:03:39 -05:00
"testing"
2014-10-08 16:57:22 -05:00
"github.com/hashicorp/terraform/terraform"
2014-09-26 18:03:39 -05:00
"github.com/mitchellh/cli"
)
2014-09-26 18:30:49 -05:00
func TestInit(t *testing.T) {
dir := tempDir(t)
2014-09-26 18:03:39 -05:00
ui := new(cli.MockUi)
2014-09-26 18:30:49 -05:00
c := &InitCommand{
2014-09-26 18:03:39 -05:00
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
2014-09-26 18:30:49 -05:00
testFixturePath("init"),
dir,
2014-09-26 18:03:39 -05:00
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
2014-09-26 18:30:49 -05:00
if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil {
t.Fatalf("err: %s", err)
2014-09-26 18:03:39 -05:00
}
}
2014-09-27 11:23:02 -05:00
func TestInit_cwd(t *testing.T) {
dir := tempDir(t)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("err: %s", err)
}
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(dir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
testFixturePath("init"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
if _, err := os.Stat("hello.tf"); err != nil {
t.Fatalf("err: %s", err)
}
}
2014-09-26 18:03:39 -05:00
func TestInit_multipleArgs(t *testing.T) {
ui := new(cli.MockUi)
c := &InitCommand{
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 TestInit_noArgs(t *testing.T) {
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}
2014-11-04 14:19:39 -06:00
// https://github.com/hashicorp/terraform/issues/518
func TestInit_dstInSrc(t *testing.T) {
2014-11-04 14:19:39 -06:00
dir := tempDir(t)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("err: %s", err)
}
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(dir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
if _, err := os.Create("issue518.tf"); err != nil {
t.Fatalf("err: %s", err)
}
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
".",
"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)
}
}
2014-10-08 16:57:22 -05:00
func TestInit_remoteState(t *testing.T) {
tmp, cwd := testCwd(t)
2014-10-09 16:45:08 -05:00
defer testFixCwd(t, tmp, cwd)
2014-10-08 16:57:22 -05:00
s := terraform.NewState()
2014-10-08 17:15:14 -05:00
conf, srv := testRemoteState(t, s, 200)
2014-10-08 16:57:22 -05:00
defer srv.Close()
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
"-backend", "HTTP",
2015-02-23 12:56:27 -06:00
"-backend-config", "address=" + conf.Config["address"],
2014-10-08 16:57:22 -05:00
testFixturePath("init"),
tmp,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
if _, err := os.Stat(filepath.Join(tmp, "hello.tf")); err != nil {
t.Fatalf("err: %s", err)
}
2015-02-22 12:50:25 -06:00
if _, err := os.Stat(filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)); err != nil {
t.Fatalf("missing state: %s", err)
2014-10-08 16:57:22 -05:00
}
}
func TestInit_remoteStateSubdir(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
subdir := filepath.Join(tmp, "subdir")
s := terraform.NewState()
conf, srv := testRemoteState(t, s, 200)
defer srv.Close()
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
"-backend", "http",
"-backend-config", "address=" + conf.Config["address"],
testFixturePath("init"),
subdir,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
if _, err := os.Stat(filepath.Join(subdir, "hello.tf")); err != nil {
t.Fatalf("err: %s", err)
}
if _, err := os.Stat(filepath.Join(subdir, DefaultDataDir, DefaultStateFilename)); err != nil {
t.Fatalf("missing state: %s", err)
}
}
func TestInit_remoteStateWithLocal(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
statePath := filepath.Join(tmp, DefaultStateFilename)
// Write some state
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)
}
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
"-backend", "http",
2015-02-23 12:56:27 -06:00
"-backend-config", "address=http://google.com",
testFixturePath("init"),
}
if code := c.Run(args); code == 0 {
t.Fatalf("should have failed: \n%s", ui.OutputWriter.String())
}
}
func TestInit_remoteStateWithRemote(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil {
t.Fatalf("err: %s", err)
}
// Write some state
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)
}
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{
"-backend", "http",
2015-02-23 12:56:27 -06:00
"-backend-config", "address=http://google.com",
testFixturePath("init"),
}
if code := c.Run(args); code == 0 {
t.Fatalf("should have failed: \n%s", ui.OutputWriter.String())
}
}