Skip to content

Commit

Permalink
add ping node utils and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Harby committed Oct 22, 2023
1 parent 2f12e7a commit 3f365a4
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 62 deletions.
14 changes: 7 additions & 7 deletions packages/grid_client/tests/modules/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
NameContractGetModel,
NodeContractCreateModel,
NodeContractUpdateModel,
randomChoice,
} from "../../src";
import { getClient } from "../client_loader";
import { generateHash, log } from "../utils";
import { checkNodeAvail, generateHash, log } from "../utils";

jest.setTimeout(300000);

Expand All @@ -36,7 +35,8 @@ test("TC1269 - Contracts: Create Node Contract", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
farmId: 1,
} as FilterOptions);
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;
const hash = generateHash(generateString(8));
const data = generateString(64);
const publicIp = 0;
Expand Down Expand Up @@ -106,11 +106,11 @@ test("TC1271 - Contracts: Get Node Contract By Node ID & Contract Hash", async (
availableFor: await gridClient.twins.get_my_twin_id(),
farmId: 1,
} as FilterOptions);
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;
const hash = generateHash(generateString(8));
const data = generateString(64);
const publicIp = 0;
const twinId = await gridClient.twins.get_my_twin_id();

// Create Contract
const contract: NodeContractCreateModel = {
Expand Down Expand Up @@ -149,7 +149,6 @@ test("TC1272 - Contracts: Get Name Contract", async () => {

//TestData
const name = generateString(15).toLowerCase();
const twinId = await gridClient.twins.get_my_twin_id();

//Create Contract
const contract: NameContractCreateModel = {
Expand Down Expand Up @@ -189,7 +188,8 @@ test("TC1273 - Contracts: Update Node Contract", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
farmId: 1,
} as FilterOptions);
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;
const hash = generateHash(generateString(8));
const data = generateString(64);
const newHash = generateHash(generateString(8));
Expand Down
14 changes: 7 additions & 7 deletions packages/grid_client/tests/modules/gateways.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { setTimeout } from "timers/promises";

import { FilterOptions, GatewayNameModel, generateString, GridClient, MachinesModel, randomChoice } from "../../src";
import { config, getClient } from "../client_loader";
import { generateInt, log, splitIP } from "../utils";

const exec = require("child_process").exec;
import { checkNodeAvail, generateInt, log, splitIP } from "../utils";

jest.setTimeout(300000);

Expand Down Expand Up @@ -65,7 +63,8 @@ test("TC1237 - Gateways: Expose a VM Over Gateway", async () => {
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
const gatewayNodeId = +randomChoice(gatewayNodes).nodeId;
const gatewayNodeId = await checkNodeAvail(gatewayNodes);
if (gatewayNodeId == -1) return;

//Node Selection
let nodes;
Expand Down Expand Up @@ -96,7 +95,8 @@ test("TC1237 - Gateways: Expose a VM Over Gateway", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;

//VM Model
const vms: MachinesModel = {
Expand Down Expand Up @@ -198,11 +198,11 @@ test("TC1237 - Gateways: Expose a VM Over Gateway", async () => {
for (let i = 0; i < 30; i++) {
axios
.get(domain)
.then(res => {
.then(() => {
log("gateway is reachable");
reachable = true;
})
.catch(err => {
.catch(() => {
log("gateway is not reachable");
});
if (reachable) {
Expand Down
44 changes: 31 additions & 13 deletions packages/grid_client/tests/modules/kubernetes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
randomChoice,
} from "../../src";
import { config, getClient } from "../client_loader";
import { bytesToGB, generateInt, k8sWait, log, RemoteRun, splitIP } from "../utils";
import { bytesToGB, checkNodeAvail, generateInt, k8sWait, log, RemoteRun, splitIP } from "../utils";

jest.setTimeout(500000);

Expand Down Expand Up @@ -123,10 +123,16 @@ test("TC1231 - Kubernetes: Deploy a Kubernetes Cluster", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const masterNodeId = +randomChoice(masterNode).nodeId;
let workerNodeId = +randomChoice(workerNode).nodeId;
while (masterNodeId == workerNodeId) {
workerNodeId = +randomChoice(workerNode).nodeId;
const masterNodeId = await checkNodeAvail(masterNode);
if (masterNodeId == -1) return;
let workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;

let maxCount = 3;
while (masterNodeId == workerNodeId && maxCount > 0) {
workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;
maxCount--;
}

//K8s Model
Expand Down Expand Up @@ -383,10 +389,16 @@ test("TC1232 - Kubernetes: Add Worker", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const masterNodeId = +randomChoice(masterNode).nodeId;
let workerNodeId = +randomChoice(workerNode).nodeId;
while (masterNodeId == workerNodeId) {
workerNodeId = +randomChoice(workerNode).nodeId;
const masterNodeId = await checkNodeAvail(masterNode);
if (masterNodeId == -1) return;
let workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;

let maxCount = 3;
while (masterNodeId == workerNodeId && maxCount > 0) {
workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;
maxCount--;
}

//K8s Model
Expand Down Expand Up @@ -630,10 +642,16 @@ test("TC1233 - Kubernetes: Delete Worker", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const masterNodeId = +randomChoice(masterNode).nodeId;
let workerNodeId = +randomChoice(workerNode).nodeId;
while (masterNodeId == workerNodeId) {
workerNodeId = +randomChoice(workerNode).nodeId;
const masterNodeId = await checkNodeAvail(masterNode);
if (masterNodeId == -1) return;
let workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;

let maxCount = 3;
while (masterNodeId == workerNodeId && maxCount > 0) {
workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;
maxCount--;
}

//K8s Model
Expand Down
72 changes: 48 additions & 24 deletions packages/grid_client/tests/modules/qsfs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { setTimeout } from "timers/promises";

import {
FilterOptions,
generateString,
Expand All @@ -10,7 +8,7 @@ import {
randomChoice,
} from "../../src";
import { config, getClient } from "../client_loader";
import { bytesToGB, generateInt, k8sWait, log, RemoteRun, splitIP } from "../utils";
import { bytesToGB, checkNodeAvail, generateInt, k8sWait, log, RemoteRun, splitIP } from "../utils";

jest.setTimeout(300000);

Expand Down Expand Up @@ -81,10 +79,15 @@ test("TC1234 - QSFS: Deploy QSFS underneath a VM", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
if (allNodes.length >= 2) {
const qsfsNode1 = +randomChoice(allNodes).nodeId;
let qsfsNode2 = +randomChoice(allNodes).nodeId;
while (qsfsNode1 == qsfsNode2) {
qsfsNode2 = +randomChoice(allNodes).nodeId;
const qsfsNode1 = await checkNodeAvail(allNodes);
if (qsfsNode1 == -1) return;
let qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
let maxCount = 3;
while (qsfsNode1 == qsfsNode2 && maxCount > 0) {
qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
maxCount--;
}
qsfsNodes.push(qsfsNode1, qsfsNode2);
} else {
Expand All @@ -106,10 +109,15 @@ test("TC1234 - QSFS: Deploy QSFS underneath a VM", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
if (allNodes.length >= 2) {
const qsfsNode1 = +randomChoice(allNodes).nodeId;
let qsfsNode2 = +randomChoice(allNodes).nodeId;
while (qsfsNode1 == qsfsNode2) {
qsfsNode2 = +randomChoice(allNodes).nodeId;
const qsfsNode1 = await checkNodeAvail(allNodes);
if (qsfsNode1 == -1) return;
let qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
let maxCount = 3;
while (qsfsNode1 == qsfsNode2 && maxCount > 0) {
qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
maxCount--;
}
qsfsNodes.push(qsfsNode1, qsfsNode2);
} else {
Expand Down Expand Up @@ -146,7 +154,8 @@ test("TC1234 - QSFS: Deploy QSFS underneath a VM", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;

//QSFS Model
const qsfs: QSFSZDBSModel = {
Expand Down Expand Up @@ -355,10 +364,15 @@ test("TC1235 - QSFS: Deploy QSFS Underneath a Kubernetes Cluster", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
if (allNodes.length >= 2) {
const qsfsNode1 = +randomChoice(allNodes).nodeId;
let qsfsNode2 = +randomChoice(allNodes).nodeId;
while (qsfsNode1 == qsfsNode2) {
qsfsNode2 = +randomChoice(allNodes).nodeId;
const qsfsNode1 = await checkNodeAvail(allNodes);
if (qsfsNode1 == -1) return;
let qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
let maxCount = 3;
while (qsfsNode1 == qsfsNode2 && maxCount > 0) {
qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
maxCount--;
}
qsfsNodes.push(qsfsNode1, qsfsNode2);
} else {
Expand All @@ -380,10 +394,15 @@ test("TC1235 - QSFS: Deploy QSFS Underneath a Kubernetes Cluster", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
if (allNodes.length >= 2) {
const qsfsNode1 = +randomChoice(allNodes).nodeId;
let qsfsNode2 = +randomChoice(allNodes).nodeId;
while (qsfsNode1 == qsfsNode2) {
qsfsNode2 = +randomChoice(allNodes).nodeId;
const qsfsNode1 = await checkNodeAvail(allNodes);
if (qsfsNode1 == -1) return;
let qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
let maxCount = 3;
while (qsfsNode1 == qsfsNode2 && maxCount > 0) {
qsfsNode2 = await checkNodeAvail(allNodes);
if (qsfsNode2 == -1) return;
maxCount--;
}
qsfsNodes.push(qsfsNode1, qsfsNode2);
} else {
Expand Down Expand Up @@ -452,10 +471,15 @@ test("TC1235 - QSFS: Deploy QSFS Underneath a Kubernetes Cluster", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const masterNodeId = +randomChoice(masterNode).nodeId;
let workerNodeId = +randomChoice(workerNode).nodeId;
while (masterNodeId == workerNodeId) {
workerNodeId = +randomChoice(workerNode).nodeId;
const masterNodeId = await checkNodeAvail(masterNode);
if (masterNodeId == -1) return;
let workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;
let maxCount = 3;
while (masterNodeId == workerNodeId && maxCount > 0) {
workerNodeId = await checkNodeAvail(workerNode);
if (workerNodeId == -1) return;
maxCount--;
}

//QSFS Config
Expand Down
21 changes: 14 additions & 7 deletions packages/grid_client/tests/modules/vm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FilterOptions, generateString, GridClient, MachineModel, MachinesModel, randomChoice } from "../../src";
import { config, getClient } from "../client_loader";
import { bytesToGB, generateInt, log, RemoteRun, splitIP } from "../utils";
import { bytesToGB, checkNodeAvail, generateInt, log, RemoteRun, splitIP } from "../utils";

jest.setTimeout(300000);

Expand Down Expand Up @@ -76,7 +76,8 @@ test("TC1228 - VM: Deploy a VM", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;

//VM Model
const vms: MachinesModel = {
Expand Down Expand Up @@ -243,7 +244,8 @@ test("TC1229 - VM: Deploy a VM With a Disk", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;

//VM Model
const vms: MachinesModel = {
Expand Down Expand Up @@ -419,10 +421,15 @@ test("TC1230 - VM: Deploy Multiple VMs on Different Nodes", async () => {
} as FilterOptions);
}

const vm1NodeId = +randomChoice(vm1Nodes).nodeId;
let vm2NodeId = +randomChoice(vm2Nodes).nodeId;
while (vm1NodeId == vm2NodeId) {
vm2NodeId = +randomChoice(vm2Nodes).nodeId;
const vm1NodeId = await checkNodeAvail(vm1Nodes);
if (vm1NodeId == -1) return;
let vm2NodeId = await checkNodeAvail(vm2Nodes);
if (vm2NodeId == -1) return;
let maxCount = 3;
while (vm1NodeId == vm2NodeId && maxCount > 0) {
vm2NodeId = await checkNodeAvail(vm2Nodes);
if (vm2NodeId == -1) return;
maxCount--;
}
const vmNodes = [vm1NodeId, vm2NodeId];

Expand Down
7 changes: 4 additions & 3 deletions packages/grid_client/tests/modules/zdb.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createClient } from "redis";

import { FilterOptions, generateString, GridClient, randomChoice, ZDBModel, ZdbModes, ZDBSModel } from "../../src";
import { FilterOptions, generateString, GridClient, ZDBModel, ZdbModes, ZDBSModel } from "../../src";
import { getClient } from "../client_loader";
import { bytesToGB, generateInt, log } from "../utils";
import { bytesToGB, checkNodeAvail, generateInt, log } from "../utils";

jest.setTimeout(300000);

Expand Down Expand Up @@ -70,7 +70,8 @@ test("TC1236 - ZDB: Deploy ZDBs", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
}
const nodeId = +randomChoice(nodes).nodeId;
const nodeId = await checkNodeAvail(nodes);
if (nodeId == -1) return;

//Zdb Model
const zdb: ZDBModel = {
Expand Down
23 changes: 22 additions & 1 deletion packages/grid_client/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { setTimeout } from "timers/promises";
import { default as urlParser } from "url-parse";
import { inspect } from "util";

import { randomChoice } from "../src";
import { getClient } from "./client_loader";

const os = require("os");
Expand Down Expand Up @@ -80,4 +81,24 @@ async function k8sWait(masterSSHClient, k8sMasterName, k8sWorkerName, waitTime,
}
}

export { log, generateHash, generateInt, splitIP, bytesToGB, RemoteRun, returnRelay, k8sWait };
async function checkNodeAvail(Nodes) {
const gridClient = await getClient();
let node;
while (Nodes.length > 0) {
node = randomChoice(Nodes);
try {
log(await gridClient.zos.pingNode({ nodeId: node.nodeId }));
break;
} catch (error) {
log("node " + node.nodeId + " is not responding, trying different node.");
Nodes.splice(+[Nodes.indexOf(node)], 1);
}
}
if (Nodes.length == 0) {
log("No nodes available with the needed resources for this test");
return -1;
}
return node.nodeId;
}

export { log, generateHash, generateInt, splitIP, bytesToGB, RemoteRun, returnRelay, k8sWait, checkNodeAvail };

0 comments on commit 3f365a4

Please sign in to comment.