refactor tests to use modern states.State in favor of terraform.State where possible

This commit is contained in:
Kristin Laemmert 2020-09-30 16:07:54 -04:00
parent b0f58479c5
commit 479655ad47
39 changed files with 261 additions and 407 deletions

View File

@ -6,8 +6,9 @@ import (
"testing" "testing"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/remote" "github.com/hashicorp/terraform/states/remote"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/states/statefile"
) )
func TestRemoteClient_impl(t *testing.T) { func TestRemoteClient_impl(t *testing.T) {
@ -46,14 +47,13 @@ func TestRemoteClient_withRunID(t *testing.T) {
client := testRemoteClient(t) client := testRemoteClient(t)
// Create a new empty state. // Create a new empty state.
state := bytes.NewBuffer(nil) sf := statefile.New(states.NewState(), "", 0)
if err := terraform.WriteState(terraform.NewState(), state); err != nil { var buf bytes.Buffer
t.Fatalf("expected no error, got: %v", err) statefile.Write(sf, &buf)
}
// Store the new state to verify (this will be done // Store the new state to verify (this will be done
// by the mock that is used) that the run ID is set. // by the mock that is used) that the run ID is set.
if err := client.Put(state.Bytes()); err != nil { if err := client.Put(buf.Bytes()); err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
} }
} }

View File

@ -133,8 +133,8 @@ func TestState_basic(t *testing.T) {
"path": cty.StringVal("./testdata/null_outputs.tfstate"), "path": cty.StringVal("./testdata/null_outputs.tfstate"),
}), }),
"outputs": cty.ObjectVal(map[string]cty.Value{ "outputs": cty.ObjectVal(map[string]cty.Value{
"map": cty.NullVal(cty.DynamicPseudoType), "map": cty.NullVal(cty.Map(cty.String)),
"list": cty.NullVal(cty.DynamicPseudoType), "list": cty.NullVal(cty.List(cty.String)),
}), }),
"defaults": cty.NullVal(cty.DynamicPseudoType), "defaults": cty.NullVal(cty.DynamicPseudoType),
"workspace": cty.NullVal(cty.String), "workspace": cty.NullVal(cty.String),

View File

@ -1,7 +1,12 @@
{ {
"version": 1, "version": 4,
"modules": [{ "terraform_version": "0.13.0",
"path": ["root"], "serial": 0,
"outputs": { "foo": "bar" } "lineage": "",
}] "outputs": {
"foo": {
"value": "bar",
"type": "string"
}
}
} }

View File

@ -1,88 +1,70 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.7.0", "terraform_version": "0.13.0",
"serial": 3, "serial": 0,
"modules": [ "lineage": "",
{ "outputs": {
"path": [ "computed_map": {
"root" "sensitive": false,
"type": [
"map",
"string"
], ],
"outputs": { "value": {
"computed_map": { "key1": "value1"
"sensitive": false,
"type": "map",
"value": {
"key1": "value1"
}
},
"computed_set": {
"sensitive": false,
"type": "list",
"value": [
"setval1",
"setval2"
]
},
"map": {
"sensitive": false,
"type": "map",
"value": {
"key": "test",
"test": "test"
}
},
"set": {
"sensitive": false,
"type": "list",
"value": [
"test1",
"test2"
]
}
},
"resources": {
"test_resource.main": {
"type": "test_resource",
"primary": {
"id": "testId",
"attributes": {
"computed_list.#": "2",
"computed_list.0": "listval1",
"computed_list.1": "listval2",
"computed_map.%": "1",
"computed_map.key1": "value1",
"computed_read_only": "value_from_api",
"computed_read_only_force_new": "value_from_api",
"computed_set.#": "2",
"computed_set.2337322984": "setval1",
"computed_set.307881554": "setval2",
"id": "testId",
"list_of_map.#": "2",
"list_of_map.0.%": "2",
"list_of_map.0.key1": "value1",
"list_of_map.0.key2": "value2",
"list_of_map.1.%": "2",
"list_of_map.1.key3": "value3",
"list_of_map.1.key4": "value4",
"map.%": "2",
"map.key": "test",
"map.test": "test",
"map_that_look_like_set.%": "2",
"map_that_look_like_set.12352223": "hello",
"map_that_look_like_set.36234341": "world",
"optional_computed_map.%": "0",
"required": "Hello World",
"required_map.%": "3",
"required_map.key1": "value1",
"required_map.key2": "value2",
"required_map.key3": "value3",
"set.#": "2",
"set.2326977762": "test1",
"set.331058520": "test2"
}
}
}
} }
},
"computed_set": {
"sensitive": false,
"type": [
"list",
"string"
],
"value": [
"setval1",
"setval2"
]
},
"map": {
"sensitive": false,
"type": [
"map",
"string"
],
"value": {
"key": "test",
"test": "test"
}
},
"set": {
"sensitive": false,
"type": [
"list",
"string"
],
"value": [
"test1",
"test2"
]
}
},
"resources": [
{
"mode": "managed",
"type": "test_resource",
"name": "main",
"each": "list",
"provider": "provider[\"registry.terraform.io/hashicorp/test\"]",
"instances": [
{
"index_key": 0,
"schema_version": 0,
"attributes": {
"id": "testId"
},
"private": "bnVsbA=="
}
]
} }
] ]
} }

