Add a NodeConfigurator class, to modify a config.json

Since Heartwood is removing some of the radicle-node options we were
relying on when starting nodes for our tests, we need to move those to
the `config.json` file.

To make it more DX friendly, I added a `NodeConfigurator` class which
reads the correspondig `config.json`, stores it and allows to configure
it before starting a node.

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-08-22 09:54:34 +02:00
parent 3dd590d8e6
commit cccee12477
4 changed files with 31 additions and 42 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ export default async function globalSetup(): Promise<() => void> {
gitOptions: gitOptions["alice"],
});
await palm.startHttpd(defaultHttpdPort);
await palm.startNode({ trackingPolicy: "track", trackingScope: "all" });
await palm.startNode({ policy: "track", scope: "all" });
console.log("Creating source-browsing fixture");
await createSourceBrowsingFixture(peerManager, palm);
+6 -4
View File
@@ -310,10 +310,12 @@ export async function createSourceBrowsingFixture(
gitOptions: gitOptions["bob"],
});
const bobProjectPath = Path.join(bob.checkoutPath, "source-browsing");
await alice.startNode();
await bob.startNode();
await alice.connect(palm);
await bob.connect(palm);
await alice.startNode({ connect: [palm.address] });
await bob.startNode({ connect: [palm.address] });
await alice.waitForEvent({ type: "peerConnected", nid: palm.nodeId }, 1000);
await palm.waitForEvent({ type: "peerConnected", nid: alice.nodeId }, 1000);
await bob.waitForEvent({ type: "peerConnected", nid: palm.nodeId }, 1000);
await palm.waitForEvent({ type: "peerConnected", nid: bob.nodeId }, 1000);
await alice.git(["clone", sourceBrowsingDir], { cwd: alice.checkoutPath });
await alice.git(["checkout", "feature/branch"], { cwd: aliceProjectPath });
+1 -1
View File
@@ -51,7 +51,7 @@ export default async function globalSetup(): Promise<() => void> {
gitOptions: gitOptions["alice"],
});
await palm.startHttpd(defaultHttpdPort);
await palm.startNode({ trackingPolicy: "track", trackingScope: "all" });
await palm.startNode({ policy: "track", scope: "all" });
console.log("Creating source-browsing fixture");
await createSourceBrowsingFixture(peerManager, palm);
+23 -36
View File
@@ -203,17 +203,6 @@ export class RadiclePeer {
};
await execa("rad", ["auth", "--alias", name], { env });
const config = await Fs.readFile(
Path.join(radHome, "config.json"),
"utf-8",
);
const parsedConfig = NodeConfigSchema.parse(JSON.parse(config));
parsedConfig.node.network = "test";
await Fs.writeFile(
Path.join(radHome, "config.json"),
JSON.stringify(parsedConfig),
"utf-8",
);
const { stdout: nodeId } = await execa("rad", ["self", "--nid"], { env });
return new RadiclePeer({
@@ -265,27 +254,22 @@ export class RadiclePeer {
this.#httpdBaseUrl = undefined;
}
public async startNode(params?: {
trackingScope?: "trusted" | "all";
trackingPolicy?: "track" | "block";
}) {
public async startNode(nodeParams: Partial<NodeConfig["node"]> = {}) {
const gitPort = await getPort();
const gitSocketAddr = `0.0.0.0:${gitPort}`;
const listenPort = await getPort();
this.#listenSocketAddr = `0.0.0.0:${listenPort}`;
// We need to set the `config.json` file after creating the node identity and before starting it.
// Since we aren't able to pass these configuration options through the CLI.
await updateNodeConfig(this.#radHome, nodeParams);
const args = [
"--git-daemon",
gitSocketAddr,
"--listen",
this.#listenSocketAddr,
];
if (params?.trackingScope) {
args.push("--tracking-scope", params.trackingScope);
}
if (params?.trackingPolicy) {
args.push("--tracking-policy", params.trackingPolicy);
}
this.#nodeProcess = this.spawn("radicle-node", args);
@@ -353,23 +337,11 @@ export class RadiclePeer {
}
}
public async connect(remote: RadiclePeer) {
if (!remote.#listenSocketAddr) {
public get address(): string {
if (!this.#listenSocketAddr) {
throw new Error("Remote node has no listen addr yet");
}
await this.rad(
["node", "connect", `${remote.nodeId}@${remote.#listenSocketAddr}`],
{ cwd: this.#radHome },
);
await this.waitForEvent(
{ type: "peerConnected", nid: remote.nodeId },
1000,
);
await remote.waitForEvent(
{ type: "peerConnected", nid: this.nodeId },
1000,
);
return `${this.nodeId}@${this.#listenSocketAddr}`;
}
public uiUrl(): string {
@@ -426,3 +398,18 @@ export class RadiclePeer {
return childProcess;
}
}
async function updateNodeConfig(
radHome: string,
nodeParams: Partial<NodeConfig["node"]>,
) {
const configPath = Path.join(radHome, "config.json");
const config = await Fs.readFile(configPath, "utf-8");
const nodeConfig = NodeConfigSchema.parse(JSON.parse(config));
nodeConfig.node = {
...nodeConfig.node,
network: "test",
...nodeParams,
};
await Fs.writeFile(configPath, JSON.stringify(nodeConfig), "utf-8");
}