mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
terraform: don't validate orphans
This commit is contained in:
parent
6a3fa2f4db
commit
8d065ce5c4
@ -712,6 +712,11 @@ func (c *Context) validateWalkFn(rws *[]string, res *[]error) depgraph.WalkFunc
|
||||
return nil
|
||||
}
|
||||
|
||||
// Don't validate orphans since they never have a config
|
||||
if rn.Orphan {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("[INFO] Validating resource: %s", rn.Resource.Id)
|
||||
ws, es := rn.Resource.Provider.ValidateResource(
|
||||
rn.Type, rn.Resource.Config)
|
||||
|
@ -8,9 +8,13 @@ import (
|
||||
)
|
||||
|
||||
func TestContextValidate(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
config := testConfig(t, "validate-good")
|
||||
c := testContext(t, &ContextOpts{
|
||||
Config: config,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
w, e := c.Validate()
|
||||
@ -23,9 +27,13 @@ func TestContextValidate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextValidate_badVar(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
config := testConfig(t, "validate-bad-var")
|
||||
c := testContext(t, &ContextOpts{
|
||||
Config: config,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
w, e := c.Validate()
|
||||
@ -37,6 +45,39 @@ func TestContextValidate_badVar(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextValidate_orphans(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
config := testConfig(t, "validate-good")
|
||||
state := &State{
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.web": &ResourceState{
|
||||
ID: "bar",
|
||||
Type: "aws_instance",
|
||||
},
|
||||
},
|
||||
}
|
||||
c := testContext(t, &ContextOpts{
|
||||
Config: config,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
State: state,
|
||||
})
|
||||
|
||||
p.ValidateResourceFn = func(
|
||||
t string, c *ResourceConfig) ([]string, []error) {
|
||||
return nil, c.CheckSet([]string{"foo"})
|
||||
}
|
||||
|
||||
w, e := c.Validate()
|
||||
if len(w) > 0 {
|
||||
t.Fatalf("bad: %#v", w)
|
||||
}
|
||||
if len(e) > 0 {
|
||||
t.Fatalf("bad: %#v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextValidate_providerConfig_bad(t *testing.T) {
|
||||
config := testConfig(t, "validate-bad-pc")
|
||||
p := testProvider("aws")
|
||||
|
@ -38,6 +38,7 @@ type MockResourceProvider struct {
|
||||
ValidateConfig *ResourceConfig
|
||||
ValidateReturnWarns []string
|
||||
ValidateReturnErrors []error
|
||||
ValidateResourceFn func(string, *ResourceConfig) ([]string, []error)
|
||||
ValidateResourceCalled bool
|
||||
ValidateResourceType string
|
||||
ValidateResourceConfig *ResourceConfig
|
||||
@ -61,6 +62,11 @@ func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]
|
||||
p.ValidateResourceCalled = true
|
||||
p.ValidateResourceType = t
|
||||
p.ValidateResourceConfig = c
|
||||
|
||||
if p.ValidateResourceFn != nil {
|
||||
return p.ValidateResourceFn(t, c)
|
||||
}
|
||||
|
||||
return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
resource "aws_instance" "foo" {
|
||||
num = "2"
|
||||
foo = "bar"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
|
Loading…
Reference in New Issue
Block a user