|
| 1 | +package modresolve |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-quicktest/qt" |
| 7 | +) |
| 8 | + |
| 9 | +func TestResolver(t *testing.T) { |
| 10 | + testCases := []struct { |
| 11 | + testName string |
| 12 | + in string |
| 13 | + catchAllDefault string |
| 14 | + err string |
| 15 | + lookups map[string]Location |
| 16 | + }{{ |
| 17 | + testName: "MultipleFallbacks", |
| 18 | + in: "registry.somewhere,registry.other", |
| 19 | + err: "duplicate catch-all registry", |
| 20 | + }, { |
| 21 | + testName: "NoRegistryOrDefault", |
| 22 | + catchAllDefault: "", |
| 23 | + err: "no catch-all registry or default", |
| 24 | + }, { |
| 25 | + testName: "InvalidRegistry", |
| 26 | + in: "$#foo", |
| 27 | + err: `invalid registry "\$#foo": invalid host name "\$#foo" in registry`, |
| 28 | + }, { |
| 29 | + testName: "InvalidSecuritySuffix", |
| 30 | + in: "foo.com+bogus", |
| 31 | + err: `invalid registry "foo.com\+bogus": unknown suffix \("\+bogus"\), need \+insecure, \+secure or no suffix\)`, |
| 32 | + }, { |
| 33 | + testName: "IPV6AddrWithoutBrackets", |
| 34 | + in: "::1", |
| 35 | + err: `invalid registry "::1": invalid host name "::1" in registry`, |
| 36 | + }, { |
| 37 | + testName: "EmptyElement", |
| 38 | + in: "foo.com,", |
| 39 | + err: `empty registry part`, |
| 40 | + }, { |
| 41 | + testName: "MissingPrefix", |
| 42 | + in: "=foo.com", |
| 43 | + err: `empty module prefix`, |
| 44 | + }, { |
| 45 | + testName: "MissingRegistry", |
| 46 | + in: "x.com=", |
| 47 | + err: `empty registry reference`, |
| 48 | + }, { |
| 49 | + testName: "InvalidModulePrefix", |
| 50 | + in: "foo#=foo.com", |
| 51 | + err: `invalid module path "foo#": invalid char '#'`, |
| 52 | + }, { |
| 53 | + testName: "DuplicateModulePrefix", |
| 54 | + in: "x.com=r.org,x.com=q.org", |
| 55 | + err: `duplicate module prefix "x.com"`, |
| 56 | + }, { |
| 57 | + testName: "NoDefaultCatchAll", |
| 58 | + in: "x.com=r.org", |
| 59 | + err: `no default catch-all registry provided`, |
| 60 | + }, { |
| 61 | + testName: "InvalidCatchAll", |
| 62 | + in: "x.com=r.org", |
| 63 | + catchAllDefault: "bogus", |
| 64 | + err: `invalid catch-all registry "bogus": invalid host name "bogus" in registry`, |
| 65 | + }, { |
| 66 | + testName: "InvalidRegistryRef", |
| 67 | + in: "foo.com//bar", |
| 68 | + err: `invalid registry "foo.com//bar": invalid reference syntax \("foo.com//bar"\)`, |
| 69 | + }, { |
| 70 | + testName: "RegistryRefWithDigest", |
| 71 | + in: "foo.com/bar@sha256:f3c16f525a1b7c204fc953d6d7db7168d84ebf4902f83c3a37d113b18c28981f", |
| 72 | + err: `invalid registry "foo.com/bar@sha256:f3c16f525a1b7c204fc953d6d7db7168d84ebf4902f83c3a37d113b18c28981f": cannot have an associated tag or digest`, |
| 73 | + }, { |
| 74 | + testName: "RegistryRefWithTag", |
| 75 | + in: "foo.com/bar:sometag", |
| 76 | + err: `invalid registry "foo.com/bar:sometag": cannot have an associated tag or digest`, |
| 77 | + }, { |
| 78 | + testName: "SingleCatchAll", |
| 79 | + catchAllDefault: "registry.somewhere", |
| 80 | + lookups: map[string]Location{ |
| 81 | + "fruit.com/apple": { |
| 82 | + Host: "registry.somewhere", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, { |
| 86 | + testName: "CatchAllWithNoDefault", |
| 87 | + in: "registry.somewhere", |
| 88 | + lookups: map[string]Location{ |
| 89 | + "fruit.com/apple": { |
| 90 | + Host: "registry.somewhere", |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, { |
| 94 | + testName: "CatchAllWithDefault", |
| 95 | + in: "registry.somewhere", |
| 96 | + catchAllDefault: "other.cue.somewhere", |
| 97 | + lookups: map[string]Location{ |
| 98 | + "fruit.com/apple": { |
| 99 | + Host: "registry.somewhere", |
| 100 | + }, |
| 101 | + "": { |
| 102 | + Host: "registry.somewhere", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, { |
| 106 | + testName: "PrefixWithCatchAllNoDefault", |
| 107 | + in: "example.com=registry.example.com/offset,registry.somewhere", |
| 108 | + lookups: map[string]Location{ |
| 109 | + "fruit.com/apple": { |
| 110 | + Host: "registry.somewhere", |
| 111 | + }, |
| 112 | + "example.com/blah": { |
| 113 | + Host: "registry.example.com", |
| 114 | + Prefix: "offset", |
| 115 | + }, |
| 116 | + "example.com": { |
| 117 | + Host: "registry.example.com", |
| 118 | + Prefix: "offset", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, { |
| 122 | + testName: "PrefixWithCatchAllDefault", |
| 123 | + in: "example.com=registry.example.com/offset", |
| 124 | + catchAllDefault: "registry.somewhere", |
| 125 | + lookups: map[string]Location{ |
| 126 | + "fruit.com/apple": { |
| 127 | + Host: "registry.somewhere", |
| 128 | + }, |
| 129 | + "example.com/blah": { |
| 130 | + Host: "registry.example.com", |
| 131 | + Prefix: "offset", |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, { |
| 135 | + testName: "LocalhostIsInsecure", |
| 136 | + in: "localhost:5000", |
| 137 | + lookups: map[string]Location{ |
| 138 | + "fruit.com/apple": { |
| 139 | + Host: "localhost:5000", |
| 140 | + Insecure: true, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, { |
| 144 | + testName: "SecureLocalhost", |
| 145 | + in: "localhost:1234+secure", |
| 146 | + lookups: map[string]Location{ |
| 147 | + "fruit.com/apple": { |
| 148 | + Host: "localhost:1234", |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, { |
| 152 | + testName: "127.0.0.1IsInsecure", |
| 153 | + in: "127.0.0.1", |
| 154 | + lookups: map[string]Location{ |
| 155 | + "fruit.com/apple": { |
| 156 | + Host: "127.0.0.1", |
| 157 | + Insecure: true, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, { |
| 161 | + testName: "[::1]IsInsecure", |
| 162 | + in: "[::1]", |
| 163 | + lookups: map[string]Location{ |
| 164 | + "fruit.com/apple": { |
| 165 | + Host: "[::1]", |
| 166 | + Insecure: true, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }} |
| 170 | + |
| 171 | + for _, tc := range testCases { |
| 172 | + t.Run(tc.testName, func(t *testing.T) { |
| 173 | + r, err := ParseCUERegistry(tc.in, tc.catchAllDefault) |
| 174 | + if tc.err != "" { |
| 175 | + qt.Assert(t, qt.ErrorMatches(err, tc.err)) |
| 176 | + return |
| 177 | + } |
| 178 | + qt.Assert(t, qt.IsNil(err)) |
| 179 | + for prefix, want := range tc.lookups { |
| 180 | + got := r.Resolve(prefix) |
| 181 | + qt.Assert(t, qt.Equals(got, want), qt.Commentf("prefix %q", prefix)) |
| 182 | + } |
| 183 | + }) |
| 184 | + } |
| 185 | +} |
0 commit comments