Skip to content

Commit

Permalink
fixes for the nostr test
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledyoussef24 committed Feb 20, 2025
1 parent 4fd9075 commit f08ac8d
Showing 1 changed file with 60 additions and 98 deletions.
158 changes: 60 additions & 98 deletions packages/grid_client/tests/modules/applications/nostr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,102 +16,51 @@ import { generateInt, getOnlineNode, log } from "../../utils";
jest.setTimeout(1250000);

let gridClient: GridClient;
let deploymentName: string;

beforeAll(async () => {
gridClient = await getClient();
// Use "ns" prefix to indicate a Nostr deployment
deploymentName = "ns" + generateString(10);
gridClient.clientOptions.projectName = `nostr/${deploymentName}`;
gridClient._connect();
return gridClient;
});

async function deploy(client: GridClient, vms: MachinesModel, subdomain: string, gatewayNode: any) {
// Deploy VM
const resultVM = await client.machines.deploy(vms);
log("================= Deploying VM =================");
log(resultVM);
log("================= Deploying VM =================");

// Get WG interface details
const wgnet = (await client.machines.getObj(vms.name))[0].interfaces[0];

// Deploy Gateway
const gateway: GatewayNameModel = {
name: subdomain,
network: wgnet.network,
node_id: gatewayNode.nodeId,
tls_passthrough: false,
backends: [`http://${wgnet.ip}:8080`],
};

const resultGateway = await client.gateway.deploy_name(gateway);
log("================= Deploying Gateway =================");
log(resultGateway);
log("================= Deploying Gateway =================");
}

async function getDeployment(client: GridClient, name: string, subdomain: string) {
// Get VM deployment
const resultVM = await client.machines.getObj(name);
log("================= Getting VM Deployment =================");
log(resultVM);
log("================= Getting VM Deployment =================");

// Get Gateway deployment
const resultGateway = await client.gateway.getObj(subdomain);
log("================= Getting Gateway Deployment =================");
log(resultGateway);
log(`https://${resultGateway[0].domain}`);
log("================= Getting Gateway Deployment =================");
}

