github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/internal/providercache/cached_provider_test.go (about)

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