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

     1  package getproviders
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/apparentlymart/go-versions/versions"
     8  	"github.com/google/go-cmp/cmp"
     9  
    10  	svchost "github.com/hashicorp/terraform-svchost"
    11  	"github.com/eliastor/durgaform/internal/addrs"
    12  )
    13  
    14  func TestFilesystemMirrorSourceAllAvailablePackages(t *testing.T) {
    15  	source := NewFilesystemMirrorSource("testdata/filesystem-mirror")
    16  	got, err := source.AllAvailablePackages()
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	want := map[addrs.Provider]PackageMetaList{
    22  		nullProvider: {
    23  			{
    24  				Provider:       nullProvider,
    25  				Version:        versions.MustParseVersion("2.0.0"),
    26  				TargetPlatform: Platform{"darwin", "amd64"},
    27  				Filename:       "durgaform-provider-null_2.0.0_darwin_amd64.zip",
    28  				Location:       PackageLocalDir("testdata/filesystem-mirror/registry.durgaform.io/hashicorp/null/2.0.0/darwin_amd64"),
    29  			},
    30  			{
    31  				Provider:       nullProvider,
    32  				Version:        versions.MustParseVersion("2.0.0"),
    33  				TargetPlatform: Platform{"linux", "amd64"},
    34  				Filename:       "durgaform-provider-null_2.0.0_linux_amd64.zip",
    35  				Location:       PackageLocalDir("testdata/filesystem-mirror/registry.durgaform.io/hashicorp/null/2.0.0/linux_amd64"),
    36  			},
    37  			{
    38  				Provider:       nullProvider,
    39  				Version:        versions.MustParseVersion("2.1.0"),
    40  				TargetPlatform: Platform{"linux", "amd64"},
    41  				Filename:       "durgaform-provider-null_2.1.0_linux_amd64.zip",
    42  				Location:       PackageLocalArchive("testdata/filesystem-mirror/registry.durgaform.io/hashicorp/null/terraform-provider-null_2.1.0_linux_amd64.zip"),
    43  			},
    44  			{
    45  				Provider:       nullProvider,
    46  				Version:        versions.MustParseVersion("2.0.0"),
    47  				TargetPlatform: Platform{"windows", "amd64"},
    48  				Filename:       "durgaform-provider-null_2.0.0_windows_amd64.zip",
    49  				Location:       PackageLocalDir("testdata/filesystem-mirror/registry.durgaform.io/hashicorp/null/2.0.0/windows_amd64"),
    50  			},
    51  		},
    52  		randomBetaProvider: {
    53  			{
    54  				Provider:       randomBetaProvider,
    55  				Version:        versions.MustParseVersion("1.2.0"),
    56  				TargetPlatform: Platform{"linux", "amd64"},
    57  				Filename:       "durgaform-provider-random-beta_1.2.0_linux_amd64.zip",
    58  				Location:       PackageLocalDir("testdata/filesystem-mirror/registry.durgaform.io/hashicorp/random-beta/1.2.0/linux_amd64"),
    59  			},
    60  		},
    61  		randomProvider: {
    62  			{
    63  				Provider:       randomProvider,
    64  				Version:        versions.MustParseVersion("1.2.0"),
    65  				TargetPlatform: Platform{"linux", "amd64"},
    66  				Filename:       "durgaform-provider-random_1.2.0_linux_amd64.zip",
    67  				Location:       PackageLocalDir("testdata/filesystem-mirror/registry.durgaform.io/hashicorp/random/1.2.0/linux_amd64"),
    68  			},
    69  		},
    70  
    71  		happycloudProvider: {
    72  			{
    73  				Provider:       happycloudProvider,
    74  				Version:        versions.MustParseVersion("0.1.0-alpha.2"),
    75  				TargetPlatform: Platform{"darwin", "amd64"},
    76  				Filename:       "durgaform-provider-happycloud_0.1.0-alpha.2_darwin_amd64.zip",
    77  				Location:       PackageLocalDir("testdata/filesystem-mirror/tfe.example.com/AwesomeCorp/happycloud/0.1.0-alpha.2/darwin_amd64"),
    78  			},
    79  		},
    80  		legacyProvider: {
    81  			{
    82  				Provider:       legacyProvider,
    83  				Version:        versions.MustParseVersion("1.0.0"),
    84  				TargetPlatform: Platform{"linux", "amd64"},
    85  				Filename:       "durgaform-provider-legacy_1.0.0_linux_amd64.zip",
    86  				Location:       PackageLocalDir("testdata/filesystem-mirror/registry.durgaform.io/-/legacy/1.0.0/linux_amd64"),
    87  			},
    88  		},
    89  	}
    90  
    91  	if diff := cmp.Diff(want, got); diff != "" {
    92  		t.Errorf("incorrect result\n%s", diff)
    93  	}
    94  }
    95  
    96  // In this test the directory layout is invalid (missing the hostname
    97  // subdirectory). The provider installer should ignore the invalid directory.
    98  func TestFilesystemMirrorSourceAllAvailablePackages_invalid(t *testing.T) {
    99  	source := NewFilesystemMirrorSource("testdata/filesystem-mirror-invalid")
   100  	_, err := source.AllAvailablePackages()
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  }
   105  
   106  func TestFilesystemMirrorSourceAvailableVersions(t *testing.T) {
   107  	source := NewFilesystemMirrorSource("testdata/filesystem-mirror")
   108  	got, _, err := source.AvailableVersions(context.Background(), nullProvider)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	want := VersionList{
   114  		versions.MustParseVersion("2.0.0"),
   115  		versions.MustParseVersion("2.1.0"),
   116  	}
   117  
   118  	if diff := cmp.Diff(want, got); diff != "" {
   119  		t.Errorf("incorrect result\n%s", diff)
   120  	}
   121  }
   122  
   123  func TestFilesystemMirrorSourcePackageMeta(t *testing.T) {
   124  	t.Run("available platform", func(t *testing.T) {
   125  		source := NewFilesystemMirrorSource("testdata/filesystem-mirror")
   126  		got, err := source.PackageMeta(
   127  			context.Background(),
   128  			nullProvider,
   129  			versions.MustParseVersion("2.0.0"),
   130  			Platform{"linux", "amd64"},
   131  		)
   132  		if err != nil {
   133  			t.Fatal(err)
   134  		}
   135  
   136  		want := PackageMeta{
   137  			Provider:       nullProvider,
   138  			Version:        versions.MustParseVersion("2.0.0"),
   139  			TargetPlatform: Platform{"linux", "amd64"},
   140  			Filename:       "durgaform-provider-null_2.0.0_linux_amd64.zip",
   141  			Location:       PackageLocalDir("testdata/filesystem-mirror/registry.durgaform.io/hashicorp/null/2.0.0/linux_amd64"),
   142  		}
   143  
   144  		if diff := cmp.Diff(want, got); diff != "" {
   145  			t.Errorf("incorrect result\n%s", diff)
   146  		}
   147  
   148  		if gotHashes := got.AcceptableHashes(); len(gotHashes) != 0 {
   149  			t.Errorf("wrong acceptable hashes\ngot:  %#v\nwant: none", gotHashes)
   150  		}
   151  	})
   152  	t.Run("unavailable platform", func(t *testing.T) {
   153  		source := NewFilesystemMirrorSource("testdata/filesystem-mirror")
   154  		// We'll request a version that does exist in the fixture directory,
   155  		// but for a platform that isn't supported.
   156  		_, err := source.PackageMeta(
   157  			context.Background(),
   158  			nullProvider,
   159  			versions.MustParseVersion("2.0.0"),
   160  			Platform{"nonexist", "nonexist"},
   161  		)
   162  
   163  		if err == nil {
   164  			t.Fatalf("succeeded; want error")
   165  		}
   166  
   167  		// This specific error type is important so callers can use it to
   168  		// generate an actionable error message e.g. by checking to see if
   169  		// _any_ versions of this provider support the given platform, or
   170  		// similar helpful hints.
   171  		wantErr := ErrPlatformNotSupported{
   172  			Provider: nullProvider,
   173  			Version:  versions.MustParseVersion("2.0.0"),
   174  			Platform: Platform{"nonexist", "nonexist"},
   175  		}
   176  		if diff := cmp.Diff(wantErr, err); diff != "" {
   177  			t.Errorf("incorrect error\n%s", diff)
   178  		}
   179  	})
   180  }
   181  
   182  var nullProvider = addrs.Provider{
   183  	Hostname:  svchost.Hostname("registry.durgaform.io"),
   184  	Namespace: "hashicorp",
   185  	Type:      "null",
   186  }
   187  var randomProvider = addrs.Provider{
   188  	Hostname:  svchost.Hostname("registry.durgaform.io"),
   189  	Namespace: "hashicorp",
   190  	Type:      "random",
   191  }
   192  var randomBetaProvider = addrs.Provider{
   193  	Hostname:  svchost.Hostname("registry.durgaform.io"),
   194  	Namespace: "hashicorp",
   195  	Type:      "random-beta",
   196  }
   197  var happycloudProvider = addrs.Provider{
   198  	Hostname:  svchost.Hostname("tfe.example.com"),
   199  	Namespace: "awesomecorp",
   200  	Type:      "happycloud",
   201  }
   202  var legacyProvider = addrs.NewLegacyProvider("legacy")