Add test/untaint tests with locked state

add missing lock-state flag to untaint
This commit is contained in:
James Bardin 2017-02-03 16:13:42 -05:00
parent 82e59cd826
commit cd96bb5aca
3 changed files with 91 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package command
import (
"os"
"strings"
"testing"
"github.com/hashicorp/terraform/terraform"
@ -44,6 +45,50 @@ func TestTaint(t *testing.T) {
testStateOutput(t, statePath, testTaintStr)
}
func TestTaint_lockedState(t *testing.T) {
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",
},
},
},
},
},
}
statePath := testStateFile(t, state)
unlock, err := testLockState(statePath)
if err != nil {
t.Fatal(err)
}
defer unlock()
ui := new(cli.MockUi)
c := &TaintCommand{
Meta: Meta{
Ui: ui,
},
}
args := []string{
"-state", statePath,
"test_instance.foo",
}
if code := c.Run(args); code == 0 {
t.Fatal("expected error")
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, "locked") {
t.Fatal("command output does not look like a lock error:", output)
}
}
func TestTaint_backup(t *testing.T) {
// Get a temp cwd
tmp, cwd := testCwd(t)

View File

@ -25,6 +25,7 @@ func (c *UntaintCommand) Run(args []string) int {
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
cmdFlags.BoolVar(&c.Meta.lockState, "state-lock", true, "lock state")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1

View File

@ -50,6 +50,51 @@ test_instance.foo:
testStateOutput(t, statePath, expected)
}
func TestUntaint_lockedState(t *testing.T) {
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",
Tainted: true,
},
},
},
},
},
}
statePath := testStateFile(t, state)
unlock, err := testLockState(statePath)
if err != nil {
t.Fatal(err)
}
defer unlock()
ui := new(cli.MockUi)
c := &UntaintCommand{
Meta: Meta{
Ui: ui,
},
}
args := []string{
"-state", statePath,
"test_instance.foo",
}
if code := c.Run(args); code == 0 {
t.Fatal("expected error")
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, "locked") {
t.Fatal("command output does not look like a lock error:", output)
}
}
func TestUntaint_backup(t *testing.T) {
// Get a temp cwd
tmp, cwd := testCwd(t)