github.com/hashicorp/packer@v1.14.3/hcl2template/addrs/plugin_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package addrs
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  )
    12  
    13  func TestPluginParseSourceString(t *testing.T) {
    14  	tests := []struct {
    15  		name      string
    16  		source    string
    17  		want      *Plugin
    18  		wantDiags bool
    19  	}{
    20  		{"invalid: only one component, rejected", "potato", nil, true},
    21  		{"invalid: two components in name", "hashicorp/azr", nil, true},
    22  		{"valid: three components, nothing superfluous", "github.com/hashicorp/azr", &Plugin{"github.com/hashicorp/azr"}, false},
    23  		{"valid: 16 components, nothing superfluous", "github.com/hashicorp/azr/a/b/c/d/e/f/g/h/i/j/k/l/m", &Plugin{"github.com/hashicorp/azr/a/b/c/d/e/f/g/h/i/j/k/l/m"}, false},
    24  		{"invalid: trailing slash", "github.com/hashicorp/azr/", nil, true},
    25  		{"invalid: reject because scheme specified", "https://github.com/hashicorp/azr", nil, true},
    26  		{"invalid: reject because query non nil", "github.com/hashicorp/azr?arg=1", nil, true},
    27  		{"invalid: reject because fragment present", "github.com/hashicorp/azr#anchor", nil, true},
    28  		{"invalid: leading and trailing slashes are removed", "/github.com/hashicorp/azr/", nil, true},
    29  		{"invalid: leading slashes are removed", "/github.com/hashicorp/azr", nil, true},
    30  		{"invalid: plugin name contains packer-", "/github.com/hashicorp/packer-azr", nil, true},
    31  		{"invalid: plugin name contains packer-plugin-", "/github.com/hashicorp/packer-plugin-azr", nil, true},
    32  		{"invalid: 17 components, too many parts to URL", "github.com/hashicorp/azr/a/b/c/d/e/f/g/h/i/j/k/l/m/n", nil, true},
    33  	}
    34  	for _, tt := range tests {
    35  		t.Run(tt.name, func(t *testing.T) {
    36  			got, err := ParsePluginSourceString(tt.source)
    37  			if !reflect.DeepEqual(got, tt.want) {
    38  				t.Errorf("ParsePluginSourceString() got = %v, want %v", got, tt.want)
    39  			}
    40  			if tt.wantDiags && err == nil {
    41  				t.Errorf("Expected error, but got none")
    42  			}
    43  			if !tt.wantDiags && err != nil {
    44  				t.Errorf("Unexpected error: %s", err)
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestPluginName(t *testing.T) {
    51  	tests := []struct {
    52  		name         string
    53  		pluginString string
    54  		expectName   string
    55  	}{
    56  		{
    57  			"valid minimal name",
    58  			"github.com/hashicorp/amazon",
    59  			"amazon",
    60  		},
    61  		{
    62  			// Technically we can call `Name` on a plugin created manually
    63  			// but this is invalid as the Source's Name should not contain
    64  			// `packer-plugin-`.
    65  			"invalid name with prefix",
    66  			"github.com/hashicorp/packer-plugin-amazon",
    67  			"packer-plugin-amazon",
    68  		},
    69  	}
    70  
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			plug := &Plugin{
    74  				Source: tt.pluginString,
    75  			}
    76  
    77  			name := plug.Name()
    78  			if name != tt.expectName {
    79  				t.Errorf("Expected plugin %q to have %q as name, got %q", tt.pluginString, tt.expectName, name)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestPluginParts(t *testing.T) {
    86  	tests := []struct {
    87  		name          string
    88  		pluginSource  string
    89  		expectedParts []string
    90  	}{
    91  		{
    92  			"valid with two parts",
    93  			"factiartory.com/packer",
    94  			[]string{"factiartory.com", "packer"},
    95  		},
    96  		{
    97  			"valid with four parts",
    98  			"factiartory.com/hashicrop/fields/packer",
    99  			[]string{"factiartory.com", "hashicrop", "fields", "packer"},
   100  		},
   101  		{
   102  			"valid, with double-slashes in the name",
   103  			"factiartory.com/hashicrop//fields/packer//",
   104  			[]string{"factiartory.com", "hashicrop", "fields", "packer"},
   105  		},
   106  	}
   107  
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			plugin := &Plugin{tt.pluginSource}
   111  			diff := cmp.Diff(plugin.Parts(), tt.expectedParts)
   112  			if diff != "" {
   113  				t.Errorf("Difference found between expected and computed parts: %s", diff)
   114  			}
   115  		})
   116  	}
   117  }