github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/command/state_pull_test.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"strings"
     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 := t.TempDir()
    15  	testCopyDir(t, testFixturePath("state-pull-backend"), td)
    16  	defer testChdir(t, td)()
    17  
    18  	expected, err := ioutil.ReadFile("local-state.tfstate")
    19  	if err != nil {
    20  		t.Fatalf("error reading state: %v", err)
    21  	}
    22  
    23  	p := testProvider()
    24  	ui := new(cli.MockUi)
    25  	c := &StatePullCommand{
    26  		Meta: Meta{
    27  			testingOverrides: metaOverridesForProvider(p),
    28  			Ui:               ui,
    29  		},
    30  	}
    31  
    32  	args := []string{}
    33  	if code := c.Run(args); code != 0 {
    34  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    35  	}
    36  
    37  	actual := ui.OutputWriter.Bytes()
    38  	if bytes.Equal(actual, expected) {
    39  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
    40  	}
    41  }
    42  
    43  func TestStatePull_noState(t *testing.T) {
    44  	testCwd(t)
    45  
    46  	p := testProvider()
    47  	ui := cli.NewMockUi()
    48  	c := &StatePullCommand{
    49  		Meta: Meta{
    50  			testingOverrides: metaOverridesForProvider(p),
    51  			Ui:               ui,
    52  		},
    53  	}
    54  
    55  	args := []string{}
    56  	if code := c.Run(args); code != 0 {
    57  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    58  	}
    59  
    60  	actual := ui.OutputWriter.String()
    61  	if actual != "" {
    62  		t.Fatalf("bad: %s", actual)
    63  	}
    64  }
    65  
    66  func TestStatePull_checkRequiredVersion(t *testing.T) {
    67  	// Create a temporary working directory that is empty
    68  	td := t.TempDir()
    69  	testCopyDir(t, testFixturePath("command-check-required-version"), td)
    70  	defer testChdir(t, td)()
    71  
    72  	p := testProvider()
    73  	ui := cli.NewMockUi()
    74  	c := &StatePullCommand{
    75  		Meta: Meta{
    76  			testingOverrides: metaOverridesForProvider(p),
    77  			Ui:               ui,
    78  		},
    79  	}
    80  
    81  	args := []string{}
    82  	if code := c.Run(args); code != 1 {
    83  		t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
    84  	}
    85  
    86  	// Required version diags are correct
    87  	errStr := ui.ErrorWriter.String()
    88  	if !strings.Contains(errStr, `required_version = "~> 0.9.0"`) {
    89  		t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr)
    90  	}
    91  	if strings.Contains(errStr, `required_version = ">= 0.13.0"`) {
    92  		t.Fatalf("output should not point to met version constraint, but is:\n\n%s", errStr)
    93  	}
    94  }