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