backport of commit 33ef3dc516

pull/38328/head
Daniel Banck 2 months ago committed by Daniel Banck
parent dea81a8272
commit bb75d4c32d

@ -15,6 +15,9 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/cli"
"github.com/hashicorp/terraform/internal/backend"
backendInit "github.com/hashicorp/terraform/internal/backend/init"
backendCloud "github.com/hashicorp/terraform/internal/cloud"
"github.com/hashicorp/terraform/internal/moduleref"
)
@ -183,6 +186,133 @@ func TestModules_uninstalledModules(t *testing.T) {
}
}
func TestModules_constVariable(t *testing.T) {
t.Run("missing value", func(t *testing.T) {
wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var")
t.Chdir(wd.RootModuleDir())
ui := cli.NewMockUi()
view, done := testView(t)
cmd := &ModulesCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
View: view,
WorkingDir: wd,
},
}
args := []string{}
code := cmd.Run(args)
if code == 0 {
t.Fatalf("expected error, got 0")
}
output := done(t).All()
if !strings.Contains(output, "No value for required variable") {
t.Fatalf("expected missing variable error, got: %s", output)
}
})
t.Run("value via cli", func(t *testing.T) {
wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var")
t.Chdir(wd.RootModuleDir())
ui := cli.NewMockUi()
view, done := testView(t)
cmd := &ModulesCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
View: view,
WorkingDir: wd,
},
}
args := []string{}
code := cmd.Run(append(args, "-var", "module_name=child"))
if code != 0 {
t.Fatalf("Got a non-zero exit code: %d\n%s", code, done(t).All())
}
actual := done(t).All()
expectedOutputHuman := `
Modules declared by configuration:
.
"child"[./modules/child]
`
if runtime.GOOS == "windows" {
expectedOutputHuman = `
Modules declared by configuration:
.
"child"[.\modules\child]
`
}
if diff := cmp.Diff(expectedOutputHuman, actual); diff != "" {
t.Fatalf("unexpected output:\n%s\n", diff)
}
})
t.Run("value via backend", func(t *testing.T) {
server := cloudTestServerWithVars(t)
defer server.Close()
d := testDisco(server)
previousBackend := backendInit.Backend("cloud")
backendInit.Set("cloud", func() backend.Backend { return backendCloud.New(d) })
defer backendInit.Set("cloud", previousBackend)
wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var-cloud-backend")
t.Chdir(wd.RootModuleDir())
ui := cli.NewMockUi()
view, done := testView(t)
cmd := &ModulesCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
View: view,
WorkingDir: wd,
Services: d,
},
}
args := []string{}
code := cmd.Run(args)
if code != 0 {
t.Fatalf("Got a non-zero exit code: %d\n%s", code, done(t).All())
}
actual := done(t).All()
expectedOutputHuman := `
Modules declared by configuration:
.
"child"[./modules/example]
`
if runtime.GOOS == "windows" {
expectedOutputHuman = `
Modules declared by configuration:
.
"child"[.\modules\example]
`
}
if diff := cmp.Diff(expectedOutputHuman, actual); diff != "" {
t.Fatalf("unexpected output:\n%s\n", diff)
}
})
}
func compareJSONOutput(t *testing.T, got string, want string) {
var expected, actual moduleref.Manifest

@ -0,0 +1,14 @@
{
"Modules": [
{
"Key": "",
"Source": "",
"Dir": "."
},
{
"Key": "child",
"Source": "./modules/example",
"Dir": "modules/example"
}
]
}

@ -0,0 +1,18 @@
{
"version": 3,
"terraform_version": "1.15.0",
"backend": {
"type": "cloud",
"config": {
"hostname": null,
"organization": "hashicorp",
"token": null,
"workspaces": {
"name": "test",
"project": null,
"tags": null
}
},
"hash": 1816475682
}
}

@ -0,0 +1,17 @@
terraform {
cloud {
organization = "hashicorp"
workspaces {
name = "test"
}
}
}
variable "module_name" {
type = string
const = true
}
module "child" {
source = "./modules/${var.module_name}"
}
Loading…
Cancel
Save