github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/command/unlock_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/copy"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  // Since we can't unlock a local state file, just test that calling unlock
    13  // doesn't fail.
    14  func TestUnlock(t *testing.T) {
    15  	td := tempDir(t)
    16  	os.MkdirAll(td, 0755)
    17  	defer os.RemoveAll(td)
    18  	defer testChdir(t, td)()
    19  
    20  	// Write the legacy state
    21  	statePath := DefaultStateFilename
    22  	{
    23  		f, err := os.Create(statePath)
    24  		if err != nil {
    25  			t.Fatalf("err: %s", err)
    26  		}
    27  		err = terraform.WriteState(testState(), f)
    28  		f.Close()
    29  		if err != nil {
    30  			t.Fatalf("err: %s", err)
    31  		}
    32  	}
    33  
    34  	p := testProvider()
    35  	ui := new(cli.MockUi)
    36  	c := &UnlockCommand{
    37  		Meta: Meta{
    38  			ContextOpts: testCtxConfig(p),
    39  			Ui:          ui,
    40  		},
    41  	}
    42  
    43  	args := []string{
    44  		"-force",
    45  		"LOCK_ID",
    46  	}
    47  
    48  	if code := c.Run(args); code != 1 {
    49  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
    50  	}
    51  }
    52  
    53  // Newly configured backend
    54  func TestUnlock_inmemBackend(t *testing.T) {
    55  	// Create a temporary working directory that is empty
    56  	td := tempDir(t)
    57  	copy.CopyDir(testFixturePath("backend-inmem-locked"), td)
    58  	defer os.RemoveAll(td)
    59  	defer testChdir(t, td)()
    60  
    61  	// init backend
    62  	ui := new(cli.MockUi)
    63  	ci := &InitCommand{
    64  		Meta: Meta{
    65  			Ui: ui,
    66  		},
    67  	}
    68  	if code := ci.Run(nil); code != 0 {
    69  		t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter)
    70  	}
    71  
    72  	ui = new(cli.MockUi)
    73  	c := &UnlockCommand{
    74  		Meta: Meta{
    75  			Ui: ui,
    76  		},
    77  	}
    78  
    79  	// run with the incorrect lock ID
    80  	args := []string{
    81  		"-force",
    82  		"LOCK_ID",
    83  	}
    84  
    85  	if code := c.Run(args); code == 0 {
    86  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
    87  	}
    88  
    89  	ui = new(cli.MockUi)
    90  	c = &UnlockCommand{
    91  		Meta: Meta{
    92  			Ui: ui,
    93  		},
    94  	}
    95  
    96  	// lockID set in the test fixture
    97  	args[1] = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"
    98  	if code := c.Run(args); code != 0 {
    99  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
   100  	}
   101  
   102  }