opentofu/internal/registry/regsrc/module_test.go
Martin Atkins 4c5b866dea Move registry/ to internal/registry/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

142 lines
4.1 KiB
Go

package regsrc
import (
"testing"
)
func TestModule(t *testing.T) {
tests := []struct {
name string
source string
wantString string
wantDisplay string
wantNorm string
wantErr bool
}{
{
name: "public registry",
source: "hashicorp/consul/aws",
wantString: "hashicorp/consul/aws",
wantDisplay: "hashicorp/consul/aws",
wantNorm: "hashicorp/consul/aws",
wantErr: false,
},
{
name: "public registry, submodule",
source: "hashicorp/consul/aws//foo",
wantString: "hashicorp/consul/aws//foo",
wantDisplay: "hashicorp/consul/aws//foo",
wantNorm: "hashicorp/consul/aws//foo",
wantErr: false,
},
{
name: "public registry, explicit host",
source: "registry.terraform.io/hashicorp/consul/aws",
wantString: "registry.terraform.io/hashicorp/consul/aws",
wantDisplay: "hashicorp/consul/aws",
wantNorm: "hashicorp/consul/aws",
wantErr: false,
},
{
name: "public registry, mixed case",
source: "HashiCorp/Consul/aws",
wantString: "HashiCorp/Consul/aws",
wantDisplay: "hashicorp/consul/aws",
wantNorm: "hashicorp/consul/aws",
wantErr: false,
},
{
name: "private registry, custom port",
source: "Example.com:1234/HashiCorp/Consul/aws",
wantString: "Example.com:1234/HashiCorp/Consul/aws",
wantDisplay: "example.com:1234/hashicorp/consul/aws",
wantNorm: "example.com:1234/hashicorp/consul/aws",
wantErr: false,
},
{
name: "IDN registry",
source: "Испытание.com/HashiCorp/Consul/aws",
wantString: "Испытание.com/HashiCorp/Consul/aws",
wantDisplay: "испытание.com/hashicorp/consul/aws",
wantNorm: "xn--80akhbyknj4f.com/hashicorp/consul/aws",
wantErr: false,
},
{
name: "IDN registry, submodule, custom port",
source: "Испытание.com:1234/HashiCorp/Consul/aws//Foo",
wantString: "Испытание.com:1234/HashiCorp/Consul/aws//Foo",
// Note we DO lowercase submodule names. This might causes issues on
// some filesystems (e.g. HFS+) that are case-sensitive where
// //modules/Foo and //modules/foo describe different paths, but
// it's less confusing in general just to not support that. Any user
// with a module with submodules in both cases is already asking for
// portability issues, and terraform can ensure it does
// case-insensitive search for the dir in those cases.
wantDisplay: "испытание.com:1234/hashicorp/consul/aws//foo",
wantNorm: "xn--80akhbyknj4f.com:1234/hashicorp/consul/aws//foo",
wantErr: false,
},
{
name: "invalid host",
source: "---.com/HashiCorp/Consul/aws",
wantErr: true,
},
{
name: "invalid format",
source: "foo/var/baz/qux",
wantErr: true,
},
{
name: "invalid suffix",
source: "foo/var/baz?otherthing",
wantErr: true,
},
{
name: "valid host, invalid format",
source: "foo.com/var/baz?otherthing",
wantErr: true,
},
{
name: "disallow github",
source: "github.com/HashiCorp/Consul/aws",
wantErr: true,
},
{
name: "disallow bitbucket",
source: "bitbucket.org/HashiCorp/Consul/aws",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseModuleSource(tt.source)
if (err != nil) != tt.wantErr {
t.Fatalf("ParseModuleSource() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
if v := got.String(); v != tt.wantString {
t.Fatalf("String() = %v, want %v", v, tt.wantString)
}
if v := got.Display(); v != tt.wantDisplay {
t.Fatalf("Display() = %v, want %v", v, tt.wantDisplay)
}
if v := got.Normalized(); v != tt.wantNorm {
t.Fatalf("Normalized() = %v, want %v", v, tt.wantNorm)
}
gotDisplay, err := ParseModuleSource(tt.wantDisplay)
if err != nil {
t.Fatalf("ParseModuleSource(wantDisplay) error = %v", err)
}
if !got.Equal(gotDisplay) {
t.Fatalf("Equal() failed for %s and %s", tt.source, tt.wantDisplay)
}
})
}
}