terraform/command/plan_test.go

108 lines
2.1 KiB
Go
Raw Normal View History

2014-06-19 22:51:05 +02:00
package command
import (
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
2014-06-20 20:47:02 +02:00
func TestPlan_noState(t *testing.T) {
2014-06-19 22:51:05 +02:00
p := testProvider()
ui := new(cli.MockUi)
2014-06-20 20:47:02 +02:00
c := &PlanCommand{
2014-06-19 22:51:05 +02:00
TFConfig: testTFConfig(p),
Ui: ui,
}
args := []string{
2014-06-20 20:47:02 +02:00
testFixturePath("plan"),
2014-06-19 22:51:05 +02:00
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
2014-06-26 18:56:29 +02:00
// Verify that refresh was called
if !p.RefreshCalled {
t.Fatal("refresh should be called")
}
2014-06-19 22:51:05 +02:00
// Verify that the provider was called with the existing state
expectedState := &terraform.ResourceState{
Type: "test_instance",
}
if !reflect.DeepEqual(p.DiffState, expectedState) {
t.Fatalf("bad: %#v", p.DiffState)
}
}
2014-06-19 22:51:28 +02:00
2014-06-26 18:56:29 +02:00
func TestPlan_refresh(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
TFConfig: testTFConfig(p),
Ui: ui,
}
args := []string{
"-refresh=false",
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if p.RefreshCalled {
t.Fatal("refresh should not be called")
}
}
2014-06-20 20:47:02 +02:00
func TestPlan_state(t *testing.T) {
2014-06-19 22:51:05 +02:00
// Write out some prior state
tf, err := ioutil.TempFile("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := tf.Name()
defer os.Remove(tf.Name())
originalState := &terraform.State{
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
ID: "bar",
Type: "test_instance",
},
},
}
err = terraform.WriteState(originalState, tf)
tf.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
p := testProvider()
ui := new(cli.MockUi)
2014-06-20 20:47:02 +02:00
c := &PlanCommand{
2014-06-19 22:51:05 +02:00
TFConfig: testTFConfig(p),
Ui: ui,
}
args := []string{
"-state", statePath,
2014-06-20 20:47:02 +02:00
testFixturePath("plan"),
2014-06-19 22:51:05 +02:00
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
// Verify that the provider was called with the existing state
expectedState := originalState.Resources["test_instance.foo"]
if !reflect.DeepEqual(p.DiffState, expectedState) {
t.Fatalf("bad: %#v", p.DiffState)
}
}