/
githubmirror
/
terraform
Обзор
Документация
Войти
/
githubmirror
/
terraform
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
internal/command/unlock_test.go
121 строка
2 KB
Sarah French
refactor: Replace remaining use of `cli.NewMockUi()` or `new(cli.MockUi)` in tests in the command package
16 июл 2026, 00:45
16 июл 2026, 00:45
91f04bd
Код
Авторство
О чём код?
// Copyright IBM Corp. 2014, 2026 // SPDX-License-Identifier: BUSL-1.1 package command import ( "os" "testing" "github.com/hashicorp/cli" "github.com/hashicorp/terraform/internal/backend/remote-state/inmem" "github.com/hashicorp/terraform/internal/command/workdir" ) // Since we can't unlock a local state file, just test that calling unlock // doesn't fail. func TestUnlock(t *testing.T) { td := t.TempDir() os.MkdirAll(td, 0755) t.Chdir(td) // Write the legacy state statePath := DefaultStateFilename { emptyStateFile := workdir.NewBackendStateFile() emptyStateFileRaw, err := workdir.EncodeBackendStateFile(emptyStateFile) if err != nil { t.Fatal(err) } err = os.WriteFile(statePath, emptyStateFileRaw, os.ModePerm) if err != nil { t.Fatalf("err: %s", err) } } p := testProvider() ui := testUiWrapped(t) view, _ := testView(t) c := &UnlockCommand{ Meta: Meta{ testingOverrides: metaOverridesForProvider(p), Ui: ui, View: view, }, } args := []string{ "-force", "LOCK_ID", } if code := c.Run(args); code != 1 { t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String()) } // make sure we don't crash with arguments in the wrong order args = []string{ "LOCK_ID", "-force", } if code := c.Run(args); code != cli.RunResultHelp { t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String()) } } // Newly configured backend func TestUnlock_inmemBackend(t *testing.T) { // Create a temporary working directory that is empty td := t.TempDir() testCopyDir(t, testFixturePath("backend-inmem-locked"), td) t.Chdir(td) defer inmem.Reset() // init backend ui := testUiWrapped(t) view, _ := testView(t) ci := &InitCommand{ Meta: Meta{ Ui: ui, View: view, }, } if code := ci.Run(nil); code != 0 { t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter) } ui = testUiWrapped(t) c := &UnlockCommand{ Meta: Meta{ Ui: ui, View: view, }, } // run with the incorrect lock ID args := []string{ "-force", "LOCK_ID", } if code := c.Run(args); code == 0 { t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String()) } ui = testUiWrapped(t) c = &UnlockCommand{ Meta: Meta{ Ui: ui, View: view, }, } // lockID set in the test fixture args[1] = "2b6a6738-5dd5-50d6-c0ae-f6352977666b" if code := c.Run(args); code != 0 { t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String()) } }