2015-02-09 19:14:09 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2015-02-09 19:14:09 +01:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
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 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2015-02-09 19:14:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMissingProvisionerTransformer(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-provisioner-basic")
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2015-02-09 19:14:09 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
tf := &ConfigTransformer{Config: mod}
|
2015-02-09 19:14:09 +01:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 04:56:16 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-01-27 04:56:16 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 21:52:50 +02:00
|
|
|
{
|
2016-02-04 13:46:45 +01:00
|
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
2015-06-19 21:52:50 +02:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-02-04 13:46:45 +01:00
|
|
|
transform := &ProvisionerTransformer{}
|
2015-06-19 21:52:50 +02:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-02-09 19:14:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformMissingProvisionerBasicStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:25:11 +01:00
|
|
|
func TestMissingProvisionerTransformer_module(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-provisioner-module")
|
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2016-11-03 18:25:11 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
concreteResource := func(a *NodeAbstractResourceInstance) dag.Vertex {
|
2016-11-03 18:25:11 +01:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
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 23:24:45 +02:00
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsFlat: map[string]string{
|
|
|
|
"id": "foo",
|
2016-11-03 18:25:11 +01: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 23:24:45 +02:00
|
|
|
Status: states.ObjectReady,
|
2016-11-03 18:25:11 +01: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 23:24:45 +02:00
|
|
|
addrs.ProviderConfig{
|
|
|
|
Type: "aws",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "aws_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsFlat: map[string]string{
|
|
|
|
"id": "foo",
|
2016-11-03 18:25:11 +01: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 23:24:45 +02:00
|
|
|
Status: states.ObjectReady,
|
2016-11-03 18:25:11 +01: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 23:24:45 +02:00
|
|
|
addrs.ProviderConfig{
|
|
|
|
Type: "aws",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
})
|
2016-11-03 18:25:11 +01:00
|
|
|
|
|
|
|
tf := &StateTransformer{
|
2018-09-20 21:30:52 +02:00
|
|
|
ConcreteCurrent: concreteResource,
|
|
|
|
State: state,
|
2016-11-03 18:25:11 +01:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-05-09 02:29:23 +02:00
|
|
|
t.Logf("graph after StateTransformer:\n%s", g.StringWithNodeTypes())
|
2016-11-03 18:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2016-11-03 18:25:11 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-05-09 02:29:23 +02:00
|
|
|
t.Logf("graph after MissingProvisionerTransformer:\n%s", g.StringWithNodeTypes())
|
2016-11-03 18:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &ProvisionerTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-05-09 02:29:23 +02:00
|
|
|
t.Logf("graph after ProvisionerTransformer:\n%s", g.StringWithNodeTypes())
|
2016-11-03 18:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformMissingProvisionerModuleStr)
|
|
|
|
if actual != expected {
|
2018-05-09 02:29:23 +02:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-11-03 18:25:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 13:46:45 +01:00
|
|
|
func TestCloseProvisionerTransformer(t *testing.T) {
|
|
|
|
mod := testModule(t, "transform-provisioner-basic")
|
2015-02-09 19:14:09 +01:00
|
|
|
|
2018-05-05 04:24:06 +02:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2015-02-09 19:14:09 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
tf := &ConfigTransformer{Config: mod}
|
2015-02-09 19:14:09 +01:00
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 04:56:16 +01:00
|
|
|
{
|
2018-05-05 04:24:06 +02:00
|
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
2017-01-27 04:56:16 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 19:14:09 +01:00
|
|
|
{
|
2016-02-04 13:46:45 +01:00
|
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
2015-02-09 19:14:09 +01:00
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transform := &ProvisionerTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 21:52:50 +02:00
|
|
|
{
|
|
|
|
transform := &CloseProvisionerTransformer{}
|
|
|
|
if err := transform.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 19:14:09 +01:00
|
|
|
actual := strings.TrimSpace(g.String())
|
2016-02-04 13:46:45 +01:00
|
|
|
expected := strings.TrimSpace(testTransformCloseProvisionerBasicStr)
|
2015-02-09 19:14:09 +01:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testTransformMissingProvisionerBasicStr = `
|
|
|
|
aws_instance.web
|
2016-02-04 13:46:45 +01:00
|
|
|
provisioner.shell
|
|
|
|
provisioner.shell
|
2015-02-09 19:14:09 +01:00
|
|
|
`
|
|
|
|
|
2016-11-03 18:25:11 +01:00
|
|
|
const testTransformMissingProvisionerModuleStr = `
|
|
|
|
aws_instance.foo
|
|
|
|
provisioner.shell
|
|
|
|
module.child.aws_instance.foo
|
|
|
|
module.child.provisioner.shell
|
|
|
|
module.child.provisioner.shell
|
|
|
|
provisioner.shell
|
|
|
|
`
|
|
|
|
|
2016-02-04 13:46:45 +01:00
|
|
|
const testTransformCloseProvisionerBasicStr = `
|
2015-02-09 19:14:09 +01:00
|
|
|
aws_instance.web
|
2016-02-04 13:46:45 +01:00
|
|
|
provisioner.shell
|
|
|
|
provisioner.shell
|
|
|
|
provisioner.shell (close)
|
2015-06-19 21:52:50 +02:00
|
|
|
aws_instance.web
|
2015-02-09 19:14:09 +01:00
|
|
|
`
|