github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/command/unlock_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/iaas-resource-provision/iaas-rpc/internal/backend/remote-state/inmem"
     8  	"github.com/mitchellh/cli"
     9  
    10  	legacy "github.com/iaas-resource-provision/iaas-rpc/internal/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 := tempDir(t)
    17  	os.MkdirAll(td, 0755)
    18  	defer os.RemoveAll(td)
    19  	defer testChdir(t, td)()
    20  
    21  	// Write the legacy state
    22  	statePath := DefaultStateFilename
    23  	{
    24  		f, err := os.Create(statePath)
    25  		if err != nil {
    26  			t.Fatalf("err: %s", err)
    27  		}
    28  		err = legacy.WriteState(legacy.NewState(), f)
    29  		f.Close()
    30  		if err != nil {
    31  			t.Fatalf("err: %s", err)
    32  		}
    33  	}
    34  
    35  	p := testProvider()
    36  	ui := new(cli.MockUi)
    37  	view, _ := testView(t)
    38  	c := &UnlockCommand{
    39  		Meta: Meta{
    40  			testingOverrides: metaOverridesForProvider(p),
    41  			Ui:               ui,
    42  			View:             view,
    43  		},
    44  	}
    45  
    46  	args := []string{
    47  		"-force",
    48  		"LOCK_ID",
    49  	}
    50  
    51  	if code := c.Run(args); code != 1 {
    52  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
    53  	}
    54  
    55  	// make sure we don't crash with arguments in the wrong order
    56  	args = []string{
    57  		"LOCK_ID",
    58  		"-force",
    59  	}
    60  
    61  	if code := c.Run(args); code != cli.RunResultHelp {
    62  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
    63  	}
    64  }
    65  
    66  // Newly configured backend
    67  func TestUnlock_inmemBackend(t *testing.T) {
    68  	// Create a temporary working directory that is empty
    69  	td := tempDir(t)
    70  	testCopyDir(t, testFixturePath("backend-inmem-locked"), td)
    71  	defer os.RemoveAll(td)
    72  	defer testChdir(t, td)()
    73  	defer inmem.Reset()
    74  
    75  	// init backend
    76  	ui := new(cli.MockUi)
    77  	view, _ := testView(t)
    78  	ci := &InitCommand{
    79  		Meta: Meta{
    80  			Ui:   ui,
    81  			View: view,
    82  		},
    83  	}
    84  	if code := ci.Run(nil); code != 0 {
    85  		t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter)
    86  	}
    87  
    88  	ui = new(cli.MockUi)
    89  	c := &UnlockCommand{
    90  		Meta: Meta{
    91  			Ui:   ui,
    92  			View: view,
    93  		},
    94  	}
    95  
    96  	// run with the incorrect lock ID
    97  	args := []string{
    98  		"-force",
    99  		"LOCK_ID",
   100  	}
   101  
   102  	if code := c.Run(args); code == 0 {
   103  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
   104  	}
   105  
   106  	ui = new(cli.MockUi)
   107  	c = &UnlockCommand{
   108  		Meta: Meta{
   109  			Ui:   ui,
   110  			View: view,
   111  		},
   112  	}
   113  
   114  	// lockID set in the test fixture
   115  	args[1] = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"
   116  	if code := c.Run(args); code != 0 {
   117  		t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
   118  	}
   119  
   120  }