command: test for moving resource with count [GH-7797]
This commit is contained in:
parent
8afbb0ee0e
commit
0d1ea84d39
|
@ -139,6 +139,25 @@ func (c *StateMvCommand) addableResult(results []*terraform.StateFilterResult) i
|
|||
}
|
||||
|
||||
return result
|
||||
|
||||
case *terraform.ResourceState:
|
||||
// If a module state then we should add the full list of modules
|
||||
result := []*terraform.ResourceState{v}
|
||||
if len(results) > 1 {
|
||||
for _, r := range results[1:] {
|
||||
rs, ok := r.Value.(*terraform.ResourceState)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if rs.Type == v.Type {
|
||||
result = append(result, rs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
default:
|
||||
// By default just add the first result
|
||||
return v
|
||||
|
|
|
@ -223,6 +223,83 @@ func TestStateMv_noState(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStateMv_stateOutNew_count(t *testing.T) {
|
||||
state := &terraform.State{
|
||||
Modules: []*terraform.ModuleState{
|
||||
&terraform.ModuleState{
|
||||
Path: []string{"root"},
|
||||
Resources: map[string]*terraform.ResourceState{
|
||||
"test_instance.foo.0": &terraform.ResourceState{
|
||||
Type: "test_instance",
|
||||
Primary: &terraform.InstanceState{
|
||||
ID: "foo",
|
||||
Attributes: map[string]string{
|
||||
"foo": "value",
|
||||
"bar": "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
"test_instance.foo.1": &terraform.ResourceState{
|
||||
Type: "test_instance",
|
||||
Primary: &terraform.InstanceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"foo": "value",
|
||||
"bar": "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
"test_instance.bar": &terraform.ResourceState{
|
||||
Type: "test_instance",
|
||||
Primary: &terraform.InstanceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"foo": "value",
|
||||
"bar": "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
statePath := testStateFile(t, state)
|
||||
stateOutPath := statePath + ".out"
|
||||
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(p),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
"-state-out", stateOutPath,
|
||||
"test_instance.foo",
|
||||
"test_instance.bar",
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
// Test it is correct
|
||||
testStateOutput(t, stateOutPath, testStateMvCount_stateOut)
|
||||
testStateOutput(t, statePath, testStateMvCount_stateOutSrc)
|
||||
|
||||
// Test we have backups
|
||||
backups := testStateBackups(t, filepath.Dir(statePath))
|
||||
if len(backups) != 1 {
|
||||
t.Fatalf("bad: %#v", backups)
|
||||
}
|
||||
testStateOutput(t, backups[0], testStateMvCount_stateOutOriginal)
|
||||
}
|
||||
|
||||
func TestStateMv_stateOutNew_nestedModule(t *testing.T) {
|
||||
state := &terraform.State{
|
||||
Modules: []*terraform.ModuleState{
|
||||
|
@ -326,6 +403,39 @@ test_instance.baz:
|
|||
foo = value
|
||||
`
|
||||
|
||||
const testStateMvCount_stateOut = `
|
||||
test_instance.bar.0:
|
||||
ID = foo
|
||||
bar = value
|
||||
foo = value
|
||||
test_instance.bar.1:
|
||||
ID = bar
|
||||
bar = value
|
||||
foo = value
|
||||
`
|
||||
|
||||
const testStateMvCount_stateOutSrc = `
|
||||
test_instance.bar:
|
||||
ID = bar
|
||||
bar = value
|
||||
foo = value
|
||||
`
|
||||
|
||||
const testStateMvCount_stateOutOriginal = `
|
||||
test_instance.bar:
|
||||
ID = bar
|
||||
bar = value
|
||||
foo = value
|
||||
test_instance.foo.0:
|
||||
ID = foo
|
||||
bar = value
|
||||
foo = value
|
||||
test_instance.foo.1:
|
||||
ID = bar
|
||||
bar = value
|
||||
foo = value
|
||||
`
|
||||
|
||||
const testStateMvNestedModule_stateOut = `
|
||||
<no state>
|
||||
module.bar:
|
||||
|
|
Loading…
Reference in New Issue