opentofu/internal/getproviders/legacy_lookup_test.go
Martin Atkins 0b734a2803 command: Make provider installation interruptible
In earlier commits we started to make the installation codepath
context-aware so that it could be canceled in the event of a SIGINT, but
we didn't complete wiring that through the API of the getproviders
package.

Here we make the getproviders.Source interface methods, along with some
other functions that can make network requests, take a context.Context
argument and act appropriately if that context is cancelled.

The main providercache.Installer.EnsureProviderVersions method now also
has some context-awareness so that it can abort its work early if its
context reports any sort of error. That avoids waiting for the process
to wind through all of the remaining iterations of the various loops,
logging each request failure separately, and instead returns just
a single aggregate "canceled" error.

We can then set things up in the "terraform init" and
"terraform providers mirror" commands so that the context will be
cancelled if we get an interrupt signal, allowing provider installation
to abort early while still atomically completing any local-side effects
that may have started.
2020-09-29 10:00:35 -07:00

103 lines
2.4 KiB
Go

package getproviders
import (
"context"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
)
func TestLookupLegacyProvider(t *testing.T) {
source, _, close := testRegistrySource(t)
defer close()
got, gotMoved, err := LookupLegacyProvider(
context.Background(),
addrs.NewLegacyProvider("legacy"),
source,
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
want := addrs.Provider{
Hostname: defaultRegistryHost,
Namespace: "legacycorp",
Type: "legacy",
}
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
if !gotMoved.IsZero() {
t.Errorf("wrong moved result\ngot: %#v\nwant: %#v", gotMoved, addrs.Provider{})
}
}
func TestLookupLegacyProvider_moved(t *testing.T) {
source, _, close := testRegistrySource(t)
defer close()
got, gotMoved, err := LookupLegacyProvider(
context.Background(),
addrs.NewLegacyProvider("moved"),
source,
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
want := addrs.Provider{
Hostname: defaultRegistryHost,
Namespace: "hashicorp",
Type: "moved",
}
wantMoved := addrs.Provider{
Hostname: defaultRegistryHost,
Namespace: "acme",
Type: "moved",
}
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
if gotMoved != wantMoved {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", gotMoved, wantMoved)
}
}
func TestLookupLegacyProvider_invalidResponse(t *testing.T) {
source, _, close := testRegistrySource(t)
defer close()
got, _, err := LookupLegacyProvider(
context.Background(),
addrs.NewLegacyProvider("invalid"),
source,
)
if !got.IsZero() {
t.Errorf("got non-zero addr\ngot: %#v\nwant: %#v", got, nil)
}
wantErr := "Error parsing provider ID from Registry: Invalid provider source string"
if gotErr := err.Error(); !strings.Contains(gotErr, wantErr) {
t.Fatalf("unexpected error: got %q, want %q", gotErr, wantErr)
}
}
func TestLookupLegacyProvider_unexpectedTypeChange(t *testing.T) {
source, _, close := testRegistrySource(t)
defer close()
got, _, err := LookupLegacyProvider(
context.Background(),
addrs.NewLegacyProvider("changetype"),
source,
)
if !got.IsZero() {
t.Errorf("got non-zero addr\ngot: %#v\nwant: %#v", got, nil)
}
wantErr := `Registry returned provider with type "newtype", expected "changetype"`
if gotErr := err.Error(); gotErr != wantErr {
t.Fatalf("unexpected error: got %q, want %q", gotErr, wantErr)
}
}