package activator import ( "strings" "testing" "github.com/carlosframework/platform/internal/provision" "github.com/carlosframework/platform/internal/registry" ) func managed(host, db, sock string) registry.Route { return registry.Route{Host: host, Kind: "instance", Hibernate: true, DBPath: db, Socket: sock} } func TestClassifyFlatAndNested(t *testing.T) { in := LayoutInputs{DataDir: "/data"} if l, _ := Classify(managed("h.example.com", "/data/h.example.com.db", "/run/carlos/h.example.com.sock"), in); l != LayoutFlat { t.Errorf("flat row = %s", l) } if l, _ := Classify(managed("h.example.com", provision.NestedDBPath("/data", "h.example.com"), provision.NestedSocket("h.example.com")), in); l != LayoutNested { t.Errorf("nested row = %s", l) } if l, r := Classify(managed("h.example.com", "/data/sub/store.db", "/run/carlos/h.example.com.sock"), in); l != LayoutForeign || !strings.Contains(r, "store.db") { t.Errorf("custom row = %s %q", l, r) } // A nested DB with a flat socket is not nested: both columns must agree. if l, _ := Classify(managed("h.example.com", provision.NestedDBPath("/data", "h.example.com"), "/run/carlos/h.example.com.sock"), in); l != LayoutForeign { t.Errorf("half-migrated row = %s", l) } } func TestClassifyFencedWinsOverEverything(t *testing.T) { in := LayoutInputs{DataDir: "/data", Journal: func(h string) bool { return h == "h.example.com" }} if l, r := Classify(managed("h.example.com", "/data/h.example.com.db", "/run/carlos/h.example.com.sock"), in); l != LayoutFenced || r == "" { t.Errorf("fenced = %s %q", l, r) } if l, _ := Classify(managed("o.example.com", "/data/o.example.com.db", "/run/carlos/o.example.com.sock"), in); l != LayoutFlat { t.Errorf("other host = %s", l) } } // The identifier collision is exercised through the SameID seam rather than // a real hash collision: two nested rows for different hosts, SameID forced // to report them equal, must classify as a conflict that names the other // host. func TestClassifyConflictRefusesBothHosts(t *testing.T) { long := strings.Repeat("a", 60) + ".example.com" twin := strings.Repeat("a", 60) + ".example.org" a := managed(long, provision.NestedDBPath("/data", long), provision.NestedSocket(long)) b := managed(twin, provision.NestedDBPath("/data", twin), provision.NestedSocket(twin)) in := LayoutInputs{DataDir: "/data", Peers: func() ([]registry.Route, error) { return []registry.Route{a, b}, nil }, SameID: func(x, y string) bool { return true }} if l, r := Classify(a, in); l != LayoutConflict || !strings.Contains(r, twin) { t.Errorf("conflict = %s %q", l, r) } } func TestClassifyIgnoresUnmanagedPeers(t *testing.T) { a := managed("h.example.com", "/data/h.example.com.db", "/run/carlos/h.example.com.sock") ext := registry.Route{Host: "x.example.com", Kind: "instance", Hibernate: false, Socket: "/run/carlos/x.example.com.sock"} in := LayoutInputs{DataDir: "/data", Peers: func() ([]registry.Route, error) { return []registry.Route{a, ext}, nil }, SameID: func(x, y string) bool { return true }} if l, _ := Classify(a, in); l != LayoutFlat { t.Errorf("an unmanaged peer must not conflict: %s", l) } if ActivatorManaged(ext) { t.Error("unflagged socket route reported managed") } }