-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtestStorageProvider.ts
More file actions
69 lines (60 loc) · 2.14 KB
/
testStorageProvider.ts
File metadata and controls
69 lines (60 loc) · 2.14 KB
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
import { Validator, standardizeError } from "../../index.js";
/**
* testStorageProvider uploads one data item and retrieves it
* again to verify the storage provider works
*
* @method testStorageProvider
* @param {Validator} this
* @param {string} data
* @return {Promise<void>}
*/
export async function testStorageProvider(
this: Validator,
data: string
): Promise<void> {
try {
const storageProvider = this.storageProviderFactory();
if (storageProvider.name === "NoStorageProvider") {
throw new Error(
`Storage provider id ${this.pool.data?.current_storage_provider_id} not found`
);
}
const bundle = Buffer.from(data);
this.logger.info(
`Uploading to storage provider ${storageProvider.name} ...`
);
const { storageId } = await storageProvider.saveBundle(bundle, []);
this.logger.info(
`Successfully uploaded to storage provider ${storageProvider.name} with storage id: ${storageId}`
);
this.logger.info(
`Retrieving from storage provider ${storageProvider.name} with storage id ${storageId} ...`
);
const { storageData } = await storageProvider.retrieveBundle(
storageId,
60 * 1000
);
this.logger.info(
`Successfully retrieved bundle from storage provider ${storageProvider.name} with storage id: ${storageId}`
);
this.logger.info(`Comparing byte size with original data`);
if (bundle.byteLength !== storageData.byteLength) {
throw new Error(
`Byte size does not match, uploaded ${bundle.byteLength} bytes, retrieved ${storageData.byteLength}`
);
}
this.logger.info(`Comparing data content with original data`);
if (bundle.toString() !== storageData.toString()) {
throw new Error(
`Data content does not match, uploaded \"${bundle.toString()}\", retrieved \"${storageData.toString()}\"`
);
}
this.logger.info(
`Successfully completed tests for storage provider ${storageProvider.name}, everything valid ✅`
);
} catch (err) {
this.logger.fatal(`Failed test storage provider. Exiting ...`);
this.logger.fatal(standardizeError(err));
process.exit(1);
}
}