async function cancel(client: GridClient, name: string, subdomain: string) {
// Cancel VM deployment
const resultVM = await client.machines.delete({ name });
log("================= Canceling VM Deployment =================");
log(resultVM);
log("================= Canceling VM Deployment =================");

// Cancel Gateway deployment
const resultGateway = await client.gateway.delete_name({ name: subdomain });
log("================= Canceling Gateway Deployment =================");
log(resultGateway);
log("================= Canceling Gateway Deployment =================");
}
test("TCXXXX - Applications: Deploy Nostr with WireGuard", async () => {
test("TC2954 - Applications: Deploy Nostr", async () => {
/**********************************************
Test Suite: Grid3_Client_TS (Automated)
Test Cases: TCXXXX - Applications: Deploy Nostr with WireGuard
Test Cases: TC2954 - Applications: Deploy Nostr
Scenario:
- Generate Test Data/Nostr Config/Gateway Config.
- Select a Node To Deploy the Nostr on.
- Select a Node To Deploy the Nostr instance on.
- Select a Gateway Node To Deploy the gateway on.
- Deploy the Nostr solution.
- Assert that the generated data matches
the deployment details.
- Pass the WireGuard IP of the Created Nostr instance
to the Gateway Config.
- Assert that the generated data matches the deployment details.
- Pass the IP of the created Nostr instance to the Gateway Config.
- Deploy the Gateway.
- Assert that the generated data matches
the deployment details.
- Assert that the Gateway points at the WireGuard IP
of the created Nostr.
- Assert that the returned domain is working
and returns correct data.
**********************************************/

// Test Data
- Assert that the generated data matches the deployment details.
- Assert that the Gateway points at the IP of the created Nostr instance.
- Assert that the returned domain is working and returns correct data.
**********************************************/
const name = "gw" + generateString(10).toLowerCase();
const subdomain = name; // Using the same variable for gateway subdomain
const tlsPassthrough = false;
const cpu = 2;
const memory = 4;
const rootfsSize = 0;
const diskSize = 50;
const cpu = 1;
const memory = 2;
const rootfsSize = 2;
const diskSize = 15;
const networkName = generateString(15);
const vmName = generateString(15);
const diskName = generateString(15);
const mountPoint = "/mnt/data";
const mountPoint = "/var/lib/docker";
const publicIp = false;
const ipRange = "10.252.0.0/16";
const ipRangeClassA = "10." + generateInt(1, 255) + ".0.0/16";
const ipRangeClassB = "172." + generateInt(16, 31) + ".0.0/16";
const ipRangeClassC = "192.168.0.0/16";
const ipRange = randomChoice([ipRangeClassA, ipRangeClassB, ipRangeClassC]);
const metadata = "{'deploymentType': 'nostr'}";
const description = "Test deploying Nostr via ts grid3 client with WireGuard";
const description = "test deploying Nostr via ts grid3 client";

// Gateway Node Selection
const gatewayNodes = await gridClient.capacity.filterNodes({
Expand All @@ -134,13 +83,16 @@ test("TCXXXX - Applications: Deploy Nostr with WireGuard", async () => {
} as FilterOptions);
const nodeId = await getOnlineNode(nodes);
if (nodeId === -1) throw new Error("No nodes available to complete this test");
const domain = subdomain + "." + GatewayNode.publicConfig.domain;

// VM Model
// VM Model updated for Nostr
const vms: MachinesModel = {
name: name,
name: deploymentName,
network: {
name: networkName,
ip_range: ipRange,
addAccess: true,
accessNodeId: GatewayNode!.nodeId,
},
machines: [
{
Expand All @@ -159,10 +111,11 @@ test("TCXXXX - Applications: Deploy Nostr with WireGuard", async () => {
flist: "https://hub.grid.tf/tf-official-apps/nostr_relay-mycelium.flist",
entrypoint: "/sbin/zinit init",
public_ip: publicIp,
planetary: false, // Planetary disabled
planetary: true,
mycelium: true,
env: {
SSH_KEY: config.ssh_key,
NOSTR_HOSTNAME: domain,
},
},
],
Expand All @@ -181,38 +134,31 @@ test("TCXXXX - Applications: Deploy Nostr with WireGuard", async () => {
const result = await gridClient.machines.getObj(vms.name);
log(result);

// Retrieve the WireGuard network details of the VM
const wgnet = result[0].interfaces[0];

// Gateway Backend Configuration
const backends = [`http://${wgnet.ip}:8080`];
log(backends);
// Get WG interface details and deploy Gateway using port 8080
const wgnet = (await gridClient.machines.getObj(vms.name))[0].interfaces[0];

// Gateway Model
const gw: GatewayNameModel = {
name: name,
const gateway: GatewayNameModel = {
name: subdomain,
network: wgnet.network,
node_id: GatewayNode.nodeId,
tls_passthrough: tlsPassthrough,
backends: backends,
tls_passthrough: false,
backends: [`http://${wgnet.ip}:8080`],
};

const gatewayRes = await gridClient.gateway.deploy_name(gw);
const gatewayRes = await gridClient.gateway.deploy_name(gateway);
log(gatewayRes);

// Gateway Assertions
expect(gatewayRes.contracts.created).toHaveLength(1);

const gatewayResult = await gridClient.gateway.getObj(gw.name);
const gatewayResult = await gridClient.gateway.getObj(gateway.name);
log(gatewayResult);

// Gateway Assertions
expect(gatewayResult[0].name).toBe(name);
expect(gatewayResult[0].backends).toStrictEqual(backends);
expect(gatewayResult[0].name).toBe(subdomain);
expect(gatewayResult[0].backends).toStrictEqual(gateway.backends);

// Gateway reachability check
const site = `http://${wgnet.ip}:8080/`;
log(`Testing Gateway URL: ${site}`);
// Use HTTP for the reachability check since the backend is HTTP
const site = "http://" + gatewayResult[0].domain;
let reachable = false;

for (let i = 0; i <= 250; i++) {
Expand All @@ -235,3 +181,19 @@ test("TCXXXX - Applications: Deploy Nostr with WireGuard", async () => {
if (i === 250) throw new Error("Gateway is unreachable after retries");
}
});

afterAll(async () => {
const vmNames = await gridClient.machines.list();
for (const name of vmNames) {
const res = await gridClient.machines.delete({ name });
log(res);
}

const gwNames = await gridClient.gateway.list();
for (const name of gwNames) {
const res = await gridClient.gateway.delete_name({ name });
log(res);
}

return await gridClient.disconnect();
}, 130000);

0 comments on commit f08ac8d

Please sign in to comment.