command/output: Add regression test

Co-authored-by: Daniel Schmidt <danielmschmidt92@gmail.com>
pull/38487/head
Radek Simko 3 weeks ago
parent 1b57756ff2
commit d1ea20ac1a
No known key found for this signature in database
GPG Key ID: 1F1C84FE689A88D7

@ -13,9 +13,13 @@ import (
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/backend"
backendInit "github.com/hashicorp/terraform/internal/backend/init"
"github.com/hashicorp/terraform/internal/backend/remote-state/inmem"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/states/statefile"
"github.com/hashicorp/terraform/internal/tfdiags"
)
func TestOutput(t *testing.T) {
@ -381,3 +385,81 @@ func TestOutput_stateDefault(t *testing.T) {
t.Fatalf("bad: %#v", actual)
}
}
// deprecatedInmemBackend wraps the inmem backend and injects a deprecation
// warning from PrepareConfig, simulating a backend with deprecated attributes
// (like the S3 backend's dynamodb_table).
type deprecatedInmemBackend struct {
backend.Backend
}
func (b *deprecatedInmemBackend) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
newObj, diags := b.Backend.PrepareConfig(obj)
diags = diags.Append(tfdiags.SimpleWarning(`The attribute "deprecated_attr" is deprecated.`))
return newObj, diags
}
func TestOutputRaw_warningsSuppressed(t *testing.T) {
// Pre-populate the inmem backend with a state containing an output value
inmem.Reset()
originalState := states.BuildState(func(s *states.SyncState) {
s.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar"),
false,
)
})
// Register a backend that wraps inmem with a deprecation warning,
// simulating a backend like S3 whose PrepareConfig warns about
// deprecated attributes (e.g. dynamodb_table).
backendInit.Set("inmem", func() backend.Backend {
return &deprecatedInmemBackend{Backend: inmem.New()}
})
defer backendInit.Set("inmem", inmem.New)
td := t.TempDir()
testCopyDir(t, testFixturePath("output-backend-with-deprecation"), td)
t.Chdir(td)
// Write the state into the inmem backend's default workspace
b := inmem.New()
b.Configure(cty.ObjectVal(map[string]cty.Value{
"lock_id": cty.NullVal(cty.String),
}))
sMgr, sDiags := b.StateMgr(backend.DefaultStateName)
if sDiags.HasErrors() {
t.Fatalf("unexpected error: %s", sDiags.Err())
}
sMgr.WriteState(originalState)
if err := sMgr.PersistState(nil); err != nil {
t.Fatalf("unexpected error: %s", err)
}
view, done := testView(t)
c := &OutputCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
View: view,
},
}
args := []string{"-raw", "foo"}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("unexpected exit code %d\nstderr:\n%s", code, output.Stderr())
}
// The key assertion: warnings must not appear in raw output
// as they would be indistinguishable from the value.
stderr := output.Stderr()
if strings.Contains(stderr, "deprecated") {
t.Fatalf("warnings should be suppressed, got:\n%s", stderr)
}
actual := strings.TrimSpace(output.Stdout())
if actual != `bar` {
t.Fatalf("expected output \"bar\", got: %#v", actual)
}
}

@ -0,0 +1,12 @@
{
"version": 3,
"serial": 0,
"lineage": "test-output-deprecation",
"backend": {
"type": "inmem",
"config": {
"lock_id": null
},
"hash": 3947750061
}
}

@ -0,0 +1,7 @@
terraform {
backend "inmem" {}
}
output "foo" {
value = "bar"
}
Loading…
Cancel
Save