mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
We originally introduced the idea of language experiments as a way to get early feedback on not-yet-proven feature ideas, ideally as part of the initial exploration of the solution space rather than only after a solution has become relatively clear. Unfortunately, our tradeoff of making them available in normal releases behind an explicit opt-in in order to make it easier to participate in the feedback process had the unintended side-effect of making it feel okay to use experiments in production and endure the warnings they generate. This in turn has made us reluctant to make use of the experiments feature lest experiments become de-facto production features which we then feel compelled to preserve even though we aren't yet ready to graduate them to stable features. In an attempt to tweak that compromise, here we make the availability of experiments _at all_ a build-time flag which will not be set by default, and therefore experiments will not be available in most release builds. The intent (not yet implemented in this PR) is for our release process to set this flag only when it knows it's building an alpha release or a development snapshot not destined for release at all, which will therefore allow us to still use the alpha releases as a vehicle for giving feedback participants access to a feature (without needing to install a Go toolchain) but will not encourage pretending that these features are production-ready before they graduate from experimental. Only language experiments have an explicit framework for dealing with them which outlives any particular experiment, so most of the changes here are to that generalized mechanism. However, the intent is that non-language experiments, such as experimental CLI commands, would also in future check Meta.AllowExperimentalFeatures and gate the use of those experiments too, so that we can be consistent that experimental features will never be available unless you explicitly choose to use an alpha release or a custom build from source code. Since there are already some experiments active at the time of this commit which were not previously subject to this restriction, we'll pragmatically leave those as exceptions that will remain generally available for now, and so this new approach will apply only to new experiments started in the future. Once those experiments have all concluded, we will be left with no more exceptions unless we explicitly choose to make an exception for some reason we've not imagined yet. It's important that we be able to write tests that rely on experiments either being available or not being available, so here we're using our typical approach of making "package main" deal with the global setting that applies to Terraform CLI executables while making the layers below all support fine-grain selection of this behavior so that tests with different needs can run concurrently without trampling on one another. As a compromise, the integration tests in the terraform package will run with experiments enabled _by default_ since we commonly need to exercise experiments in those tests, but they can selectively opt-out if they need to by overriding the loader setting back to false again.
140 lines
5.1 KiB
Go
140 lines
5.1 KiB
Go
package configs
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/terraform/internal/experiments"
|
|
)
|
|
|
|
func TestExperimentsConfig(t *testing.T) {
|
|
// The experiment registrations are global, so we need to do some special
|
|
// patching in order to get a predictable set for our tests.
|
|
current := experiments.Experiment("current")
|
|
concluded := experiments.Experiment("concluded")
|
|
currentExperiments := experiments.NewSet(current)
|
|
concludedExperiments := map[experiments.Experiment]string{
|
|
concluded: "Reticulate your splines.",
|
|
}
|
|
defer experiments.OverrideForTesting(t, currentExperiments, concludedExperiments)()
|
|
|
|
t.Run("current", func(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
parser.AllowLanguageExperiments(true)
|
|
mod, diags := parser.LoadConfigDir("testdata/experiments/current")
|
|
if got, want := len(diags), 1; got != want {
|
|
t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
|
|
}
|
|
got := diags[0]
|
|
want := &hcl.Diagnostic{
|
|
Severity: hcl.DiagWarning,
|
|
Summary: `Experimental feature "current" is active`,
|
|
Detail: "Experimental features are available only in alpha releases of Terraform and are subject to breaking changes or total removal in later versions, based on feedback. We recommend against using experimental features in production.\n\nIf you have feedback on the design of this feature, please open a GitHub issue to discuss it.",
|
|
Subject: &hcl.Range{
|
|
Filename: "testdata/experiments/current/current_experiment.tf",
|
|
Start: hcl.Pos{Line: 2, Column: 18, Byte: 29},
|
|
End: hcl.Pos{Line: 2, Column: 25, Byte: 36},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong warning\n%s", diff)
|
|
}
|
|
if got, want := len(mod.ActiveExperiments), 1; got != want {
|
|
t.Errorf("wrong number of experiments %d; want %d", got, want)
|
|
}
|
|
if !mod.ActiveExperiments.Has(current) {
|
|
t.Errorf("module does not indicate current experiment as active")
|
|
}
|
|
})
|
|
t.Run("concluded", func(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
parser.AllowLanguageExperiments(true)
|
|
_, diags := parser.LoadConfigDir("testdata/experiments/concluded")
|
|
if got, want := len(diags), 1; got != want {
|
|
t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
|
|
}
|
|
got := diags[0]
|
|
want := &hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: `Experiment has concluded`,
|
|
Detail: `Experiment "concluded" is no longer available. Reticulate your splines.`,
|
|
Subject: &hcl.Range{
|
|
Filename: "testdata/experiments/concluded/concluded_experiment.tf",
|
|
Start: hcl.Pos{Line: 2, Column: 18, Byte: 29},
|
|
End: hcl.Pos{Line: 2, Column: 27, Byte: 38},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong error\n%s", diff)
|
|
}
|
|
})
|
|
t.Run("concluded", func(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
parser.AllowLanguageExperiments(true)
|
|
_, diags := parser.LoadConfigDir("testdata/experiments/unknown")
|
|
if got, want := len(diags), 1; got != want {
|
|
t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
|
|
}
|
|
got := diags[0]
|
|
want := &hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: `Unknown experiment keyword`,
|
|
Detail: `There is no current experiment with the keyword "unknown".`,
|
|
Subject: &hcl.Range{
|
|
Filename: "testdata/experiments/unknown/unknown_experiment.tf",
|
|
Start: hcl.Pos{Line: 2, Column: 18, Byte: 29},
|
|
End: hcl.Pos{Line: 2, Column: 25, Byte: 36},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong error\n%s", diff)
|
|
}
|
|
})
|
|
t.Run("invalid", func(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
parser.AllowLanguageExperiments(true)
|
|
_, diags := parser.LoadConfigDir("testdata/experiments/invalid")
|
|
if got, want := len(diags), 1; got != want {
|
|
t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
|
|
}
|
|
got := diags[0]
|
|
want := &hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: `Invalid expression`,
|
|
Detail: `A static list expression is required.`,
|
|
Subject: &hcl.Range{
|
|
Filename: "testdata/experiments/invalid/invalid_experiments.tf",
|
|
Start: hcl.Pos{Line: 2, Column: 17, Byte: 28},
|
|
End: hcl.Pos{Line: 2, Column: 24, Byte: 35},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong error\n%s", diff)
|
|
}
|
|
})
|
|
t.Run("disallowed", func(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
parser.AllowLanguageExperiments(false) // The default situation for release builds
|
|
_, diags := parser.LoadConfigDir("testdata/experiments/current")
|
|
if got, want := len(diags), 1; got != want {
|
|
t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
|
|
}
|
|
got := diags[0]
|
|
want := &hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: `Module uses experimental features`,
|
|
Detail: `Experimental features are intended only for gathering early feedback on new language designs, and so are available only in alpha releases of Terraform.`,
|
|
Subject: &hcl.Range{
|
|
Filename: "testdata/experiments/current/current_experiment.tf",
|
|
Start: hcl.Pos{Line: 2, Column: 3, Byte: 14},
|
|
End: hcl.Pos{Line: 2, Column: 14, Byte: 25},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong error\n%s", diff)
|
|
}
|
|
})
|
|
}
|