Give a machine port one holder: refuse ssh's, another module's and a doubled one, and release the assignment a given port replaces (hq ADR 0100)
This commit is contained in:
@@ -257,3 +257,64 @@ func TestAGivenPortIsNeverAssigned(t *testing.T) {
|
||||
t.Fatalf("a fixed port given to another module was handed over: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// novox/hq ADR 0100: a given machine port has one holder. It is refused when another module has it,
|
||||
// assigned or given, when the same layer gives it twice, and when it is ssh's; and when it replaces
|
||||
// what the mesh assigned for that port, the assignment is given back.
|
||||
func TestAGivenPortHasOneHolderAndReplacesTheAssignment(t *testing.T) {
|
||||
inv, node := aNodeWithModules(t, "postgres", "web", "cache")
|
||||
ctx := t.Context()
|
||||
give := func(module string, ports map[string]any) error {
|
||||
return inv.SetSettings(ctx, node, module, map[string]any{catalogue.PortsSetting: ports})
|
||||
}
|
||||
|
||||
web, err := inv.PortFor(ctx, node, "web", 8080, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := give("postgres", map[string]any{"5432": web.Machine}); !errors.Is(err, ErrPortTaken) {
|
||||
t.Fatalf("a port the mesh assigned to web was given to postgres: %v", err)
|
||||
}
|
||||
if err := give("postgres", map[string]any{"5432": 22}); err == nil {
|
||||
t.Fatal("ssh's port was given")
|
||||
}
|
||||
if err := give("postgres", map[string]any{"5432": 5433, "5433": 5433}); err == nil {
|
||||
t.Fatal("one machine port was given for two ports")
|
||||
}
|
||||
if err := give("cache", map[string]any{"6379": 6380}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := give("postgres", map[string]any{"5432": 6380}); !errors.Is(err, ErrPortTaken) {
|
||||
t.Fatalf("a port given to cache was given to postgres: %v", err)
|
||||
}
|
||||
|
||||
// Postgres was assigned a port for 5432; given one, the assignment is released and the number
|
||||
// is free again.
|
||||
assigned, err := inv.PortFor(ctx, node, "postgres", 5432, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := give("postgres", map[string]any{"5432": 5433}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Given again, the same: its own given port is not a collision with itself.
|
||||
if err := give("postgres", map[string]any{"5432": 5433}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
held, err := inv.PortsFor(ctx, node)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, a := range held {
|
||||
if a.Module == "postgres" {
|
||||
t.Fatalf("the assignment a given port replaced is still held: %+v", a)
|
||||
}
|
||||
}
|
||||
other, err := inv.PortFor(ctx, node, "cache", 11211, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if other.Machine != assigned.Machine {
|
||||
t.Fatalf("the released port %d was not free again (got %d)", assigned.Machine, other.Machine)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user