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"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/remote"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/states/statefile"
)
func TestRemoteClient_impl(t *testing.T) {
@ -46,14 +47,13 @@ func TestRemoteClient_withRunID(t *testing.T) {
client := testRemoteClient(t)
// Create a new empty state.
state := bytes.NewBuffer(nil)
if err := terraform.WriteState(terraform.NewState(), state); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
sf := statefile.New(states.NewState(), "", 0)
var buf bytes.Buffer
statefile.Write(sf, &buf)
// Store the new state to verify (this will be done
// 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)
}
}

View File

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

View File

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

View File

@ -1,23 +1,25 @@
{
"version": 3,
"terraform_version": "0.7.0",
"serial": 3,
"modules": [
{
"path": [
"root"
],
"version": 4,
"terraform_version": "0.13.0",
"serial": 0,
"lineage": "",
"outputs": {
"computed_map": {
"sensitive": false,
"type": "map",
"type": [
"map",
"string"
],
"value": {
"key1": "value1"
}
},
"computed_set": {
"sensitive": false,
"type": "list",
"type": [
"list",
"string"
],
"value": [
"setval1",
"setval2"
@ -25,7 +27,10 @@
},
"map": {
"sensitive": false,
"type": "map",
"type": [
"map",
"string"
],
"value": {
"key": "test",
"test": "test"
@ -33,56 +38,33 @@
},
"set": {
"sensitive": false,
"type": "list",
"type": [
"list",
"string"
],
"value": [
"test1",
"test2"
]
}
},
"resources": {
"test_resource.main": {
"resources": [
{
"mode": "managed",
"type": "test_resource",
"primary": {
"id": "testId",
"name": "main",
"each": "list",
"provider": "provider[\"registry.terraform.io/hashicorp/test\"]",
"instances": [
{
"index_key": 0,
"schema_version": 0,
"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"
}
}
}
}
"id": "testId"
},
"private": "bnVsbA=="
}
]
}
]
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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