github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/unlock_test.go (about)

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