github.com/magodo/terraform@v0.11.12-beta1/registry/client_test.go (about)

     1  package registry
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	version "github.com/hashicorp/go-version"
     9  	"github.com/hashicorp/terraform/registry/regsrc"
    10  	"github.com/hashicorp/terraform/registry/test"
    11  	"github.com/hashicorp/terraform/svchost/disco"
    12  )
    13  
    14  func TestLookupModuleVersions(t *testing.T) {
    15  	server := test.Registry()
    16  	defer server.Close()
    17  
    18  	client := NewClient(test.Disco(server), nil)
    19  
    20  	// test with and without a hostname
    21  	for _, src := range []string{
    22  		"example.com/test-versions/name/provider",
    23  		"test-versions/name/provider",
    24  	} {
    25  		modsrc, err := regsrc.ParseModuleSource(src)
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  
    30  		resp, err := client.Versions(modsrc)
    31  		if err != nil {
    32  			t.Fatal(err)
    33  		}
    34  
    35  		if len(resp.Modules) != 1 {
    36  			t.Fatal("expected 1 module, got", len(resp.Modules))
    37  		}
    38  
    39  		mod := resp.Modules[0]
    40  		name := "test-versions/name/provider"
    41  		if mod.Source != name {
    42  			t.Fatalf("expected module name %q, got %q", name, mod.Source)
    43  		}
    44  
    45  		if len(mod.Versions) != 4 {
    46  			t.Fatal("expected 4 versions, got", len(mod.Versions))
    47  		}
    48  
    49  		for _, v := range mod.Versions {
    50  			_, err := version.NewVersion(v.Version)
    51  			if err != nil {
    52  				t.Fatalf("invalid version %q: %s", v.Version, err)
    53  			}
    54  		}
    55  	}
    56  }
    57  
    58  func TestInvalidRegistry(t *testing.T) {
    59  	server := test.Registry()
    60  	defer server.Close()
    61  
    62  	client := NewClient(test.Disco(server), nil)
    63  
    64  	src := "non-existent.localhost.localdomain/test-versions/name/provider"
    65  	modsrc, err := regsrc.ParseModuleSource(src)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	if _, err := client.Versions(modsrc); err == nil {
    71  		t.Fatal("expected error")
    72  	}
    73  }
    74  
    75  func TestRegistryAuth(t *testing.T) {
    76  	server := test.Registry()
    77  	defer server.Close()
    78  
    79  	client := NewClient(test.Disco(server), nil)
    80  
    81  	src := "private/name/provider"
    82  	mod, err := regsrc.ParseModuleSource(src)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	_, err = client.Versions(mod)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	_, err = client.Location(mod, "1.0.0")
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	// Also test without a credentials source
    97  	client.services.SetCredentialsSource(nil)
    98  
    99  	// both should fail without auth
   100  	_, err = client.Versions(mod)
   101  	if err == nil {
   102  		t.Fatal("expected error")
   103  	}
   104  	_, err = client.Location(mod, "1.0.0")
   105  	if err == nil {
   106  		t.Fatal("expected error")
   107  	}
   108  }
   109  
   110  func TestLookupModuleLocationRelative(t *testing.T) {
   111  	server := test.Registry()
   112  	defer server.Close()
   113  
   114  	client := NewClient(test.Disco(server), nil)
   115  
   116  	src := "relative/foo/bar"
   117  	mod, err := regsrc.ParseModuleSource(src)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	got, err := client.Location(mod, "0.2.0")
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	want := server.URL + "/relative-path"
   128  	if got != want {
   129  		t.Errorf("wrong location %s; want %s", got, want)
   130  	}
   131  }
   132  
   133  func TestAccLookupModuleVersions(t *testing.T) {
   134  	if os.Getenv("TF_ACC") == "" {
   135  		t.Skip()
   136  	}
   137  	regDisco := disco.New()
   138  
   139  	// test with and without a hostname
   140  	for _, src := range []string{
   141  		"terraform-aws-modules/vpc/aws",
   142  		regsrc.PublicRegistryHost.String() + "/terraform-aws-modules/vpc/aws",
   143  	} {
   144  		modsrc, err := regsrc.ParseModuleSource(src)
   145  		if err != nil {
   146  			t.Fatal(err)
   147  		}
   148  
   149  		s := NewClient(regDisco, nil)
   150  		resp, err := s.Versions(modsrc)
   151  		if err != nil {
   152  			t.Fatal(err)
   153  		}
   154  
   155  		if len(resp.Modules) != 1 {
   156  			t.Fatal("expected 1 module, got", len(resp.Modules))
   157  		}
   158  
   159  		mod := resp.Modules[0]
   160  		name := "terraform-aws-modules/vpc/aws"
   161  		if mod.Source != name {
   162  			t.Fatalf("expected module name %q, got %q", name, mod.Source)
   163  		}
   164  
   165  		if len(mod.Versions) == 0 {
   166  			t.Fatal("expected multiple versions, got 0")
   167  		}
   168  
   169  		for _, v := range mod.Versions {
   170  			_, err := version.NewVersion(v.Version)
   171  			if err != nil {
   172  				t.Fatalf("invalid version %q: %s", v.Version, err)
   173  			}
   174  		}
   175  	}
   176  }
   177  
   178  // the error should reference the config source exatly, not the discovered path.
   179  func TestLookupLookupModuleError(t *testing.T) {
   180  	server := test.Registry()
   181  	defer server.Close()
   182  
   183  	client := NewClient(test.Disco(server), nil)
   184  
   185  	// this should not be found in teh registry
   186  	src := "bad/local/path"
   187  	mod, err := regsrc.ParseModuleSource(src)
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  
   192  	_, err = client.Location(mod, "0.2.0")
   193  	if err == nil {
   194  		t.Fatal("expected error")
   195  	}
   196  
   197  	// check for the exact quoted string to ensure we didn't prepend a hostname.
   198  	if !strings.Contains(err.Error(), `"bad/local/path"`) {
   199  		t.Fatal("error should not include the hostname. got:", err)
   200  	}
   201  }