Fix experimental gating of PSS feature (#37526)

* Stop `state_store` being parsed when experiments aren't enabled

* Update tests that show the feature is experimentally gated

* Refactor to use unparsed hcl.Block for diagnostics when experiments aren't enabled
pull/37520/head
Sarah French 8 months ago committed by GitHub
parent d39fcc4f0b
commit 5be24bf1d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,104 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"strings"
"testing"
"github.com/hashicorp/cli"
)
func TestInit_stateStoreBlockIsExperimental(t *testing.T) {
t.Run("init command", func(t *testing.T) {
// Create a temporary working directory with state_store in use
td := t.TempDir()
testCopyDir(t, testFixturePath("init-with-state-store"), td)
t.Chdir(td)
ui := new(cli.MockUi)
view, done := testView(t)
c := &InitCommand{
Meta: Meta{
Ui: ui,
View: view,
AllowExperimentalFeatures: false,
},
}
args := []string{}
code := c.Run(args)
testOutput := done(t)
if code != 1 {
t.Fatalf("unexpected output: \n%s", testOutput.All())
}
// Check output
output := testOutput.Stderr()
if !strings.Contains(output, `Blocks of type "state_store" are not expected here`) {
t.Fatalf("doesn't look like experiment is blocking access': %s", output)
}
})
t.Run("non-init command: plan", func(t *testing.T) {
// Create a temporary working directory with state_store in use
td := t.TempDir()
testCopyDir(t, testFixturePath("init-with-state-store"), td)
t.Chdir(td)
ui := new(cli.MockUi)
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
Ui: ui,
View: view,
AllowExperimentalFeatures: false,
},
}
args := []string{}
code := c.Run(args)
testOutput := done(t)
if code != 1 {
t.Fatalf("unexpected output: \n%s", testOutput.All())
}
// Check output
output := testOutput.Stderr()
if !strings.Contains(output, `Blocks of type "state_store" are not expected here`) {
t.Fatalf("doesn't look like experiment is blocking access': %s", output)
}
})
t.Run("non-init command: state list", func(t *testing.T) {
// Create a temporary working directory with state_store in use
td := t.TempDir()
testCopyDir(t, testFixturePath("init-with-state-store"), td)
t.Chdir(td)
ui := new(cli.MockUi)
view, done := testView(t)
c := &StateListCommand{
Meta: Meta{
Ui: ui,
View: view,
AllowExperimentalFeatures: false,
},
}
args := []string{}
code := c.Run(args)
testOutput := done(t)
if code != 1 {
t.Fatalf("unexpected output: \n%s", testOutput.All())
}
// Check output
output := ui.ErrorWriter.String()
if !strings.Contains(output, `Blocks of type "state_store" are not expected here`) {
t.Fatalf("doesn't look like experiment is blocking access': %s", output)
}
})
}

@ -3227,36 +3227,6 @@ func TestInit_testsWithModule(t *testing.T) {
}
}
func TestInit_stateStoreBlockIsExperimental(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("init-with-state-store"), td)
t.Chdir(td)
ui := new(cli.MockUi)
view, done := testView(t)
c := &InitCommand{
Meta: Meta{
Ui: ui,
View: view,
AllowExperimentalFeatures: false,
},
}
args := []string{}
code := c.Run(args)
testOutput := done(t)
if code != 1 {
t.Fatalf("unexpected output: \n%s", testOutput.All())
}
// Check output
output := testOutput.Stderr()
if !strings.Contains(output, `Blocks of type "state_store" are not expected here`) {
t.Fatalf("doesn't look like experiment is blocking access': %s", output)
}
}
// newMockProviderSource is a helper to succinctly construct a mock provider
// source that contains a set of packages matching the given provider versions
// that are available for installation (from temporary local files).

@ -122,10 +122,20 @@ func parseConfigFile(body hcl.Body, diags hcl.Diagnostics, override, allowExperi
}
case "state_store":
stateStoreCfg, cfgDiags := decodeStateStoreBlock(innerBlock)
diags = append(diags, cfgDiags...)
if stateStoreCfg != nil {
file.StateStores = append(file.StateStores, stateStoreCfg)
if allowExperiments {
stateStoreCfg, cfgDiags := decodeStateStoreBlock(innerBlock)
diags = append(diags, cfgDiags...)
if stateStoreCfg != nil {
file.StateStores = append(file.StateStores, stateStoreCfg)
}
} else {
// Prevent parsing of state_store blocks in all commands unless experiments enabled.
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported block type",
Detail: "Blocks of type \"state_store\" are not expected here.",
Subject: &innerBlock.TypeRange,
})
}
case "cloud":
cloudCfg, cfgDiags := decodeCloudBlock(innerBlock)

Loading…
Cancel
Save