mirror of https://github.com/hashicorp/terraform
parent
b62ff0412f
commit
82af81b606
@ -0,0 +1,89 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func TestApply(t *testing.T) {
|
||||
tf, err := ioutil.TempFile("", "tf")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
statePath := tf.Name()
|
||||
tf.Close()
|
||||
os.Remove(tf.Name())
|
||||
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &ApplyCommand{
|
||||
TFConfig: testTFConfig(p),
|
||||
Ui: ui,
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
testFixturePath("apply"),
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(statePath); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
f, err := os.Open(statePath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
state, err := terraform.ReadState(f)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if state == nil {
|
||||
t.Fatal("state should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApply_noState(t *testing.T) {
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &ApplyCommand{
|
||||
TFConfig: testTFConfig(p),
|
||||
Ui: ui,
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state=",
|
||||
testFixturePath("apply"),
|
||||
}
|
||||
if code := c.Run(args); code != 1 {
|
||||
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestApply_stateNoExist(t *testing.T) {
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &ApplyCommand{
|
||||
TFConfig: testTFConfig(p),
|
||||
Ui: ui,
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state=idontexist.tfstate",
|
||||
testFixturePath("apply"),
|
||||
}
|
||||
// TODO
|
||||
return
|
||||
if code := c.Run(args); code != 1 {
|
||||
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
// This is the directory where our test fixtures are.
|
||||
const fixtureDir = "./test-fixtures"
|
||||
|
||||
func testFixturePath(name string) string {
|
||||
return filepath.Join(fixtureDir, name, "main.tf")
|
||||
}
|
||||
|
||||
func testTFConfig(p terraform.ResourceProvider) *terraform.Config {
|
||||
return &terraform.Config{
|
||||
Providers: map[string]terraform.ResourceProviderFactory{
|
||||
"test": func() (terraform.ResourceProvider, error) {
|
||||
return p, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func testProvider() *terraform.MockResourceProvider {
|
||||
p := new(terraform.MockResourceProvider)
|
||||
p.ResourcesReturn = []terraform.ResourceType{
|
||||
terraform.ResourceType{
|
||||
Name: "test_instance",
|
||||
},
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
resource "test_instance" "foo" {
|
||||
ami = "bar"
|
||||
}
|
||||
Loading…
Reference in new issue