2017-10-28 01:17:26 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2017-11-27 22:51:26 +01:00
|
|
|
"net/url"
|
2017-10-28 01:17:26 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-11-27 22:51:26 +01:00
|
|
|
"strings"
|
2017-10-28 01:17:26 +02:00
|
|
|
"testing"
|
2017-11-27 22:51:26 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/registry/regsrc"
|
|
|
|
"github.com/hashicorp/terraform/registry/test"
|
2017-10-28 01:17:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetModule(t *testing.T) {
|
2017-11-27 22:51:26 +01:00
|
|
|
server := test.Registry()
|
2017-10-28 01:17:26 +02:00
|
|
|
defer server.Close()
|
2017-11-27 22:51:26 +01:00
|
|
|
disco := test.Disco(server)
|
2017-10-28 01:17:26 +02:00
|
|
|
|
|
|
|
td, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-05 21:28:29 +02:00
|
|
|
storage := NewStorage(td, disco)
|
2017-10-28 01:17:26 +02:00
|
|
|
|
2017-11-27 22:51:26 +01:00
|
|
|
// this module exists in a test fixture, and is known by the test.Registry
|
2017-10-28 01:17:26 +02:00
|
|
|
// relative to our cwd.
|
|
|
|
err = storage.GetModule(filepath.Join(td, "foo"), "registry/local/sub")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// list everything to make sure nothing else got unpacked in here
|
|
|
|
ls, err := ioutil.ReadDir(td)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var names []string
|
|
|
|
for _, info := range ls {
|
|
|
|
names = append(names, info.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(len(names) == 1 && names[0] == "foo") {
|
|
|
|
t.Fatalf("expected only directory 'foo', found entries %q", names)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(filepath.Join(td, "foo", "main.tf"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-27 22:51:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GitHub archives always contain the module source in a single subdirectory,
|
|
|
|
// so the registry will return a path with with a `//*` suffix. We need to make
|
|
|
|
// sure this doesn't intefere with our internal handling of `//` subdir.
|
|
|
|
func TestRegistryGitHubArchive(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
disco := test.Disco(server)
|
|
|
|
storage := testStorage(t, disco)
|
|
|
|
|
|
|
|
tree := NewTree("", testConfig(t, "registry-tar-subdir"))
|
|
|
|
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-10-28 01:17:26 +02:00
|
|
|
|
2017-11-27 22:51:26 +01:00
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop the registry server, and make sure that we don't need to call out again
|
|
|
|
server.Close()
|
|
|
|
tree = NewTree("", testConfig(t, "registry-tar-subdir"))
|
|
|
|
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
expected := strings.TrimSpace(treeLoadSubdirStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("got: \n\n%s\nexpected: \n\n%s", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the //subdir notation can be used with registry modules
|
|
|
|
func TestRegisryModuleSubdir(t *testing.T) {
|
|
|
|
server := test.Registry()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
disco := test.Disco(server)
|
|
|
|
storage := testStorage(t, disco)
|
|
|
|
tree := NewTree("", testConfig(t, "registry-subdir"))
|
|
|
|
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
expected := strings.TrimSpace(treeLoadRegistrySubdirStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("got: \n\n%s\nexpected: \n\n%s", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccRegistryDiscover(t *testing.T) {
|
|
|
|
if os.Getenv("TF_ACC") == "" {
|
|
|
|
t.Skip("skipping ACC test")
|
|
|
|
}
|
|
|
|
|
|
|
|
// simply check that we get a valid github URL for this from the registry
|
|
|
|
module, err := regsrc.ParseModuleSource("hashicorp/consul/aws")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
s := NewStorage("/tmp", nil)
|
2018-07-31 00:44:06 +02:00
|
|
|
loc, err := s.registry.ModuleLocation(module, "")
|
2017-11-27 22:51:26 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(loc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(u.Host, "github.com") {
|
|
|
|
t.Fatalf("expected host 'github.com', got: %q", u.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(u.String(), "consul") {
|
|
|
|
t.Fatalf("url doesn't contain 'consul': %s", u.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccRegistryLoad(t *testing.T) {
|
|
|
|
if os.Getenv("TF_ACC") == "" {
|
|
|
|
t.Skip("skipping ACC test")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := testStorage(t, nil)
|
|
|
|
tree := NewTree("", testConfig(t, "registry-load"))
|
|
|
|
|
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO expand this further by fetching some metadata from the registry
|
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
if !strings.Contains(actual, "(path: vault)") {
|
|
|
|
t.Fatal("missing vault module, got:\n", actual)
|
|
|
|
}
|
2017-10-28 01:17:26 +02:00
|
|
|
}
|