github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/state_pull_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "bytes" 8 "io/ioutil" 9 "strings" 10 "testing" 11 12 "github.com/mitchellh/cli" 13 ) 14 15 func TestStatePull(t *testing.T) { 16 // Create a temporary working directory that is empty 17 td := t.TempDir() 18 testCopyDir(t, testFixturePath("state-pull-backend"), td) 19 defer testChdir(t, td)() 20 21 expected, err := ioutil.ReadFile("local-state.tfstate") 22 if err != nil { 23 t.Fatalf("error reading state: %v", err) 24 } 25 26 p := testProvider() 27 ui := new(cli.MockUi) 28 c := &StatePullCommand{ 29 Meta: Meta{ 30 testingOverrides: metaOverridesForProvider(p), 31 Ui: ui, 32 }, 33 } 34 35 args := []string{} 36 if code := c.Run(args); code != 0 { 37 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 38 } 39 40 actual := ui.OutputWriter.Bytes() 41 if bytes.Equal(actual, expected) { 42 t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected) 43 } 44 } 45 46 func TestStatePull_noState(t *testing.T) { 47 testCwd(t) 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 } 68 69 func TestStatePull_checkRequiredVersion(t *testing.T) { 70 // Create a temporary working directory that is empty 71 td := t.TempDir() 72 testCopyDir(t, testFixturePath("command-check-required-version"), td) 73 defer testChdir(t, td)() 74 75 p := testProvider() 76 ui := cli.NewMockUi() 77 c := &StatePullCommand{ 78 Meta: Meta{ 79 testingOverrides: metaOverridesForProvider(p), 80 Ui: ui, 81 }, 82 } 83 84 args := []string{} 85 if code := c.Run(args); code != 1 { 86 t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String()) 87 } 88 89 // Required version diags are correct 90 errStr := ui.ErrorWriter.String() 91 if !strings.Contains(errStr, `required_version = "~> 0.9.0"`) { 92 t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr) 93 } 94 if strings.Contains(errStr, `required_version = ">= 0.13.0"`) { 95 t.Fatalf("output should not point to met version constraint, but is:\n\n%s", errStr) 96 } 97 }