core: Add test to show that data resource reads are not functioning properly

This commit is contained in:
Radek Simko 2018-10-02 09:45:17 +01:00 committed by Martin Atkins
parent 6f49a5d124
commit 84d4e78481
4 changed files with 110 additions and 0 deletions

View File

@ -1887,6 +1887,66 @@ func TestContext2Plan_computedDataResource(t *testing.T) {
)
}
func TestContext2Plan_computedInFunction(t *testing.T) {
m := testModule(t, "plan-computed-in-function")
p := testProvider("aws")
p.GetSchemaReturn = &ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"aws_instance": {
Attributes: map[string]*configschema.Attribute{
"attr": {Type: cty.Number, Optional: true},
},
},
},
DataSources: map[string]*configschema.Block{
"aws_data_source": {
Attributes: map[string]*configschema.Attribute{
"computed": {Type: cty.List(cty.String), Computed: true},
},
},
},
}
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Config: m,
ProviderResolver: providers.ResolverFixed(
map[string]providers.Factory{
"aws": testProviderFuncFixed(p),
},
),
})
defer ctx.acquireRun("validate")()
pgb := &PlanGraphBuilder{
Config: ctx.config,
State: ctx.state,
Components: ctx.components,
Schemas: ctx.schemas,
Targets: ctx.targets,
}
graph, _ := pgb.Build(addrs.RootModuleInstance)
// walk
walker := &ContextGraphWalker{
Context: ctx,
State: ctx.state.SyncWrapper(),
Changes: ctx.changes.SyncWrapper(),
Operation: walkPlan,
StopContext: ctx.runContext,
RootVariableValues: ctx.variables,
}
watchStop, watchWait := ctx.watchStop(walker)
diags := graph.Walk(walker)
close(watchStop)
<-watchWait
if diags.HasErrors() {
t.Fatalf("unexpected errors: %s", diags.Err())
}
}
func TestContext2Plan_computedDataCountResource(t *testing.T) {
m := testModule(t, "plan-computed-data-count")
p := testProvider("aws")

View File

@ -174,6 +174,42 @@ func TestContext2Validate_computedVar(t *testing.T) {
}
}
func TestContext2Validate_computedInFunction(t *testing.T) {
p := testProvider("aws")
p.GetSchemaReturn = &ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"aws_instance": {
Attributes: map[string]*configschema.Attribute{
"attr": {Type: cty.Number, Optional: true},
},
},
},
DataSources: map[string]*configschema.Block{
"aws_data_source": {
Attributes: map[string]*configschema.Attribute{
"optional_attr": {Type: cty.String, Optional: true},
"computed": {Type: cty.String, Computed: true},
},
},
},
}
m := testModule(t, "validate-computed-in-function")
c := testContext2(t, &ContextOpts{
Config: m,
ProviderResolver: providers.ResolverFixed(
map[string]providers.Factory{
"aws": testProviderFuncFixed(p),
},
),
})
diags := c.Validate()
if diags.HasErrors() {
t.Fatalf("unexpected error: %s", diags.Err())
}
}
// Test that validate allows through computed counts. We do this and allow
// them to fail during "plan" since we can't know if the computed values
// can be realized during a plan.

View File

@ -0,0 +1,7 @@
data "aws_data_source" "foo" {
}
resource "aws_instance" "bar" {
attr = "${length(data.aws_data_source.foo.computed)}"
}

View File

@ -0,0 +1,7 @@
data "aws_data_source" "foo" {
optional_attr = "value"
}
resource "aws_instance" "bar" {
attr = "${length(data.aws_data_source.foo.computed)}"
}