forked from IBM-Cloud/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Test for new refresh graph behaviour
Tests on DynamicExpand for both resources and data sources, cover scale in/out scenarios, and also a verification for the behaviour of config orphans.
- Loading branch information
1 parent
f63ad1d
commit 11b4794
Showing
6 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package terraform | ||
|
||
import "testing" | ||
|
||
func TestRefreshGraphBuilder_configOrphans(t *testing.T) { | ||
|
||
m := testModule(t, "refresh-config-orphan") | ||
|
||
state := &State{ | ||
Modules: []*ModuleState{ | ||
&ModuleState{ | ||
Path: rootModulePath, | ||
Resources: map[string]*ResourceState{ | ||
"aws_instance.foo.0": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "foo", | ||
}, | ||
}, | ||
}, | ||
"aws_instance.foo.1": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "bar", | ||
}, | ||
}, | ||
}, | ||
"aws_instance.foo.2": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "baz", | ||
}, | ||
}, | ||
}, | ||
"data.aws_instance.foo.0": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "foo", | ||
}, | ||
}, | ||
}, | ||
"data.aws_instance.foo.1": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "bar", | ||
}, | ||
}, | ||
}, | ||
"data.aws_instance.foo.2": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "baz", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
b := &RefreshGraphBuilder{ | ||
Module: m, | ||
State: state, | ||
Providers: []string{"aws"}, | ||
} | ||
g, err := b.Build(rootModulePath) | ||
if err != nil { | ||
t.Fatalf("Error building graph: %s", err) | ||
} | ||
|
||
actual := g.StringWithNodeTypes() | ||
expected := `aws_instance.foo - *terraform.NodeRefreshableManagedResource | ||
provider.aws - *terraform.NodeApplyableProvider | ||
data.aws_instance.foo[0] - *terraform.NodeRefreshableManagedResourceInstance | ||
provider.aws - *terraform.NodeApplyableProvider | ||
data.aws_instance.foo[1] - *terraform.NodeRefreshableManagedResourceInstance | ||
provider.aws - *terraform.NodeApplyableProvider | ||
data.aws_instance.foo[2] - *terraform.NodeRefreshableManagedResourceInstance | ||
provider.aws - *terraform.NodeApplyableProvider | ||
provider.aws - *terraform.NodeApplyableProvider | ||
provider.aws (close) - *terraform.graphNodeCloseProvider | ||
aws_instance.foo - *terraform.NodeRefreshableManagedResource | ||
data.aws_instance.foo[0] - *terraform.NodeRefreshableManagedResourceInstance | ||
data.aws_instance.foo[1] - *terraform.NodeRefreshableManagedResourceInstance | ||
data.aws_instance.foo[2] - *terraform.NodeRefreshableManagedResourceInstance | ||
` | ||
if expected != actual { | ||
t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package terraform | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) { | ||
var stateLock sync.RWMutex | ||
|
||
addr, err := ParseResourceAddress("data.aws_instance.foo") | ||
if err != nil { | ||
t.Fatalf("bad: %s", err) | ||
} | ||
|
||
m := testModule(t, "refresh-data-scale-inout") | ||
|
||
state := &State{ | ||
Modules: []*ModuleState{ | ||
&ModuleState{ | ||
Path: rootModulePath, | ||
Resources: map[string]*ResourceState{ | ||
"data.aws_instance.foo.0": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "foo", | ||
}, | ||
}, | ||
}, | ||
"data.aws_instance.foo.1": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "bar", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
n := &NodeRefreshableDataResource{ | ||
NodeAbstractCountResource: &NodeAbstractCountResource{ | ||
NodeAbstractResource: &NodeAbstractResource{ | ||
Addr: addr, | ||
Config: m.Config().Resources[0], | ||
}, | ||
}, | ||
} | ||
|
||
g, err := n.DynamicExpand(&MockEvalContext{ | ||
PathPath: []string{"root"}, | ||
StateState: state, | ||
StateLock: &stateLock, | ||
}) | ||
|
||
actual := g.StringWithNodeTypes() | ||
expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance | ||
root - terraform.graphNodeRoot | ||
data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance | ||
` | ||
if expected != actual { | ||
t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual) | ||
} | ||
} | ||
|
||
func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) { | ||
var stateLock sync.RWMutex | ||
|
||
addr, err := ParseResourceAddress("data.aws_instance.foo") | ||
if err != nil { | ||
t.Fatalf("bad: %s", err) | ||
} | ||
|
||
m := testModule(t, "refresh-data-scale-inout") | ||
|
||
state := &State{ | ||
Modules: []*ModuleState{ | ||
&ModuleState{ | ||
Path: rootModulePath, | ||
Resources: map[string]*ResourceState{ | ||
"data.aws_instance.foo.0": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "foo", | ||
}, | ||
}, | ||
}, | ||
"data.aws_instance.foo.1": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "bar", | ||
}, | ||
}, | ||
}, | ||
"data.aws_instance.foo.2": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "baz", | ||
}, | ||
}, | ||
}, | ||
"data.aws_instance.foo.3": &ResourceState{ | ||
Type: "aws_instance", | ||
Deposed: []*InstanceState{ | ||
&InstanceState{ | ||
ID: "qux", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
n := &NodeRefreshableDataResource{ | ||
NodeAbstractCountResource: &NodeAbstractCountResource{ | ||
NodeAbstractResource: &NodeAbstractResource{ | ||
Addr: addr, | ||
Config: m.Config().Resources[0], | ||
}, | ||
}, | ||
} | ||
|
||
g, err := n.DynamicExpand(&MockEvalContext{ | ||
PathPath: []string{"root"}, | ||
StateState: state, | ||
StateLock: &stateLock, | ||
}) | ||
|
||
actual := g.StringWithNodeTypes() | ||
expected := `data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResource | ||
root - terraform.graphNodeRoot | ||
data.aws_instance.foo[0] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[1] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[2] - *terraform.NodeRefreshableDataResourceInstance | ||
data.aws_instance.foo[3] - *terraform.NodeDestroyableDataResource | ||
` | ||
if expected != actual { | ||
t.Fatalf("Expected:\n%s\nGot:\n%s", expected, actual) | ||
} | ||
} |
Oops, something went wrong.