github.com/kevinklinger/open_terraform@v1.3.6/noninternal/command/unlock_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/kevinklinger/open_terraform/noninternal/backend/remote-state/inmem"
     8  	"github.com/mitchellh/cli"
     9  
    10  	legacy "github.com/kevinklinger/open_terraform/noninternal/legacy/terraform"
    11  )
    12  
    13  // Since we can't unlock a local state file, just test that calling unlock
    14  // doesn't fail.
    15  func TestUnlock(t *testing.T) {
    16  	td := t.TempDir()
    17  	os.MkdirAll(td, 0755)
    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 = legacy.WriteState(legacy.NewState(), 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  	view, _ := testView(t)
    37  	c := &UnlockCommand{
    38  		Meta: Meta{
    39  			testingOverrides: metaOverridesForProvider(p),
    40  			Ui:               ui,
    41  			View:             view,
    42  		},
    43  	}
    44  
    45  	args := []string{
    46  		"-force",
    47  		"LOCK_ID",
    48  	}
    49  
    50  	if code := c.Run(args); code != 1 {
    51  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
    52  	}
    53  
    54  	// make sure we don't crash with arguments in the wrong order
    55  	args = []string{
    56  		"LOCK_ID",
    57  		"-force",
    58  	}
    59  
    60  	if code := c.Run(args); code != cli.RunResultHelp {
    61  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
    62  	}
    63  }
    64  
    65  // Newly configured backend
    66  func TestUnlock_inmemBackend(t *testing.T) {
    67  	// Create a temporary working directory that is empty
    68  	td := t.TempDir()
    69  	testCopyDir(t, testFixturePath("backend-inmem-locked"), td)
    70  	defer testChdir(t, td)()
    71  	defer inmem.Reset()
    72  
    73  	// init backend
    74  	ui := new(cli.MockUi)
    75  	view, _ := testView(t)
    76  	ci := &InitCommand{
    77  		Meta: Meta{
    78  			Ui:   ui,
    79  			View: view,
    80  		},
    81  	}
    82  	if code := ci.Run(nil); code != 0 {
    83  		t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter)
    84  	}
    85  
    86  	ui = new(cli.MockUi)
    87  	c := &UnlockCommand{
    88  		Meta: Meta{
    89  			Ui:   ui,
    90  			View: view,
    91  		},
    92  	}
    93  
    94  	// run with the incorrect lock ID
    95  	args := []string{
    96  		"-force",
    97  		"LOCK_ID",
    98  	}
    99  
   100  	if code := c.Run(args); code == 0 {
   101  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
   102  	}
   103  
   104  	ui = new(cli.MockUi)
   105  	c = &UnlockCommand{
   106  		Meta: Meta{
   107  			Ui:   ui,
   108  			View: view,
   109  		},
   110  	}
   111  
   112  	// lockID set in the test fixture
   113  	args[1] = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"
   114  	if code := c.Run(args); code != 0 {
   115  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
   116  	}
   117  
   118  }