mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
terraform: test path variables
This commit is contained in:
parent
1e00b4386c
commit
d714c6f2f1
@ -86,7 +86,9 @@ type PathValueType byte
|
||||
|
||||
const (
|
||||
PathValueInvalid PathValueType = iota
|
||||
PathValueCwd
|
||||
PathValueModule
|
||||
PathValueRoot
|
||||
)
|
||||
|
||||
// A ResourceVariable is a variable that is referencing the field
|
||||
@ -226,8 +228,12 @@ func NewPathVariable(key string) (*PathVariable, error) {
|
||||
var fieldType PathValueType
|
||||
parts := strings.SplitN(key, ".", 2)
|
||||
switch parts[1] {
|
||||
case "cwd":
|
||||
fieldType = PathValueCwd
|
||||
case "module":
|
||||
fieldType = PathValueModule
|
||||
case "root":
|
||||
fieldType = PathValueRoot
|
||||
}
|
||||
|
||||
return &PathVariable{
|
||||
|
@ -3,6 +3,7 @@ package terraform
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -54,7 +55,7 @@ type ContextOpts struct {
|
||||
Provisioners map[string]ResourceProvisionerFactory
|
||||
Variables map[string]string
|
||||
|
||||
UIInput UIInput
|
||||
UIInput UIInput
|
||||
}
|
||||
|
||||
// NewContext creates a new context.
|
||||
@ -1437,6 +1438,24 @@ func (c *walkContext) computeVars(
|
||||
}
|
||||
|
||||
vs[n] = value
|
||||
case *config.PathVariable:
|
||||
switch v.Type {
|
||||
case config.PathValueCwd:
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
"Couldn't get cwd for var %s: %s",
|
||||
v.FullKey(), err)
|
||||
}
|
||||
|
||||
vs[n] = wd
|
||||
case config.PathValueModule:
|
||||
if t := c.Context.module.Child(c.Path[1:]); t != nil {
|
||||
vs[n] = t.Config().Dir
|
||||
}
|
||||
case config.PathValueRoot:
|
||||
vs[n] = c.Context.module.Config().Dir
|
||||
}
|
||||
case *config.ResourceVariable:
|
||||
var attr string
|
||||
var err error
|
||||
|
@ -2,6 +2,7 @@ package terraform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
@ -2822,6 +2823,43 @@ func TestContextPlan_moduleDestroy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextPlan_pathVar(t *testing.T) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
m := testModule(t, "plan-path-var")
|
||||
p := testProvider("aws")
|
||||
p.DiffFn = testDiffFn
|
||||
ctx := testContext(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
plan, err := ctx.Plan(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(plan.String())
|
||||
expected := strings.TrimSpace(testTerraformPlanPathVarStr)
|
||||
|
||||
// Warning: this ordering REALLY matters for this test. The
|
||||
// order is: cwd, module, root.
|
||||
expected = fmt.Sprintf(
|
||||
expected,
|
||||
cwd,
|
||||
m.Config().Dir,
|
||||
m.Config().Dir)
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextPlan_diffVar(t *testing.T) {
|
||||
m := testModule(t, "plan-diffvar")
|
||||
p := testProvider("aws")
|
||||
|
@ -875,3 +875,18 @@ STATE:
|
||||
|
||||
<no state>
|
||||
`
|
||||
|
||||
const testTerraformPlanPathVarStr = `
|
||||
DIFF:
|
||||
|
||||
CREATE: aws_instance.foo
|
||||
cwd: "" => "%s/barpath"
|
||||
module: "" => "%s/foopath"
|
||||
root: "" => "%s/barpath"
|
||||
type: "" => "aws_instance"
|
||||
|
||||
STATE:
|
||||
|
||||
<no state>
|
||||
`
|
||||
|
||||
|
5
terraform/test-fixtures/plan-path-var/main.tf
Normal file
5
terraform/test-fixtures/plan-path-var/main.tf
Normal file
@ -0,0 +1,5 @@
|
||||
resource "aws_instance" "foo" {
|
||||
cwd = "${path.cwd}/barpath"
|
||||
module = "${path.module}/foopath"
|
||||
root = "${path.root}/barpath"
|
||||
}
|
Loading…
Reference in New Issue
Block a user