github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/providercache/cached_provider_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package providercache
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/terramate-io/tf/addrs"
    10  	"github.com/terramate-io/tf/getproviders"
    11  )
    12  
    13  func TestCachedProviderHash(t *testing.T) {
    14  	cp := &CachedProvider{
    15  		Provider: addrs.NewProvider(
    16  			addrs.DefaultProviderRegistryHost,
    17  			"hashicorp", "null",
    18  		),
    19  		Version: getproviders.MustParseVersion("2.0.0"),
    20  
    21  		PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/darwin_amd64",
    22  	}
    23  
    24  	want := getproviders.MustParseHash("h1:qjsREM4DqEWECD43FcPqddZ9oxCG+IaMTxvWPciS05g=")
    25  	got, err := cp.Hash()
    26  	if err != nil {
    27  		t.Fatalf("unexpected error: %s", err)
    28  	}
    29  
    30  	if got != want {
    31  		t.Errorf("wrong Hash result\ngot:  %s\nwant: %s", got, want)
    32  	}
    33  
    34  	gotMatches, err := cp.MatchesHash(want)
    35  	if err != nil {
    36  		t.Fatalf("unexpected error: %s", err)
    37  	}
    38  	if wantMatches := true; gotMatches != wantMatches {
    39  		t.Errorf("wrong MatchesHash result\ngot:  %#v\nwant: %#v", gotMatches, wantMatches)
    40  	}
    41  
    42  	// The windows build has a different hash because its executable filename
    43  	// has a .exe suffix, but the darwin build (hashed above) does not.
    44  	cp2 := &CachedProvider{
    45  		Provider: addrs.NewProvider(
    46  			addrs.DefaultProviderRegistryHost,
    47  			"hashicorp", "null",
    48  		),
    49  		Version: getproviders.MustParseVersion("2.0.0"),
    50  
    51  		PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
    52  	}
    53  	gotMatches, err = cp2.MatchesHash(want)
    54  	if err != nil {
    55  		t.Fatalf("unexpected error: %s", err)
    56  	}
    57  	if wantMatches := false; gotMatches != wantMatches {
    58  		t.Errorf("wrong MatchesHash result for other package\ngot:  %#v\nwant: %#v", gotMatches, wantMatches)
    59  	}
    60  
    61  }
    62  
    63  func TestExecutableFile(t *testing.T) {
    64  	testCases := map[string]struct {
    65  		cp   *CachedProvider
    66  		file string
    67  		err  string
    68  	}{
    69  		"linux": {
    70  			cp: &CachedProvider{
    71  				Provider:   addrs.NewProvider(addrs.DefaultProviderRegistryHost, "hashicorp", "null"),
    72  				Version:    getproviders.MustParseVersion("2.0.0"),
    73  				PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/linux_amd64",
    74  			},
    75  			file: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/linux_amd64/terraform-provider-null",
    76  		},
    77  		"windows": {
    78  			cp: &CachedProvider{
    79  				Provider:   addrs.NewProvider(addrs.DefaultProviderRegistryHost, "hashicorp", "null"),
    80  				Version:    getproviders.MustParseVersion("2.0.0"),
    81  				PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
    82  			},
    83  			file: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64/terraform-provider-null.exe",
    84  		},
    85  		"missing-executable": {
    86  			cp: &CachedProvider{
    87  				Provider:   addrs.NewProvider(addrs.DefaultProviderRegistryHost, "missing", "executable"),
    88  				Version:    getproviders.MustParseVersion("2.0.0"),
    89  				PackageDir: "testdata/cachedir/registry.terraform.io/missing/executable/2.0.0/linux_amd64",
    90  			},
    91  			err: "could not find executable file starting with terraform-provider-executable",
    92  		},
    93  		"missing-dir": {
    94  			cp: &CachedProvider{
    95  				Provider:   addrs.NewProvider(addrs.DefaultProviderRegistryHost, "missing", "packagedir"),
    96  				Version:    getproviders.MustParseVersion("2.0.0"),
    97  				PackageDir: "testdata/cachedir/registry.terraform.io/missing/packagedir/2.0.0/linux_amd64",
    98  			},
    99  			err: "could not read package directory: open testdata/cachedir/registry.terraform.io/missing/packagedir/2.0.0/linux_amd64: no such file or directory",
   100  		},
   101  	}
   102  
   103  	for name, tc := range testCases {
   104  		t.Run(name, func(t *testing.T) {
   105  			file, err := tc.cp.ExecutableFile()
   106  			if file != tc.file {
   107  				t.Errorf("wrong file\n got: %q\nwant: %q", file, tc.file)
   108  			}
   109  			if err == nil && tc.err != "" {
   110  				t.Fatalf("no error returned, want: %q", tc.err)
   111  			} else if err != nil && err.Error() != tc.err {
   112  				t.Errorf("wrong error\n got: %q\nwant: %q", err, tc.err)
   113  			}
   114  		})
   115  	}
   116  }