2017-01-19 05:50:45 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-10-18 21:41:05 +02:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-01-19 05:50:45 +01:00
|
|
|
"testing"
|
|
|
|
|
2018-10-18 21:41:05 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatePull(t *testing.T) {
|
2018-10-18 21:41:05 +02:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("state-pull-backend"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2018-10-18 21:41:05 +02:00
|
|
|
expected, err := ioutil.ReadFile("local-state.tfstate")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading state: %v", err)
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StatePullCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:41:05 +02:00
|
|
|
actual := ui.OutputWriter.Bytes()
|
|
|
|
if bytes.Equal(actual, expected) {
|
2017-01-19 05:50:45 +01:00
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 21:47:36 +01:00
|
|
|
|
|
|
|
func TestStatePull_noState(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-11-14 01:48:59 +01:00
|
|
|
ui := cli.NewMockUi()
|
2017-03-01 21:47:36 +01:00
|
|
|
c := &StatePullCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-03-01 21:47:36 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != "" {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|