mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
core: Update tests that use the apply-vars fixture
These now need to include a suitable schema in order to properly process the configuration.
This commit is contained in:
parent
a9a9cdaa15
commit
fc7453c073
@ -8336,106 +8336,102 @@ func TestContext2Apply_unknownAttributeInterpolate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestContext2Apply_vars(t *testing.T) {
|
func TestContext2Apply_vars(t *testing.T) {
|
||||||
m := testModule(t, "apply-vars")
|
fixture := contextFixtureApplyVars(t)
|
||||||
p := testProvider("aws")
|
opts := fixture.ContextOpts()
|
||||||
p.ApplyFn = testApplyFn
|
opts.Variables = InputValues{
|
||||||
p.DiffFn = testDiffFn
|
"foo": &InputValue{
|
||||||
ctx := testContext2(t, &ContextOpts{
|
Value: cty.StringVal("us-east-1"),
|
||||||
Config: m,
|
SourceType: ValueFromCaller,
|
||||||
ProviderResolver: ResourceProviderResolverFixed(
|
|
||||||
map[string]ResourceProviderFactory{
|
|
||||||
"aws": testProviderFuncFixed(p),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Variables: InputValues{
|
|
||||||
"foo": &InputValue{
|
|
||||||
Value: cty.StringVal("us-west-2"),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
"test_list": &InputValue{
|
|
||||||
Value: cty.ListVal([]cty.Value{
|
|
||||||
cty.StringVal("Hello"),
|
|
||||||
cty.StringVal("World"),
|
|
||||||
}),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
"test_map": &InputValue{
|
|
||||||
Value: cty.MapVal(map[string]cty.Value{
|
|
||||||
"Hello": cty.StringVal("World"),
|
|
||||||
"Foo": cty.StringVal("Bar"),
|
|
||||||
"Baz": cty.StringVal("Foo"),
|
|
||||||
}),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
"amis": &InputValue{
|
|
||||||
Value: cty.ListVal([]cty.Value{
|
|
||||||
cty.MapVal(map[string]cty.Value{
|
|
||||||
"us-west-1": cty.StringVal("override"),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
"test_list": &InputValue{
|
||||||
|
Value: cty.ListVal([]cty.Value{
|
||||||
|
cty.StringVal("Hello"),
|
||||||
|
cty.StringVal("World"),
|
||||||
|
}),
|
||||||
|
SourceType: ValueFromCaller,
|
||||||
|
},
|
||||||
|
"test_map": &InputValue{
|
||||||
|
Value: cty.MapVal(map[string]cty.Value{
|
||||||
|
"Hello": cty.StringVal("World"),
|
||||||
|
"Foo": cty.StringVal("Bar"),
|
||||||
|
"Baz": cty.StringVal("Foo"),
|
||||||
|
}),
|
||||||
|
SourceType: ValueFromCaller,
|
||||||
|
},
|
||||||
|
"amis": &InputValue{
|
||||||
|
Value: cty.MapVal(map[string]cty.Value{
|
||||||
|
"us-east-1": cty.StringVal("override"),
|
||||||
|
}),
|
||||||
|
SourceType: ValueFromCaller,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := testContext2(t, opts)
|
||||||
|
|
||||||
diags := ctx.Validate()
|
diags := ctx.Validate()
|
||||||
if len(diags) != 0 {
|
if len(diags) != 0 {
|
||||||
t.Fatalf("bad: %#v", diags)
|
t.Fatalf("bad: %s", diags.ErrWithWarnings())
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := ctx.Plan(); err != nil {
|
if _, diags := ctx.Plan(); diags.HasErrors() {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := ctx.Apply()
|
state, diags := ctx.Apply()
|
||||||
if err != nil {
|
if diags.HasErrors() {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
actual := strings.TrimSpace(state.String())
|
actual := strings.TrimSpace(state.String())
|
||||||
expected := strings.TrimSpace(testTerraformApplyVarsStr)
|
expected := strings.TrimSpace(testTerraformApplyVarsStr)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Fatalf("expected: %s\n got:\n%s", expected, actual)
|
t.Errorf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext2Apply_varsEnv(t *testing.T) {
|
func TestContext2Apply_varsEnv(t *testing.T) {
|
||||||
// Set the env var
|
fixture := contextFixtureApplyVarsEnv(t)
|
||||||
defer tempEnv(t, "TF_VAR_ami", "baz")()
|
opts := fixture.ContextOpts()
|
||||||
defer tempEnv(t, "TF_VAR_list", `["Hello", "World"]`)()
|
opts.Variables = InputValues{
|
||||||
defer tempEnv(t, "TF_VAR_map", `{"Hello" = "World", "Foo" = "Bar", "Baz" = "Foo"}`)()
|
"string": &InputValue{
|
||||||
|
Value: cty.StringVal("baz"),
|
||||||
m := testModule(t, "apply-vars-env")
|
SourceType: ValueFromEnvVar,
|
||||||
p := testProvider("aws")
|
},
|
||||||
p.ApplyFn = testApplyFn
|
"list": &InputValue{
|
||||||
p.DiffFn = testDiffFn
|
Value: cty.ListVal([]cty.Value{
|
||||||
ctx := testContext2(t, &ContextOpts{
|
cty.StringVal("Hello"),
|
||||||
Config: m,
|
cty.StringVal("World"),
|
||||||
ProviderResolver: ResourceProviderResolverFixed(
|
}),
|
||||||
map[string]ResourceProviderFactory{
|
SourceType: ValueFromEnvVar,
|
||||||
"aws": testProviderFuncFixed(p),
|
},
|
||||||
},
|
"map": &InputValue{
|
||||||
),
|
Value: cty.MapVal(map[string]cty.Value{
|
||||||
})
|
"Hello": cty.StringVal("World"),
|
||||||
|
"Foo": cty.StringVal("Bar"),
|
||||||
|
"Baz": cty.StringVal("Foo"),
|
||||||
|
}),
|
||||||
|
SourceType: ValueFromEnvVar,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := testContext2(t, opts)
|
||||||
|
|
||||||
diags := ctx.Validate()
|
diags := ctx.Validate()
|
||||||
if len(diags) != 0 {
|
if len(diags) != 0 {
|
||||||
t.Fatalf("bad: %#v", diags)
|
t.Fatalf("bad: %s", diags.ErrWithWarnings())
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := ctx.Plan(); err != nil {
|
if _, diags := ctx.Plan(); diags.HasErrors() {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := ctx.Apply()
|
state, diags := ctx.Apply()
|
||||||
if err != nil {
|
if diags.HasErrors() {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
actual := strings.TrimSpace(state.String())
|
actual := strings.TrimSpace(state.String())
|
||||||
expected := strings.TrimSpace(testTerraformApplyVarsEnvStr)
|
expected := strings.TrimSpace(testTerraformApplyVarsEnvStr)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Fatalf("bad: \n%s", actual)
|
t.Errorf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
84
terraform/context_fixtures_test.go
Normal file
84
terraform/context_fixtures_test.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package terraform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/config/configschema"
|
||||||
|
"github.com/hashicorp/terraform/configs"
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
)
|
||||||
|
|
||||||
|
// contextTestFixture is a container for a set of objects that work together
|
||||||
|
// to create a base testing scenario. This is used to represent some common
|
||||||
|
// situations used as the basis for multiple tests.
|
||||||
|
type contextTestFixture struct {
|
||||||
|
Config *configs.Config
|
||||||
|
ProviderResolver ResourceProviderResolver
|
||||||
|
Provisioners map[string]ResourceProvisionerFactory
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContextOpts returns a ContextOps pre-populated with the elements of this
|
||||||
|
// fixture. Each call returns a distinct object, so callers can apply further
|
||||||
|
// _shallow_ modifications to the options as needed.
|
||||||
|
func (f *contextTestFixture) ContextOpts() *ContextOpts {
|
||||||
|
return &ContextOpts{
|
||||||
|
Config: f.Config,
|
||||||
|
ProviderResolver: f.ProviderResolver,
|
||||||
|
Provisioners: f.Provisioners,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// contextFixtureApplyVars builds and returns a test fixture for testing
|
||||||
|
// input variables, primarily during the apply phase. The configuration is
|
||||||
|
// loaded from test-fixtures/apply-vars, and the provider resolver is
|
||||||
|
// configured with a resource type schema for aws_instance that matches
|
||||||
|
// what's used in that configuration.
|
||||||
|
func contextFixtureApplyVars(t *testing.T) *contextTestFixture {
|
||||||
|
c := testModule(t, "apply-vars")
|
||||||
|
p := mockProviderWithResourceTypeSchema("aws_instance", &configschema.Block{
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"foo": {Type: cty.String, Optional: true},
|
||||||
|
"bar": {Type: cty.String, Optional: true},
|
||||||
|
"baz": {Type: cty.String, Optional: true},
|
||||||
|
"num": {Type: cty.Number, Optional: true},
|
||||||
|
"list": {Type: cty.List(cty.String), Optional: true},
|
||||||
|
"map": {Type: cty.Map(cty.String), Optional: true},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
p.ApplyFn = testApplyFn
|
||||||
|
p.DiffFn = testDiffFn
|
||||||
|
return &contextTestFixture{
|
||||||
|
Config: c,
|
||||||
|
ProviderResolver: ResourceProviderResolverFixed(
|
||||||
|
map[string]ResourceProviderFactory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// contextFixtureApplyVarsEnv builds and returns a test fixture for testing
|
||||||
|
// input variables set from the environment. The configuration is
|
||||||
|
// loaded from test-fixtures/apply-vars-env, and the provider resolver is
|
||||||
|
// configured with a resource type schema for aws_instance that matches
|
||||||
|
// what's used in that configuration.
|
||||||
|
func contextFixtureApplyVarsEnv(t *testing.T) *contextTestFixture {
|
||||||
|
c := testModule(t, "apply-vars-env")
|
||||||
|
p := mockProviderWithResourceTypeSchema("aws_instance", &configschema.Block{
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"string": {Type: cty.String, Optional: true},
|
||||||
|
"list": {Type: cty.List(cty.String), Optional: true},
|
||||||
|
"map": {Type: cty.Map(cty.String), Optional: true},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
p.ApplyFn = testApplyFn
|
||||||
|
p.DiffFn = testDiffFn
|
||||||
|
return &contextTestFixture{
|
||||||
|
Config: c,
|
||||||
|
ProviderResolver: ResourceProviderResolverFixed(
|
||||||
|
map[string]ResourceProviderFactory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
@ -847,47 +847,36 @@ func TestContext2Validate_interpolateMap(t *testing.T) {
|
|||||||
|
|
||||||
// Manually validate using the new PlanGraphBuilder
|
// Manually validate using the new PlanGraphBuilder
|
||||||
func TestContext2Validate_PlanGraphBuilder(t *testing.T) {
|
func TestContext2Validate_PlanGraphBuilder(t *testing.T) {
|
||||||
m := testModule(t, "apply-vars")
|
fixture := contextFixtureApplyVars(t)
|
||||||
p := testProvider("aws")
|
opts := fixture.ContextOpts()
|
||||||
p.ApplyFn = testApplyFn
|
opts.Variables = InputValues{
|
||||||
p.DiffFn = testDiffFn
|
"foo": &InputValue{
|
||||||
c := testContext2(t, &ContextOpts{
|
Value: cty.StringVal("us-east-1"),
|
||||||
Config: m,
|
SourceType: ValueFromCaller,
|
||||||
ProviderResolver: ResourceProviderResolverFixed(
|
|
||||||
map[string]ResourceProviderFactory{
|
|
||||||
"aws": testProviderFuncFixed(p),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Variables: InputValues{
|
|
||||||
"foo": &InputValue{
|
|
||||||
Value: cty.StringVal("us-west-2"),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
"test_list": &InputValue{
|
|
||||||
Value: cty.ListVal([]cty.Value{
|
|
||||||
cty.StringVal("Hello"),
|
|
||||||
cty.StringVal("World"),
|
|
||||||
}),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
"test_map": &InputValue{
|
|
||||||
Value: cty.MapVal(map[string]cty.Value{
|
|
||||||
"Hello": cty.StringVal("World"),
|
|
||||||
"Foo": cty.StringVal("Bar"),
|
|
||||||
"Baz": cty.StringVal("Foo"),
|
|
||||||
}),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
"amis": &InputValue{
|
|
||||||
Value: cty.ListVal([]cty.Value{
|
|
||||||
cty.MapVal(map[string]cty.Value{
|
|
||||||
"us-east-1": cty.StringVal("override"),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
SourceType: ValueFromCaller,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
"test_list": &InputValue{
|
||||||
|
Value: cty.ListVal([]cty.Value{
|
||||||
|
cty.StringVal("Hello"),
|
||||||
|
cty.StringVal("World"),
|
||||||
|
}),
|
||||||
|
SourceType: ValueFromCaller,
|
||||||
|
},
|
||||||
|
"test_map": &InputValue{
|
||||||
|
Value: cty.MapVal(map[string]cty.Value{
|
||||||
|
"Hello": cty.StringVal("World"),
|
||||||
|
"Foo": cty.StringVal("Bar"),
|
||||||
|
"Baz": cty.StringVal("Foo"),
|
||||||
|
}),
|
||||||
|
SourceType: ValueFromCaller,
|
||||||
|
},
|
||||||
|
"amis": &InputValue{
|
||||||
|
Value: cty.MapVal(map[string]cty.Value{
|
||||||
|
"us-east-1": cty.StringVal("override"),
|
||||||
|
}),
|
||||||
|
SourceType: ValueFromCaller,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
c := testContext2(t, opts)
|
||||||
|
|
||||||
graph, diags := (&PlanGraphBuilder{
|
graph, diags := (&PlanGraphBuilder{
|
||||||
Config: c.config,
|
Config: c.config,
|
||||||
|
@ -965,16 +965,20 @@ const testTerraformApplyVarsStr = `
|
|||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = foo
|
ID = foo
|
||||||
provider = provider.aws
|
provider = provider.aws
|
||||||
bar = foo
|
bar = override
|
||||||
baz = override
|
baz = override
|
||||||
foo = us-west-2
|
foo = us-east-1
|
||||||
type = aws_instance
|
type = aws_instance
|
||||||
aws_instance.foo:
|
aws_instance.foo:
|
||||||
ID = foo
|
ID = foo
|
||||||
provider = provider.aws
|
provider = provider.aws
|
||||||
bar = baz
|
bar = baz
|
||||||
list = Hello,World
|
list.# = 2
|
||||||
map = Baz,Foo,Hello
|
list.0 = Hello
|
||||||
|
list.1 = World
|
||||||
|
map.Baz = Foo
|
||||||
|
map.Foo = Bar
|
||||||
|
map.Hello = World
|
||||||
num = 2
|
num = 2
|
||||||
type = aws_instance
|
type = aws_instance
|
||||||
`
|
`
|
||||||
@ -983,9 +987,13 @@ const testTerraformApplyVarsEnvStr = `
|
|||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = foo
|
ID = foo
|
||||||
provider = provider.aws
|
provider = provider.aws
|
||||||
bar = Hello,World
|
list.# = 2
|
||||||
baz = Baz,Foo,Hello
|
list.0 = Hello
|
||||||
foo = baz
|
list.1 = World
|
||||||
|
map.Baz = Foo
|
||||||
|
map.Foo = Bar
|
||||||
|
map.Hello = World
|
||||||
|
string = baz
|
||||||
type = aws_instance
|
type = aws_instance
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
variable "ami" {
|
variable "string" {
|
||||||
default = "foo"
|
default = "foo"
|
||||||
type = "string"
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "list" {
|
variable "list" {
|
||||||
default = []
|
default = []
|
||||||
type = "list"
|
type = list(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "map" {
|
variable "map" {
|
||||||
default = {}
|
default = {}
|
||||||
type = "map"
|
type = map(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_instance" "bar" {
|
resource "aws_instance" "bar" {
|
||||||
foo = "${var.ami}"
|
string = var.string
|
||||||
bar = "${join(",", var.list)}"
|
list = var.list
|
||||||
baz = "${join(",", keys(var.map))}"
|
map = var.map
|
||||||
}
|
}
|
||||||
|
@ -6,11 +6,11 @@ variable "amis" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
variable "test_list" {
|
variable "test_list" {
|
||||||
type = "list"
|
type = list(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "test_map" {
|
variable "test_map" {
|
||||||
type = "map"
|
type = map(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "bar" {
|
variable "bar" {
|
||||||
@ -20,14 +20,14 @@ variable "bar" {
|
|||||||
variable "foo" {}
|
variable "foo" {}
|
||||||
|
|
||||||
resource "aws_instance" "foo" {
|
resource "aws_instance" "foo" {
|
||||||
num = "2"
|
num = "2"
|
||||||
bar = "${var.bar}"
|
bar = var.bar
|
||||||
list = "${join(",", var.test_list)}"
|
list = var.test_list
|
||||||
map = "${join(",", keys(var.test_map))}"
|
map = var.test_map
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_instance" "bar" {
|
resource "aws_instance" "bar" {
|
||||||
foo = "${var.foo}"
|
foo = var.foo
|
||||||
bar = "${lookup(var.amis, var.foo)}"
|
bar = var.amis[var.foo]
|
||||||
baz = "${var.amis["us-east-1"]}"
|
baz = var.amis["us-east-1"]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user