Skip to content

Commit

Permalink
Don't nil module maps during state upgrade
Browse files Browse the repository at this point in the history
The Outputs and Resources maps in the state modules are expected to be
non-nil, and initialized that way when a new module is added to the
state.  The V1->V2 upgrade was setting the maps to nil if the len == 0.
  • Loading branch information
jbardin committed Jun 29, 2016
1 parent 7abbb0f commit 1b75c51
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
7 changes: 1 addition & 6 deletions terraform/state_upgrade_v1_to_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package terraform

import (
"fmt"

"github.com/mitchellh/copystructure"
)

Expand Down Expand Up @@ -76,9 +77,6 @@ func (old *moduleStateV1) upgradeToV2() (*ModuleState, error) {
Sensitive: false,
}
}
if len(outputs) == 0 {
outputs = nil
}

resources := make(map[string]*ResourceState)
for key, oldResource := range old.Resources {
Expand All @@ -88,9 +86,6 @@ func (old *moduleStateV1) upgradeToV2() (*ModuleState, error) {
}
resources[key] = upgraded
}
if len(resources) == 0 {
resources = nil
}

dependencies, err := copystructure.Copy(old.Dependencies)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions terraform/upgrade_state_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,54 @@ func TestReadUpgradeStateV1toV3_outputs(t *testing.T) {
}
}

// Upgrading the state should not lose empty module Outputs and Resources maps
// during upgrade. The init for a new module initializes new maps, so we may not
// be expecting to check for a nil map.
func TestReadUpgradeStateV1toV3_emptyState(t *testing.T) {
// ReadState should transparently detect the old version but will upgrade
// it on Write.
orig, err := ReadStateV1([]byte(testV1EmptyState))
if err != nil {
t.Fatalf("err: %s", err)
}

stateV2, err := upgradeStateV1ToV2(orig)
for _, m := range stateV2.Modules {
if m.Resources == nil {
t.Fatal("V1 to V2 upgrade lost module.Resources")
}
if m.Outputs == nil {
t.Fatal("V1 to V2 upgrade lost module.Outputs")
}
}

stateV3, err := upgradeStateV2ToV3(stateV2)
for _, m := range stateV3.Modules {
if m.Resources == nil {
t.Fatal("V2 to V3 upgrade lost module.Resources")
}
if m.Outputs == nil {
t.Fatal("V2 to V3 upgrade lost module.Outputs")
}
}

}

const testV1EmptyState = `{
"version": 1,
"serial": 0,
"modules": [
{
"path": [
"root"
],
"outputs": {},
"resources": {}
}
]
}
`

const testV1State = `{
"version": 1,
"serial": 9,
Expand Down

0 comments on commit 1b75c51

Please sign in to comment.