| 1 | package registry |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // TestPathRoundTrip: a written prefix row comes back, and a repoint of the |
| 6 | // same (host, prefix) replaces rather than duplicates. |
| 7 | func TestPathRoundTrip(t *testing.T) { |
| 8 | reg := openTestRegistry(t) |
| 9 | if err := reg.UpsertPath(RoutePath{ |
| 10 | Host: "ti.to", Prefix: "paulca", Target: "go.tito.io", |
| 11 | Account: "tito", App: "classic", |
| 12 | }); err != nil { |
| 13 | t.Fatal(err) |
| 14 | } |
| 15 | got, err := reg.ListPaths() |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | if len(got) != 1 { |
| 20 | t.Fatalf("ListPaths returned %d rows, want 1", len(got)) |
| 21 | } |
| 22 | if got[0].Target != "go.tito.io" { |
| 23 | t.Fatalf("target = %q, want go.tito.io", got[0].Target) |
| 24 | } |
| 25 | |
| 26 | if err := reg.UpsertPath(RoutePath{ |
| 27 | Host: "ti.to", Prefix: "paulca", Target: "beta.tito.io", |
| 28 | Account: "tito", App: "classic", |
| 29 | }); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | got, err = reg.ListPaths() |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if len(got) != 1 { |
| 37 | t.Fatalf("repoint produced %d rows, want 1 — (host, prefix) is the key", len(got)) |
| 38 | } |
| 39 | if got[0].Target != "beta.tito.io" { |
| 40 | t.Fatalf("target = %q, want beta.tito.io after repoint", got[0].Target) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // TestDeletePathIsIdempotent: deleting a row that is already gone is a |
| 45 | // success, the same contract DeleteAlias carries. The reconcile pass deletes |
| 46 | // unconditionally, so this must not error. |
| 47 | func TestDeletePathIsIdempotent(t *testing.T) { |
| 48 | reg := openTestRegistry(t) |
| 49 | if err := reg.DeletePath("ti.to", "nobody"); err != nil { |
| 50 | t.Fatalf("deleting an absent path row: %v", err) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // TestPathsAreScopedToTheirHost: two hosts may carry the same prefix without |
| 55 | // colliding — the key is the pair, not the prefix. |
| 56 | func TestPathsAreScopedToTheirHost(t *testing.T) { |
| 57 | reg := openTestRegistry(t) |
| 58 | for _, h := range []string{"ti.to", "other.example"} { |
| 59 | if err := reg.UpsertPath(RoutePath{ |
| 60 | Host: h, Prefix: "paulca", Target: "go.tito.io", |
| 61 | Account: "tito", App: "classic", |
| 62 | }); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | } |
| 66 | got, err := reg.ListPaths() |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if len(got) != 2 { |
| 71 | t.Fatalf("got %d rows, want 2 — (host, prefix) is the key, not prefix alone", len(got)) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // TestFingerprintChangesOnPathMutation is the load-bearing test of this |
| 76 | // task. Fingerprint() is the 2s content hash the edge polls; a route_paths |
| 77 | // change it cannot see is a flip that reconciles perfectly and never reaches |
| 78 | // the live map — a silent false negative, the failure class Fingerprint's own |
| 79 | // header names as the one to engineer against. |
| 80 | func TestFingerprintChangesOnPathMutation(t *testing.T) { |
| 81 | reg := openTestRegistry(t) |
| 82 | if err := reg.UpsertPath(RoutePath{ |
| 83 | Host: "ti.to", Prefix: "paulca", Target: "go.tito.io", |
| 84 | Account: "tito", App: "classic", |
| 85 | }); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | before, err := reg.Fingerprint() |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | if err := reg.UpsertPath(RoutePath{ |
| 93 | Host: "ti.to", Prefix: "paulca", Target: "beta.tito.io", |
| 94 | Account: "tito", App: "classic", |
| 95 | }); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | after, err := reg.Fingerprint() |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | if before == after { |
| 103 | t.Fatalf("fingerprint unchanged across a route_paths repoint: %q", after) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // TestFingerprintChangesOnPathRemoval: the un-flip direction. Rollback is a |
| 108 | // row disappearing, and it must reach the edge on the same 2s bound as the |
| 109 | // flip did. |
| 110 | func TestFingerprintChangesOnPathRemoval(t *testing.T) { |
| 111 | reg := openTestRegistry(t) |
| 112 | if err := reg.UpsertPath(RoutePath{ |
| 113 | Host: "ti.to", Prefix: "paulca", Target: "go.tito.io", |
| 114 | Account: "tito", App: "classic", |
| 115 | }); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | before, err := reg.Fingerprint() |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if err := reg.DeletePath("ti.to", "paulca"); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | after, err := reg.Fingerprint() |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | if before == after { |
| 130 | t.Fatalf("fingerprint unchanged across a route_paths removal: %q", after) |
| 131 | } |
| 132 | } |
| 133 | |