View File

@ -1,13 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.7.0", "terraform_version": "0.13.0",
"serial": 3, "serial": 0,
"modules": [ "lineage": ""
{
"path": [
"root"
],
"outputs": {}
}
]
} }

View File

@ -1,24 +1,22 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.7.0", "terraform_version": "0.13.0",
"serial": 3, "serial": 0,
"modules": [ "lineage": "",
{ "outputs": {
"path": [ "map": {
"root" "value": null,
], "type": [
"outputs": { "map",
"map": { "string"
"sensitive": false, ]
"type": "map", },
"value": null "list": {
}, "value": null,
"list": { "type": [
"sensitive": false, "list",
"type": "list", "string"
"value": null ]
}
}
} }
] }
} }

View File

@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/states" "github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/terraform"
) )
func TestStateMv(t *testing.T) { func TestStateMv(t *testing.T) {
@ -1123,36 +1122,29 @@ func TestStateMv_fromBackendToLocal(t *testing.T) {
defer os.RemoveAll(td) defer os.RemoveAll(td)
defer testChdir(t, td)() defer testChdir(t, td)()
state := &terraform.State{ state := states.NewState()
Modules: []*terraform.ModuleState{ state.Module(addrs.RootModuleInstance).SetResourceInstanceCurrent(
&terraform.ModuleState{ mustResourceAddr("test_instance.foo").Resource.Instance(addrs.NoKey),
Path: []string{"root"}, &states.ResourceInstanceObjectSrc{
Resources: map[string]*terraform.ResourceState{ AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
"test_instance.foo": &terraform.ResourceState{ Status: states.ObjectReady,
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
Attributes: map[string]string{
"foo": "value",
"bar": "value",
},
},
},
"test_instance.baz": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "foo",
Attributes: map[string]string{
"foo": "value",
"bar": "value",
},
},
},
},
},
}, },
} addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("test"),
Module: addrs.RootModule,
},
)
state.Module(addrs.RootModuleInstance).SetResourceInstanceCurrent(
mustResourceAddr("test_instance.baz").Resource.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("test"),
Module: addrs.RootModule,
},
)
// the local backend state file is "foo" // the local backend state file is "foo"
statePath := "local-state.tfstate" statePath := "local-state.tfstate"
@ -1166,7 +1158,7 @@ func TestStateMv_fromBackendToLocal(t *testing.T) {
} }
defer f.Close() defer f.Close()
if err := terraform.WriteState(state, f); err != nil { if err := writeStateForTesting(state, f); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change", "lineage": "backend-change",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change", "lineage": "backend-change",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change-env2", "lineage": "backend-change-env2",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change-env1", "lineage": "backend-change-env1",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change-env2", "lineage": "backend-change-env2",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change-env2", "lineage": "backend-change-env2",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change", "lineage": "backend-change",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change-env2", "lineage": "backend-change-env2",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change", "lineage": "backend-change",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-change", "lineage": "backend-change",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "legacy" "lineage": "legacy"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "configured" "lineage": "configured"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-new-legacy", "lineage": "backend-new-legacy",
"remote": { "remote": {

View File

@ -1,16 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 8, "serial": 8,
"lineage": "remote", "lineage": "remote"
"modules": [
{
"path": [
"root"
],
"outputs": {},
"resources": {},
"depends_on": []
}
]
} }

View File

@ -1,21 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.8.2",
"serial": 8, "serial": 8,
"lineage": "local", "lineage": "local",
"modules": [ "outputs": {
{ "foo": {
"path": [ "type": "string",
"root" "value": "bar"
],
"outputs": {
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {},
"depends_on": []
} }
] }
} }

