2018-06-08 02:27:57 +02:00
|
|
|
package states
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-test/deep"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestState(t *testing.T) {
|
|
|
|
// This basic tests exercises the main mutation methods to construct
|
|
|
|
// a state. It is not fully comprehensive, so other tests should visit
|
|
|
|
// more esoteric codepaths.
|
|
|
|
|
|
|
|
state := NewState()
|
|
|
|
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
if rootModule == nil {
|
|
|
|
t.Errorf("root module is nil; want valid object")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootModule.SetLocalValue("foo", cty.StringVal("foo value"))
|
|
|
|
rootModule.SetOutputValue("bar", cty.StringVal("bar value"), false)
|
|
|
|
rootModule.SetOutputValue("secret", cty.StringVal("secret value"), true)
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
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
|
|
|
&ResourceInstanceObjectSrc{
|
2018-06-08 02:27:57 +02:00
|
|
|
Status: ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-06-08 02:27:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
childModule := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
|
|
|
|
childModule.SetOutputValue("pizza", cty.StringVal("hawaiian"), false)
|
|
|
|
|
|
|
|
want := &State{
|
|
|
|
Modules: map[string]*Module{
|
|
|
|
"": {
|
|
|
|
Addr: addrs.RootModuleInstance,
|
|
|
|
LocalValues: map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("foo value"),
|
|
|
|
},
|
|
|
|
OutputValues: map[string]*OutputValue{
|
|
|
|
"bar": {
|
|
|
|
Value: cty.StringVal("bar value"),
|
|
|
|
Sensitive: false,
|
|
|
|
},
|
|
|
|
"secret": {
|
|
|
|
Value: cty.StringVal("secret value"),
|
|
|
|
Sensitive: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resources: map[string]*Resource{
|
|
|
|
"test_thing.baz": {
|
|
|
|
Addr: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "baz",
|
2020-03-13 18:51:55 +01:00
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
|
2018-06-08 02:27:57 +02:00
|
|
|
EachMode: EachList,
|
|
|
|
Instances: map[addrs.InstanceKey]*ResourceInstance{
|
|
|
|
addrs.IntKey(0): {
|
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
|
|
|
Current: &ResourceInstanceObjectSrc{
|
2018-06-08 02:27:57 +02:00
|
|
|
SchemaVersion: 1,
|
|
|
|
Status: ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
|
|
|
},
|
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
|
|
|
Deposed: map[DeposedKey]*ResourceInstanceObjectSrc{},
|
2018-06-08 02:27:57 +02:00
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderConfig: addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-06-08 02:27:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"module.child": {
|
|
|
|
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey),
|
|
|
|
LocalValues: map[string]cty.Value{},
|
|
|
|
OutputValues: map[string]*OutputValue{
|
|
|
|
"pizza": {
|
|
|
|
Value: cty.StringVal("hawaiian"),
|
|
|
|
Sensitive: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resources: map[string]*Resource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Our structure goes deep, so we need to temporarily override the
|
|
|
|
// deep package settings to ensure that we visit the full structure.
|
|
|
|
oldDeepDepth := deep.MaxDepth
|
|
|
|
oldDeepCompareUnexp := deep.CompareUnexportedFields
|
|
|
|
deep.MaxDepth = 50
|
|
|
|
deep.CompareUnexportedFields = true
|
|
|
|
defer func() {
|
|
|
|
deep.MaxDepth = oldDeepDepth
|
|
|
|
deep.CompareUnexportedFields = oldDeepCompareUnexp
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, problem := range deep.Equal(state, want) {
|
|
|
|
t.Error(problem)
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 16:32:12 +02:00
|
|
|
|
|
|
|
func TestStateDeepCopy(t *testing.T) {
|
|
|
|
state := NewState()
|
|
|
|
|
|
|
|
rootModule := state.RootModule()
|
|
|
|
if rootModule == nil {
|
|
|
|
t.Errorf("root module is nil; want valid object")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootModule.SetLocalValue("foo", cty.StringVal("foo value"))
|
|
|
|
rootModule.SetOutputValue("bar", cty.StringVal("bar value"), false)
|
|
|
|
rootModule.SetOutputValue("secret", cty.StringVal("secret value"), true)
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&ResourceInstanceObjectSrc{
|
|
|
|
Status: ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
|
|
|
Private: []byte("private data"),
|
2019-10-08 20:11:02 +02:00
|
|
|
Dependencies: []addrs.AbsResource{},
|
2019-06-04 16:32:12 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-06-04 16:32:12 +02:00
|
|
|
)
|
|
|
|
rootModule.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "bar",
|
|
|
|
}.Instance(addrs.IntKey(0)),
|
|
|
|
&ResourceInstanceObjectSrc{
|
|
|
|
Status: ObjectReady,
|
|
|
|
SchemaVersion: 1,
|
|
|
|
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
|
|
|
|
Private: []byte("private data"),
|
2019-10-08 20:11:02 +02:00
|
|
|
Dependencies: []addrs.AbsResource{
|
|
|
|
{
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
Resource: addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-04 16:32:12 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-06-04 16:32:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
childModule := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
|
|
|
|
childModule.SetOutputValue("pizza", cty.StringVal("hawaiian"), false)
|
|
|
|
|
|
|
|
stateCopy := state.DeepCopy()
|
|
|
|
if !state.Equal(stateCopy) {
|
|
|
|
t.Fatalf("\nexpected:\n%q\ngot:\n%q\n", state, stateCopy)
|
|
|
|
}
|
|
|
|
}
|