Skip to content

Commit 915e685

Browse files
authored
Adding workload unit tests (#3403)
* Adding workload unit tests * Adding workload type instances * Adding workload type instances * Adding public ipv4, network and qsfs instances * Fixing network's tests and most of qsfs tests * Refactoring workload test file and fixing serialize and deserialize failing qsfs test * Remove console logs * initialize zmachine light and znetwork light instances
1 parent 8c9a1fd commit 915e685

File tree

2 files changed

+280
-1
lines changed

2 files changed

+280
-1
lines changed

packages/grid_client/src/zos/znet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ class Znet extends WorkloadData {
5555
}
5656
}
5757

58-
export { Znet, Peer };
58+
export { Znet, Peer, Mycelium };
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
import { plainToClass } from "class-transformer";
2+
3+
import {
4+
DeploymentResult,
5+
Encryption,
6+
MachineInterface,
7+
Mycelium,
8+
MyceliumIP,
9+
NetworkLight,
10+
Peer,
11+
QuantumCompression,
12+
QuantumSafeConfig,
13+
QuantumSafeFSConfig,
14+
QuantumSafeMeta,
15+
ResultStates,
16+
Workload,
17+
WorkloadTypes,
18+
ZdbBackend,
19+
ZdbGroup,
20+
ZdbModes,
21+
ZmachineLight,
22+
ZmachineLightNetwork,
23+
} from "../../src";
24+
import {
25+
ComputeCapacity,
26+
GatewayFQDNProxy,
27+
GatewayNameProxy,
28+
Mount,
29+
PublicIP,
30+
QuantumSafeFS,
31+
Volume,
32+
Zdb,
33+
Zlogs,
34+
Zmachine,
35+
ZmachineNetwork,
36+
Zmount,
37+
Znet,
38+
} from "../../src";
39+
import { PublicIPv4 } from "../../src/zos/ipv4";
40+
41+
let workload: Workload;
42+
43+
const createDataInstance = (type: WorkloadTypes) => {
44+
let instance;
45+
const network = new ZmachineNetwork();
46+
const networklight = new ZmachineLightNetwork();
47+
const interfaces = new MachineInterface();
48+
const myceliumip = new MyceliumIP();
49+
50+
const computeCapacity = new ComputeCapacity();
51+
computeCapacity.cpu = 1;
52+
computeCapacity.memory = 256 * 1024 ** 2;
53+
const disks = new Mount();
54+
disks.name = "zdisk";
55+
disks.mountpoint = "/mnt/data";
56+
const peer = new Peer();
57+
peer.subnet = "10.0.1.0/24";
58+
peer.wireguard_public_key = "9I8H7G6F5E4D3C2B1A0J";
59+
peer.allowed_ips = ["10.0.1.6"];
60+
peer.endpoint = "185.206.122.31:5566";
61+
const mycelium = new Mycelium();
62+
mycelium.hex_key = "abc123";
63+
64+
const rootfs_size = 2;
65+
const size = 100 * 1024 ** 2;
66+
const qsfsConfig = new QuantumSafeFSConfig();
67+
const encryption = new Encryption();
68+
const meta = new QuantumSafeMeta();
69+
const config = new QuantumSafeConfig();
70+
const backends = new ZdbBackend();
71+
const groups = new ZdbGroup();
72+
const compression = new QuantumCompression();
73+
const qsfsCache = 262144000;
74+
75+
switch (type) {
76+
case WorkloadTypes.zmachine:
77+
instance = new Zmachine();
78+
network.planetary = true;
79+
network.public_ip = "10.249.0.0/16";
80+
network.interfaces = [
81+
{
82+
network: "znetwork",
83+
ip: "10.20.2.2",
84+
},
85+
];
86+
network.mycelium = {
87+
network: "mycelium_net",
88+
hex_seed: "abc123",
89+
};
90+
91+
instance.flist = "https://hub.grid.tf/tf-official-vms/ubuntu-22.04.flist";
92+
instance.network = network;
93+
instance.size = rootfs_size * 1024 ** 3;
94+
instance.mounts = [disks];
95+
instance.entrypoint = "/sbin/zinit init";
96+
instance.compute_capacity = computeCapacity;
97+
instance.env = { key: "value" };
98+
instance.corex = false;
99+
instance.gpu = ["AMD", "NIVIDIA"];
100+
break;
101+
case WorkloadTypes.zmachinelight:
102+
instance = new ZmachineLight();
103+
instance.flist = "https://hub.grid.tf/tf-official-vms/ubuntu-22.04.flist";
104+
interfaces.network = "znetwork";
105+
interfaces.ip = "10.20.2.2";
106+
networklight.interfaces = [interfaces];
107+
myceliumip.network = "mycelium_net";
108+
myceliumip.hex_seed = "abc123";
109+
networklight.mycelium = myceliumip;
110+
instance.network = networklight;
111+
instance.size = rootfs_size * 1024 ** 3;
112+
instance.compute_capacity = computeCapacity;
113+
instance.mounts = [disks];
114+
instance.env = { key: "value" };
115+
instance.entrypoint = "/sbin/zinit init";
116+
instance.corex = false;
117+
instance.gpu = ["AMD", "NIVIDIA"];
118+
break;
119+
case WorkloadTypes.zmount:
120+
instance = new Zmount();
121+
instance.size = size;
122+
break;
123+
124+
case WorkloadTypes.volume:
125+
instance = new Volume();
126+
instance.size = size;
127+
break;
128+
129+
case WorkloadTypes.network:
130+
instance = new Znet();
131+
instance.subnet = "10.0.0.1/32";
132+
instance.ip_range = "10.0.0.2/32";
133+
instance.wireguard_private_key = "2BwI0a7lVYxeKsh7jklashakdfjasdf7jksdHf";
134+
instance.wireguard_listen_port = 5566;
135+
instance.peers = [peer];
136+
instance.mycelium = mycelium;
137+
break;
138+
case WorkloadTypes.networklight:
139+
instance = new NetworkLight();
140+
instance.subnet = "10.0.0.1/32";
141+
instance.mycelium = mycelium;
142+
break;
143+
case WorkloadTypes.zdb:
144+
instance = new Zdb();
145+
instance.size = size;
146+
instance.mode = ZdbModes.user;
147+
instance.password = "123456";
148+
instance.public = false;
149+
break;
150+
151+
case WorkloadTypes.ip:
152+
instance = new PublicIP();
153+
instance.v4 = true;
154+
instance.v6 = false;
155+
break;
156+
157+
case WorkloadTypes.ipv4:
158+
instance = new PublicIPv4();
159+
break;
160+
161+
case WorkloadTypes.qsfs:
162+
instance = new QuantumSafeFS();
163+
164+
qsfsConfig.minimal_shards = 2;
165+
qsfsConfig.expected_shards = 3;
166+
qsfsConfig.redundant_groups = 0;
167+
qsfsConfig.redundant_nodes = 0;
168+
qsfsConfig.max_zdb_data_dir_size = 2;
169+
encryption.algorithm = "algorithm";
170+
encryption.key = "EncryptionKey12345678";
171+
qsfsConfig.encryption = encryption;
172+
meta.type = "qsfs";
173+
config.prefix = "qsfs";
174+
config.encryption = encryption;
175+
backends.address = "localhost";
176+
backends.namespace = "http://localhost";
177+
backends.password = "password";
178+
groups.backends = [backends];
179+
qsfsConfig.groups = [groups];
180+
config.backends = [backends];
181+
meta.config = config;
182+
qsfsConfig.meta = meta;
183+
compression.algorithm = "algorithm";
184+
qsfsConfig.compression = compression;
185+
186+
instance.cache = qsfsCache;
187+
instance.config = qsfsConfig;
188+
break;
189+
190+
case WorkloadTypes.zlogs:
191+
instance = new Zlogs();
192+
instance.zmachine = "zmachine";
193+
instance.output = "zlog";
194+
break;
195+
196+
case WorkloadTypes.gatewayfqdnproxy:
197+
instance = new GatewayFQDNProxy();
198+
instance.fqdn = "dmftv9qfff.gent02.dev.grid.tf";
199+
instance.tls_passthrough = false;
200+
instance.backends = ["http://185.206.122.43:80"];
201+
break;
202+
203+
case WorkloadTypes.gatewaynameproxy:
204+
instance = new GatewayNameProxy();
205+
instance.name = "GatewayNameProxy";
206+
instance.tls_passthrough = false;
207+
instance.backends = ["http://185.206.122.43:80"];
208+
break;
209+
210+
default:
211+
throw new Error(`Invalid WorkloadType: ${type}`);
212+
}
213+
214+
return instance;
215+
};
216+
217+
describe.each(Object.values(WorkloadTypes))("Workload Tests for %s", type => {
218+
beforeEach(() => {
219+
const dataInstance = createDataInstance(type);
220+
workload = new Workload();
221+
workload.version = 1;
222+
workload.name = "Test Workload";
223+
workload.type = type;
224+
workload.metadata = "Metadata";
225+
workload.description = "A test for workload";
226+
workload.data = dataInstance;
227+
workload.result = new DeploymentResult();
228+
workload.result.created = Date.now();
229+
workload.result.state = ResultStates.ok;
230+
workload.result.message = "Deployment successful";
231+
workload.result.data = workload.data;
232+
});
233+
234+
test("should create a valid Workload instance", () => {
235+
expect(workload).toBeInstanceOf(Workload);
236+
});
237+
238+
test("should handle valid Workload properties", () => {
239+
expect(workload.version).toBe(1);
240+
expect(workload.name).toBe("Test Workload");
241+
expect(workload.type).toBe(type);
242+
expect(workload.metadata).toBe("Metadata");
243+
expect(workload.description).toBe("A test for workload");
244+
expect(workload.result.created).toBeGreaterThan(0);
245+
expect(workload.result.state).toBe(ResultStates.ok);
246+
expect(workload.result.message).toBe("Deployment successful");
247+
});
248+
249+
test("should correctly serialize and deserialize Workload", () => {
250+
const serialized = JSON.stringify(workload);
251+
const deserialized = plainToClass(Workload, JSON.parse(serialized));
252+
expect(deserialized).toBeInstanceOf(Workload);
253+
expect(deserialized).toEqual(workload);
254+
});
255+
256+
test("should correctly compute the challenge string", () => {
257+
const expectedChallenge =
258+
workload.version.toString() +
259+
workload.name +
260+
workload.type.toString() +
261+
workload.metadata +
262+
workload.description +
263+
workload.data.challenge();
264+
265+
expect(workload.challenge()).toBe(expectedChallenge);
266+
});
267+
268+
test("should handle invalid DeploymentResult", () => {
269+
const invalidResult = new DeploymentResult();
270+
invalidResult.created = Date.now();
271+
invalidResult.state = "invalid-state" as any;
272+
invalidResult.message = "Deployment successful";
273+
invalidResult.data = workload.data;
274+
275+
workload.result = invalidResult;
276+
277+
expect(() => workload.challenge()).not.toThrow();
278+
});
279+
});

0 commit comments

Comments
 (0)