opentofu/command/command_test.go

283 lines
5.9 KiB
Go
Raw Normal View History

2014-06-18 23:36:44 -05:00
package command
import (
"io/ioutil"
"os"
2014-06-18 23:36:44 -05:00
"path/filepath"
2015-02-26 12:29:23 -06:00
"strings"
"testing"
2014-06-18 23:36:44 -05:00
"github.com/hashicorp/go-getter"
2014-09-24 17:48:46 -05:00
"github.com/hashicorp/terraform/config/module"
2014-06-18 23:36:44 -05:00
"github.com/hashicorp/terraform/terraform"
)
// This is the directory where our test fixtures are.
2014-07-11 23:03:56 -05:00
var fixtureDir = "./test-fixtures"
func init() {
2014-09-29 13:24:16 -05:00
test = true
2014-07-11 23:03:56 -05:00
// Expand the fixture dir on init because we change the working
// directory in some tests.
var err error
fixtureDir, err = filepath.Abs(fixtureDir)
if err != nil {
panic(err)
}
}
2014-06-18 23:36:44 -05:00
2014-09-24 17:48:46 -05:00
func tempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.RemoveAll(dir); err != nil {
t.Fatalf("err: %s", err)
}
return dir
}
2014-06-18 23:36:44 -05:00
func testFixturePath(name string) string {
2014-07-11 22:38:03 -05:00
return filepath.Join(fixtureDir, name)
2014-06-18 23:36:44 -05:00
}
2014-07-03 13:46:40 -05:00
func testCtxConfig(p terraform.ResourceProvider) *terraform.ContextOpts {
return &terraform.ContextOpts{
2014-06-18 23:36:44 -05:00
Providers: map[string]terraform.ResourceProviderFactory{
"test": func() (terraform.ResourceProvider, error) {
return p, nil
},
},
}
}
func testCtxConfigWithShell(p terraform.ResourceProvider, pr terraform.ResourceProvisioner) *terraform.ContextOpts {
return &terraform.ContextOpts{
Providers: map[string]terraform.ResourceProviderFactory{
"test": func() (terraform.ResourceProvider, error) {
return p, nil
},
},
Provisioners: map[string]terraform.ResourceProvisionerFactory{
"shell": func() (terraform.ResourceProvisioner, error) {
return pr, nil
},
},
}
}
2014-09-24 17:48:46 -05:00
func testModule(t *testing.T, name string) *module.Tree {
mod, err := module.NewTreeModule("", filepath.Join(fixtureDir, name))
if err != nil {
t.Fatalf("err: %s", err)
}
s := &getter.FolderStorage{StorageDir: tempDir(t)}
2014-09-24 17:48:46 -05:00
if err := mod.Load(s, module.GetModeGet); err != nil {
t.Fatalf("err: %s", err)
}
return mod
}
func testPlanFile(t *testing.T, plan *terraform.Plan) string {
path := testTempFile(t)
f, err := os.Create(path)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
if err := terraform.WritePlan(plan, f); err != nil {
t.Fatalf("err: %s", err)
}
return path
}
func testReadPlan(t *testing.T, path string) *terraform.Plan {
f, err := os.Open(path)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
p, err := terraform.ReadPlan(f)
if err != nil {
t.Fatalf("err: %s", err)
}
return p
}
2014-09-17 13:15:07 -05:00
// testState returns a test State structure that we use for a lot of tests.
func testState() *terraform.State {
return &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
},
},
}
}
func testStateFile(t *testing.T, s *terraform.State) string {
path := testTempFile(t)
f, err := os.Create(path)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
if err := terraform.WriteState(s, f); err != nil {
t.Fatalf("err: %s", err)
}
return path
}
2015-02-26 12:29:23 -06:00
// testStateFileDefault writes the state out to the default statefile
// in the cwd. Use `testCwd` to change into a temp cwd.
func testStateFileDefault(t *testing.T, s *terraform.State) string {
f, err := os.Create(DefaultStateFilename)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
if err := terraform.WriteState(s, f); err != nil {
t.Fatalf("err: %s", err)
}
return DefaultStateFilename
}
2015-03-05 16:55:15 -06:00
// testStateFileRemote writes the state out to the remote statefile
// in the cwd. Use `testCwd` to change into a temp cwd.
func testStateFileRemote(t *testing.T, s *terraform.State) string {
path := filepath.Join(DefaultDataDir, DefaultStateFilename)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatalf("err: %s", err)
}
f, err := os.Create(path)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
if err := terraform.WriteState(s, f); err != nil {
t.Fatalf("err: %s", err)
}
return path
}
2015-02-26 12:29:23 -06:00
// testStateOutput tests that the state at the given path contains
// the expected state string.
func testStateOutput(t *testing.T, path string, expected string) {
f, err := os.Open(path)
if err != nil {
t.Fatalf("err: %s", err)
}
newState, err := terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(newState.String())
expected = strings.TrimSpace(expected)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
2014-06-18 23:36:44 -05:00
func testProvider() *terraform.MockResourceProvider {
p := new(terraform.MockResourceProvider)
2014-09-17 18:33:24 -05:00
p.DiffReturn = &terraform.InstanceDiff{}
p.RefreshFn = func(
2014-09-17 13:15:07 -05:00
info *terraform.InstanceInfo,
s *terraform.InstanceState) (*terraform.InstanceState, error) {
return s, nil
}
2014-06-18 23:36:44 -05:00
p.ResourcesReturn = []terraform.ResourceType{
terraform.ResourceType{
Name: "test_instance",
},
}
return p
}
func testTempFile(t *testing.T) string {
tf, err := ioutil.TempFile("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
result := tf.Name()
if err := tf.Close(); err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Remove(result); err != nil {
t.Fatalf("err: %s", err)
}
return result
}
func testTempDir(t *testing.T) string {
d, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
return d
}
2014-10-08 14:08:35 -05:00
2015-02-26 12:29:23 -06:00
// testCwd is used to change the current working directory
2014-10-08 14:08:35 -05:00
// into a test directory that should be remoted after
func testCwd(t *testing.T) (string, string) {
tmp, err := ioutil.TempDir("", "tf")
2014-10-08 14:08:35 -05:00
if err != nil {
t.Fatalf("err: %v", err)
}
2014-10-08 14:08:35 -05:00
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %v", err)
}
2014-10-09 16:45:08 -05:00
if err := os.Chdir(tmp); err != nil {
t.Fatalf("err: %v", err)
}
2014-10-08 14:08:35 -05:00
return tmp, cwd
}
2014-10-09 16:45:08 -05:00
// testFixCwd is used to as a defer to testDir
func testFixCwd(t *testing.T, tmp, cwd string) {
if err := os.Chdir(cwd); err != nil {
t.Fatalf("err: %v", err)
}
2014-10-09 16:45:08 -05:00
if err := os.RemoveAll(tmp); err != nil {
t.Fatalf("err: %v", err)
}
2014-10-08 14:08:35 -05:00
}