github.com/medzin/terraform@v0.11.11/config/module/storage_test.go (about)

     1  package module
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/url"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/hashicorp/terraform/registry/regsrc"
    12  	"github.com/hashicorp/terraform/registry/test"
    13  )
    14  
    15  func TestGetModule(t *testing.T) {
    16  	server := test.Registry()
    17  	defer server.Close()
    18  	disco := test.Disco(server)
    19  
    20  	td, err := ioutil.TempDir("", "tf")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer os.RemoveAll(td)
    25  	storage := NewStorage(td, disco)
    26  
    27  	// this module exists in a test fixture, and is known by the test.Registry
    28  	// relative to our cwd.
    29  	err = storage.GetModule(filepath.Join(td, "foo"), "registry/local/sub")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	// list everything to make sure nothing else got unpacked in here
    35  	ls, err := ioutil.ReadDir(td)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	var names []string
    41  	for _, info := range ls {
    42  		names = append(names, info.Name())
    43  	}
    44  
    45  	if !(len(names) == 1 && names[0] == "foo") {
    46  		t.Fatalf("expected only directory 'foo', found entries %q", names)
    47  	}
    48  
    49  	_, err = os.Stat(filepath.Join(td, "foo", "main.tf"))
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  }
    54  
    55  // GitHub archives always contain the module source in a single subdirectory,
    56  // so the registry will return a path with with a `//*` suffix. We need to make
    57  // sure this doesn't intefere with our internal handling of `//` subdir.
    58  func TestRegistryGitHubArchive(t *testing.T) {
    59  	server := test.Registry()
    60  	defer server.Close()
    61  
    62  	disco := test.Disco(server)
    63  	storage := testStorage(t, disco)
    64  
    65  	tree := NewTree("", testConfig(t, "registry-tar-subdir"))
    66  
    67  	storage.Mode = GetModeGet
    68  	if err := tree.Load(storage); err != nil {
    69  		t.Fatalf("err: %s", err)
    70  	}
    71  
    72  	if !tree.Loaded() {
    73  		t.Fatal("should be loaded")
    74  	}
    75  
    76  	storage.Mode = GetModeNone
    77  	if err := tree.Load(storage); err != nil {
    78  		t.Fatalf("err: %s", err)
    79  	}
    80  
    81  	// stop the registry server, and make sure that we don't need to call out again
    82  	server.Close()
    83  	tree = NewTree("", testConfig(t, "registry-tar-subdir"))
    84  
    85  	storage.Mode = GetModeGet
    86  	if err := tree.Load(storage); err != nil {
    87  		t.Fatalf("err: %s", err)
    88  	}
    89  
    90  	if !tree.Loaded() {
    91  		t.Fatal("should be loaded")
    92  	}
    93  
    94  	actual := strings.TrimSpace(tree.String())
    95  	expected := strings.TrimSpace(treeLoadSubdirStr)
    96  	if actual != expected {
    97  		t.Fatalf("got: \n\n%s\nexpected: \n\n%s", actual, expected)
    98  	}
    99  }
   100  
   101  // Test that the //subdir notation can be used with registry modules
   102  func TestRegisryModuleSubdir(t *testing.T) {
   103  	server := test.Registry()
   104  	defer server.Close()
   105  
   106  	disco := test.Disco(server)
   107  	storage := testStorage(t, disco)
   108  	tree := NewTree("", testConfig(t, "registry-subdir"))
   109  
   110  	storage.Mode = GetModeGet
   111  	if err := tree.Load(storage); err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  
   115  	if !tree.Loaded() {
   116  		t.Fatal("should be loaded")
   117  	}
   118  
   119  	storage.Mode = GetModeNone
   120  	if err := tree.Load(storage); err != nil {
   121  		t.Fatalf("err: %s", err)
   122  	}
   123  
   124  	actual := strings.TrimSpace(tree.String())
   125  	expected := strings.TrimSpace(treeLoadRegistrySubdirStr)
   126  	if actual != expected {
   127  		t.Fatalf("got: \n\n%s\nexpected: \n\n%s", actual, expected)
   128  	}
   129  }
   130  
   131  func TestAccRegistryDiscover(t *testing.T) {
   132  	if os.Getenv("TF_ACC") == "" {
   133  		t.Skip("skipping ACC test")
   134  	}
   135  
   136  	// simply check that we get a valid github URL for this from the registry
   137  	module, err := regsrc.ParseModuleSource("hashicorp/consul/aws")
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  
   142  	s := NewStorage("/tmp", nil)
   143  	loc, err := s.registry.Location(module, "")
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	u, err := url.Parse(loc)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  	if !strings.HasSuffix(u.Host, "github.com") {
   154  		t.Fatalf("expected host 'github.com', got: %q", u.Host)
   155  	}
   156  
   157  	if !strings.Contains(u.String(), "consul") {
   158  		t.Fatalf("url doesn't contain 'consul': %s", u.String())
   159  	}
   160  }
   161  
   162  func TestAccRegistryLoad(t *testing.T) {
   163  	if os.Getenv("TF_ACC") == "" {
   164  		t.Skip("skipping ACC test")
   165  	}
   166  
   167  	storage := testStorage(t, nil)
   168  	tree := NewTree("", testConfig(t, "registry-load"))
   169  
   170  	storage.Mode = GetModeGet
   171  	if err := tree.Load(storage); err != nil {
   172  		t.Fatalf("err: %s", err)
   173  	}
   174  
   175  	if !tree.Loaded() {
   176  		t.Fatal("should be loaded")
   177  	}
   178  
   179  	storage.Mode = GetModeNone
   180  	if err := tree.Load(storage); err != nil {
   181  		t.Fatalf("err: %s", err)
   182  	}
   183  
   184  	// TODO expand this further by fetching some metadata from the registry
   185  	actual := strings.TrimSpace(tree.String())
   186  	if !strings.Contains(actual, "(path: vault)") {
   187  		t.Fatal("missing vault module, got:\n", actual)
   188  	}
   189  }