github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/providercache/dir_modify_test.go (about)

     1  package providercache
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/apparentlymart/go-versions/versions"
    11  	"github.com/google/go-cmp/cmp"
    12  
    13  	"github.com/cycloidio/terraform/addrs"
    14  	"github.com/cycloidio/terraform/getproviders"
    15  )
    16  
    17  func TestInstallPackage(t *testing.T) {
    18  	tmpDirPath, err := ioutil.TempDir("", "terraform-test-providercache")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer os.RemoveAll(tmpDirPath)
    23  	tmpDirPath, err = filepath.EvalSymlinks(tmpDirPath)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	linuxPlatform := getproviders.Platform{
    29  		OS:   "linux",
    30  		Arch: "amd64",
    31  	}
    32  	nullProvider := addrs.NewProvider(
    33  		addrs.DefaultProviderRegistryHost, "hashicorp", "null",
    34  	)
    35  
    36  	tmpDir := NewDirWithPlatform(tmpDirPath, linuxPlatform)
    37  
    38  	meta := getproviders.PackageMeta{
    39  		Provider: nullProvider,
    40  		Version:  versions.MustParseVersion("2.1.0"),
    41  
    42  		ProtocolVersions: getproviders.VersionList{versions.MustParseVersion("5.0.0")},
    43  		TargetPlatform:   linuxPlatform,
    44  
    45  		Filename: "terraform-provider-null_2.1.0_linux_amd64.zip",
    46  		Location: getproviders.PackageLocalArchive("testdata/terraform-provider-null_2.1.0_linux_amd64.zip"),
    47  	}
    48  
    49  	result, err := tmpDir.InstallPackage(context.TODO(), meta, nil)
    50  	if err != nil {
    51  		t.Fatalf("InstallPackage failed: %s", err)
    52  	}
    53  	if result != nil {
    54  		t.Errorf("unexpected result %#v, wanted nil", result)
    55  	}
    56  
    57  	// Now we should see the same version reflected in the temporary directory.
    58  	got := tmpDir.AllAvailablePackages()
    59  	want := map[addrs.Provider][]CachedProvider{
    60  		nullProvider: {
    61  			CachedProvider{
    62  				Provider: nullProvider,
    63  
    64  				Version: versions.MustParseVersion("2.1.0"),
    65  
    66  				PackageDir: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.1.0/linux_amd64",
    67  			},
    68  		},
    69  	}
    70  	if diff := cmp.Diff(want, got); diff != "" {
    71  		t.Errorf("wrong cache contents after install\n%s", diff)
    72  	}
    73  }
    74  
    75  func TestLinkFromOtherCache(t *testing.T) {
    76  	srcDirPath := "testdata/cachedir"
    77  	tmpDirPath, err := ioutil.TempDir("", "terraform-test-providercache")
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	defer os.RemoveAll(tmpDirPath)
    82  	tmpDirPath, err = filepath.EvalSymlinks(tmpDirPath)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	windowsPlatform := getproviders.Platform{
    88  		OS:   "windows",
    89  		Arch: "amd64",
    90  	}
    91  	nullProvider := addrs.NewProvider(
    92  		addrs.DefaultProviderRegistryHost, "hashicorp", "null",
    93  	)
    94  
    95  	srcDir := NewDirWithPlatform(srcDirPath, windowsPlatform)
    96  	tmpDir := NewDirWithPlatform(tmpDirPath, windowsPlatform)
    97  
    98  	// First we'll check our preconditions: srcDir should have only the
    99  	// null provider version 2.0.0 in it, because we're faking that we're on
   100  	// Windows, and tmpDir should have no providers in it at all.
   101  
   102  	gotSrcInitial := srcDir.AllAvailablePackages()
   103  	wantSrcInitial := map[addrs.Provider][]CachedProvider{
   104  		nullProvider: {
   105  			CachedProvider{
   106  				Provider: nullProvider,
   107  
   108  				// We want 2.0.0 rather than 2.1.0 because the 2.1.0 package is
   109  				// still packed and thus not considered to be a cache member.
   110  				Version: versions.MustParseVersion("2.0.0"),
   111  
   112  				PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
   113  			},
   114  		},
   115  	}
   116  	if diff := cmp.Diff(wantSrcInitial, gotSrcInitial); diff != "" {
   117  		t.Fatalf("incorrect initial source directory contents\n%s", diff)
   118  	}
   119  
   120  	gotTmpInitial := tmpDir.AllAvailablePackages()
   121  	wantTmpInitial := map[addrs.Provider][]CachedProvider{}
   122  	if diff := cmp.Diff(wantTmpInitial, gotTmpInitial); diff != "" {
   123  		t.Fatalf("incorrect initial temp directory contents\n%s", diff)
   124  	}
   125  
   126  	cacheEntry := srcDir.ProviderLatestVersion(nullProvider)
   127  	if cacheEntry == nil {
   128  		// This is weird because we just checked for the presence of this above
   129  		t.Fatalf("null provider has no latest version in source directory")
   130  	}
   131  
   132  	err = tmpDir.LinkFromOtherCache(cacheEntry, nil)
   133  	if err != nil {
   134  		t.Fatalf("LinkFromOtherCache failed: %s", err)
   135  	}
   136  
   137  	// Now we should see the same version reflected in the temporary directory.
   138  	got := tmpDir.AllAvailablePackages()
   139  	want := map[addrs.Provider][]CachedProvider{
   140  		nullProvider: {
   141  			CachedProvider{
   142  				Provider: nullProvider,
   143  
   144  				// We want 2.0.0 rather than 2.1.0 because the 2.1.0 package is
   145  				// still packed and thus not considered to be a cache member.
   146  				Version: versions.MustParseVersion("2.0.0"),
   147  
   148  				PackageDir: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
   149  			},
   150  		},
   151  	}
   152  	if diff := cmp.Diff(want, got); diff != "" {
   153  		t.Errorf("wrong cache contents after link\n%s", diff)
   154  	}
   155  }