2015-07-10 15:08:49 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2018-09-05 16:35:30 -05:00
|
|
|
"errors"
|
2015-07-10 15:08:49 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2018-05-11 16:52:29 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-07-05 12:33:29 -05:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-09-05 16:35:30 -05:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"github.com/hashicorp/terraform/provisioners"
|
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2015-07-10 15:08:49 -05:00
|
|
|
)
|
|
|
|
|
2016-08-16 15:48:12 -05:00
|
|
|
func TestContext2Validate_badCount(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:48:12 -05:00
|
|
|
m := testModule(t, "validate-bad-count")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2016-08-16 15:48:12 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2016-08-16 15:48:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
func TestContext2Validate_badVar(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
"num": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "validate-bad-var")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2016-01-21 13:33:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 14:58:24 -06:00
|
|
|
func TestContext2Validate_varMapOverrideOld(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-module-pc-vars")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-12-09 14:58:24 -06:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2018-05-04 21:24:06 -05:00
|
|
|
Variables: InputValues{
|
|
|
|
"foo.foo": &InputValue{
|
|
|
|
Value: cty.StringVal("bar"),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
2016-12-09 14:58:24 -06:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2016-12-09 14:58:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 13:33:16 -06:00
|
|
|
func TestContext2Validate_varNoDefaultExplicitType(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-var-no-default-explicit-type")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2016-01-21 13:33:16 -06:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_computedVar(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pt := testProvider("test")
|
|
|
|
pt.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "validate-computed-var")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
2018-05-11 17:30:27 -05:00
|
|
|
"test": testProviderFuncFixed(pt),
|
2017-04-21 19:40:46 -05:00
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
|
|
|
|
if !c.IsComputed("value") {
|
|
|
|
return nil, []error{fmt.Errorf("value isn't computed")}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, c.CheckSet([]string{"value"})
|
|
|
|
}
|
|
|
|
|
2018-08-24 18:13:50 -05:00
|
|
|
p.ConfigureFn = func(c *ResourceConfig) error {
|
2015-07-10 15:08:49 -05:00
|
|
|
return fmt.Errorf("Configure should not be called for provider")
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:15:43 -06:00
|
|
|
// 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.
|
|
|
|
func TestContext2Validate_countComputed(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DataSources: map[string]*configschema.Block{
|
|
|
|
"aws_data_source": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"compute": {Type: cty.String, Optional: true},
|
|
|
|
"value": {Type: cty.String, Computed: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:15:43 -06:00
|
|
|
m := testModule(t, "validate-count-computed")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2017-01-27 23:15:43 -06:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2017-01-27 23:15:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
func TestContext2Validate_countNegative(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "validate-count-negative")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_countVariable(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "apply-count-variable")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_countVariableNoDefault(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
|
|
|
m := testModule(t, "validate-count-variable")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2017-04-21 19:40:46 -05:00
|
|
|
},
|
2018-05-11 17:30:27 -05:00
|
|
|
},
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_moduleBadOutput(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "validate-bad-module-output")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_moduleGood(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "validate-good-module")
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_moduleBadResource(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-module-bad-rc")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{
|
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_moduleDepsShouldNotCycle(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-module-deps-cycle")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
ctx := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := ctx.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 14:25:59 -05:00
|
|
|
func TestContext2Validate_moduleProviderInheritOrphan(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-module-pc-inherit-orphan")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"set": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-02 14:25:59 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-11-02 14:25:59 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
State: mustShimLegacyState(&State{
|
2017-11-02 14:25:59 -05:00
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: []string{"root", "child"},
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.bar": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
}),
|
2017-11-02 14:25:59 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
|
|
|
|
v, ok := c.Get("set")
|
|
|
|
if !ok {
|
|
|
|
return nil, []error{fmt.Errorf("not set")}
|
|
|
|
}
|
|
|
|
if v != "bar" {
|
|
|
|
return nil, []error{fmt.Errorf("bad: %#v", v)}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2017-11-02 14:25:59 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
|
|
|
|
func TestContext2Validate_moduleProviderVar(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-module-pc-vars")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2018-05-04 21:24:06 -05:00
|
|
|
Variables: InputValues{
|
|
|
|
"provider_var": &InputValue{
|
|
|
|
Value: cty.StringVal("bar"),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
2015-07-10 15:08:49 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
|
|
|
|
return nil, c.CheckSet([]string{"foo"})
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_moduleProviderInheritUnused(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-module-pc-inherit-unused")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
|
|
|
|
return nil, c.CheckSet([]string{"foo"})
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_orphans(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
"num": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "validate-good")
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
state := mustShimLegacyState(&State{
|
2015-07-10 15:08:49 -05:00
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: rootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.web": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
})
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
State: state,
|
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
p.ValidateResourceTypeConfigFn = func(req providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
if req.Config.GetAttr("foo").IsNull() {
|
|
|
|
diags.Append(errors.New("foo is not set"))
|
|
|
|
}
|
|
|
|
return providers.ValidateResourceTypeConfigResponse{
|
|
|
|
Diagnostics: diags,
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_providerConfig_bad(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-bad-pc")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
p.ValidateProviderConfigResponse = providers.ValidateProviderConfigResponse{
|
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if len(diags) != 1 {
|
|
|
|
t.Fatalf("wrong number of diagnostics %d; want %d", len(diags), 1)
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
2017-11-21 17:08:00 -06:00
|
|
|
if !strings.Contains(diags.Err().Error(), "bad") {
|
|
|
|
t.Fatalf("bad: %s", diags.Err().Error())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_providerConfig_badEmpty(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-bad-pc-empty")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
p.ValidateProviderConfigResponse = providers.ValidateProviderConfigResponse{
|
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_providerConfig_good(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-bad-pc")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_provisionerConfig_bad(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-bad-prov-conf")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pr := simpleMockProvisioner()
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2018-08-17 14:32:35 -05:00
|
|
|
Provisioners: map[string]ProvisionerFactory{
|
2015-07-10 15:08:49 -05:00
|
|
|
"shell": testProvisionerFuncFixed(pr),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
p.ValidateProviderConfigResponse = providers.ValidateProviderConfigResponse{
|
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_provisionerConfig_good(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-bad-prov-conf")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pr := simpleMockProvisioner()
|
2018-09-05 16:35:30 -05:00
|
|
|
pr.ValidateProvisionerConfigFn = func(req provisioners.ValidateProvisionerConfigRequest) provisioners.ValidateProvisionerConfigResponse {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
if req.Config.GetAttr("test_string").IsNull() {
|
|
|
|
diags.Append(errors.New("test_string is not set"))
|
|
|
|
}
|
|
|
|
return provisioners.ValidateProvisionerConfigResponse{
|
|
|
|
Diagnostics: diags,
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 17:30:27 -05:00
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2018-08-17 14:32:35 -05:00
|
|
|
Provisioners: map[string]ProvisionerFactory{
|
2015-07-10 15:08:49 -05:00
|
|
|
"shell": testProvisionerFuncFixed(pr),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_requiredVar(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-required-var")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_resourceConfig_bad(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-bad-rc")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{
|
|
|
|
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if !diags.HasErrors() {
|
2018-05-11 17:30:27 -05:00
|
|
|
t.Fatalf("succeeded; want error")
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_resourceConfig_good(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-bad-rc")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2017-04-21 19:40:46 -05:00
|
|
|
},
|
2018-05-11 17:30:27 -05:00
|
|
|
},
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
2018-05-11 17:30:27 -05:00
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_tainted(t *testing.T) {
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
"num": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
m := testModule(t, "validate-good")
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
state := mustShimLegacyState(&State{
|
2015-07-10 15:08:49 -05:00
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: rootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
2016-04-21 14:59:10 -05:00
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Tainted: true,
|
2015-07-10 15:08:49 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
})
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2015-07-10 15:08:49 -05:00
|
|
|
State: state,
|
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
p.ValidateResourceTypeConfigFn = func(req providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
if req.Config.GetAttr("foo").IsNull() {
|
|
|
|
diags.Append(errors.New("foo is not set"))
|
|
|
|
}
|
|
|
|
return providers.ValidateResourceTypeConfigResponse{
|
|
|
|
Diagnostics: diags,
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := c.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_targetedDestroy(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-targeted")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
pr := simpleMockProvisioner()
|
2015-07-10 15:08:49 -05:00
|
|
|
p.ApplyFn = testApplyFn
|
|
|
|
p.DiffFn = testDiffFn
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
"num": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:08:49 -05:00
|
|
|
ctx := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2018-08-17 14:32:35 -05:00
|
|
|
Provisioners: map[string]ProvisionerFactory{
|
2015-07-10 15:08:49 -05:00
|
|
|
"shell": testProvisionerFuncFixed(pr),
|
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
State: mustShimLegacyState(&State{
|
2015-07-10 15:08:49 -05:00
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: rootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": resourceState("aws_instance", "i-bcd345"),
|
|
|
|
"aws_instance.bar": resourceState("aws_instance", "i-abc123"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
}),
|
2018-05-04 21:24:06 -05:00
|
|
|
Targets: []addrs.Targetable{
|
|
|
|
addrs.RootModuleInstance.Resource(
|
|
|
|
addrs.ManagedResourceMode, "aws_instance", "foo",
|
|
|
|
),
|
|
|
|
},
|
2015-07-10 15:08:49 -05:00
|
|
|
Destroy: true,
|
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := ctx.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_varRefFilled(t *testing.T) {
|
|
|
|
m := testModule(t, "validate-variable-ref")
|
|
|
|
p := testProvider("aws")
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-07-10 15:08:49 -05:00
|
|
|
c := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2018-05-04 21:24:06 -05:00
|
|
|
Variables: InputValues{
|
|
|
|
"foo": &InputValue{
|
|
|
|
Value: cty.StringVal("bar"),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
2015-07-10 15:08:49 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-09-05 16:35:30 -05:00
|
|
|
var value cty.Value
|
|
|
|
p.ValidateResourceTypeConfigFn = func(req providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse {
|
|
|
|
value = req.Config.GetAttr("foo")
|
|
|
|
return providers.ValidateResourceTypeConfigResponse{}
|
2015-07-10 15:08:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Validate()
|
2018-09-05 16:35:30 -05:00
|
|
|
if !value.RawEquals(cty.StringVal("bar")) {
|
2015-07-10 15:08:49 -05:00
|
|
|
t.Fatalf("bad: %#v", value)
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 11:46:06 -05:00
|
|
|
|
|
|
|
// Module variables weren't being interpolated during Validate phase.
|
|
|
|
// related to https://github.com/hashicorp/terraform/issues/5322
|
|
|
|
func TestContext2Validate_interpolateVar(t *testing.T) {
|
|
|
|
input := new(MockUIInput)
|
|
|
|
|
|
|
|
m := testModule(t, "input-interpolate-var")
|
|
|
|
p := testProvider("null")
|
|
|
|
p.ApplyFn = testApplyFn
|
|
|
|
p.DiffFn = testDiffFn
|
2018-05-11 17:30:27 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"template_file": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"template": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-23 11:46:06 -05:00
|
|
|
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"template": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2016-05-23 11:46:06 -05:00
|
|
|
UIInput: input,
|
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := ctx.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2016-05-23 11:46:06 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 15:54:14 -05:00
|
|
|
|
|
|
|
// When module vars reference something that is actually computed, this
|
|
|
|
// shouldn't cause validation to fail.
|
|
|
|
func TestContext2Validate_interpolateComputedModuleVarDef(t *testing.T) {
|
|
|
|
input := new(MockUIInput)
|
|
|
|
|
|
|
|
m := testModule(t, "validate-computed-module-var-ref")
|
|
|
|
p := testProvider("aws")
|
|
|
|
p.ApplyFn = testApplyFn
|
|
|
|
p.DiffFn = testDiffFn
|
2018-05-11 16:52:29 -05:00
|
|
|
p.GetSchemaReturn = &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"attr": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-23 15:54:14 -05:00
|
|
|
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2016-05-23 15:54:14 -05:00
|
|
|
UIInput: input,
|
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := ctx.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2016-05-23 15:54:14 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-26 15:05:50 -05:00
|
|
|
|
|
|
|
// Computed values are lost when a map is output from a module
|
|
|
|
func TestContext2Validate_interpolateMap(t *testing.T) {
|
|
|
|
input := new(MockUIInput)
|
|
|
|
|
|
|
|
m := testModule(t, "issue-9549")
|
2018-06-01 13:06:25 -05:00
|
|
|
p := testProvider("template")
|
2016-10-26 15:05:50 -05:00
|
|
|
p.ApplyFn = testApplyFn
|
|
|
|
p.DiffFn = testDiffFn
|
|
|
|
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2017-04-21 19:40:46 -05:00
|
|
|
"template": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
2016-10-26 15:05:50 -05:00
|
|
|
UIInput: input,
|
|
|
|
})
|
|
|
|
|
2017-11-21 17:08:00 -06:00
|
|
|
diags := ctx.Validate()
|
|
|
|
if diags.HasErrors() {
|
2018-05-11 16:52:29 -05:00
|
|
|
t.Fatalf("unexpected error: %s", diags.Err())
|
2016-10-26 15:05:50 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-02 09:53:29 -06:00
|
|
|
|
|
|
|
// Manually validate using the new PlanGraphBuilder
|
|
|
|
func TestContext2Validate_PlanGraphBuilder(t *testing.T) {
|
2018-05-11 16:40:40 -05:00
|
|
|
fixture := contextFixtureApplyVars(t)
|
|
|
|
opts := fixture.ContextOpts()
|
|
|
|
opts.Variables = InputValues{
|
|
|
|
"foo": &InputValue{
|
|
|
|
Value: cty.StringVal("us-east-1"),
|
|
|
|
SourceType: ValueFromCaller,
|
2016-12-02 09:53:29 -06:00
|
|
|
},
|
2018-05-11 16:40:40 -05:00
|
|
|
"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)
|
2016-12-02 09:53:29 -06:00
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
graph, diags := (&PlanGraphBuilder{
|
|
|
|
Config: c.config,
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
State: states.NewState(),
|
2018-05-04 21:24:06 -05:00
|
|
|
Components: c.components,
|
2018-05-31 14:39:45 -05:00
|
|
|
Schemas: c.schemas,
|
2018-05-04 21:24:06 -05:00
|
|
|
Targets: c.targets,
|
|
|
|
}).Build(addrs.RootModuleInstance)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("errors from PlanGraphBuilder: %s", diags.Err())
|
2017-07-20 04:23:43 -05:00
|
|
|
}
|
2017-01-30 10:41:38 -06:00
|
|
|
defer c.acquireRun("validate-test")()
|
2018-05-04 21:24:06 -05:00
|
|
|
walker, diags := c.walk(graph, walkValidate)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Err())
|
2016-12-02 09:53:29 -06:00
|
|
|
}
|
2018-05-04 21:24:06 -05:00
|
|
|
if len(walker.NonFatalDiagnostics) > 0 {
|
|
|
|
t.Fatal(walker.NonFatalDiagnostics.Err())
|
2016-12-02 09:53:29 -06:00
|
|
|
}
|
|
|
|
}
|
2018-05-22 13:16:08 -05:00
|
|
|
|
|
|
|
func TestContext2Validate_invalidOutput(t *testing.T) {
|
|
|
|
m := testModuleInline(t, map[string]string{
|
|
|
|
"main.tf": `
|
|
|
|
data "aws_data_source" "name" {}
|
|
|
|
|
|
|
|
output "out" {
|
|
|
|
value = "${data.aws_data_source.name.missing}"
|
|
|
|
}`,
|
|
|
|
})
|
|
|
|
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2018-05-22 13:16:08 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
diags := ctx.Validate()
|
|
|
|
if !diags.HasErrors() {
|
|
|
|
// Should get this error:
|
|
|
|
// Unsupported attribute: This object does not have an attribute named "missing"
|
|
|
|
t.Fatal("succeeded; want errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext2Validate_invalidModuleOutput(t *testing.T) {
|
|
|
|
m := testModuleInline(t, map[string]string{
|
|
|
|
"child/main.tf": `
|
|
|
|
data "aws_data_source" "name" {}
|
|
|
|
|
|
|
|
output "out" {
|
|
|
|
value = "${data.aws_data_source.name.missing}"
|
|
|
|
}`,
|
|
|
|
"main.tf": `
|
|
|
|
module "child" {
|
|
|
|
source = "./child"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
foo = "${module.child.out}"
|
|
|
|
}`,
|
|
|
|
})
|
|
|
|
|
|
|
|
p := testProvider("aws")
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
|
|
Config: m,
|
2018-08-17 14:32:35 -05:00
|
|
|
ProviderResolver: providers.ResolverFixed(
|
|
|
|
map[string]providers.Factory{
|
2018-05-22 13:16:08 -05:00
|
|
|
"aws": testProviderFuncFixed(p),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
diags := ctx.Validate()
|
|
|
|
if !diags.HasErrors() {
|
|
|
|
// Should get this error:
|
|
|
|
// Unsupported attribute: This object does not have an attribute named "missing"
|
|
|
|
t.Fatal("succeeded; want errors")
|
|
|
|
}
|
|
|
|
}
|