github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/getproviders/didyoumean_test.go (about)

     1  package getproviders
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	svchost "github.com/iaas-resource-provision/iaas-rpc-svchost"
     8  	"github.com/iaas-resource-provision/iaas-rpc/internal/addrs"
     9  )
    10  
    11  func TestMissingProviderSuggestion(t *testing.T) {
    12  	// Most of these test cases rely on specific "magic" provider addresses
    13  	// that are implemented by the fake registry source returned by
    14  	// testRegistrySource. Refer to that function for more details on how
    15  	// they work.
    16  
    17  	t.Run("happy path", func(t *testing.T) {
    18  		ctx := context.Background()
    19  		source, _, close := testRegistrySource(t)
    20  		defer close()
    21  
    22  		// testRegistrySource handles -/legacy as a valid legacy provider
    23  		// lookup mapping to legacycorp/legacy.
    24  		legacyAddr := addrs.NewDefaultProvider("legacy")
    25  		got := MissingProviderSuggestion(
    26  			ctx,
    27  			addrs.NewDefaultProvider("legacy"),
    28  			source,
    29  			Requirements{
    30  				legacyAddr: MustParseVersionConstraints(">= 1.0.0"),
    31  			},
    32  		)
    33  
    34  		want := addrs.Provider{
    35  			Hostname:  defaultRegistryHost,
    36  			Namespace: "legacycorp",
    37  			Type:      "legacy",
    38  		}
    39  		if got != want {
    40  			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
    41  		}
    42  	})
    43  	t.Run("provider moved", func(t *testing.T) {
    44  		ctx := context.Background()
    45  		source, _, close := testRegistrySource(t)
    46  		defer close()
    47  
    48  		// testRegistrySource handles -/moved as a valid legacy provider
    49  		// lookup mapping to hashicorp/moved but with an additional "redirect"
    50  		// to acme/moved. This mimics how for some providers there is both
    51  		// a copy under terraform-providers for v0.12 compatibility _and_ a
    52  		// copy in some other namespace for v0.13 or later to use. Our naming
    53  		// suggestions ignore the v0.12-compatible one and suggest the
    54  		// other one.
    55  		moved := addrs.NewDefaultProvider("moved")
    56  		want := addrs.Provider{
    57  			Hostname:  defaultRegistryHost,
    58  			Namespace: "acme",
    59  			Type:      "moved",
    60  		}
    61  
    62  		got := MissingProviderSuggestion(
    63  			ctx,
    64  			moved,
    65  			source,
    66  			Requirements{
    67  				moved: MustParseVersionConstraints(">= 1.0.0"),
    68  			},
    69  		)
    70  
    71  		if got != want {
    72  			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
    73  		}
    74  
    75  		// If a provider has moved, but there's provider requirements
    76  		// for something of the same type, we'll return that one
    77  		// and skip the legacy lookup process. In practice,
    78  		// hopefully this is also "acme" but it's "zcme" here to
    79  		// exercise the codepath
    80  		want2 := addrs.Provider{
    81  			Hostname:  defaultRegistryHost,
    82  			Namespace: "zcme",
    83  			Type:      "moved",
    84  		}
    85  		got2 := MissingProviderSuggestion(
    86  			ctx,
    87  			moved,
    88  			source,
    89  			Requirements{
    90  				moved: MustParseVersionConstraints(">= 1.0.0"),
    91  				want2: MustParseVersionConstraints(">= 1.0.0"),
    92  			},
    93  		)
    94  
    95  		if got2 != want2 {
    96  			t.Errorf("wrong result\ngot:  %s\nwant: %s", got2, want2)
    97  		}
    98  	})
    99  	t.Run("invalid response", func(t *testing.T) {
   100  		ctx := context.Background()
   101  		source, _, close := testRegistrySource(t)
   102  		defer close()
   103  
   104  		// testRegistrySource handles -/invalid by returning an invalid
   105  		// provider address, which MissingProviderSuggestion should reject
   106  		// and behave as if there was no suggestion available.
   107  		want := addrs.NewDefaultProvider("invalid")
   108  		got := MissingProviderSuggestion(
   109  			ctx,
   110  			want,
   111  			source,
   112  			Requirements{
   113  				want: MustParseVersionConstraints(">= 1.0.0"),
   114  			},
   115  		)
   116  		if got != want {
   117  			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
   118  		}
   119  	})
   120  	t.Run("another registry", func(t *testing.T) {
   121  		ctx := context.Background()
   122  		source, _, close := testRegistrySource(t)
   123  		defer close()
   124  
   125  		// Because this provider address isn't on registry.terraform.io,
   126  		// MissingProviderSuggestion won't even attempt to make a suggestion
   127  		// for it.
   128  		want := addrs.Provider{
   129  			Hostname:  svchost.Hostname("example.com"),
   130  			Namespace: "whatever",
   131  			Type:      "foo",
   132  		}
   133  		got := MissingProviderSuggestion(
   134  			ctx,
   135  			want,
   136  			source,
   137  			Requirements{
   138  				want: MustParseVersionConstraints(">= 1.0.0"),
   139  			},
   140  		)
   141  		if got != want {
   142  			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
   143  		}
   144  	})
   145  	t.Run("another namespace", func(t *testing.T) {
   146  		ctx := context.Background()
   147  		source, _, close := testRegistrySource(t)
   148  		defer close()
   149  
   150  		// Because this provider address isn't in
   151  		// registry.terraform.io/hashicorp/..., MissingProviderSuggestion
   152  		// will provide the same addr since there's no alternative in Requirements
   153  		want := addrs.Provider{
   154  			Hostname:  defaultRegistryHost,
   155  			Namespace: "whatever",
   156  			Type:      "foo",
   157  		}
   158  		got := MissingProviderSuggestion(
   159  			ctx,
   160  			want,
   161  			source,
   162  			Requirements{
   163  				want: MustParseVersionConstraints(">= 1.0.0"),
   164  			},
   165  		)
   166  		if got != want {
   167  			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
   168  		}
   169  
   170  		// If there is a provider required that has the same type,
   171  		// but different namespace, we can suggest that
   172  		foo := addrs.Provider{
   173  			Hostname:  defaultRegistryHost,
   174  			Namespace: "hashicorp",
   175  			Type:      "foo",
   176  		}
   177  		realFoo := addrs.Provider{
   178  			Hostname:  defaultRegistryHost,
   179  			Namespace: "acme",
   180  			Type:      "foo",
   181  		}
   182  		got2 := MissingProviderSuggestion(
   183  			ctx,
   184  			foo,
   185  			source,
   186  			Requirements{
   187  				foo:     MustParseVersionConstraints(">= 1.0.0"),
   188  				realFoo: MustParseVersionConstraints(">= 1.0.0"),
   189  			},
   190  		)
   191  		if got2 != realFoo {
   192  			t.Errorf("wrong result\ngot:  %s\nwant: %s", got2, realFoo)
   193  		}
   194  	})
   195  }