2024-02-08 04:48:59 -05:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 11:33:06 -04:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2018-10-18 15:41:05 -04:00
|
|
|
"bytes"
|
2023-09-07 12:53:12 -04:00
|
|
|
"os"
|
2022-03-31 13:42:42 -04:00
|
|
|
"strings"
|
2017-01-18 23:50:45 -05:00
|
|
|
"testing"
|
|
|
|
|
|
2026-02-10 08:31:06 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/workdir"
|
2017-01-18 23:50:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestStatePull(t *testing.T) {
|
2018-10-18 15:41:05 -04:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2020-10-07 12:48:25 -04:00
|
|
|
testCopyDir(t, testFixturePath("state-pull-backend"), td)
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-01-18 23:50:45 -05:00
|
|
|
|
2023-09-07 12:53:12 -04:00
|
|
|
expected, err := os.ReadFile("local-state.tfstate")
|
2018-10-18 15:41:05 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("error reading state: %v", err)
|
|
|
|
|
}
|
2017-01-18 23:50:45 -05:00
|
|
|
|
|
|
|
|
p := testProvider()
|
2026-03-09 06:25:18 -04:00
|
|
|
view, done := testView(t)
|
2017-01-18 23:50:45 -05:00
|
|
|
c := &StatePullCommand{
|
|
|
|
|
Meta: Meta{
|
2026-02-10 08:31:06 -05:00
|
|
|
WorkingDir: workdir.NewDir("."),
|
2017-04-13 21:05:58 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2026-03-09 06:25:18 -04:00
|
|
|
View: view,
|
2017-01-18 23:50:45 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{}
|
2026-03-09 06:25:18 -04:00
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.All())
|
2017-01-18 23:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 06:25:18 -04:00
|
|
|
actual := []byte(output.Stdout())
|
2018-10-18 15:41:05 -04:00
|
|
|
if bytes.Equal(actual, expected) {
|
2017-01-18 23:50:45 -05:00
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-01 15:47:36 -05:00
|
|
|
|
|
|
|
|
func TestStatePull_noState(t *testing.T) {
|
2025-04-23 07:48:41 -04:00
|
|
|
testCwdTemp(t)
|
2017-03-01 15:47:36 -05:00
|
|
|
|
|
|
|
|
p := testProvider()
|
2026-03-09 06:25:18 -04:00
|
|
|
view, done := testView(t)
|
2017-03-01 15:47:36 -05:00
|
|
|
c := &StatePullCommand{
|
|
|
|
|
Meta: Meta{
|
2026-02-10 08:31:06 -05:00
|
|
|
WorkingDir: workdir.NewDir("."),
|
2017-04-13 21:05:58 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2026-03-09 06:25:18 -04:00
|
|
|
View: view,
|
2017-03-01 15:47:36 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{}
|
2026-03-09 06:25:18 -04:00
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.All())
|
2017-03-01 15:47:36 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 06:25:18 -04:00
|
|
|
actual := output.Stdout()
|
2017-03-01 15:47:36 -05:00
|
|
|
if actual != "" {
|
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-31 13:42:42 -04:00
|
|
|
|
|
|
|
|
func TestStatePull_checkRequiredVersion(t *testing.T) {
|
|
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2022-03-31 13:42:42 -04:00
|
|
|
testCopyDir(t, testFixturePath("command-check-required-version"), td)
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2022-03-31 13:42:42 -04:00
|
|
|
|
|
|
|
|
p := testProvider()
|
2026-03-09 06:25:18 -04:00
|
|
|
view, done := testView(t)
|
2022-03-31 13:42:42 -04:00
|
|
|
c := &StatePullCommand{
|
|
|
|
|
Meta: Meta{
|
2026-02-10 08:31:06 -05:00
|
|
|
WorkingDir: workdir.NewDir("."),
|
2022-03-31 13:42:42 -04:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2026-03-09 06:25:18 -04:00
|
|
|
View: view,
|
2022-03-31 13:42:42 -04:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{}
|
2026-03-09 06:25:18 -04:00
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code != 1 {
|
|
|
|
|
t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, output.Stderr(), output.Stdout())
|
2022-03-31 13:42:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Required version diags are correct
|
2026-03-09 06:25:18 -04:00
|
|
|
errStr := output.Stderr()
|
2022-03-31 13:42:42 -04:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|