File size: 5,262 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import fs from "node:fs";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
import { createSuiteTempRootTracker } from "../test-helpers/temp-dir.js";
import {
  acquireFleetCellOperation,
  deleteFleetCell,
  getFleetCell,
  listFleetCells,
  reserveFleetCell,
  updateFleetCellImage,
} from "./registry.js";

type ReserveFleetCellParams = Parameters<typeof reserveFleetCell>[1];

describe("fleet cell registry", () => {
  let root: string | undefined;
  let env: NodeJS.ProcessEnv;

  const tempRoot = createSuiteTempRootTracker({ prefix: "openclaw-fleet-registry-" });

  beforeEach(async () => {
    root = await tempRoot.setup();
    env = { ...process.env, OPENCLAW_STATE_DIR: root };
  });

  afterEach(async () => {
    closeOpenClawStateDatabaseForTest();
    await tempRoot.cleanup();
    root = undefined;
  });

  function params(tenantId: string, requestedPort?: number): ReserveFleetCellParams {
    if (!root) {
      throw new Error("test root not initialized");
    }
    return {
      tenantId,
      createdAtMs: 1,
      image: "ghcr.io/openclaw/openclaw:latest",
      runtime: "docker",
      containerName: `openclaw-cell-${tenantId}`,
      dataDir: path.join(root, "fleet", "cells", tenantId),
      ...(requestedPort === undefined ? {} : { requestedPort }),
    };
  }

  it("returns empty reads without creating state on a fresh install", () => {
    if (!root) {
      throw new Error("test root not initialized");
    }
    const databasePath = path.join(root, "state", "openclaw.sqlite");

    expect(listFleetCells(env)).toEqual([]);
    expect(getFleetCell(env, "missing")).toBeUndefined();
    expect(fs.existsSync(databasePath)).toBe(false);
  });

  it("persists, orders, updates, and deletes cells", () => {
    const zulu = reserveFleetCell(env, {
      ...params("zulu", 19_250),
      createdAtMs: 20,
      runtime: "podman",
    });
    const alpha = reserveFleetCell(env, {
      ...params("alpha"),
      createdAtMs: 10,
    });

    expect(alpha.hostPort).toBe(19_100);
    expect(zulu.hostPort).toBe(19_250);
    expect(listFleetCells(env).map((cell) => cell.tenantId)).toEqual(["alpha", "zulu"]);
    expect(getFleetCell(env, "zulu")).toEqual(zulu);

    updateFleetCellImage(env, "zulu", "ghcr.io/openclaw/openclaw:v2");
    expect(getFleetCell(env, "zulu")?.image).toBe("ghcr.io/openclaw/openclaw:v2");

    deleteFleetCell(env, "alpha");
    expect(getFleetCell(env, "alpha")).toBeUndefined();
  });

  it("rejects duplicate tenant ids without replacing the row", () => {
    const original = reserveFleetCell(env, params("alpha", 19_300));

    expect(() =>
      reserveFleetCell(env, {
        ...params("alpha", 19_301),
        image: "ghcr.io/openclaw/openclaw:other",
      }),
    ).toThrow("Fleet cell already exists: alpha");
    expect(getFleetCell(env, "alpha")).toEqual(original);
  });

  it("allocates the first free port and rejects explicit collisions", () => {
    expect(reserveFleetCell(env, params("alpha")).hostPort).toBe(19_100);
    expect(reserveFleetCell(env, params("beta")).hostPort).toBe(19_101);

    expect(() => reserveFleetCell(env, params("gamma", 19_100))).toThrow(/19100/);
    expect(getFleetCell(env, "gamma")).toBeUndefined();
    expect(reserveFleetCell(env, params("delta", 20_000)).hostPort).toBe(20_000);
  });

  it("serializes tenant mutations with renewable expiring leases", () => {
    const first = acquireFleetCellOperation({
      env,
      tenantId: "alpha",
      operation: "upgrade",
      owner: "first",
      nowMs: 1_000,
    });

    expect(() =>
      acquireFleetCellOperation({
        env,
        tenantId: "alpha",
        operation: "rm",
        owner: "second",
        nowMs: 1_001,
      }),
    ).toThrow(/fleet upgrade.*already running/iu);

    first.heartbeat(200_000);
    expect(() =>
      acquireFleetCellOperation({
        env,
        tenantId: "alpha",
        operation: "rm",
        owner: "second",
        nowMs: 400_000,
      }),
    ).toThrow(/already running/iu);

    first.release();
    const second = acquireFleetCellOperation({
      env,
      tenantId: "alpha",
      operation: "rm",
      owner: "second",
      nowMs: 400_001,
    });
    second.release();
  });

  it("fences an expired owner from its replacement lease", () => {
    const first = acquireFleetCellOperation({
      env,
      tenantId: "alpha",
      operation: "upgrade",
      owner: "first",
      nowMs: 1_000,
    });
    const successor = acquireFleetCellOperation({
      env,
      tenantId: "alpha",
      operation: "rm",
      owner: "successor",
      nowMs: 301_000,
    });

    expect(() => first.heartbeat(301_001)).toThrow(/lease was lost/iu);
    first.release();
    expect(() =>
      acquireFleetCellOperation({
        env,
        tenantId: "alpha",
        operation: "create",
        owner: "third",
        nowMs: 301_002,
      }),
    ).toThrow(/fleet rm.*already running/iu);

    successor.release();
  });

  it("fails image updates when the cell row disappeared", () => {
    expect(() => updateFleetCellImage(env, "missing", "image:v2")).toThrow(/disappeared/iu);
  });
});