github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/registry/regsrc/module_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package regsrc
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func TestModule(t *testing.T) {
    11  	tests := []struct {
    12  		name        string
    13  		source      string
    14  		wantString  string
    15  		wantDisplay string
    16  		wantNorm    string
    17  		wantErr     bool
    18  	}{
    19  		{
    20  			name:        "public registry",
    21  			source:      "hashicorp/consul/aws",
    22  			wantString:  "hashicorp/consul/aws",
    23  			wantDisplay: "hashicorp/consul/aws",
    24  			wantNorm:    "hashicorp/consul/aws",
    25  			wantErr:     false,
    26  		},
    27  		{
    28  			name:        "public registry, submodule",
    29  			source:      "hashicorp/consul/aws//foo",
    30  			wantString:  "hashicorp/consul/aws//foo",
    31  			wantDisplay: "hashicorp/consul/aws//foo",
    32  			wantNorm:    "hashicorp/consul/aws//foo",
    33  			wantErr:     false,
    34  		},
    35  		{
    36  			name:        "public registry, explicit host",
    37  			source:      "registry.terraform.io/hashicorp/consul/aws",
    38  			wantString:  "registry.terraform.io/hashicorp/consul/aws",
    39  			wantDisplay: "hashicorp/consul/aws",
    40  			wantNorm:    "hashicorp/consul/aws",
    41  			wantErr:     false,
    42  		},
    43  		{
    44  			name:        "public registry, mixed case",
    45  			source:      "HashiCorp/Consul/aws",
    46  			wantString:  "HashiCorp/Consul/aws",
    47  			wantDisplay: "hashicorp/consul/aws",
    48  			wantNorm:    "hashicorp/consul/aws",
    49  			wantErr:     false,
    50  		},
    51  		{
    52  			name:        "private registry, custom port",
    53  			source:      "Example.com:1234/HashiCorp/Consul/aws",
    54  			wantString:  "Example.com:1234/HashiCorp/Consul/aws",
    55  			wantDisplay: "example.com:1234/hashicorp/consul/aws",
    56  			wantNorm:    "example.com:1234/hashicorp/consul/aws",
    57  			wantErr:     false,
    58  		},
    59  		{
    60  			name:        "IDN registry",
    61  			source:      "Испытание.com/HashiCorp/Consul/aws",
    62  			wantString:  "Испытание.com/HashiCorp/Consul/aws",
    63  			wantDisplay: "испытание.com/hashicorp/consul/aws",
    64  			wantNorm:    "xn--80akhbyknj4f.com/hashicorp/consul/aws",
    65  			wantErr:     false,
    66  		},
    67  		{
    68  			name:       "IDN registry, submodule, custom port",
    69  			source:     "Испытание.com:1234/HashiCorp/Consul/aws//Foo",
    70  			wantString: "Испытание.com:1234/HashiCorp/Consul/aws//Foo",
    71  			// Note we DO lowercase submodule names. This might causes issues on
    72  			// some filesystems (e.g. HFS+) that are case-sensitive where
    73  			// //modules/Foo and //modules/foo describe different paths, but
    74  			// it's less confusing in general just to not support that. Any user
    75  			// with a module with submodules in both cases is already asking for
    76  			// portability issues, and terraform can ensure it does
    77  			// case-insensitive search for the dir in those cases.
    78  			wantDisplay: "испытание.com:1234/hashicorp/consul/aws//foo",
    79  			wantNorm:    "xn--80akhbyknj4f.com:1234/hashicorp/consul/aws//foo",
    80  			wantErr:     false,
    81  		},
    82  		{
    83  			name:    "invalid host",
    84  			source:  "---.com/HashiCorp/Consul/aws",
    85  			wantErr: true,
    86  		},
    87  		{
    88  			name:    "invalid format",
    89  			source:  "foo/var/baz/qux",
    90  			wantErr: true,
    91  		},
    92  		{
    93  			name:    "invalid suffix",
    94  			source:  "foo/var/baz?otherthing",
    95  			wantErr: true,
    96  		},
    97  		{
    98  			name:    "valid host, invalid format",
    99  			source:  "foo.com/var/baz?otherthing",
   100  			wantErr: true,
   101  		},
   102  		{
   103  			name:    "disallow github",
   104  			source:  "github.com/HashiCorp/Consul/aws",
   105  			wantErr: true,
   106  		},
   107  		{
   108  			name:    "disallow bitbucket",
   109  			source:  "bitbucket.org/HashiCorp/Consul/aws",
   110  			wantErr: true,
   111  		},
   112  	}
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			got, err := ParseModuleSource(tt.source)
   116  
   117  			if (err != nil) != tt.wantErr {
   118  				t.Fatalf("ParseModuleSource() error = %v, wantErr %v", err, tt.wantErr)
   119  			}
   120  
   121  			if err != nil {
   122  				return
   123  			}
   124  
   125  			if v := got.String(); v != tt.wantString {
   126  				t.Fatalf("String() = %v, want %v", v, tt.wantString)
   127  			}
   128  			if v := got.Display(); v != tt.wantDisplay {
   129  				t.Fatalf("Display() = %v, want %v", v, tt.wantDisplay)
   130  			}
   131  			if v := got.Normalized(); v != tt.wantNorm {
   132  				t.Fatalf("Normalized() = %v, want %v", v, tt.wantNorm)
   133  			}
   134  
   135  			gotDisplay, err := ParseModuleSource(tt.wantDisplay)
   136  			if err != nil {
   137  				t.Fatalf("ParseModuleSource(wantDisplay) error = %v", err)
   138  			}
   139  			if !got.Equal(gotDisplay) {
   140  				t.Fatalf("Equal() failed for %s and %s", tt.source, tt.wantDisplay)
   141  			}
   142  		})
   143  	}
   144  }