2014-06-19 06:36:44 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-06-27 23:43:23 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-06-19 06:36:44 +02:00
|
|
|
"path/filepath"
|
2014-06-27 23:43:23 +02:00
|
|
|
"testing"
|
2014-06-19 06:36:44 +02:00
|
|
|
|
2014-09-25 00:48:46 +02:00
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2014-06-19 06:36:44 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is the directory where our test fixtures are.
|
2014-07-12 06:03:56 +02:00
|
|
|
var fixtureDir = "./test-fixtures"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// 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-19 06:36:44 +02:00
|
|
|
|
2014-09-25 00:48:46 +02: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-19 06:36:44 +02:00
|
|
|
func testFixturePath(name string) string {
|
2014-07-12 05:38:03 +02:00
|
|
|
return filepath.Join(fixtureDir, name)
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 20:46:40 +02:00
|
|
|
func testCtxConfig(p terraform.ResourceProvider) *terraform.ContextOpts {
|
|
|
|
return &terraform.ContextOpts{
|
2014-06-19 06:36:44 +02:00
|
|
|
Providers: map[string]terraform.ResourceProviderFactory{
|
|
|
|
"test": func() (terraform.ResourceProvider, error) {
|
|
|
|
return p, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 00:48:46 +02: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 := &module.FolderStorage{StorageDir: tempDir(t)}
|
|
|
|
if err := mod.Load(s, module.GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:43:23 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:12:05 +02:00
|
|
|
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 20:15:07 +02: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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:43:23 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-06-19 06:36:44 +02:00
|
|
|
func testProvider() *terraform.MockResourceProvider {
|
|
|
|
p := new(terraform.MockResourceProvider)
|
2014-09-18 01:33:24 +02:00
|
|
|
p.DiffReturn = &terraform.InstanceDiff{}
|
2014-06-20 21:51:42 +02:00
|
|
|
p.RefreshFn = func(
|
2014-09-17 20:15:07 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState) (*terraform.InstanceState, error) {
|
2014-06-20 21:51:42 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
2014-06-19 06:36:44 +02:00
|
|
|
p.ResourcesReturn = []terraform.ResourceType{
|
|
|
|
terraform.ResourceType{
|
|
|
|
Name: "test_instance",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
2014-06-27 23:43:23 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2014-08-05 18:32:01 +02:00
|
|
|
|
|
|
|
func testTempDir(t *testing.T) string {
|
|
|
|
d, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|