github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/command/state_pull_test.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/copy"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestStatePull(t *testing.T) {
    14  	// Create a temporary working directory that is empty
    15  	td := tempDir(t)
    16  	copy.CopyDir(testFixturePath("state-pull-backend"), td)
    17  	defer os.RemoveAll(td)
    18  	defer testChdir(t, td)()
    19  
    20  	expected, err := ioutil.ReadFile("local-state.tfstate")
    21  	if err != nil {
    22  		t.Fatalf("error reading state: %v", err)
    23  	}
    24  
    25  	p := testProvider()
    26  	ui := new(cli.MockUi)
    27  	c := &StatePullCommand{
    28  		Meta: Meta{
    29  			testingOverrides: metaOverridesForProvider(p),
    30  			Ui:               ui,
    31  		},
    32  	}
    33  
    34  	args := []string{}
    35  	if code := c.Run(args); code != 0 {
    36  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    37  	}
    38  
    39  	actual := ui.OutputWriter.Bytes()
    40  	if bytes.Equal(actual, expected) {
    41  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
    42  	}
    43  }
    44  
    45  func TestStatePull_noState(t *testing.T) {
    46  	tmp, cwd := testCwd(t)
    47  	defer testFixCwd(t, tmp, cwd)
    48  
    49  	p := testProvider()
    50  	ui := cli.NewMockUi()
    51  	c := &StatePullCommand{
    52  		Meta: Meta{
    53  			testingOverrides: metaOverridesForProvider(p),
    54  			Ui:               ui,
    55  		},
    56  	}
    57  
    58  	args := []string{}
    59  	if code := c.Run(args); code != 0 {
    60  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    61  	}
    62  
    63  	actual := ui.OutputWriter.String()
    64  	if actual != "" {
    65  		t.Fatalf("bad: %s", actual)
    66  	}
    67  }