2017-11-27 22:48:39 +01:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2018-07-27 20:59:03 +02:00
|
|
|
"fmt"
|
2017-11-27 22:48:39 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
version "github.com/hashicorp/go-version"
|
|
|
|
"github.com/hashicorp/terraform/registry/regsrc"
|
|
|
|
"github.com/hashicorp/terraform/registry/test"
|
|
|
|
"github.com/hashicorp/terraform/svchost/disco"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLookupModuleVersions(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
client := NewClient(test.Disco(server), nil)
|
2017-11-27 22:48:39 +01:00
|
|
|
|
|
|
|
// test with and without a hostname
|
|
|
|
for _, src := range []string{
|
|
|
|
"example.com/test-versions/name/provider",
|
|
|
|
"test-versions/name/provider",
|
|
|
|
} {
|
|
|
|
modsrc, err := regsrc.ParseModuleSource(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
resp, err := client.ModuleVersions(modsrc)
|
2017-11-27 22:48:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Modules) != 1 {
|
|
|
|
t.Fatal("expected 1 module, got", len(resp.Modules))
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := resp.Modules[0]
|
|
|
|
name := "test-versions/name/provider"
|
|
|
|
if mod.Source != name {
|
|
|
|
t.Fatalf("expected module name %q, got %q", name, mod.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mod.Versions) != 4 {
|
|
|
|
t.Fatal("expected 4 versions, got", len(mod.Versions))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range mod.Versions {
|
|
|
|
_, err := version.NewVersion(v.Version)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid version %q: %s", v.Version, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 00:31:05 +01:00
|
|
|
func TestInvalidRegistry(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
client := NewClient(test.Disco(server), nil)
|
2018-01-18 00:31:05 +01:00
|
|
|
|
|
|
|
src := "non-existent.localhost.localdomain/test-versions/name/provider"
|
|
|
|
modsrc, err := regsrc.ParseModuleSource(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
if _, err := client.ModuleVersions(modsrc); err == nil {
|
2018-01-18 00:31:05 +01:00
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 22:48:39 +01:00
|
|
|
func TestRegistryAuth(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
client := NewClient(test.Disco(server), nil)
|
2017-11-27 22:48:39 +01:00
|
|
|
|
|
|
|
src := "private/name/provider"
|
|
|
|
mod, err := regsrc.ParseModuleSource(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
_, err = client.ModuleVersions(mod)
|
2018-10-31 16:45:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
2018-07-31 00:44:06 +02:00
|
|
|
_, err = client.ModuleLocation(mod, "1.0.0")
|
2018-10-31 16:45:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
// Also test without a credentials source
|
|
|
|
client.services.SetCredentialsSource(nil)
|
2017-11-27 22:48:39 +01:00
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// both should fail without auth
|
2018-07-31 00:44:06 +02:00
|
|
|
_, err = client.ModuleVersions(mod)
|
2018-10-31 16:45:03 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
2018-07-31 00:44:06 +02:00
|
|
|
_, err = client.ModuleLocation(mod, "1.0.0")
|
2018-10-31 16:45:03 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupModuleLocationRelative(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
client := NewClient(test.Disco(server), nil)
|
2017-11-27 22:48:39 +01:00
|
|
|
|
|
|
|
src := "relative/foo/bar"
|
|
|
|
mod, err := regsrc.ParseModuleSource(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
got, err := client.ModuleLocation(mod, "0.2.0")
|
2017-11-27 22:48:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := server.URL + "/relative-path"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("wrong location %s; want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccLookupModuleVersions(t *testing.T) {
|
|
|
|
if os.Getenv("TF_ACC") == "" {
|
|
|
|
t.Skip()
|
|
|
|
}
|
2018-07-05 21:28:29 +02:00
|
|
|
regDisco := disco.New()
|
2017-11-27 22:48:39 +01:00
|
|
|
|
|
|
|
// test with and without a hostname
|
|
|
|
for _, src := range []string{
|
|
|
|
"terraform-aws-modules/vpc/aws",
|
|
|
|
regsrc.PublicRegistryHost.String() + "/terraform-aws-modules/vpc/aws",
|
|
|
|
} {
|
|
|
|
modsrc, err := regsrc.ParseModuleSource(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
s := NewClient(regDisco, nil)
|
2018-07-31 00:44:06 +02:00
|
|
|
resp, err := s.ModuleVersions(modsrc)
|
2017-11-27 22:48:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Modules) != 1 {
|
|
|
|
t.Fatal("expected 1 module, got", len(resp.Modules))
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := resp.Modules[0]
|
|
|
|
name := "terraform-aws-modules/vpc/aws"
|
|
|
|
if mod.Source != name {
|
|
|
|
t.Fatalf("expected module name %q, got %q", name, mod.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mod.Versions) == 0 {
|
|
|
|
t.Fatal("expected multiple versions, got 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range mod.Versions {
|
|
|
|
_, err := version.NewVersion(v.Version)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid version %q: %s", v.Version, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the error should reference the config source exatly, not the discovered path.
|
|
|
|
func TestLookupLookupModuleError(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
client := NewClient(test.Disco(server), nil)
|
2017-11-27 22:48:39 +01:00
|
|
|
|
|
|
|
// this should not be found in teh registry
|
|
|
|
src := "bad/local/path"
|
|
|
|
mod, err := regsrc.ParseModuleSource(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
_, err = client.ModuleLocation(mod, "0.2.0")
|
2017-11-27 22:48:39 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for the exact quoted string to ensure we didn't prepend a hostname.
|
|
|
|
if !strings.Contains(err.Error(), `"bad/local/path"`) {
|
|
|
|
t.Fatal("error should not include the hostname. got:", err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 20:59:03 +02:00
|
|
|
|
|
|
|
func TestLookupProviderVersions(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
client := NewClient(test.Disco(server), nil)
|
2018-07-27 20:59:03 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{"foo"},
|
|
|
|
{"bar"},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2018-08-09 00:04:51 +02:00
|
|
|
provider := regsrc.NewTerraformProvider(tt.name, "", "")
|
2018-07-27 20:59:03 +02:00
|
|
|
resp, err := client.TerraformProviderVersions(provider)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("terraform-providers/%s", tt.name)
|
|
|
|
if resp.ID != name {
|
|
|
|
t.Fatalf("expected provider name %q, got %q", name, resp.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Versions) != 2 {
|
|
|
|
t.Fatal("expected 2 versions, got", len(resp.Versions))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range resp.Versions {
|
|
|
|
_, err := version.NewVersion(v.Version)
|
|
|
|
if err != nil {
|
2019-03-07 21:07:13 +01:00
|
|
|
t.Fatalf("invalid version %#v: %v", v, err)
|
2018-07-27 20:59:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupProviderLocation(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
client := NewClient(test.Disco(server), nil)
|
2018-07-27 20:59:03 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"foo",
|
|
|
|
"0.2.3",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bar",
|
|
|
|
"0.1.1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"baz",
|
|
|
|
"0.0.0",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
// FIXME: the tests are set up to succeed - os/arch is not being validated at this time
|
2018-08-09 00:04:51 +02:00
|
|
|
p := regsrc.NewTerraformProvider(tt.Name, "linux", "amd64")
|
|
|
|
|
2018-07-27 20:59:03 +02:00
|
|
|
locationMetadata, err := client.TerraformProviderLocation(p, tt.Version)
|
|
|
|
if tt.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadURL := fmt.Sprintf("https://releases.hashicorp.com/terraform-provider-%s/%s/terraform-provider-%s.zip", tt.Name, tt.Version, tt.Name)
|
|
|
|
|
|
|
|
if locationMetadata.DownloadURL != downloadURL {
|
|
|
|
t.Fatalf("incorrect download URL: expected %q, got %q", downloadURL, locationMetadata.DownloadURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|