2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-10-18 14:41:05 -05:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2022-03-31 12:42:42 -05:00
|
|
|
"strings"
|
2017-01-18 22:50:45 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatePull(t *testing.T) {
|
2018-10-18 14:41:05 -05:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2020-10-07 11:48:25 -05:00
|
|
|
testCopyDir(t, testFixturePath("state-pull-backend"), td)
|
2018-10-18 14:41:05 -05:00
|
|
|
defer testChdir(t, td)()
|
2017-01-18 22:50:45 -06:00
|
|
|
|
2018-10-18 14:41:05 -05:00
|
|
|
expected, err := ioutil.ReadFile("local-state.tfstate")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading state: %v", err)
|
|
|
|
}
|
2017-01-18 22:50:45 -06:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StatePullCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-01-18 22:50:45 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-10-18 14:41:05 -05:00
|
|
|
actual := ui.OutputWriter.Bytes()
|
|
|
|
if bytes.Equal(actual, expected) {
|
2017-01-18 22:50:45 -06:00
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 14:47:36 -06:00
|
|
|
|
|
|
|
func TestStatePull_noState(t *testing.T) {
|
2022-04-08 11:34:16 -05:00
|
|
|
testCwd(t)
|
2017-03-01 14:47:36 -06:00
|
|
|
|
|
|
|
p := testProvider()
|
2018-11-13 18:48:59 -06:00
|
|
|
ui := cli.NewMockUi()
|
2017-03-01 14:47:36 -06:00
|
|
|
c := &StatePullCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-03-01 14:47:36 -06: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)
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 12:42:42 -05:00
|
|
|
|
|
|
|
func TestStatePull_checkRequiredVersion(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2022-03-31 12:42:42 -05:00
|
|
|
testCopyDir(t, testFixturePath("command-check-required-version"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := &StatePullCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Required version diags are correct
|
|
|
|
errStr := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errStr, `required_version = "~> 0.9.0"`) {
|
|
|
|
t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr)
|
|
|
|
}
|
|
|
|
if strings.Contains(errStr, `required_version = ">= 0.13.0"`) {
|
|
|
|
t.Fatalf("output should not point to met version constraint, but is:\n\n%s", errStr)
|
|
|
|
}
|
|
|
|
}
|