@@ -223,6 +223,87 @@ func TestStateMv_noState(t *testing.T) {
223223 }
224224}
225225
226+ func TestStateMv_stateOutNew_nestedModule (t * testing.T ) {
227+ state := & terraform.State {
228+ Modules : []* terraform.ModuleState {
229+ & terraform.ModuleState {
230+ Path : []string {"root" },
231+ Resources : map [string ]* terraform.ResourceState {},
232+ },
233+
234+ & terraform.ModuleState {
235+ Path : []string {"root" , "foo" },
236+ Resources : map [string ]* terraform.ResourceState {},
237+ },
238+
239+ & terraform.ModuleState {
240+ Path : []string {"root" , "foo" , "child1" },
241+ Resources : map [string ]* terraform.ResourceState {
242+ "test_instance.foo" : & terraform.ResourceState {
243+ Type : "test_instance" ,
244+ Primary : & terraform.InstanceState {
245+ ID : "bar" ,
246+ Attributes : map [string ]string {
247+ "foo" : "value" ,
248+ "bar" : "value" ,
249+ },
250+ },
251+ },
252+ },
253+ },
254+
255+ & terraform.ModuleState {
256+ Path : []string {"root" , "foo" , "child2" },
257+ Resources : map [string ]* terraform.ResourceState {
258+ "test_instance.foo" : & terraform.ResourceState {
259+ Type : "test_instance" ,
260+ Primary : & terraform.InstanceState {
261+ ID : "bar" ,
262+ Attributes : map [string ]string {
263+ "foo" : "value" ,
264+ "bar" : "value" ,
265+ },
266+ },
267+ },
268+ },
269+ },
270+ },
271+ }
272+
273+ statePath := testStateFile (t , state )
274+ stateOutPath := statePath + ".out"
275+
276+ p := testProvider ()
277+ ui := new (cli.MockUi )
278+ c := & StateMvCommand {
279+ Meta : Meta {
280+ ContextOpts : testCtxConfig (p ),
281+ Ui : ui ,
282+ },
283+ }
284+
285+ args := []string {
286+ "-state" , statePath ,
287+ "-state-out" , stateOutPath ,
288+ "module.foo" ,
289+ "module.bar" ,
290+ }
291+ if code := c .Run (args ); code != 0 {
292+ t .Fatalf ("bad: %d\n \n %s" , code , ui .ErrorWriter .String ())
293+ }
294+
295+ // Test it is correct
296+ testStateOutput (t , stateOutPath , testStateMvNestedModule_stateOut )
297+ testStateOutput (t , statePath , testStateMvNestedModule_stateOutSrc )
298+
299+ // Test we have backups
300+ backups := testStateBackups (t , filepath .Dir (statePath ))
301+ if len (backups ) != 1 {
302+ t .Fatalf ("bad: %#v" , backups )
303+ }
304+ testStateOutput (t , backups [0 ], testStateMvNestedModule_stateOutOriginal )
305+ }
306+
226307const testStateMvOutputOriginal = `
227308test_instance.baz:
228309 ID = foo
@@ -245,6 +326,41 @@ test_instance.baz:
245326 foo = value
246327`
247328
329+ const testStateMvNestedModule_stateOut = `
330+ module.bar:
331+ <no state>
332+ module.bar.child1:
333+ test_instance.foo:
334+ ID = bar
335+ bar = value
336+ foo = value
337+ module.bar.child2:
338+ test_instance.foo:
339+ ID = bar
340+ bar = value
341+ foo = value
342+ `
343+
344+ const testStateMvNestedModule_stateOutSrc = `
345+ <no state>
346+ `
347+
348+ const testStateMvNestedModule_stateOutOriginal = `
349+ <no state>
350+ module.foo:
351+ <no state>
352+ module.foo.child1:
353+ test_instance.foo:
354+ ID = bar
355+ bar = value
356+ foo = value
357+ module.foo.child2:
358+ test_instance.foo:
359+ ID = bar
360+ bar = value
361+ foo = value
362+ `
363+
248364const testStateMvOutput_stateOut = `
249365test_instance.bar:
250366 ID = bar
0 commit comments