From 8933ef8f7016c5e9ef8fc3bc6e3489defccd2233 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 17 Oct 2017 18:01:34 -0400 Subject: [PATCH] make //subdir notation work with registry modules Append the original source subdir to the discovered subdir when locating the files in the module tree. --- config/module/get_test.go | 81 +++++++++++++++++++++++--------------- config/module/tree.go | 11 ++---- config/module/tree_test.go | 5 +++ 3 files changed, 58 insertions(+), 39 deletions(-) diff --git a/config/module/get_test.go b/config/module/get_test.go index 19dd09b8c..460af32d7 100644 --- a/config/module/get_test.go +++ b/config/module/get_test.go @@ -101,6 +101,26 @@ func mockRegistry() *httptest.Server { return server } +func setResetRegDetector(server *httptest.Server) func() { + regDetector := ®istryDetector{ + 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 := ®istryDetector{ - 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 := ®istryDetector{ - 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") diff --git a/config/module/tree.go b/config/module/tree.go index 680b8b983..03c2a1621 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -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) diff --git a/config/module/tree_test.go b/config/module/tree_test.go index 97847c6ac..ae95dbcbb 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -705,3 +705,8 @@ root foo (path: foo) bar (path: foo, bar) ` + +const treeLoadRegistrySubdirStr = ` +root + foo (path: foo) +`