opentofu/internal/command/get_test.go
kmoe 40ec62c139
command: make module installation interruptible
Earlier work to make "terraform init" interruptible made the getproviders
package context-aware in order to allow provider installation to be cancelled.

Here we make a similar change for module installation, which is now also
cancellable with SIGINT. This involves plumbing context through initwd and
getmodules. Functions which can make network requests now include a context
parameter whose cancellation cancels those requests.

Since the module installation code is shared, "terraform get" is now
also interruptible during module installation.
2021-11-11 12:28:10 +00:00

114 lines
2.6 KiB
Go

package command
import (
"strings"
"testing"
"github.com/mitchellh/cli"
)
func TestGet(t *testing.T) {
wd := tempWorkingDirFixture(t, "get")
defer testChdir(t, wd.RootModuleDir())()
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
},
}
args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
output := ui.OutputWriter.String()
if !strings.Contains(output, "- foo in") {
t.Fatalf("doesn't look like get: %s", output)
}
}
func TestGet_multipleArgs(t *testing.T) {
wd, cleanup := tempWorkingDir(t)
defer cleanup()
defer testChdir(t, wd.RootModuleDir())()
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
},
}
args := []string{
"bad",
"bad",
}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}
func TestGet_update(t *testing.T) {
wd := tempWorkingDirFixture(t, "get")
defer testChdir(t, wd.RootModuleDir())()
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
},
}
args := []string{
"-update",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
output := ui.OutputWriter.String()
if !strings.Contains(output, `- foo in`) {
t.Fatalf("doesn't look like get: %s", output)
}
}
func TestGet_cancel(t *testing.T) {
// This test runs `terraform get` as if SIGINT (or similar on other
// platforms) were sent to it, testing that it is interruptible.
wd := tempWorkingDirFixture(t, "init-registry-module")
defer testChdir(t, wd.RootModuleDir())()
// Our shutdown channel is pre-closed so init will exit as soon as it
// starts a cancelable portion of the process.
shutdownCh := make(chan struct{})
close(shutdownCh)
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
ShutdownCh: shutdownCh,
},
}
args := []string{}
if code := c.Run(args); code == 0 {
t.Fatalf("succeeded; wanted error\n%s", ui.OutputWriter.String())
}
if got, want := ui.ErrorWriter.String(), `Module installation was canceled by an interrupt signal`; !strings.Contains(got, want) {
t.Fatalf("wrong error message\nshould contain: %s\ngot:\n%s", want, got)
}
}