2017-08-31 15:10:55 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
2017-10-25 17:49:41 +02:00
|
|
|
"encoding/json"
|
2017-08-31 15:10:55 +02:00
|
|
|
"fmt"
|
2017-10-25 17:49:41 +02:00
|
|
|
"io"
|
2017-08-31 15:10:55 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-09-15 22:11:34 +02:00
|
|
|
"net/url"
|
2017-09-09 00:18:47 +02:00
|
|
|
"os"
|
2017-08-31 15:10:55 +02:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
version "github.com/hashicorp/go-version"
|
2017-10-25 23:32:43 +02:00
|
|
|
"github.com/hashicorp/terraform/registry/regsrc"
|
2017-10-25 17:49:41 +02:00
|
|
|
"github.com/hashicorp/terraform/registry/response"
|
2017-08-31 15:10:55 +02:00
|
|
|
)
|
|
|
|
|
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-10-27 21:44:00 +02:00
|
|
|
const (
|
2017-10-27 23:36:30 +02:00
|
|
|
testCredentials = "test-auth-token"
|
2017-10-27 21:44:00 +02:00
|
|
|
)
|
|
|
|
|
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-10-25 17:49:41 +02:00
|
|
|
var testMods = map[string][]testMod{
|
|
|
|
"registry/foo/bar": {{
|
2017-09-21 22:32:55 +02:00
|
|
|
location: "file:///download/registry/foo/bar/0.2.3//*?archive=tar.gz",
|
|
|
|
version: "0.2.3",
|
2017-10-25 17:49:41 +02:00
|
|
|
}},
|
|
|
|
"registry/foo/baz": {{
|
2017-09-21 22:32:55 +02:00
|
|
|
location: "file:///download/registry/foo/baz/1.10.0//*?archive=tar.gz",
|
|
|
|
version: "1.10.0",
|
2017-10-25 17:49:41 +02:00
|
|
|
}},
|
|
|
|
"registry/local/sub": {{
|
2017-09-21 22:32:55 +02:00
|
|
|
location: "test-fixtures/registry-tar-subdir/foo.tgz//*?archive=tar.gz",
|
|
|
|
version: "0.1.2",
|
2017-10-25 17:49:41 +02:00
|
|
|
}},
|
|
|
|
"exists-in-registry/identifier/provider": {{
|
2017-10-17 23:25:46 +02:00
|
|
|
location: "file:///registry/exists",
|
|
|
|
version: "0.2.0",
|
2017-10-25 17:49:41 +02:00
|
|
|
}},
|
|
|
|
"test-versions/name/provider": {
|
|
|
|
{version: "2.2.0"},
|
|
|
|
{version: "2.1.1"},
|
|
|
|
{version: "1.2.2"},
|
|
|
|
{version: "1.2.1"},
|
2017-10-17 23:25:46 +02:00
|
|
|
},
|
2017-10-27 21:44:00 +02:00
|
|
|
"private/name/provider": {
|
|
|
|
{version: "1.0.0"},
|
|
|
|
},
|
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()
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:49:41 +02:00
|
|
|
func mockRegHandler() http.Handler {
|
2017-08-31 15:10:55 +02:00
|
|
|
mux := http.NewServeMux()
|
2017-10-25 17:49:41 +02:00
|
|
|
|
|
|
|
download := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p := strings.TrimLeft(r.URL.Path, "/")
|
|
|
|
// handle download request
|
2017-10-26 01:20:05 +02:00
|
|
|
re := regexp.MustCompile(`^([-a-z]+/\w+/\w+).*/download$`)
|
2017-10-25 17:49:41 +02:00
|
|
|
// download lookup
|
|
|
|
matches := re.FindStringSubmatch(p)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:44:00 +02:00
|
|
|
// check for auth
|
|
|
|
if strings.Contains(matches[0], "private/") {
|
|
|
|
if !strings.Contains(r.Header.Get("Authorization"), testCredentials) {
|
|
|
|
http.Error(w, "", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:49:41 +02:00
|
|
|
versions, ok := testMods[matches[1]]
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mod := versions[0]
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("X-Terraform-Get", location)
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
// no body
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
versions := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p := strings.TrimLeft(r.URL.Path, "/")
|
|
|
|
re := regexp.MustCompile(`^([-a-z]+/\w+/\w+)/versions$`)
|
|
|
|
matches := re.FindStringSubmatch(p)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:44:00 +02:00
|
|
|
// check for auth
|
|
|
|
if strings.Contains(matches[1], "private/") {
|
|
|
|
if !strings.Contains(r.Header.Get("Authorization"), testCredentials) {
|
|
|
|
http.Error(w, "", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:49:41 +02:00
|
|
|
name := matches[1]
|
|
|
|
versions, ok := testMods[name]
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only adding the single requested module for now
|
|
|
|
// this is the minimal that any regisry is epected to support
|
|
|
|
mpvs := &response.ModuleProviderVersions{
|
|
|
|
Source: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range versions {
|
|
|
|
mv := &response.ModuleVersion{
|
|
|
|
Version: v.version,
|
|
|
|
}
|
|
|
|
mpvs.Versions = append(mpvs.Versions, mv)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := response.ModuleVersions{
|
|
|
|
Modules: []*response.ModuleProviderVersions{mpvs},
|
|
|
|
}
|
|
|
|
|
|
|
|
js, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
}
|
2017-08-31 15:10:55 +02:00
|
|
|
|
|
|
|
mux.Handle("/v1/modules/",
|
|
|
|
http.StripPrefix("/v1/modules/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-10-25 17:49:41 +02:00
|
|
|
if strings.HasSuffix(r.URL.Path, "/download") {
|
|
|
|
download(w, r)
|
2017-08-31 15:10:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:49:41 +02:00
|
|
|
if strings.HasSuffix(r.URL.Path, "/versions") {
|
|
|
|
versions(w, r)
|
2017-08-31 15:10:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:49:41 +02:00
|
|
|
http.NotFound(w, r)
|
2017-08-31 15:10:55 +02:00
|
|
|
})),
|
|
|
|
)
|
|
|
|
|
2017-10-25 17:49:41 +02:00
|
|
|
mux.HandleFunc("/.well-known/terraform.json", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2017-10-27 21:07:24 +02:00
|
|
|
io.WriteString(w, `{"modules.v1":"http://localhost/v1/modules/"}`)
|
2017-10-25 17:49:41 +02:00
|
|
|
})
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just enough like a registry to exercise our code.
|
|
|
|
// Returns the location of the latest version
|
|
|
|
func mockRegistry() *httptest.Server {
|
|
|
|
server := httptest.NewServer(mockRegHandler())
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-10-27 21:07:24 +02:00
|
|
|
server := mockRegistry()
|
2017-09-21 22:32:55 +02:00
|
|
|
defer server.Close()
|
2017-10-26 01:20:05 +02:00
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
disco := testDisco(server)
|
|
|
|
storage := testStorage(t, disco)
|
2017-09-21 22:32:55 +02:00
|
|
|
|
|
|
|
tree := NewTree("", testConfig(t, "registry-tar-subdir"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-09-21 22:32:55 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-09-21 22:32:55 +02:00
|
|
|
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"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-09-28 20:45:02 +02:00
|
|
|
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-10-18 00:01:34 +02:00
|
|
|
// Test that the //subdir notation can be used with registry modules
|
|
|
|
func TestRegisryModuleSubdir(t *testing.T) {
|
2017-10-27 21:07:24 +02:00
|
|
|
server := mockRegistry()
|
2017-10-18 00:01:34 +02:00
|
|
|
defer server.Close()
|
2017-10-26 01:20:05 +02:00
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
disco := testDisco(server)
|
|
|
|
storage := testStorage(t, disco)
|
2017-10-18 00:01:34 +02:00
|
|
|
tree := NewTree("", testConfig(t, "registry-subdir"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-18 00:01:34 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-10-18 00:01:34 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-10-25 23:32:43 +02:00
|
|
|
module, err := regsrc.ParseModuleSource("hashicorp/consul/aws")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
s := NewStorage("/tmp", nil, nil)
|
|
|
|
loc, err := s.lookupModuleLocation(module, "")
|
2017-09-15 22:11:34 +02: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())
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 22:59:48 +02:00
|
|
|
|
|
|
|
func TestAccRegistryLoad(t *testing.T) {
|
|
|
|
if os.Getenv("TF_ACC") == "" {
|
|
|
|
t.Skip("skipping ACC test")
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
storage := testStorage(t, nil)
|
2017-09-21 22:59:48 +02:00
|
|
|
tree := NewTree("", testConfig(t, "registry-load"))
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeGet
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-09-21 22:59:48 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tree.Loaded() {
|
|
|
|
t.Fatal("should be loaded")
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:29:29 +02:00
|
|
|
storage.Mode = GetModeNone
|
|
|
|
if err := tree.Load(storage); err != nil {
|
2017-09-21 22:59:48 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|