| 1 |
package tfaddr |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/google/go-cmp/cmp" |
| 9 |
svchost "github.com/hashicorp/terraform-svchost" |
| 10 |
) |
| 11 |
|
| 12 |
func TestParseModuleSource_simple(t *testing.T) { |
| 13 |
tests := map[string]struct { |
| 14 |
input string |
| 15 |
want Module |
| 16 |
wantErr string |
| 17 |
}{ |
| 18 |
"main registry implied": { |
| 19 |
input: "hashicorp/subnets/cidr", |
| 20 |
want: Module{ |
| 21 |
Package: ModulePackage{ |
| 22 |
Host: svchost.Hostname("registry.terraform.io"), |
| 23 |
Namespace: "hashicorp", |
| 24 |
Name: "subnets", |
| 25 |
TargetSystem: "cidr", |
| 26 |
}, |
| 27 |
Subdir: "", |
| 28 |
}, |
| 29 |
}, |
| 30 |
"main registry implied, subdir": { |
| 31 |
input: "hashicorp/subnets/cidr//examples/foo", |
| 32 |
want: Module{ |
| 33 |
Package: ModulePackage{ |
| 34 |
Host: svchost.Hostname("registry.terraform.io"), |
| 35 |
Namespace: "hashicorp", |
| 36 |
Name: "subnets", |
| 37 |
TargetSystem: "cidr", |
| 38 |
}, |
| 39 |
Subdir: "examples/foo", |
| 40 |
}, |
| 41 |
}, |
| 42 |
"custom registry": { |
| 43 |
input: "example.com/awesomecorp/network/happycloud", |
| 44 |
want: Module{ |
| 45 |
Package: ModulePackage{ |
| 46 |
Host: svchost.Hostname("example.com"), |
| 47 |
Namespace: "awesomecorp", |
| 48 |
Name: "network", |
| 49 |
TargetSystem: "happycloud", |
| 50 |
}, |
| 51 |
Subdir: "", |
| 52 |
}, |
| 53 |
}, |
| 54 |
"custom registry, subdir": { |
| 55 |
input: "example.com/awesomecorp/network/happycloud//examples/foo", |
| 56 |
want: Module{ |
| 57 |
Package: ModulePackage{ |
| 58 |
Host: svchost.Hostname("example.com"), |
| 59 |
Namespace: "awesomecorp", |
| 60 |
Name: "network", |
| 61 |
TargetSystem: "happycloud", |
| 62 |
}, |
| 63 |
Subdir: "examples/foo", |
| 64 |
}, |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
for name, test := range tests { |
| 69 |
t.Run(name, func(t *testing.T) { |
| 70 |
addr, err := ParseModuleSource(test.input) |
| 71 |
|
| 72 |
if test.wantErr != "" { |
| 73 |
switch { |
| 74 |
case err == nil: |
| 75 |
t.Errorf("unexpected success\nwant error: %s", test.wantErr) |
| 76 |
case err.Error() != test.wantErr: |
| 77 |
t.Errorf("wrong error messages\ngot: %s\nwant: %s", err.Error(), test.wantErr) |
| 78 |
} |
| 79 |
return |
| 80 |
} |
| 81 |
|
| 82 |
if err != nil { |
| 83 |
t.Fatalf("unexpected error: %s", err.Error()) |
| 84 |
} |
| 85 |
|
| 86 |
if diff := cmp.Diff(addr, test.want); diff != "" { |
| 87 |
t.Errorf("wrong result\n%s", diff) |
| 88 |
} |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
func TestParseModuleSource(t *testing.T) { |
| 95 |
tests := map[string]struct { |
| 96 |
input string |
| 97 |
wantString string |
| 98 |
wantForDisplay string |
| 99 |
wantForProtocol string |
| 100 |
wantErr string |
| 101 |
}{ |
| 102 |
"public registry": { |
| 103 |
input: `hashicorp/consul/aws`, |
| 104 |
wantString: `registry.terraform.io/hashicorp/consul/aws`, |
| 105 |
wantForDisplay: `hashicorp/consul/aws`, |
| 106 |
wantForProtocol: `hashicorp/consul/aws`, |
| 107 |
}, |
| 108 |
"public registry with subdir": { |
| 109 |
input: `hashicorp/consul/aws//foo`, |
| 110 |
wantString: `registry.terraform.io/hashicorp/consul/aws//foo`, |
| 111 |
wantForDisplay: `hashicorp/consul/aws//foo`, |
| 112 |
wantForProtocol: `hashicorp/consul/aws`, |
| 113 |
}, |
| 114 |
"public registry using explicit hostname": { |
| 115 |
input: `registry.terraform.io/hashicorp/consul/aws`, |
| 116 |
wantString: `registry.terraform.io/hashicorp/consul/aws`, |
| 117 |
wantForDisplay: `hashicorp/consul/aws`, |
| 118 |
wantForProtocol: `hashicorp/consul/aws`, |
| 119 |
}, |
| 120 |
"public registry with mixed case names": { |
| 121 |
input: `HashiCorp/Consul/aws`, |
| 122 |
wantString: `registry.terraform.io/HashiCorp/Consul/aws`, |
| 123 |
wantForDisplay: `HashiCorp/Consul/aws`, |
| 124 |
wantForProtocol: `HashiCorp/Consul/aws`, |
| 125 |
}, |
| 126 |
"private registry with non-standard port": { |
| 127 |
input: `Example.com:1234/HashiCorp/Consul/aws`, |
| 128 |
wantString: `example.com:1234/HashiCorp/Consul/aws`, |
| 129 |
wantForDisplay: `example.com:1234/HashiCorp/Consul/aws`, |
| 130 |
wantForProtocol: `HashiCorp/Consul/aws`, |
| 131 |
}, |
| 132 |
"private registry with IDN hostname": { |
| 133 |
input: `Испытание.com/HashiCorp/Consul/aws`, |
| 134 |
wantString: `испытание.com/HashiCorp/Consul/aws`, |
| 135 |
wantForDisplay: `испытание.com/HashiCorp/Consul/aws`, |
| 136 |
wantForProtocol: `HashiCorp/Consul/aws`, |
| 137 |
}, |
| 138 |
"private registry with IDN hostname and non-standard port": { |
| 139 |
input: `Испытание.com:1234/HashiCorp/Consul/aws//Foo`, |
| 140 |
wantString: `испытание.com:1234/HashiCorp/Consul/aws//Foo`, |
| 141 |
wantForDisplay: `испытание.com:1234/HashiCorp/Consul/aws//Foo`, |
| 142 |
wantForProtocol: `HashiCorp/Consul/aws`, |
| 143 |
}, |
| 144 |
"invalid hostname": { |
| 145 |
input: `---.com/HashiCorp/Consul/aws`, |
| 146 |
wantErr: `invalid module registry hostname "---.com"; internationalized domain names must be given as direct unicode characters, not in punycode`, |
| 147 |
}, |
| 148 |
"hostname with only one label": { |
| 149 |
// This was historically forbidden in our initial implementation, |
| 150 |
// so we keep it forbidden to avoid newly interpreting such |
| 151 |
// addresses as registry addresses rather than remote source |
| 152 |
// addresses. |
| 153 |
input: `foo/var/baz/qux`, |
| 154 |
wantErr: `invalid module registry hostname: must contain at least one dot`, |
| 155 |
}, |
| 156 |
"invalid target system characters": { |
| 157 |
input: `foo/var/no-no-no`, |
| 158 |
wantErr: `invalid target system "no-no-no": must be between one and 64 ASCII letters or digits`, |
| 159 |
}, |
| 160 |
"invalid target system length": { |
| 161 |
input: `foo/var/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah`, |
| 162 |
wantErr: `invalid target system "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah": must be between one and 64 ASCII letters or digits`, |
| 163 |
}, |
| 164 |
"invalid namespace": { |
| 165 |
input: `boop!/var/baz`, |
| 166 |
wantErr: `invalid namespace "boop!": must be between one and 64 characters, including ASCII letters, digits, dashes, and underscores, where dashes and underscores may not be the prefix or suffix`, |
| 167 |
}, |
| 168 |
"missing part with explicit hostname": { |
| 169 |
input: `foo.com/var/baz`, |
| 170 |
wantErr: `source address must have three more components after the hostname: the namespace, the name, and the target system`, |
| 171 |
}, |
| 172 |
"errant query string": { |
| 173 |
input: `foo/var/baz?otherthing`, |
| 174 |
wantErr: `module registry addresses may not include a query string portion`, |
| 175 |
}, |
| 176 |
"github.com": { |
| 177 |
// We don't allow using github.com like a module registry because |
| 178 |
// that conflicts with the historically-supported shorthand for |
| 179 |
// installing directly from GitHub-hosted git repositories. |
| 180 |
input: `github.com/HashiCorp/Consul/aws`, |
| 181 |
wantErr: `can't use "github.com" as a module registry host, because it's reserved for installing directly from version control repositories`, |
| 182 |
}, |
| 183 |
"bitbucket.org": { |
| 184 |
// We don't allow using bitbucket.org like a module registry because |
| 185 |
// that conflicts with the historically-supported shorthand for |
| 186 |
// installing directly from BitBucket-hosted git repositories. |
| 187 |
input: `bitbucket.org/HashiCorp/Consul/aws`, |
| 188 |
wantErr: `can't use "bitbucket.org" as a module registry host, because it's reserved for installing directly from version control repositories`, |
| 189 |
}, |
| 190 |
"local path from current dir": { |
| 191 |
// Can't use a local path when we're specifically trying to parse |
| 192 |
// a _registry_ source address. |
| 193 |
input: `./boop`, |
| 194 |
wantErr: `a module registry source address must have either three or four slash-separated components`, |
| 195 |
}, |
| 196 |
"local path from parent dir": { |
| 197 |
// Can't use a local path when we're specifically trying to parse |
| 198 |
// a _registry_ source address. |
| 199 |
input: `../boop`, |
| 200 |
wantErr: `a module registry source address must have either three or four slash-separated components`, |
| 201 |
}, |
| 202 |
"main registry implied, escaping subdir": { |
| 203 |
input: "hashicorp/subnets/cidr//../nope", |
| 204 |
wantErr: `subdirectory path "../nope" leads outside of the module package`, |
| 205 |
}, |
| 206 |
"relative path without the needed prefix": { |
| 207 |
input: "boop/bloop", |
| 208 |
wantErr: "a module registry source address must have either three or four slash-separated components", |
| 209 |
}, |
| 210 |
} |
| 211 |
|
| 212 |
for name, test := range tests { |
| 213 |
t.Run(name, func(t *testing.T) { |
| 214 |
addr, err := ParseModuleSource(test.input) |
| 215 |
|
| 216 |
if test.wantErr != "" { |
| 217 |
switch { |
| 218 |
case err == nil: |
| 219 |
t.Errorf("unexpected success\nwant error: %s", test.wantErr) |
| 220 |
case err.Error() != test.wantErr: |
| 221 |
t.Errorf("wrong error messages\ngot: %s\nwant: %s", err.Error(), test.wantErr) |
| 222 |
} |
| 223 |
return |
| 224 |
} |
| 225 |
|
| 226 |
if err != nil { |
| 227 |
t.Fatalf("unexpected error: %s", err.Error()) |
| 228 |
} |
| 229 |
|
| 230 |
if got, want := addr.String(), test.wantString; got != want { |
| 231 |
t.Errorf("wrong String() result\ngot: %s\nwant: %s", got, want) |
| 232 |
} |
| 233 |
if got, want := addr.ForDisplay(), test.wantForDisplay; got != want { |
| 234 |
t.Errorf("wrong ForDisplay() result\ngot: %s\nwant: %s", got, want) |
| 235 |
} |
| 236 |
if got, want := addr.Package.ForRegistryProtocol(), test.wantForProtocol; got != want { |
| 237 |
t.Errorf("wrong ForRegistryProtocol() result\ngot: %s\nwant: %s", got, want) |
| 238 |
} |
| 239 |
}) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
func ExampleParseModuleSource() { |
| 244 |
mAddr, err := ParseModuleSource("hashicorp/consul/aws//modules/consul-cluster") |
| 245 |
if err != nil { |
| 246 |
log.Fatal(err) |
| 247 |
} |
| 248 |
fmt.Printf("%#v", mAddr) |
| 249 |
// Output: tfaddr.Module{Package:tfaddr.ModulePackage{Host:svchost.Hostname("registry.terraform.io"), Namespace:"hashicorp", Name:"consul", TargetSystem:"aws"}, Subdir:"modules/consul-cluster"} |
| 250 |
} |