github.com/xyziemba/terraform@v0.7.1-0.20160816223025-3ea544774db1/command/state_mv_test.go (about)

     1  package command
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  func TestStateMv(t *testing.T) {
    12  	state := &terraform.State{
    13  		Modules: []*terraform.ModuleState{
    14  			&terraform.ModuleState{
    15  				Path: []string{"root"},
    16  				Resources: map[string]*terraform.ResourceState{
    17  					"test_instance.foo": &terraform.ResourceState{
    18  						Type: "test_instance",
    19  						Primary: &terraform.InstanceState{
    20  							ID: "bar",
    21  							Attributes: map[string]string{
    22  								"foo": "value",
    23  								"bar": "value",
    24  							},
    25  						},
    26  					},
    27  
    28  					"test_instance.baz": &terraform.ResourceState{
    29  						Type: "test_instance",
    30  						Primary: &terraform.InstanceState{
    31  							ID: "foo",
    32  							Attributes: map[string]string{
    33  								"foo": "value",
    34  								"bar": "value",
    35  							},
    36  						},
    37  					},
    38  				},
    39  			},
    40  		},
    41  	}
    42  
    43  	statePath := testStateFile(t, state)
    44  
    45  	p := testProvider()
    46  	ui := new(cli.MockUi)
    47  	c := &StateMvCommand{
    48  		Meta: Meta{
    49  			ContextOpts: testCtxConfig(p),
    50  			Ui:          ui,
    51  		},
    52  	}
    53  
    54  	args := []string{
    55  		"-state", statePath,
    56  		"test_instance.foo",
    57  		"test_instance.bar",
    58  	}
    59  	if code := c.Run(args); code != 0 {
    60  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    61  	}
    62  
    63  	// Test it is correct
    64  	testStateOutput(t, statePath, testStateMvOutput)
    65  
    66  	// Test we have backups
    67  	backups := testStateBackups(t, filepath.Dir(statePath))
    68  	if len(backups) != 1 {
    69  		t.Fatalf("bad: %#v", backups)
    70  	}
    71  	testStateOutput(t, backups[0], testStateMvOutputOriginal)
    72  }
    73  
    74  func TestStateMv_stateOutNew(t *testing.T) {
    75  	state := &terraform.State{
    76  		Modules: []*terraform.ModuleState{
    77  			&terraform.ModuleState{
    78  				Path: []string{"root"},
    79  				Resources: map[string]*terraform.ResourceState{
    80  					"test_instance.foo": &terraform.ResourceState{
    81  						Type: "test_instance",
    82  						Primary: &terraform.InstanceState{
    83  							ID: "bar",
    84  							Attributes: map[string]string{
    85  								"foo": "value",
    86  								"bar": "value",
    87  							},
    88  						},
    89  					},
    90  				},
    91  			},
    92  		},
    93  	}
    94  
    95  	statePath := testStateFile(t, state)
    96  	stateOutPath := statePath + ".out"
    97  
    98  	p := testProvider()
    99  	ui := new(cli.MockUi)
   100  	c := &StateMvCommand{
   101  		Meta: Meta{
   102  			ContextOpts: testCtxConfig(p),
   103  			Ui:          ui,
   104  		},
   105  	}
   106  
   107  	args := []string{
   108  		"-state", statePath,
   109  		"-state-out", stateOutPath,
   110  		"test_instance.foo",
   111  		"test_instance.bar",
   112  	}
   113  	if code := c.Run(args); code != 0 {
   114  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   115  	}
   116  
   117  	// Test it is correct
   118  	testStateOutput(t, stateOutPath, testStateMvOutput_stateOut)
   119  	testStateOutput(t, statePath, testStateMvOutput_stateOutSrc)
   120  
   121  	// Test we have backups
   122  	backups := testStateBackups(t, filepath.Dir(statePath))
   123  	if len(backups) != 1 {
   124  		t.Fatalf("bad: %#v", backups)
   125  	}
   126  	testStateOutput(t, backups[0], testStateMvOutput_stateOutOriginal)
   127  }
   128  
   129  func TestStateMv_stateOutExisting(t *testing.T) {
   130  	stateSrc := &terraform.State{
   131  		Modules: []*terraform.ModuleState{
   132  			&terraform.ModuleState{
   133  				Path: []string{"root"},
   134  				Resources: map[string]*terraform.ResourceState{
   135  					"test_instance.foo": &terraform.ResourceState{
   136  						Type: "test_instance",
   137  						Primary: &terraform.InstanceState{
   138  							ID: "bar",
   139  							Attributes: map[string]string{
   140  								"foo": "value",
   141  								"bar": "value",
   142  							},
   143  						},
   144  					},
   145  				},
   146  			},
   147  		},
   148  	}
   149  
   150  	statePath := testStateFile(t, stateSrc)
   151  
   152  	stateDst := &terraform.State{
   153  		Modules: []*terraform.ModuleState{
   154  			&terraform.ModuleState{
   155  				Path: []string{"root"},
   156  				Resources: map[string]*terraform.ResourceState{
   157  					"test_instance.qux": &terraform.ResourceState{
   158  						Type: "test_instance",
   159  						Primary: &terraform.InstanceState{
   160  							ID: "bar",
   161  						},
   162  					},
   163  				},
   164  			},
   165  		},
   166  	}
   167  
   168  	stateOutPath := testStateFile(t, stateDst)
   169  
   170  	p := testProvider()
   171  	ui := new(cli.MockUi)
   172  	c := &StateMvCommand{
   173  		Meta: Meta{
   174  			ContextOpts: testCtxConfig(p),
   175  			Ui:          ui,
   176  		},
   177  	}
   178  
   179  	args := []string{
   180  		"-state", statePath,
   181  		"-state-out", stateOutPath,
   182  		"test_instance.foo",
   183  		"test_instance.bar",
   184  	}
   185  	if code := c.Run(args); code != 0 {
   186  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   187  	}
   188  
   189  	// Test it is correct
   190  	testStateOutput(t, stateOutPath, testStateMvExisting_stateDst)
   191  	testStateOutput(t, statePath, testStateMvExisting_stateSrc)
   192  
   193  	// Test we have backups
   194  	backups := testStateBackups(t, filepath.Dir(statePath))
   195  	if len(backups) != 1 {
   196  		t.Fatalf("bad: %#v", backups)
   197  	}
   198  	testStateOutput(t, backups[0], testStateMvExisting_stateSrcOriginal)
   199  
   200  	backups = testStateBackups(t, filepath.Dir(stateOutPath))
   201  	if len(backups) != 1 {
   202  		t.Fatalf("bad: %#v", backups)
   203  	}
   204  	testStateOutput(t, backups[0], testStateMvExisting_stateDstOriginal)
   205  }
   206  
   207  func TestStateMv_noState(t *testing.T) {
   208  	tmp, cwd := testCwd(t)
   209  	defer testFixCwd(t, tmp, cwd)
   210  
   211  	p := testProvider()
   212  	ui := new(cli.MockUi)
   213  	c := &StateMvCommand{
   214  		Meta: Meta{
   215  			ContextOpts: testCtxConfig(p),
   216  			Ui:          ui,
   217  		},
   218  	}
   219  
   220  	args := []string{"from", "to"}
   221  	if code := c.Run(args); code != 1 {
   222  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   223  	}
   224  }
   225  
   226  const testStateMvOutputOriginal = `
   227  test_instance.baz:
   228    ID = foo
   229    bar = value
   230    foo = value
   231  test_instance.foo:
   232    ID = bar
   233    bar = value
   234    foo = value
   235  `
   236  
   237  const testStateMvOutput = `
   238  test_instance.bar:
   239    ID = bar
   240    bar = value
   241    foo = value
   242  test_instance.baz:
   243    ID = foo
   244    bar = value
   245    foo = value
   246  `
   247  
   248  const testStateMvOutput_stateOut = `
   249  test_instance.bar:
   250    ID = bar
   251    bar = value
   252    foo = value
   253  `
   254  
   255  const testStateMvOutput_stateOutSrc = `
   256  <no state>
   257  `
   258  
   259  const testStateMvOutput_stateOutOriginal = `
   260  test_instance.foo:
   261    ID = bar
   262    bar = value
   263    foo = value
   264  `
   265  
   266  const testStateMvExisting_stateSrc = `
   267  <no state>
   268  `
   269  
   270  const testStateMvExisting_stateDst = `
   271  test_instance.bar:
   272    ID = bar
   273    bar = value
   274    foo = value
   275  test_instance.qux:
   276    ID = bar
   277  `
   278  
   279  const testStateMvExisting_stateSrcOriginal = `
   280  test_instance.foo:
   281    ID = bar
   282    bar = value
   283    foo = value
   284  `
   285  
   286  const testStateMvExisting_stateDstOriginal = `
   287  test_instance.qux:
   288    ID = bar
   289  `