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

     1  package registry
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	version "github.com/hashicorp/go-version"
    10  	"github.com/hashicorp/terraform-plugin-sdk/httpclient"
    11  	"github.com/hashicorp/terraform-plugin-sdk/internal/registry/regsrc"
    12  	"github.com/hashicorp/terraform-plugin-sdk/internal/registry/test"
    13  	tfversion "github.com/hashicorp/terraform-plugin-sdk/internal/version"
    14  	"github.com/hashicorp/terraform-svchost/disco"
    15  )
    16  
    17  func TestLookupModuleVersions(t *testing.T) {
    18  	server := test.Registry()
    19  	defer server.Close()
    20  
    21  	client := NewClient(test.Disco(server), nil)
    22  
    23  	// test with and without a hostname
    24  	for _, src := range []string{
    25  		"example.com/test-versions/name/provider",
    26  		"test-versions/name/provider",
    27  	} {
    28  		modsrc, err := regsrc.ParseModuleSource(src)
    29  		if err != nil {
    30  			t.Fatal(err)
    31  		}
    32  
    33  		resp, err := client.ModuleVersions(modsrc)
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  
    38  		if len(resp.Modules) != 1 {
    39  			t.Fatal("expected 1 module, got", len(resp.Modules))
    40  		}
    41  
    42  		mod := resp.Modules[0]
    43  		name := "test-versions/name/provider"
    44  		if mod.Source != name {
    45  			t.Fatalf("expected module name %q, got %q", name, mod.Source)
    46  		}
    47  
    48  		if len(mod.Versions) != 4 {
    49  			t.Fatal("expected 4 versions, got", len(mod.Versions))
    50  		}
    51  
    52  		for _, v := range mod.Versions {
    53  			_, err := version.NewVersion(v.Version)
    54  			if err != nil {
    55  				t.Fatalf("invalid version %q: %s", v.Version, err)
    56  			}
    57  		}
    58  	}
    59  }
    60  
    61  func TestInvalidRegistry(t *testing.T) {
    62  	server := test.Registry()
    63  	defer server.Close()
    64  
    65  	client := NewClient(test.Disco(server), nil)
    66  
    67  	src := "non-existent.localhost.localdomain/test-versions/name/provider"
    68  	modsrc, err := regsrc.ParseModuleSource(src)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	if _, err := client.ModuleVersions(modsrc); err == nil {
    74  		t.Fatal("expected error")
    75  	}
    76  }
    77  
    78  func TestRegistryAuth(t *testing.T) {
    79  	server := test.Registry()
    80  	defer server.Close()
    81  
    82  	client := NewClient(test.Disco(server), nil)
    83  
    84  	src := "private/name/provider"
    85  	mod, err := regsrc.ParseModuleSource(src)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	_, err = client.ModuleVersions(mod)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	_, err = client.ModuleLocation(mod, "1.0.0")
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	// Also test without a credentials source
   100  	client.services.SetCredentialsSource(nil)
   101  
   102  	// both should fail without auth
   103  	_, err = client.ModuleVersions(mod)
   104  	if err == nil {
   105  		t.Fatal("expected error")
   106  	}
   107  	_, err = client.ModuleLocation(mod, "1.0.0")
   108  	if err == nil {
   109  		t.Fatal("expected error")
   110  	}
   111  }
   112  
   113  func TestLookupModuleLocationRelative(t *testing.T) {
   114  	server := test.Registry()
   115  	defer server.Close()
   116  
   117  	client := NewClient(test.Disco(server), nil)
   118  
   119  	src := "relative/foo/bar"
   120  	mod, err := regsrc.ParseModuleSource(src)
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	got, err := client.ModuleLocation(mod, "0.2.0")
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	want := server.URL + "/relative-path"
   131  	if got != want {
   132  		t.Errorf("wrong location %s; want %s", got, want)
   133  	}
   134  }
   135  
   136  func TestAccLookupModuleVersions(t *testing.T) {
   137  	if os.Getenv("TF_ACC") == "" {
   138  		t.Skip()
   139  	}
   140  	regDisco := disco.New()
   141  	regDisco.SetUserAgent(httpclient.TerraformUserAgent(tfversion.String()))
   142  
   143  	// test with and without a hostname
   144  	for _, src := range []string{
   145  		"terraform-aws-modules/vpc/aws",
   146  		regsrc.PublicRegistryHost.String() + "/terraform-aws-modules/vpc/aws",
   147  	} {
   148  		modsrc, err := regsrc.ParseModuleSource(src)
   149  		if err != nil {
   150  			t.Fatal(err)
   151  		}
   152  
   153  		s := NewClient(regDisco, nil)
   154  		resp, err := s.ModuleVersions(modsrc)
   155  		if err != nil {
   156  			t.Fatal(err)
   157  		}
   158  
   159  		if len(resp.Modules) != 1 {
   160  			t.Fatal("expected 1 module, got", len(resp.Modules))
   161  		}
   162  
   163  		mod := resp.Modules[0]
   164  		name := "terraform-aws-modules/vpc/aws"
   165  		if mod.Source != name {
   166  			t.Fatalf("expected module name %q, got %q", name, mod.Source)
   167  		}
   168  
   169  		if len(mod.Versions) == 0 {
   170  			t.Fatal("expected multiple versions, got 0")
   171  		}
   172  
   173  		for _, v := range mod.Versions {
   174  			_, err := version.NewVersion(v.Version)
   175  			if err != nil {
   176  				t.Fatalf("invalid version %q: %s", v.Version, err)
   177  			}
   178  		}
   179  	}
   180  }
   181  
   182  // the error should reference the config source exatly, not the discovered path.
   183  func TestLookupLookupModuleError(t *testing.T) {
   184  	server := test.Registry()
   185  	defer server.Close()
   186  
   187  	client := NewClient(test.Disco(server), nil)
   188  
   189  	// this should not be found in teh registry
   190  	src := "bad/local/path"
   191  	mod, err := regsrc.ParseModuleSource(src)
   192  	if err != nil {
   193  		t.Fatal(err)
   194  	}
   195  
   196  	_, err = client.ModuleLocation(mod, "0.2.0")
   197  	if err == nil {
   198  		t.Fatal("expected error")
   199  	}
   200  
   201  	// check for the exact quoted string to ensure we didn't prepend a hostname.
   202  	if !strings.Contains(err.Error(), `"bad/local/path"`) {
   203  		t.Fatal("error should not include the hostname. got:", err)
   204  	}
   205  }
   206  
   207  func TestLookupProviderVersions(t *testing.T) {
   208  	server := test.Registry()
   209  	defer server.Close()
   210  
   211  	client := NewClient(test.Disco(server), nil)
   212  
   213  	tests := []struct {
   214  		name string
   215  	}{
   216  		{"foo"},
   217  		{"bar"},
   218  	}
   219  	for _, tt := range tests {
   220  		provider := regsrc.NewTerraformProvider(tt.name, "", "")
   221  		resp, err := client.TerraformProviderVersions(provider)
   222  		if err != nil {
   223  			t.Fatal(err)
   224  		}
   225  
   226  		name := fmt.Sprintf("terraform-providers/%s", tt.name)
   227  		if resp.ID != name {
   228  			t.Fatalf("expected provider name %q, got %q", name, resp.ID)
   229  		}
   230  
   231  		if len(resp.Versions) != 2 {
   232  			t.Fatal("expected 2 versions, got", len(resp.Versions))
   233  		}
   234  
   235  		for _, v := range resp.Versions {
   236  			_, err := version.NewVersion(v.Version)
   237  			if err != nil {
   238  				t.Fatalf("invalid version %#v: %v", v, err)
   239  			}
   240  		}
   241  	}
   242  }
   243  
   244  func TestLookupProviderLocation(t *testing.T) {
   245  	server := test.Registry()
   246  	defer server.Close()
   247  
   248  	client := NewClient(test.Disco(server), nil)
   249  
   250  	tests := []struct {
   251  		Name    string
   252  		Version string
   253  		Err     bool
   254  	}{
   255  		{
   256  			"foo",
   257  			"0.2.3",
   258  			false,
   259  		},
   260  		{
   261  			"bar",
   262  			"0.1.1",
   263  			false,
   264  		},
   265  		{
   266  			"baz",
   267  			"0.0.0",
   268  			true,
   269  		},
   270  	}
   271  	for _, tt := range tests {
   272  		// FIXME: the tests are set up to succeed - os/arch is not being validated at this time
   273  		p := regsrc.NewTerraformProvider(tt.Name, "linux", "amd64")
   274  
   275  		locationMetadata, err := client.TerraformProviderLocation(p, tt.Version)
   276  		if tt.Err {
   277  			if err == nil {
   278  				t.Fatal("succeeded; want error")
   279  			}
   280  			return
   281  		} else if err != nil {
   282  			t.Fatalf("unexpected error: %s", err)
   283  		}
   284  
   285  		downloadURL := fmt.Sprintf("https://releases.hashicorp.com/terraform-provider-%s/%s/terraform-provider-%s.zip", tt.Name, tt.Version, tt.Name)
   286  
   287  		if locationMetadata.DownloadURL != downloadURL {
   288  			t.Fatalf("incorrect download URL: expected %q, got %q", downloadURL, locationMetadata.DownloadURL)
   289  		}
   290  	}
   291  
   292  }