@itentialopensource/adapter-mockdevice 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env node
2
+ /* @copyright Itential, LLC 2019 */
3
+
4
+ const fs = require('fs-extra');
5
+ const path = require('path');
6
+ const { spawnSync } = require('child_process');
7
+ const { createBundle } = require('./artifactize');
8
+
9
+ const nodeEntryPath = path.resolve('.');
10
+ createBundle(nodeEntryPath).then((pathObj) => {
11
+ const { bundlePath, bundledAdapterPath } = pathObj;
12
+ const npmIgnorePath = path.join(bundledAdapterPath, '.npmignore');
13
+ const adapterPackagePath = path.join(bundledAdapterPath, 'package.json');
14
+ const artifactPackagePath = path.join(bundlePath, 'package.json');
15
+
16
+ // remove node_modules from .npmIgnore so that node_modules are included in the resulting tar from npm pack
17
+ let npmIgnoreString;
18
+ if (fs.existsSync(npmIgnorePath)) {
19
+ npmIgnoreString = fs.readFileSync(npmIgnorePath, 'utf8');
20
+ npmIgnoreString = npmIgnoreString.replace('node_modules', '');
21
+ npmIgnoreString = npmIgnoreString.replace('\n\n', '\n');
22
+ fs.writeFileSync(npmIgnorePath, npmIgnoreString);
23
+ }
24
+
25
+ // add files to package so that node_modules are included in the resulting tar from npm pack
26
+ const adapterPackage = fs.readJSONSync(adapterPackagePath);
27
+ adapterPackage.files = ['*'];
28
+ fs.writeJSONSync(artifactPackagePath, adapterPackage, { spaces: 2 });
29
+ const npmResult = spawnSync('npm', ['pack', '-q', bundlePath], { cwd: path.resolve(bundlePath, '..') });
30
+ if (npmResult.status === 0) {
31
+ fs.removeSync(bundlePath);
32
+ console.log('Bundle folder removed');
33
+ }
34
+ console.log('Script successful');
35
+ });
@@ -0,0 +1,27 @@
1
+ #!/bin/sh
2
+ # @copyright Itential, LLC 2019
3
+
4
+ #exit on any failure in the pipeline
5
+ set -e
6
+
7
+ # --------------------------------------------------
8
+ # pre-commit
9
+ # --------------------------------------------------
10
+ # Contains the standard set of tasks to runbefore
11
+ # committing changes to the repo. If any tasks fail
12
+ # then the commit will be aborted.
13
+ # --------------------------------------------------
14
+
15
+ printf "%b" "Running pre-commit hooks...\\n"
16
+
17
+ # verify testing script is stubbed and no credentials
18
+ # node utils/testRunner.js -r
19
+
20
+ # security audit on the code
21
+ npm audit --registry=https://registry.npmjs.org --audit-level=moderate
22
+
23
+ # lint the code
24
+ npm run lint
25
+
26
+ # test the code
27
+ npm run test
package/utils/setup.js ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env node
2
+ /* @copyright Itential, LLC 2019 */
3
+
4
+ const fs = require('fs');
5
+
6
+ /**
7
+ * This script will execute before an npm install command. The purpose is to
8
+ * write out some standard git hooks that will enable folks working on this
9
+ * project to benefit from the protections that the hooks provide.
10
+ */
11
+
12
+ const precommit = fs.readFileSync('utils/pre-commit.sh', 'utf8');
13
+
14
+ fs.stat('.git', (err) => {
15
+ if (err == null) {
16
+ // git repo, not an npm repo.
17
+ // add pre-commit hook if it doesn't exist
18
+ fs.stat('.git/hooks/pre-commit', (statErr) => {
19
+ if (statErr == null || statErr.code === 'ENOENT') {
20
+ fs.writeFile('.git/hooks/pre-commit', precommit, {
21
+ mode: 0o755
22
+ }, (writeErr) => {
23
+ if (writeErr) {
24
+ return console.log(writeErr.message);
25
+ }
26
+ return null;
27
+ });
28
+ } else {
29
+ console.log(statErr.message);
30
+ }
31
+ });
32
+ }
33
+ });
@@ -0,0 +1,298 @@
1
+ #!/usr/bin/env node
2
+ /* @copyright Itential, LLC 2019 */
3
+
4
+ const fs = require('fs-extra');
5
+ const rl = require('readline-sync');
6
+ const execute = require('child_process').exec;
7
+
8
+ /**
9
+ * This script will determine the type of integration test to run
10
+ * based on input. If other information is needed, it will solicit
11
+ * that input and then edit the integration test accordingly.
12
+ */
13
+
14
+ let stub = true;
15
+ let isRapidFail = false;
16
+ let isSaveMockData = false;
17
+ let host = 'replace.hostorip.here';
18
+ let username = 'username';
19
+ let password = 'password';
20
+ let protocol = 'http';
21
+ let port = 80;
22
+ let sslenable = false;
23
+ let sslinvalid = false;
24
+ const dstub = true;
25
+ const disRapidFail = false;
26
+ const disSaveMockData = false;
27
+ const dhost = 'replace.hostorip.here';
28
+ const dusername = 'username';
29
+ const dpassword = 'password';
30
+ const dprotocol = 'http';
31
+ const dport = 80;
32
+ const dsslenable = false;
33
+ const dsslinvalid = false;
34
+
35
+ let stderror = false;
36
+ let running = false;
37
+
38
+ /**
39
+ * Updates the integration test file with the proper vars
40
+ */
41
+ function replaceTestVars(test) {
42
+ if (!fs.existsSync(test)) {
43
+ console.log(`Could not find ${test}`);
44
+ return 'error';
45
+ }
46
+
47
+ let intTest = fs.readFileSync(test, 'utf8');
48
+
49
+ // replace stub variable but check if it exists first
50
+ let sindex = intTest.indexOf('const stub');
51
+ let eindex = intTest.indexOf(';', sindex);
52
+ let replStr = intTest.substring(sindex, eindex + 1);
53
+ if (sindex > -1) {
54
+ intTest = intTest.replace(replStr, `const stub = ${stub};`);
55
+ }
56
+
57
+ // replace isRapidFail variable but check if it exists first
58
+ sindex = intTest.indexOf('const isRapidFail');
59
+ eindex = intTest.indexOf(';', sindex);
60
+ replStr = intTest.substring(sindex, eindex + 1);
61
+ if (sindex > -1) {
62
+ intTest = intTest.replace(replStr, `const isRapidFail = ${isRapidFail};`);
63
+ }
64
+
65
+ // replace isSaveMockData variable but check if it exists first
66
+ sindex = intTest.indexOf('const isSaveMockData');
67
+ eindex = intTest.indexOf(';', sindex);
68
+ replStr = intTest.substring(sindex, eindex + 1);
69
+ if (sindex > -1) {
70
+ intTest = intTest.replace(replStr, `const isSaveMockData = ${isSaveMockData};`);
71
+ }
72
+
73
+ // replace host variable
74
+ sindex = intTest.indexOf('const host');
75
+ eindex = intTest.indexOf(';', sindex);
76
+ replStr = intTest.substring(sindex, eindex + 1);
77
+ intTest = intTest.replace(replStr, `const host = '${host}';`);
78
+
79
+ // replace username variable
80
+ sindex = intTest.indexOf('const username');
81
+ eindex = intTest.indexOf(';', sindex);
82
+ replStr = intTest.substring(sindex, eindex + 1);
83
+ intTest = intTest.replace(replStr, `const username = '${username}';`);
84
+
85
+ // replace password variable
86
+ sindex = intTest.indexOf('const password');
87
+ eindex = intTest.indexOf(';', sindex);
88
+ replStr = intTest.substring(sindex, eindex + 1);
89
+ intTest = intTest.replace(replStr, `const password = '${password}';`);
90
+
91
+ // replace protocol variable
92
+ sindex = intTest.indexOf('const protocol');
93
+ eindex = intTest.indexOf(';', sindex);
94
+ replStr = intTest.substring(sindex, eindex + 1);
95
+ intTest = intTest.replace(replStr, `const protocol = '${protocol}';`);
96
+
97
+ // replace port variable
98
+ sindex = intTest.indexOf('const port');
99
+ eindex = intTest.indexOf(';', sindex);
100
+ replStr = intTest.substring(sindex, eindex + 1);
101
+ intTest = intTest.replace(replStr, `const port = ${port};`);
102
+
103
+ // replace sslenable variable
104
+ sindex = intTest.indexOf('const sslenable');
105
+ eindex = intTest.indexOf(';', sindex);
106
+ replStr = intTest.substring(sindex, eindex + 1);
107
+ intTest = intTest.replace(replStr, `const sslenable = ${sslenable};`);
108
+
109
+ // replace sslinvalid variable
110
+ sindex = intTest.indexOf('const sslinvalid');
111
+ eindex = intTest.indexOf(';', sindex);
112
+ replStr = intTest.substring(sindex, eindex + 1);
113
+ intTest = intTest.replace(replStr, `const sslinvalid = ${sslinvalid};`);
114
+
115
+ console.log(`Updates to ${test} complete`);
116
+ fs.writeFileSync(test, intTest);
117
+ return 'success';
118
+ }
119
+
120
+ /**
121
+ * Updates the integration test file and runs the script
122
+ */
123
+ function runTest(callback) {
124
+ replaceTestVars('test/integration/adapterTestIntegration.js');
125
+
126
+ let cmdPath = 'npm run test:integration';
127
+ console.log('\nRUNNING INTEGRATION TESTS - THIS WILL TAKE SOME TIME AND WILL NOT PRINT UNTIL TEST IS COMPLETE!\n');
128
+ if (stderror) {
129
+ console.log('\nNOTE: standard error from tests is included - unless test failed, these may be expected errors:\n');
130
+ cmdPath += ' 2>&1';
131
+ } else {
132
+ console.log('stderr not shown');
133
+ }
134
+
135
+ return execute(cmdPath, (cerror, stdout) => {
136
+ console.log('executed tests:\n');
137
+ console.log(`${stdout}\n`);
138
+ if (cerror) {
139
+ console.log('\x1b[31m%s\x1b[0m', '\nexec error:\n');
140
+ console.log('\x1b[31m%s\x1b[0m', `${cerror}\n`);
141
+ }
142
+ // reset the defaults
143
+ stub = dstub;
144
+ isRapidFail = disRapidFail;
145
+ isSaveMockData = disSaveMockData;
146
+ host = dhost;
147
+ username = dusername;
148
+ password = dpassword;
149
+ protocol = dprotocol;
150
+ port = dport;
151
+ sslenable = dsslenable;
152
+ sslinvalid = dsslinvalid;
153
+ replaceTestVars('test/integration/adapterTestIntegration.js');
154
+ return callback('done');
155
+ });
156
+ }
157
+
158
+ /**
159
+ * Updates the unit test file and runs the script
160
+ */
161
+ function runUnitTest(callback) {
162
+ let cmdPath = 'npm run test:unit';
163
+ console.log('\nRUNNING UNIT TESTS - THIS WILL TAKE SOME TIME AND WILL NOT PRINT UNTIL TEST IS COMPLETE!\n');
164
+
165
+ if (stderror) {
166
+ console.log('\nNOTE: standard error from tests is included- unless test failed, these may be expected errors:\n');
167
+ cmdPath += ' 2>&1';
168
+ } else {
169
+ console.log('stderr not shown');
170
+ }
171
+
172
+ return execute(cmdPath, (cerror, stdout) => {
173
+ console.log('executed tests:\n');
174
+ console.log(`${stdout}\n`);
175
+
176
+ if (cerror) {
177
+ console.log('\x1b[31m%s\x1b[0m', '\nexec error:\n');
178
+ console.log('\x1b[31m%s\x1b[0m', `${cerror}\n`);
179
+ }
180
+
181
+ return callback('done');
182
+ });
183
+ }
184
+
185
+ // print process.argv
186
+ const args = process.argv.slice(2);
187
+
188
+ // go through the arguments that where provided
189
+ for (let a = 0; a < args.length; a += 1) {
190
+ if (args[a].toUpperCase() === '-H' || args[a].toUpperCase() === '--HELP') {
191
+ let message = '\nThis tool is used to make it easier to run integration tests.\n';
192
+ message += '\n';
193
+ message += 'Options:\n';
194
+ message += '-h, --help: Prints this message\n';
195
+ message += '-f, --failfast: Fail the test when the first test fails\n';
196
+ message += '-m, --mockdata: Update mock data files with the results from testing (only if running integrated)\n';
197
+ message += '-r, --reset: Resets the variables back to stub settings and removes credentials\n';
198
+ message += '-s, --stderror: Displays the standard error from the run, this can have data even if all the tests pass\n';
199
+ message += '-u, --unit: Runs just the unit tests as well\n';
200
+ console.log(message);
201
+ running = true;
202
+ }
203
+
204
+ if (args[a].toUpperCase() === '-F' || args[a].toUpperCase() === '--FAILFAST') {
205
+ isRapidFail = true;
206
+ }
207
+ if (args[a].toUpperCase() === '-M' || args[a].toUpperCase() === '--MOCKDATA') {
208
+ isSaveMockData = true;
209
+ }
210
+ if (args[a].toUpperCase() === '-R' || args[a].toUpperCase() === '--RESET') {
211
+ running = true;
212
+ replaceTestVars('test/integration/adapterTestIntegration.js');
213
+ replaceTestVars('test/unit/adapterTestUnit.js');
214
+ console.log('test reset complete');
215
+ }
216
+ if (args[a].toUpperCase() === '-S' || args[a].toUpperCase() === '--STDERROR') {
217
+ stderror = true;
218
+ }
219
+ if (args[a].toUpperCase() === '-U' || args[a].toUpperCase() === '--UNIT') {
220
+ running = true;
221
+ runUnitTest((status) => {
222
+ console.log(status);
223
+ process.exit(1);
224
+ });
225
+ }
226
+ }
227
+
228
+ if (!running) {
229
+ // how are we running the test?
230
+ let answer = rl.question('\nDo you want to run the integration test integrated with the other system? (no): ');
231
+ if (answer && (answer.toLowerCase() === 'yes' || answer.toLowerCase() === 'y')) {
232
+ stub = false;
233
+ console.log('Need more information about the integration!');
234
+ } else {
235
+ running = true;
236
+ runTest((status) => {
237
+ console.log(status);
238
+ process.exit(1);
239
+ });
240
+ }
241
+
242
+ if (!running) {
243
+ // how are we running the test?
244
+ answer = rl.question('\nWhat is the dns or ip of the system you want to test with? (localhost): ');
245
+ if (answer) {
246
+ host = answer;
247
+ } else {
248
+ host = 'localhost';
249
+ }
250
+
251
+ // need the username to authenticate with
252
+ answer = rl.question('\nWhat is the username to authenticate, if no authentication just return? (username): ');
253
+ if (answer) {
254
+ username = answer;
255
+ }
256
+
257
+ // need the password to authenticate with
258
+ answer = rl.question('\nWhat is the password to authenticate, if no authentication just return? (password): ', { hideEchoBack: true });
259
+ if (answer) {
260
+ password = answer;
261
+ }
262
+
263
+ // need the protocol used with other system
264
+ answer = rl.question('\nWhat is the protocol used to communicate with the system? (http): ');
265
+ if (answer) {
266
+ protocol = answer;
267
+ }
268
+
269
+ if (protocol === 'https') {
270
+ // if protocol is https, set default port to 443
271
+ port = 443;
272
+ // need the port used with other system
273
+ answer = rl.question('\nWhat is the port used to communicate with the system? (443): ');
274
+ port = 443; // update default answer to 443 for https
275
+ if (answer) {
276
+ port = Number(answer);
277
+ }
278
+
279
+ // turn on ssl and accept invalid certs
280
+ sslenable = true;
281
+ sslinvalid = true;
282
+ runTest((status) => {
283
+ console.log(status);
284
+ process.exit(1);
285
+ });
286
+ } else {
287
+ // need the port used with other system
288
+ answer = rl.question('\nWhat is the port used to communicate with the system? (80): ');
289
+ if (answer) {
290
+ port = Number(answer);
291
+ }
292
+ runTest((status) => {
293
+ console.log(status);
294
+ process.exit(1);
295
+ });
296
+ }
297
+ }
298
+ }