2017-08-31 15:10:55 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-09-15 22:11:34 +02:00
|
|
|
"net/url"
|
2017-09-09 00:18:47 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-08-31 15:10:55 +02:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-09-09 00:18:47 +02:00
|
|
|
getter "github.com/hashicorp/go-getter"
|
2017-08-31 15:10:55 +02:00
|
|
|
version "github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
2017-09-21 22:32:55 +02:00
|
|
|
// Map of module names and location of test modules.
|
|
|
|
// Only one version for now, as we only lookup latest from the registry.
|
|
|
|
type testMod struct {
|
|
|
|
location string
|
|
|
|
version string
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:12:14 +02:00
|
|
|
// All the locationes from the mockRegistry start with a file:// scheme. If
|
|
|
|
// the the location string here doesn't have a scheme, the mockRegistry will
|
|
|
|
// find the absolute path and return a complete URL.
|
2017-09-21 22:32:55 +02:00
|
|
|
var testMods = map[string]testMod{
|
|
|
|
"registry/foo/bar": {
|
|
|
|
location: "file:///download/registry/foo/bar/0.2.3//*?archive=tar.gz",
|
|
|
|
version: "0.2.3",
|
|
|
|
},
|
|
|
|
"registry/foo/baz": {
|
|
|
|
location: "file:///download/registry/foo/baz/1.10.0//*?archive=tar.gz",
|
|
|
|
version: "1.10.0",
|
|
|
|
},
|
|
|
|
"registry/local/sub": {
|
|
|
|
location: "test-fixtures/registry-tar-subdir/foo.tgz//*?archive=tar.gz",
|
|
|
|
version: "0.1.2",
|
|
|
|
},
|
2017-08-31 15:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func latestVersion(versions []string) string {
|
|
|
|
var col version.Collection
|
|
|
|
for _, v := range versions {
|
|
|
|
ver, err := version.NewVersion(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
col = append(col, ver)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(col)
|
|
|
|
return col[len(col)-1].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just enough like a registry to exercise our code.
|
|
|
|
// Returns the location of the latest version
|
|
|
|
func mockRegistry() *httptest.Server {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
|
|
|
|
mux.Handle("/v1/modules/",
|
|
|
|
http.StripPrefix("/v1/modules/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p := strings.TrimLeft(r.URL.Path, "/")
|
|
|
|
// handle download request
|
|
|
|
download := regexp.MustCompile(`^(\w+/\w+/\w+)/download$`)
|
|
|
|
|
|
|
|
// download lookup
|
|
|
|
matches := download.FindStringSubmatch(p)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-21 22:32:55 +02:00
|
|
|
mod, ok := testMods[matches[1]]
|
2017-08-31 15:10:55 +02:00
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-21 22:32:55 +02:00
|
|
|
location := mod.location
|
|
|
|
if !strings.HasPrefix(location, "file:///") {
|
|
|
|
// we can't use filepath.Abs because it will clean `//`
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
location = fmt.Sprintf("file://%s/%s", wd, location)
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:10:55 +02:00
|
|
|
w.Header().Set(xTerraformGet, location)
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
// no body
|
|
|
|
return
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetectRegistry(t *testing.T) {
|
|
|
|
server := mockRegistry()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
detector := registryDetector{
|
2017-09-28 20:45:02 +02:00
|
|
|
api: server.URL + "/v1/modules",
|
2017-08-31 15:10:55 +02:00
|
|
|
client: server.Client(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
2017-09-09 00:18:47 +02:00
|
|
|
source string
|
2017-08-31 15:10:55 +02:00
|
|
|
location string
|
|
|
|
found bool
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
2017-09-09 00:18:47 +02:00
|
|
|
source: "registry/foo/bar",
|
2017-09-21 22:32:55 +02:00
|
|
|
location: testMods["registry/foo/bar"].location,
|
2017-08-31 15:10:55 +02:00
|
|
|
found: true,
|
|
|
|
},
|
|
|
|
{
|
2017-09-09 00:18:47 +02:00
|
|
|
source: "registry/foo/baz",
|
2017-09-21 22:32:55 +02:00
|
|
|
location: testMods["registry/foo/baz"].location,
|
2017-08-31 15:10:55 +02:00
|
|
|
found: true,
|
|
|
|
},
|
|
|
|
// this should not be found, but not stop detection
|
|
|
|
{
|
2017-09-09 00:18:47 +02:00
|
|
|
source: "registry/foo/notfound",
|
2017-08-31 15:10:55 +02:00
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// a full url should not be detected
|
|
|
|
{
|
2017-09-09 00:18:47 +02:00
|
|
|
source: "http://example.com/registry/foo/notfound",
|
2017-08-31 15:10:55 +02:00
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// paths should not be detected
|
|
|
|
{
|
2017-09-09 00:18:47 +02:00
|
|
|
source: "./local/foo/notfound",
|
2017-08-31 15:10:55 +02:00
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
{
|
2017-09-09 00:18:47 +02:00
|
|
|
source: "/local/foo/notfound",
|
2017-08-31 15:10:55 +02:00
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// wrong number of parts can't be regisry IDs
|
|
|
|
{
|
2017-09-09 00:18:47 +02:00
|
|
|
source: "something/registry/foo/notfound",
|
2017-08-31 15:10:55 +02:00
|
|
|
found: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
|
2017-09-09 00:18:47 +02:00
|
|
|
t.Run(tc.source, func(t *testing.T) {
|
|
|
|
loc, ok, err := detector.Detect(tc.source, "")
|
2017-08-31 15:10:55 +02:00
|
|
|
if (err == nil) == tc.err {
|
|
|
|
t.Fatalf("expected error? %t; got error :%v", tc.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok != tc.found {
|
|
|
|
t.Fatalf("expected OK == %t", tc.found)
|
|
|
|
}
|
|
|
|
|
|
|
|
loc = strings.TrimPrefix(loc, server.URL+"/")
|
|
|
|
if strings.TrimPrefix(loc, server.URL) != tc.location {
|
|
|
|
t.Fatalf("expected location: %q, got %q", tc.location, loc)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 00:18:47 +02:00
|
|
|
|
|
|
|
// check that the full set of detectors works as expected
|
|
|
|
func TestDetectors(t *testing.T) {
|
|
|
|
server := mockRegistry()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regDetector := ®istryDetector{
|
2017-09-28 20:45:02 +02:00
|
|
|
api: server.URL + "/v1/modules",
|
2017-09-09 00:18:47 +02:00
|
|
|
client: server.Client(),
|
|
|
|
}
|
|
|
|
|
|
|
|
detectors := []getter.Detector{
|
|
|
|
new(getter.GitHubDetector),
|
|
|
|
new(getter.BitBucketDetector),
|
|
|
|
new(getter.S3Detector),
|
|
|
|
new(localDetector),
|
|
|
|
regDetector,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
source string
|
|
|
|
location string
|
|
|
|
fixture string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
source: "registry/foo/bar",
|
2017-09-21 22:32:55 +02:00
|
|
|
location: "file:///download/registry/foo/bar/0.2.3//*?archive=tar.gz",
|
2017-09-09 00:18:47 +02:00
|
|
|
},
|
|
|
|
// this should not be found, but not stop detection
|
|
|
|
{
|
|
|
|
source: "registry/foo/notfound",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
// a full url should be unchanged
|
|
|
|
{
|
|
|
|
source: "http://example.com/registry/foo/notfound?" +
|
|
|
|
"checksum=sha256:f19056b80a426d797ff9e470da069c171a6c6befa83e2da7f6c706207742acab",
|
|
|
|
location: "http://example.com/registry/foo/notfound?" +
|
|
|
|
"checksum=sha256:f19056b80a426d797ff9e470da069c171a6c6befa83e2da7f6c706207742acab",
|
|
|
|
},
|
|
|
|
|
|
|
|
// forced getters will return untouched
|
|
|
|
{
|
|
|
|
source: "git::http://example.com/registry/foo/notfound?param=value",
|
|
|
|
location: "git::http://example.com/registry/foo/notfound?param=value",
|
|
|
|
},
|
|
|
|
|
|
|
|
// local paths should be detected as such, even if they're match
|
|
|
|
// registry modules.
|
|
|
|
{
|
|
|
|
source: "./registry/foo/bar",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
source: "/registry/foo/bar",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// wrong number of parts can't be regisry IDs
|
|
|
|
{
|
|
|
|
source: "something/registry/foo/notfound",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// make sure a local module that looks like a registry id takes precedence
|
|
|
|
{
|
|
|
|
source: "namespace/identifier/provider",
|
|
|
|
fixture: "discover-subdirs",
|
|
|
|
// this should be found locally
|
|
|
|
location: "file://" + filepath.Join(wd, fixtureDir, "discover-subdirs/namespace/identifier/provider"),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
|
|
|
|
t.Run(tc.source, func(t *testing.T) {
|
|
|
|
dir := wd
|
|
|
|
if tc.fixture != "" {
|
|
|
|
dir = filepath.Join(wd, fixtureDir, tc.fixture)
|
|
|
|
if err := os.Chdir(dir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(wd)
|
|
|
|
}
|
|
|
|
|
|
|
|
loc, err := getter.Detect(tc.source, dir, detectors)
|
|
|
|
if (err == nil) == tc.err {
|
|
|
|
t.Fatalf("expected error? %t; got error :%v", tc.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
loc = strings.TrimPrefix(loc, server.URL+"/")
|
|
|
|
if strings.TrimPrefix(loc, server.URL) != tc.location {
|
|
|
|
t.Fatalf("expected location: %q, got %q", tc.location, loc)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:11:34 +02:00
|
|
|
|
2017-09-21 22:32:55 +02: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 := mockRegistry()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
regDetector := ®istryDetector{
|
2017-09-28 20:45:02 +02:00
|
|
|
api: server.URL + "/v1/modules",
|
2017-09-21 22:32:55 +02:00
|
|
|
client: server.Client(),
|
|
|
|
}
|
|
|
|
|
|
|
|
origDetectors := detectors
|
|
|
|
defer func() {
|
|
|
|
detectors = origDetectors
|
|
|
|
}()
|
|
|
|
|
|
|
|
detectors = []getter.Detector{
|
|
|
|
new(getter.GitHubDetector),
|
|
|
|
new(getter.BitBucketDetector),
|
|
|
|
new(getter.S3Detector),
|
|
|
|
new(localDetector),
|
|
|
|
regDetector,
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "registry-tar-subdir"))
|
|
|
|
|
|
|
|
if err := tree.Load(storage, GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Load(storage, GetModeNone); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-28 20:45:02 +02:00
|
|
|
// 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"))
|
|
|
|
|
|
|
|
if err := tree.Load(storage, GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
2017-09-21 22:32:55 +02:00
|
|
|
actual := strings.TrimSpace(tree.String())
|
|
|
|
expected := strings.TrimSpace(treeLoadSubdirStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("got: \n\n%s\nexpected: \n\n%s", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:11:34 +02:00
|
|
|
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
|
|
|
|
loc, err := getter.Detect("hashicorp/consul/aws", "./", detectors)
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 22:59:48 +02:00
|
|
|
|
|
|
|
func TestAccRegistryLoad(t *testing.T) {
|
|
|
|
if os.Getenv("TF_ACC") == "" {
|
|
|
|
t.Skip("skipping ACC test")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := testStorage(t)
|
|
|
|
tree := NewTree("", testConfig(t, "registry-load"))
|
|
|
|
|
|
|
|
if err := tree.Load(storage, GetModeGet); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Load(storage, GetModeNone); 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)
|
|
|
|
}
|
|
|
|
}
|