@ -8,14 +8,13 @@ import (
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
getter "github.com/hashicorp/go-getter"
version "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/registry/regsrc"
"github.com/hashicorp/terraform/registry/response"
)
@ -179,199 +178,10 @@ func mockTLSRegistry() *httptest.Server {
return server
}
func setResetRegDetector ( server * httptest . Server ) func ( ) {
regDetector := & registryDetector {
api : server . URL + "/v1/modules" ,
client : server . Client ( ) ,
}
origDetectors := detectors
detectors = [ ] getter . Detector {
new ( getter . GitHubDetector ) ,
new ( getter . BitBucketDetector ) ,
new ( getter . S3Detector ) ,
regDetector ,
new ( getter . FileDetector ) ,
}
return func ( ) {
detectors = origDetectors
}
}
func TestDetectRegistry ( t * testing . T ) {
server := mockRegistry ( )
defer server . Close ( )
detector := registryDetector {
api : server . URL + "/v1/modules" ,
client : server . Client ( ) ,
}
for _ , tc := range [ ] struct {
source string
location string
found bool
err bool
} {
{
source : "registry/foo/bar" ,
location : testMods [ "registry/foo/bar" ] [ 0 ] . location ,
found : true ,
} ,
{
source : "registry/foo/baz" ,
location : testMods [ "registry/foo/baz" ] [ 0 ] . location ,
found : true ,
} ,
// this should not be found, and is no longer valid as a local source
{
source : "registry/foo/notfound" ,
err : true ,
} ,
// a full url should not be detected
{
source : "http://example.com/registry/foo/notfound" ,
found : false ,
} ,
// paths should not be detected
{
source : "./local/foo/notfound" ,
found : false ,
} ,
{
source : "/local/foo/notfound" ,
found : false ,
} ,
// wrong number of parts can't be regisry IDs
{
source : "something/registry/foo/notfound" ,
found : false ,
} ,
} {
t . Run ( tc . source , func ( t * testing . T ) {
loc , ok , err := detector . Detect ( tc . source , "" )
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 )
}
} )
}
}
// check that the full set of detectors works as expected
func TestDetectors ( t * testing . T ) {
server := mockRegistry ( )
defer server . Close ( )
defer setResetRegDetector ( server ) ( )
wd , err := os . Getwd ( )
if err != nil {
t . Fatal ( err )
}
for _ , tc := range [ ] struct {
source string
location string
fixture string
err bool
} {
{
source : "registry/foo/bar" ,
location : "file:///download/registry/foo/bar/0.2.3//*?archive=tar.gz" ,
} ,
// this should not be found, and is no longer a valid local source
{
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" ,
location : "file://" + filepath . Join ( wd , "registry/foo/bar" ) ,
} ,
{
source : "/registry/foo/bar" ,
location : "file:///registry/foo/bar" ,
} ,
// Wrong number of parts can't be registry IDs.
// This is returned as a local path for now, but may return an error at
// some point.
{
source : "something/here/registry/foo/notfound" ,
location : "file://" + filepath . Join ( wd , "something/here/registry/foo/notfound" ) ,
} ,
// make sure a local module that looks like a registry id can be found
{
source : "namespace/identifier/provider" ,
fixture : "discover-subdirs" ,
err : true ,
} ,
// The registry takes precedence over local paths if they don't start
// with a relative or absolute path
{
source : "exists-in-registry/identifier/provider" ,
fixture : "discover-registry-local" ,
// registry should take precidence
location : "file:///registry/exists" ,
} ,
} {
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 )
}
} )
}
}
/ *
// FIXME: verifying the behavior in these tests is still important, so they
// need to be updated.
//
// 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.
@ -441,6 +251,7 @@ func TestRegisryModuleSubdir(t *testing.T) {
t . Fatalf ( "got: \n\n%s\nexpected: \n\n%s" , actual , expected )
}
}
* /
func TestAccRegistryDiscover ( t * testing . T ) {
if os . Getenv ( "TF_ACC" ) == "" {
@ -448,7 +259,12 @@ func TestAccRegistryDiscover(t *testing.T) {
}
// simply check that we get a valid github URL for this from the registry
loc , err := getter . Detect ( "hashicorp/consul/aws" , "./" , detectors )
module , err := regsrc . ParseModuleSource ( "hashicorp/consul/aws" )
if err != nil {
t . Fatal ( err )
}
loc , err := lookupModuleLocation ( nil , module , "" )
if err != nil {
t . Fatal ( err )
}