github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/initwd/from_module_test.go (about)

     1  package initwd
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	version "github.com/hashicorp/go-version"
    10  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs"
    11  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs/configload"
    12  	"github.com/hashicorp/terraform-plugin-sdk/internal/registry"
    13  	"github.com/hashicorp/terraform-plugin-sdk/internal/tfdiags"
    14  )
    15  
    16  func TestDirFromModule_registry(t *testing.T) {
    17  	if os.Getenv("TF_ACC") == "" {
    18  		t.Skip("this test accesses registry.terraform.io and github.com; set TF_ACC=1 to run it")
    19  	}
    20  
    21  	fixtureDir := filepath.Clean("testdata/empty")
    22  	tmpDir, done := tempChdir(t, fixtureDir)
    23  
    24  	// the module installer runs filepath.EvalSymlinks() on the destination
    25  	// directory before copying files, and the resultant directory is what is
    26  	// returned by the install hooks. Without this, tests could fail on machines
    27  	// where the default temp dir was a symlink.
    28  	dir, err := filepath.EvalSymlinks(tmpDir)
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  	modsDir := filepath.Join(dir, ".terraform/modules")
    33  	defer done()
    34  
    35  	hooks := &testInstallHooks{}
    36  
    37  	reg := registry.NewClient(nil, nil)
    38  	diags := DirFromModule(dir, modsDir, "hashicorp/module-installer-acctest/aws//examples/main", reg, hooks)
    39  	assertNoDiagnostics(t, diags)
    40  
    41  	v := version.Must(version.NewVersion("0.0.1"))
    42  
    43  	wantCalls := []testInstallHookCall{
    44  		// The module specified to populate the root directory is not mentioned
    45  		// here, because the hook mechanism is defined to talk about descendent
    46  		// modules only and so a caller to InitDirFromModule is expected to
    47  		// produce its own user-facing announcement about the root module being
    48  		// installed.
    49  
    50  		// Note that "root" in the following examples is, confusingly, the
    51  		// label on the module block in the example we've installed here:
    52  		//     module "root" {
    53  
    54  		{
    55  			Name:        "Download",
    56  			ModuleAddr:  "root",
    57  			PackageAddr: "hashicorp/module-installer-acctest/aws",
    58  			Version:     v,
    59  		},
    60  		{
    61  			Name:       "Install",
    62  			ModuleAddr: "root",
    63  			Version:    v,
    64  			LocalPath:  filepath.Join(dir, ".terraform/modules/root/hashicorp-terraform-aws-module-installer-acctest-5e87aff"),
    65  		},
    66  		{
    67  			Name:       "Install",
    68  			ModuleAddr: "root.child_a",
    69  			LocalPath:  filepath.Join(dir, ".terraform/modules/root/hashicorp-terraform-aws-module-installer-acctest-5e87aff/modules/child_a"),
    70  		},
    71  		{
    72  			Name:       "Install",
    73  			ModuleAddr: "root.child_a.child_b",
    74  			LocalPath:  filepath.Join(dir, ".terraform/modules/root/hashicorp-terraform-aws-module-installer-acctest-5e87aff/modules/child_b"),
    75  		},
    76  	}
    77  
    78  	if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
    79  		return
    80  	}
    81  
    82  	loader, err := configload.NewLoader(&configload.Config{
    83  		ModulesDir: modsDir,
    84  	})
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	// Make sure the configuration is loadable now.
    90  	// (This ensures that correct information is recorded in the manifest.)
    91  	config, loadDiags := loader.LoadConfig(".")
    92  	if assertNoDiagnostics(t, tfdiags.Diagnostics{}.Append(loadDiags)) {
    93  		return
    94  	}
    95  
    96  	wantTraces := map[string]string{
    97  		"":                     "in example",
    98  		"root":                 "in root module",
    99  		"root.child_a":         "in child_a module",
   100  		"root.child_a.child_b": "in child_b module",
   101  	}
   102  	gotTraces := map[string]string{}
   103  	config.DeepEach(func(c *configs.Config) {
   104  		path := strings.Join(c.Path, ".")
   105  		if c.Module.Variables["v"] == nil {
   106  			gotTraces[path] = "<missing>"
   107  			return
   108  		}
   109  		varDesc := c.Module.Variables["v"].Description
   110  		gotTraces[path] = varDesc
   111  	})
   112  	assertResultDeepEqual(t, gotTraces, wantTraces)
   113  }