mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-29 10:21:01 -06:00
9576a5b2d8
If a configuration requires a partial provider version (with some parts unspecified), Terraform considers this as a constrained-to-zero version. For example, a version constraint of 1.2 will result in an attempt to install version 1.2.0, even if 1.2.1 is available. When writing the dependency locks file, we previously would write 1.2.*, as this is the in-memory representation of 1.2. This would then cause an error on re-reading the locks file, as this is not a valid constraint format. Instead, we now explicitly convert the constraint to its zero-filled representation before writing the locks file. This ensures that it correctly round-trips. Because this change is made in getproviders.VersionConstraintsString, it also affects the output of the providers sub-command.
167 lines
3.9 KiB
Go
167 lines
3.9 KiB
Go
package command
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestProviders(t *testing.T) {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if err := os.Chdir(testFixturePath("providers/basic")); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer os.Chdir(cwd)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
wantOutput := []string{
|
|
"provider[registry.terraform.io/hashicorp/foo]",
|
|
"provider[registry.terraform.io/hashicorp/bar]",
|
|
"provider[registry.terraform.io/hashicorp/baz]",
|
|
}
|
|
|
|
output := ui.OutputWriter.String()
|
|
for _, want := range wantOutput {
|
|
if !strings.Contains(output, want) {
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviders_noConfigs(t *testing.T) {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if err := os.Chdir(testFixturePath("")); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer os.Chdir(cwd)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code == 0 {
|
|
t.Fatal("expected command to return non-zero exit code" +
|
|
" when no configs are available")
|
|
}
|
|
|
|
output := ui.ErrorWriter.String()
|
|
expectedErrMsg := "No configuration files"
|
|
if !strings.Contains(output, expectedErrMsg) {
|
|
t.Errorf("Expected error message: %s\nGiven output: %s", expectedErrMsg, output)
|
|
}
|
|
}
|
|
|
|
func TestProviders_modules(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("providers/modules"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
// first run init with mock provider sources to install the module
|
|
initUi := new(cli.MockUi)
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
"foo": {"1.0.0"},
|
|
"bar": {"2.0.0"},
|
|
"baz": {"1.2.2"},
|
|
})
|
|
defer close()
|
|
m := Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: initUi,
|
|
ProviderSource: providerSource,
|
|
}
|
|
ic := &InitCommand{
|
|
Meta: m,
|
|
}
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
t.Fatalf("init failed\n%s", initUi.ErrorWriter)
|
|
}
|
|
|
|
// Providers command
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
wantOutput := []string{
|
|
"provider[registry.terraform.io/hashicorp/foo] 1.0.0", // from required_providers
|
|
"provider[registry.terraform.io/hashicorp/bar] 2.0.0", // from provider config
|
|
"── module.kiddo", // tree node for child module
|
|
"provider[registry.terraform.io/hashicorp/baz]", // implied by a resource in the child module
|
|
}
|
|
|
|
output := ui.OutputWriter.String()
|
|
for _, want := range wantOutput {
|
|
if !strings.Contains(output, want) {
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviders_state(t *testing.T) {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if err := os.Chdir(testFixturePath("providers/state")); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer os.Chdir(cwd)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
wantOutput := []string{
|
|
"provider[registry.terraform.io/hashicorp/foo] 1.0.0", // from required_providers
|
|
"provider[registry.terraform.io/hashicorp/bar] 2.0.0", // from a provider config block
|
|
"Providers required by state", // header for state providers
|
|
"provider[registry.terraform.io/hashicorp/baz]", // from a resouce in state (only)
|
|
}
|
|
|
|
output := ui.OutputWriter.String()
|
|
for _, want := range wantOutput {
|
|
if !strings.Contains(output, want) {
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
}
|
|
}
|
|
}
|