make //subdir notation work with registry modules

Append the original source subdir to the discovered subdir when locating
the files in the module tree.
This commit is contained in:
James Bardin 2017-10-17 18:01:34 -04:00
parent a359c4ee2f
commit 8933ef8f70
3 changed files with 58 additions and 39 deletions

View File

@ -101,6 +101,26 @@ func mockRegistry() *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(localDetector),
}
return func() {
detectors = origDetectors
}
}
func TestDetectRegistry(t *testing.T) {
server := mockRegistry()
defer server.Close()
@ -178,25 +198,13 @@ func TestDetectRegistry(t *testing.T) {
func TestDetectors(t *testing.T) {
server := mockRegistry()
defer server.Close()
defer setResetRegDetector(server)()
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
regDetector := &registryDetector{
api: server.URL + "/v1/modules",
client: server.Client(),
}
detectors := []getter.Detector{
new(getter.GitHubDetector),
new(getter.BitBucketDetector),
new(getter.S3Detector),
regDetector,
new(localDetector),
}
for _, tc := range []struct {
source string
location string
@ -291,24 +299,7 @@ func TestDetectors(t *testing.T) {
func TestRegistryGitHubArchive(t *testing.T) {
server := mockRegistry()
defer server.Close()
regDetector := &registryDetector{
api: server.URL + "/v1/modules",
client: server.Client(),
}
origDetectors := detectors
defer func() {
detectors = origDetectors
}()
detectors = []getter.Detector{
new(getter.GitHubDetector),
new(getter.BitBucketDetector),
new(getter.S3Detector),
new(localDetector),
regDetector,
}
defer setResetRegDetector(server)()
storage := testStorage(t)
tree := NewTree("", testConfig(t, "registry-tar-subdir"))
@ -344,6 +335,34 @@ func TestRegistryGitHubArchive(t *testing.T) {
}
}
// Test that the //subdir notation can be used with registry modules
func TestRegisryModuleSubdir(t *testing.T) {
server := mockRegistry()
defer server.Close()
defer setResetRegDetector(server)()
storage := testStorage(t)
tree := NewTree("", testConfig(t, "registry-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)
}
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")

View File

@ -241,14 +241,9 @@ func (t *Tree) Load(s getter.Storage, mode GetMode) error {
// For example, the registry always adds a subdir of `//*`,
// indicating that we need to strip off the first component from the
// tar archive, though we may not yet know what it is called.
//
// TODO: This can cause us to lose the previously detected subdir. It
// was never an issue before, since none of the supported detectors
// previously had this behavior, but we may want to add this ability to
// registry modules.
source, subDir2 := getter.SourceDirSubdir(source)
if subDir2 != "" {
subDir = subDir2
source, detectedSubDir := getter.SourceDirSubdir(source)
if detectedSubDir != "" {
subDir = filepath.Join(detectedSubDir, subDir)
}
log.Printf("[TRACE] getting module source %q", source)

View File

@ -705,3 +705,8 @@ root
foo (path: foo)
bar (path: foo, bar)
`
const treeLoadRegistrySubdirStr = `
root
foo (path: foo)
`