View File

@ -1,21 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 8, "serial": 8,
"lineage": "backend-new-migrate", "lineage": "backend-new-migrate",
"modules": [ "outputs": {
{ "foo": {
"path": [ "type": "string",
"root" "value": "bar"
],
"outputs": {
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {},
"depends_on": []
} }
] }
} }

View File

@ -1,5 +1,5 @@
{ {
"version": 3, "version": 4,
"serial": 0, "serial": 0,
"lineage": "hello" "lineage": "hello"
} }

View File

@ -1,5 +1,5 @@
{ {
"version": 3, "version": 4,
"serial": 0, "serial": 0,
"lineage": "hello" "lineage": "hello"
} }

View File

@ -1,5 +1,5 @@
{ {
"version": 3, "version": 4,
"serial": 0, "serial": 0,
"lineage": "different" "lineage": "different"
} }

View File

@ -1,5 +1,5 @@
{ {
"version": 3, "version": 4,
"serial": 0, "serial": 0,
"lineage": "hello" "lineage": "hello"
} }

View File

@ -1,5 +1,5 @@
{ {
"version": 3, "version": 4,
"serial": 0, "serial": 0,
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03", "lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
"remote": { "remote": {
@ -7,15 +7,5 @@
"config": { "config": {
"path": "local-state.tfstate" "path": "local-state.tfstate"
} }
}, }
"modules": [
{
"path": [
"root"
],
"outputs": {},
"resources": {},
"depends_on": []
}
]
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "hello" "lineage": "hello"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "hello" "lineage": "hello"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 10, "serial": 10,
"lineage": "hello" "lineage": "hello"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend-unchanged-with-legacy" "lineage": "backend-unchanged-with-legacy"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "configured" "lineage": "configured"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "configuredUnchanged" "lineage": "configuredUnchanged"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "legacy" "lineage": "legacy"
} }

View File

@ -1,6 +1,6 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "backend" "lineage": "backend"
} }

View File

@ -1,18 +1,12 @@
{ {
"version": 3, "version": 4,
"terraform_version": "0.8.2", "terraform_version": "0.14.0",
"serial": 7, "serial": 7,
"lineage": "configuredUnset", "lineage": "configuredUnset",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,17 +1,11 @@
{ {
"version": 3, "version": 4,
"serial": 1, "serial": 1,
"lineage": "mismatch", "lineage": "mismatch",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "bar"
"foo": {
"type": "string",
"value": "bar"
}
},
"resources": {}
} }
] }
} }

View File

@ -1,17 +1,11 @@
{ {
"version": 3, "version": 4,
"serial": 2, "serial": 2,
"lineage": "hello", "lineage": "hello",
"modules": [ "outputs": {
{ "foo": {
"path": ["root"], "type": "string",
"outputs": { "value": "baz"
"foo": {
"type": "string",
"value": "baz"
}
},
"resources": {}
} }
] }
} }