terraform/states/resource_test.go

57 lines
1.6 KiB
Go
Raw Normal View History

states: New package with modern models for Terraform state Our previous state models in the "terraform" package had a few limitations that are addressed here: - Instance attributes were stored as map[string]string with dot-separated keys representing traversals through a data structure. Now that we have a full type system, it's preferable to store it as a real data structure. - The existing state structures skipped over the "resource" concept and went straight to resource instance, requiring heuristics to decide whether a particular resource should appear as a single object or as a list of objects when used in configuration expressions. - Related to the previous point, the state models also used incorrect terminology where "ResourceState" was really a resource instance state and "InstanceState" was really the state of a particular remote object associated with an instance. These new models use the correct names for each of these, introducing the idea of a "ResourceInstanceObject" as the local record of a remote object associated with an instance. This is a first pass at fleshing out a new model for state. Undoubtedly there will be further iterations of this as we work on integrating these new models into the "terraform" package. These new model types no longer serve double-duty as a description of the JSON state file format, since they are for in-memory use only. A subsequent commit will introduce a separate package that deals with persisting state to files and reloading those files later.
2018-06-08 02:27:57 +02:00
package states
import (
"testing"
)
func TestResourceInstanceDeposeCurrentObject(t *testing.T) {
obj := &ResourceInstanceObject{
// Empty for the sake of this test, because we're just going to
// compare by pointer below anyway.
}
is := NewResourceInstance()
is.Current = obj
var dk DeposedKey
t.Run("first depose", func(t *testing.T) {
dk = is.deposeCurrentObject() // dk is randomly-generated but should be eight characters long
states: New package with modern models for Terraform state Our previous state models in the "terraform" package had a few limitations that are addressed here: - Instance attributes were stored as map[string]string with dot-separated keys representing traversals through a data structure. Now that we have a full type system, it's preferable to store it as a real data structure. - The existing state structures skipped over the "resource" concept and went straight to resource instance, requiring heuristics to decide whether a particular resource should appear as a single object or as a list of objects when used in configuration expressions. - Related to the previous point, the state models also used incorrect terminology where "ResourceState" was really a resource instance state and "InstanceState" was really the state of a particular remote object associated with an instance. These new models use the correct names for each of these, introducing the idea of a "ResourceInstanceObject" as the local record of a remote object associated with an instance. This is a first pass at fleshing out a new model for state. Undoubtedly there will be further iterations of this as we work on integrating these new models into the "terraform" package. These new model types no longer serve double-duty as a description of the JSON state file format, since they are for in-memory use only. A subsequent commit will introduce a separate package that deals with persisting state to files and reloading those files later.
2018-06-08 02:27:57 +02:00
t.Logf("deposedKey is %q", dk)
if got := is.Current; got != nil {
t.Errorf("current is %#v; want nil", got)
}
if got, want := is.Deposed[dk], obj; got != want {
t.Errorf("deposed object pointer is %#v; want %#v", got, want)
}
if got, want := len(is.Deposed), 1; got != want {
t.Errorf("wrong len(is.Deposed) %d; want %d", got, want)
}
if got, want := len(dk), 8; got != want {
t.Errorf("wrong len(deposedkey) %d; want %d", got, want)
}
})
t.Run("second depose", func(t *testing.T) {
notDK := is.deposeCurrentObject()
states: New package with modern models for Terraform state Our previous state models in the "terraform" package had a few limitations that are addressed here: - Instance attributes were stored as map[string]string with dot-separated keys representing traversals through a data structure. Now that we have a full type system, it's preferable to store it as a real data structure. - The existing state structures skipped over the "resource" concept and went straight to resource instance, requiring heuristics to decide whether a particular resource should appear as a single object or as a list of objects when used in configuration expressions. - Related to the previous point, the state models also used incorrect terminology where "ResourceState" was really a resource instance state and "InstanceState" was really the state of a particular remote object associated with an instance. These new models use the correct names for each of these, introducing the idea of a "ResourceInstanceObject" as the local record of a remote object associated with an instance. This is a first pass at fleshing out a new model for state. Undoubtedly there will be further iterations of this as we work on integrating these new models into the "terraform" package. These new model types no longer serve double-duty as a description of the JSON state file format, since they are for in-memory use only. A subsequent commit will introduce a separate package that deals with persisting state to files and reloading those files later.
2018-06-08 02:27:57 +02:00
if notDK != NotDeposed {
t.Errorf("got deposedKey %q; want NotDeposed", notDK)
}
// Make sure we really did abort early, and haven't corrupted the
// state somehow.
if got := is.Current; got != nil {
t.Errorf("current is %#v; want nil", got)
}
if got, want := is.Deposed[dk], obj; got != want {
t.Errorf("deposed object pointer is %#v; want %#v", got, want)
}
if got, want := len(is.Deposed), 1; got != want {
t.Errorf("wrong len(is.Deposed) %d; want %d", got, want)
}
if got, want := len(dk), 8; got != want {
t.Errorf("wrong len(deposedkey) %d; want %d", got, want)
}
})
}