|
1 | 1 | package templaterouter
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "io/ioutil" |
4 | 5 | "regexp"
|
| 6 | + "strings" |
5 | 7 | "testing"
|
| 8 | + |
| 9 | + fileutil "github.com/openshift/origin/pkg/util/file" |
6 | 10 | )
|
7 | 11 |
|
8 | 12 | func TestFirstMatch(t *testing.T) {
|
@@ -297,3 +301,63 @@ func TestMatchPattern(t *testing.T) {
|
297 | 301 | }
|
298 | 302 | }
|
299 | 303 | }
|
| 304 | + |
| 305 | +func TestSortMap(t *testing.T) { |
| 306 | + testData := []string{ |
| 307 | + `^api-stg\.127\.0\.0\.1\.nip\.io(:[0-9]+)?(/.*)?$ stg:api-route`, |
| 308 | + `^api-prod\.127\.0\.0\.1\.nip\.io(:[0-9]+)?(/.*)?$ prod:api-route`, |
| 309 | + `^[^\.]*\.127\.0\.0\.1\.nip\.io(:[0-9]+)?(/.*)?$ prod:wildcard-route`, |
| 310 | + `^3dev\.127\.0\.0\.1\.nip\.io(:[0-9]+)?(/.*)?$ dev:api-route`, |
| 311 | + `^api-prod\.127\.0\.0\.1\.nip\.io(:[0-9]+)?/x/y/z(/.*)?$ prod:api-path-route`, |
| 312 | + `^3app-admin\.127\.0\.0\.1\.nip\.io(:[0-9]+)?(/.*)?$ dev:admin-route`, |
| 313 | + `^[^\.]*\.foo\.127\.0\.0\.1\.nip\.io(:[0-9]+)?(/.*)?$ devel2:foo-wildcard-route`, |
| 314 | + `^zzz-production\.wildcard\.test(:[0-9]+)?/x/y/z(/.*)?$ test:api-route`, |
| 315 | + `^backend-app\.127\.0\.0\.1\.nip\.io(:[0-9]+)?(/.*)?$ prod:backend-route`, |
| 316 | + `^[^\.]*\.foo\.wildcard\.test(:[0-9]+)?(/.*)?$ devel2:foo-wildcard-test`, |
| 317 | + } |
| 318 | + |
| 319 | + expectedOrder := []string{ |
| 320 | + "test:api-route", |
| 321 | + "prod:backend-route", |
| 322 | + "stg:api-route", |
| 323 | + "prod:api-path-route", |
| 324 | + "prod:api-route", |
| 325 | + "dev:api-route", |
| 326 | + "dev:admin-route", |
| 327 | + "devel2:foo-wildcard-test", |
| 328 | + "devel2:foo-wildcard-route", |
| 329 | + "prod:wildcard-route", |
| 330 | + } |
| 331 | + |
| 332 | + fileName := "" |
| 333 | + tempFile, err := ioutil.TempFile("", "sort-map-test") |
| 334 | + if err == nil { |
| 335 | + fileName = tempFile.Name() |
| 336 | + err = tempFile.Close() |
| 337 | + } |
| 338 | + if err != nil || len(fileName) == 0 { |
| 339 | + t.Errorf("TestSortMap unexpected error creating a temp file: %s", fileName) |
| 340 | + } |
| 341 | + if err := ioutil.WriteFile(fileName, []byte(strings.Join(testData, "\n")), 0664); err != nil { |
| 342 | + t.Errorf("TestSortMap unexpected error writing temp file %s: %v", fileName, err) |
| 343 | + } |
| 344 | + |
| 345 | + if !sortMap(fileName) { |
| 346 | + t.Errorf("TestSortMap temp file %s sort failed", fileName) |
| 347 | + } |
| 348 | + |
| 349 | + lines, err := fileutil.ReadLines(fileName) |
| 350 | + if err != nil { |
| 351 | + t.Errorf("TestSortMap error reading sorted temp file %s: %v", fileName, err) |
| 352 | + } |
| 353 | + if len(lines) != len(expectedOrder) { |
| 354 | + t.Errorf("TestSortMap sorted data length %d did not match expected length %d", |
| 355 | + len(lines), len(expectedOrder)) |
| 356 | + } |
| 357 | + for idx, suffix := range expectedOrder { |
| 358 | + if !strings.HasSuffix(lines[idx], suffix) { |
| 359 | + t.Errorf("TestSortMap sorted data %s at index %d did not match expectation %s", |
| 360 | + lines[idx], idx, suffix) |
| 361 | + } |
| 362 | + } |
| 363 | +} |
0 commit comments