github.com/opentofu/opentofu@v1.7.1/internal/command/unlock_test.go (about)

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