github.com/sfdevops1/terrra4orm@v0.11.12-beta1/configs/configload/loader_install_test.go (about)

     1  package configload
     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/configs"
    11  )
    12  
    13  func TestLoaderInstallModules_local(t *testing.T) {
    14  	fixtureDir := filepath.Clean("test-fixtures/local-modules")
    15  	loader, done := tempChdirLoader(t, fixtureDir)
    16  	defer done()
    17  
    18  	hooks := &testInstallHooks{}
    19  
    20  	diags := loader.InstallModules(".", false, hooks)
    21  	assertNoDiagnostics(t, diags)
    22  
    23  	wantCalls := []testInstallHookCall{
    24  		{
    25  			Name:        "Install",
    26  			ModuleAddr:  "child_a",
    27  			PackageAddr: "",
    28  			LocalPath:   "child_a",
    29  		},
    30  		{
    31  			Name:        "Install",
    32  			ModuleAddr:  "child_a.child_b",
    33  			PackageAddr: "",
    34  			LocalPath:   "child_a/child_b",
    35  		},
    36  	}
    37  
    38  	if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
    39  		return
    40  	}
    41  
    42  	// Make sure the configuration is loadable now.
    43  	// (This ensures that correct information is recorded in the manifest.)
    44  	config, loadDiags := loader.LoadConfig(".")
    45  	assertNoDiagnostics(t, loadDiags)
    46  
    47  	wantTraces := map[string]string{
    48  		"":                "in root module",
    49  		"child_a":         "in child_a module",
    50  		"child_a.child_b": "in child_b module",
    51  	}
    52  	gotTraces := map[string]string{}
    53  	config.DeepEach(func(c *configs.Config) {
    54  		path := strings.Join(c.Path, ".")
    55  		if c.Module.Variables["v"] == nil {
    56  			gotTraces[path] = "<missing>"
    57  			return
    58  		}
    59  		varDesc := c.Module.Variables["v"].Description
    60  		gotTraces[path] = varDesc
    61  	})
    62  	assertResultDeepEqual(t, gotTraces, wantTraces)
    63  }
    64  
    65  func TestLoaderInstallModules_registry(t *testing.T) {
    66  	if os.Getenv("TF_ACC") == "" {
    67  		t.Skip("this test accesses registry.terraform.io and github.com; set TF_ACC=1 to run it")
    68  	}
    69  
    70  	fixtureDir := filepath.Clean("test-fixtures/registry-modules")
    71  	loader, done := tempChdirLoader(t, fixtureDir)
    72  	defer done()
    73  
    74  	hooks := &testInstallHooks{}
    75  
    76  	diags := loader.InstallModules(".", false, hooks)
    77  	assertNoDiagnostics(t, diags)
    78  
    79  	v := version.Must(version.NewVersion("0.0.1"))
    80  
    81  	wantCalls := []testInstallHookCall{
    82  		// the configuration builder visits each level of calls in lexicographical
    83  		// order by name, so the following list is kept in the same order.
    84  
    85  		// acctest_child_a accesses //modules/child_a directly
    86  		{
    87  			Name:        "Download",
    88  			ModuleAddr:  "acctest_child_a",
    89  			PackageAddr: "hashicorp/module-installer-acctest/aws", // intentionally excludes the subdir because we're downloading the whole package here
    90  			Version:     v,
    91  		},
    92  		{
    93  			Name:       "Install",
    94  			ModuleAddr: "acctest_child_a",
    95  			Version:    v,
    96  			LocalPath:  ".terraform/modules/acctest_child_a/hashicorp-terraform-aws-module-installer-acctest-853d038/modules/child_a",
    97  		},
    98  
    99  		// acctest_child_a.child_b
   100  		// (no download because it's a relative path inside acctest_child_a)
   101  		{
   102  			Name:       "Install",
   103  			ModuleAddr: "acctest_child_a.child_b",
   104  			LocalPath:  ".terraform/modules/acctest_child_a/hashicorp-terraform-aws-module-installer-acctest-853d038/modules/child_b",
   105  		},
   106  
   107  		// acctest_child_b accesses //modules/child_b directly
   108  		{
   109  			Name:        "Download",
   110  			ModuleAddr:  "acctest_child_b",
   111  			PackageAddr: "hashicorp/module-installer-acctest/aws", // intentionally excludes the subdir because we're downloading the whole package here
   112  			Version:     v,
   113  		},
   114  		{
   115  			Name:       "Install",
   116  			ModuleAddr: "acctest_child_b",
   117  			Version:    v,
   118  			LocalPath:  ".terraform/modules/acctest_child_b/hashicorp-terraform-aws-module-installer-acctest-853d038/modules/child_b",
   119  		},
   120  
   121  		// acctest_root
   122  		{
   123  			Name:        "Download",
   124  			ModuleAddr:  "acctest_root",
   125  			PackageAddr: "hashicorp/module-installer-acctest/aws",
   126  			Version:     v,
   127  		},
   128  		{
   129  			Name:       "Install",
   130  			ModuleAddr: "acctest_root",
   131  			Version:    v,
   132  			LocalPath:  ".terraform/modules/acctest_root/hashicorp-terraform-aws-module-installer-acctest-853d038",
   133  		},
   134  
   135  		// acctest_root.child_a
   136  		// (no download because it's a relative path inside acctest_root)
   137  		{
   138  			Name:       "Install",
   139  			ModuleAddr: "acctest_root.child_a",
   140  			LocalPath:  ".terraform/modules/acctest_root/hashicorp-terraform-aws-module-installer-acctest-853d038/modules/child_a",
   141  		},
   142  
   143  		// acctest_root.child_a.child_b
   144  		// (no download because it's a relative path inside acctest_root, via acctest_root.child_a)
   145  		{
   146  			Name:       "Install",
   147  			ModuleAddr: "acctest_root.child_a.child_b",
   148  			LocalPath:  ".terraform/modules/acctest_root/hashicorp-terraform-aws-module-installer-acctest-853d038/modules/child_b",
   149  		},
   150  	}
   151  
   152  	if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
   153  		return
   154  	}
   155  
   156  	// Make sure the configuration is loadable now.
   157  	// (This ensures that correct information is recorded in the manifest.)
   158  	config, loadDiags := loader.LoadConfig(".")
   159  	assertNoDiagnostics(t, loadDiags)
   160  
   161  	wantTraces := map[string]string{
   162  		"":                             "in local caller for registry-modules",
   163  		"acctest_root":                 "in root module",
   164  		"acctest_root.child_a":         "in child_a module",
   165  		"acctest_root.child_a.child_b": "in child_b module",
   166  		"acctest_child_a":              "in child_a module",
   167  		"acctest_child_a.child_b":      "in child_b module",
   168  		"acctest_child_b":              "in child_b module",
   169  	}
   170  	gotTraces := map[string]string{}
   171  	config.DeepEach(func(c *configs.Config) {
   172  		path := strings.Join(c.Path, ".")
   173  		if c.Module.Variables["v"] == nil {
   174  			gotTraces[path] = "<missing>"
   175  			return
   176  		}
   177  		varDesc := c.Module.Variables["v"].Description
   178  		gotTraces[path] = varDesc
   179  	})
   180  	assertResultDeepEqual(t, gotTraces, wantTraces)
   181  
   182  }
   183  
   184  func TestLoaderInstallModules_goGetter(t *testing.T) {
   185  	if os.Getenv("TF_ACC") == "" {
   186  		t.Skip("this test accesses github.com; set TF_ACC=1 to run it")
   187  	}
   188  
   189  	fixtureDir := filepath.Clean("test-fixtures/go-getter-modules")
   190  	loader, done := tempChdirLoader(t, fixtureDir)
   191  	defer done()
   192  
   193  	hooks := &testInstallHooks{}
   194  
   195  	diags := loader.InstallModules(".", false, hooks)
   196  	assertNoDiagnostics(t, diags)
   197  
   198  	wantCalls := []testInstallHookCall{
   199  		// the configuration builder visits each level of calls in lexicographical
   200  		// order by name, so the following list is kept in the same order.
   201  
   202  		// acctest_child_a accesses //modules/child_a directly
   203  		{
   204  			Name:        "Download",
   205  			ModuleAddr:  "acctest_child_a",
   206  			PackageAddr: "github.com/hashicorp/terraform-aws-module-installer-acctest?ref=v0.0.1", // intentionally excludes the subdir because we're downloading the whole repo here
   207  		},
   208  		{
   209  			Name:       "Install",
   210  			ModuleAddr: "acctest_child_a",
   211  			LocalPath:  ".terraform/modules/acctest_child_a/modules/child_a",
   212  		},
   213  
   214  		// acctest_child_a.child_b
   215  		// (no download because it's a relative path inside acctest_child_a)
   216  		{
   217  			Name:       "Install",
   218  			ModuleAddr: "acctest_child_a.child_b",
   219  			LocalPath:  ".terraform/modules/acctest_child_a/modules/child_b",
   220  		},
   221  
   222  		// acctest_child_b accesses //modules/child_b directly
   223  		{
   224  			Name:        "Download",
   225  			ModuleAddr:  "acctest_child_b",
   226  			PackageAddr: "github.com/hashicorp/terraform-aws-module-installer-acctest?ref=v0.0.1", // intentionally excludes the subdir because we're downloading the whole package here
   227  		},
   228  		{
   229  			Name:       "Install",
   230  			ModuleAddr: "acctest_child_b",
   231  			LocalPath:  ".terraform/modules/acctest_child_b/modules/child_b",
   232  		},
   233  
   234  		// acctest_root
   235  		{
   236  			Name:        "Download",
   237  			ModuleAddr:  "acctest_root",
   238  			PackageAddr: "github.com/hashicorp/terraform-aws-module-installer-acctest?ref=v0.0.1",
   239  		},
   240  		{
   241  			Name:       "Install",
   242  			ModuleAddr: "acctest_root",
   243  			LocalPath:  ".terraform/modules/acctest_root",
   244  		},
   245  
   246  		// acctest_root.child_a
   247  		// (no download because it's a relative path inside acctest_root)
   248  		{
   249  			Name:       "Install",
   250  			ModuleAddr: "acctest_root.child_a",
   251  			LocalPath:  ".terraform/modules/acctest_root/modules/child_a",
   252  		},
   253  
   254  		// acctest_root.child_a.child_b
   255  		// (no download because it's a relative path inside acctest_root, via acctest_root.child_a)
   256  		{
   257  			Name:       "Install",
   258  			ModuleAddr: "acctest_root.child_a.child_b",
   259  			LocalPath:  ".terraform/modules/acctest_root/modules/child_b",
   260  		},
   261  	}
   262  
   263  	if assertResultDeepEqual(t, hooks.Calls, wantCalls) {
   264  		return
   265  	}
   266  
   267  	// Make sure the configuration is loadable now.
   268  	// (This ensures that correct information is recorded in the manifest.)
   269  	config, loadDiags := loader.LoadConfig(".")
   270  	assertNoDiagnostics(t, loadDiags)
   271  
   272  	wantTraces := map[string]string{
   273  		"":                             "in local caller for go-getter-modules",
   274  		"acctest_root":                 "in root module",
   275  		"acctest_root.child_a":         "in child_a module",
   276  		"acctest_root.child_a.child_b": "in child_b module",
   277  		"acctest_child_a":              "in child_a module",
   278  		"acctest_child_a.child_b":      "in child_b module",
   279  		"acctest_child_b":              "in child_b module",
   280  	}
   281  	gotTraces := map[string]string{}
   282  	config.DeepEach(func(c *configs.Config) {
   283  		path := strings.Join(c.Path, ".")
   284  		if c.Module.Variables["v"] == nil {
   285  			gotTraces[path] = "<missing>"
   286  			return
   287  		}
   288  		varDesc := c.Module.Variables["v"].Description
   289  		gotTraces[path] = varDesc
   290  	})
   291  	assertResultDeepEqual(t, gotTraces, wantTraces)
   292  
   293  }
   294  
   295  type testInstallHooks struct {
   296  	Calls []testInstallHookCall
   297  }
   298  
   299  type testInstallHookCall struct {
   300  	Name        string
   301  	ModuleAddr  string
   302  	PackageAddr string
   303  	Version     *version.Version
   304  	LocalPath   string
   305  }
   306  
   307  func (h *testInstallHooks) Download(moduleAddr, packageAddr string, version *version.Version) {
   308  	h.Calls = append(h.Calls, testInstallHookCall{
   309  		Name:        "Download",
   310  		ModuleAddr:  moduleAddr,
   311  		PackageAddr: packageAddr,
   312  		Version:     version,
   313  	})
   314  }
   315  
   316  func (h *testInstallHooks) Install(moduleAddr string, version *version.Version, localPath string) {
   317  	h.Calls = append(h.Calls, testInstallHookCall{
   318  		Name:       "Install",
   319  		ModuleAddr: moduleAddr,
   320  		Version:    version,
   321  		LocalPath:  localPath,
   322  	})
   323  }