github.com/magodo/terraform@v0.11.12-beta1/registry/regsrc/module_test.go (about)

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