From f1376e5e5bfaf2571d60286c56690a3a9fdc8071 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sat, 29 Jan 2022 15:27:38 +0100 Subject: [PATCH] update testing setup.js --- test/lib/setup.js | 539 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 377 insertions(+), 162 deletions(-) diff --git a/test/lib/setup.js b/test/lib/setup.js index a268d0d..f8aa782 100644 --- a/test/lib/setup.js +++ b/test/lib/setup.js @@ -9,6 +9,8 @@ const pkg = require(rootDir + 'package.json'); const debug = typeof v8debug === 'object'; pkg.main = pkg.main || 'main.js'; +let JSONLDB; + let adapterName = path.normalize(rootDir).replace(/\\/g, '/').split('/'); adapterName = adapterName[adapterName.length - 2]; let adapterStarted = false; @@ -18,6 +20,21 @@ function getAppName() { return parts[parts.length - 3].split('.')[0]; } +function loadJSONLDB() { + if (!JSONLDB) { + const dbPath = require.resolve('@alcalzone/jsonl-db', { + paths: [rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller'] + }); + console.log('JSONLDB path: ' + dbPath); + try { + const { JsonlDB } = require(dbPath); + JSONLDB = JsonlDB; + } catch (err) { + console.log('Jsonl require error: ' + err); + } + } +} + const appName = getAppName().toLowerCase(); let objects; @@ -25,6 +42,8 @@ let states; let pid = null; +let systemConfig = null; + function copyFileSync(source, target) { let targetFile = target; @@ -84,26 +103,65 @@ if (!fs.existsSync(rootDir + 'tmp')) { fs.mkdirSync(rootDir + 'tmp'); } -function storeOriginalFiles() { +async function storeOriginalFiles() { console.log('Store original files...'); const dataDir = rootDir + 'tmp/' + appName + '-data/'; - let f = fs.readFileSync(dataDir + 'objects.json'); - const objects = JSON.parse(f.toString()); - if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) { - objects['system.adapter.admin.0'].common.enabled = false; - } - if (objects['system.adapter.admin.1'] && objects['system.adapter.admin.1'].common) { - objects['system.adapter.admin.1'].common.enabled = false; + if (fs.existsSync(dataDir + 'objects.json')) { + const f = fs.readFileSync(dataDir + 'objects.json'); + const objects = JSON.parse(f.toString()); + if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) { + objects['system.adapter.admin.0'].common.enabled = false; + } + if (objects['system.adapter.admin.1'] && objects['system.adapter.admin.1'].common) { + objects['system.adapter.admin.1'].common.enabled = false; + } + + fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects)); + console.log('Store original objects.json'); } - fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects)); - try { - f = fs.readFileSync(dataDir + 'states.json'); - fs.writeFileSync(dataDir + 'states.json.original', f); + if (fs.existsSync(dataDir + 'states.json')) { + try { + const f = fs.readFileSync(dataDir + 'states.json'); + fs.writeFileSync(dataDir + 'states.json.original', f); + console.log('Store original states.json'); + } catch (err) { + console.log('no states.json found - ignore'); + } } - catch (err) { - console.log('no states.json found - ignore'); + + if (fs.existsSync(dataDir + 'objects.jsonl')) { + loadJSONLDB(); + const db = new JSONLDB(dataDir + 'objects.jsonl'); + await db.open(); + + const admin0 = db.get('system.adapter.admin.0'); + if (admin0) { + if (admin0.common) { + admin0.common.enabled = false; + db.set('system.adapter.admin.0', admin0); + } + } + + const admin1 = db.get('system.adapter.admin.1'); + if (admin1) { + if (admin1.common) { + admin1.common.enabled = false; + db.set('system.adapter.admin.1', admin1); + } + } + await db.close(); + + const f = fs.readFileSync(dataDir + 'objects.jsonl'); + fs.writeFileSync(dataDir + 'objects.jsonl.original', f); + console.log('Store original objects.jsonl'); + } + + if (fs.existsSync(dataDir + 'states.jsonl')) { + const f = fs.readFileSync(dataDir + 'states.jsonl'); + fs.writeFileSync(dataDir + 'states.jsonl.original', f); + console.log('Store original states.jsonl'); } } @@ -111,38 +169,74 @@ function restoreOriginalFiles() { console.log('restoreOriginalFiles...'); const dataDir = rootDir + 'tmp/' + appName + '-data/'; - let f = fs.readFileSync(dataDir + 'objects.json.original'); - fs.writeFileSync(dataDir + 'objects.json', f); - try { - f = fs.readFileSync(dataDir + 'states.json.original'); + if (fs.existsSync(dataDir + 'objects.json.original')) { + const f = fs.readFileSync(dataDir + 'objects.json.original'); + fs.writeFileSync(dataDir + 'objects.json', f); + } + if (fs.existsSync(dataDir + 'objects.json.original')) { + const f = fs.readFileSync(dataDir + 'states.json.original'); fs.writeFileSync(dataDir + 'states.json', f); } - catch (err) { - console.log('no states.json.original found - ignore'); - } + if (fs.existsSync(dataDir + 'objects.jsonl.original')) { + const f = fs.readFileSync(dataDir + 'objects.jsonl.original'); + fs.writeFileSync(dataDir + 'objects.jsonl', f); + } + if (fs.existsSync(dataDir + 'objects.jsonl.original')) { + const f = fs.readFileSync(dataDir + 'states.jsonl.original'); + fs.writeFileSync(dataDir + 'states.jsonl', f); + } } -function checkIsAdapterInstalled(cb, counter, customName) { +async function checkIsAdapterInstalled(cb, counter, customName) { customName = customName || pkg.name.split('.').pop(); counter = counter || 0; const dataDir = rootDir + 'tmp/' + appName + '-data/'; console.log('checkIsAdapterInstalled...'); try { - const f = fs.readFileSync(dataDir + 'objects.json'); - const objects = JSON.parse(f.toString()); - if (objects['system.adapter.' + customName + '.0']) { - console.log('checkIsAdapterInstalled: ready!'); - setTimeout(function () { - if (cb) cb(); - }, 100); - return; - } else { - console.warn('checkIsAdapterInstalled: still not ready'); - } - } catch (err) { + if (fs.existsSync(dataDir + 'objects.json')) { + const f = fs.readFileSync(dataDir + 'objects.json'); + const objects = JSON.parse(f.toString()); + if (objects['system.adapter.' + customName + '.0']) { + console.log('checkIsAdapterInstalled: ready!'); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } else { + console.warn('checkIsAdapterInstalled: still not ready'); + } + } else if (fs.existsSync(dataDir + 'objects.jsonl')) { + loadJSONLDB(); + const db = new JSONLDB(dataDir + 'objects.jsonl'); + try { + await db.open(); + } catch (err) { + if (err.message.includes('Failed to lock DB file')) { + console.log('checkIsAdapterInstalled: DB still opened ...'); + } + throw err; + } + const obj = db.get('system.adapter.' + customName + '.0'); + await db.close(); + + if (obj) { + console.log('checkIsAdapterInstalled: ready!'); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } else { + console.warn('checkIsAdapterInstalled: still not ready'); + } + } else { + console.error('checkIsAdapterInstalled: No objects file found in datadir ' + dataDir); + } + + } catch (err) { + console.log('checkIsAdapterInstalled: catch ' + err); } if (counter > 20) { @@ -156,20 +250,47 @@ function checkIsAdapterInstalled(cb, counter, customName) { } } -function checkIsControllerInstalled(cb, counter) { +async function checkIsControllerInstalled(cb, counter) { counter = counter || 0; const dataDir = rootDir + 'tmp/' + appName + '-data/'; console.log('checkIsControllerInstalled...'); try { - const f = fs.readFileSync(dataDir + 'objects.json'); - const objects = JSON.parse(f.toString()); - if (objects['system.certificates']) { - console.log('checkIsControllerInstalled: installed!'); - setTimeout(function () { - if (cb) cb(); - }, 100); - return; + if (fs.existsSync(dataDir + 'objects.json')) { + const f = fs.readFileSync(dataDir + 'objects.json'); + const objects = JSON.parse(f.toString()); + if (objects['system.certificates']) { + console.log('checkIsControllerInstalled: installed!'); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } + } else if (fs.existsSync(dataDir + 'objects.jsonl')) { + loadJSONLDB(); + const db = new JSONLDB(dataDir + 'objects.jsonl'); + try { + await db.open(); + } catch (err) { + if (err.message.includes('Failed to lock DB file')) { + console.log('checkIsControllerInstalled: DB still opened ...'); + } + throw err; + } + + const obj = db.get('system.certificates'); + await db.close(); + + if (obj) { + console.log('checkIsControllerInstalled: installed!'); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } + + } else { + console.error('checkIsControllerInstalled: No objects file found in datadir ' + dataDir); } } catch (err) { @@ -296,13 +417,17 @@ function installJsController(cb) { const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); config.objects.port = 19001; config.states.port = 19000; + + // TEST WISE! + //config.objects.type = 'jsonl'; + //config.states.type = 'jsonl'; fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); console.log('Setup finished.'); copyAdapterToController(); - installAdapter(function () { - storeOriginalFiles(); + installAdapter(async function () { + await storeOriginalFiles(); if (cb) cb(true); }); }); @@ -359,12 +484,16 @@ function installJsController(cb) { const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); config.objects.port = 19001; config.states.port = 19000; + + // TEST WISE! + //config.objects.type = 'jsonl'; + //config.states.type = 'jsonl'; fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); copyAdapterToController(); - installAdapter(function () { - storeOriginalFiles(); + installAdapter(async function () { + await storeOriginalFiles(); if (cb) cb(true); }); }); @@ -445,46 +574,89 @@ function clearDB() { } function setupController(cb) { - installJsController(function (isInited) { - clearControllerLog(); - clearDB(); + installJsController(async function (isInited) { + try { + clearControllerLog(); + clearDB(); - if (!isInited) { - restoreOriginalFiles(); - copyAdapterToController(); + if (!isInited) { + restoreOriginalFiles(); + copyAdapterToController(); + } + // read system.config object + const dataDir = rootDir + 'tmp/' + appName + '-data/'; + + if (fs.existsSync(dataDir + 'objects.json')) { + let objs; + try { + objs = fs.readFileSync(dataDir + 'objects.json'); + objs = JSON.parse(objs); + } catch (e) { + console.log('ERROR reading/parsing system configuration. Ignore'); + objs = {'system.config': {}}; + } + if (!objs || !objs['system.config']) { + objs = {'system.config': {}}; + } + + systemConfig = objs['system.config']; + if (cb) cb(objs['system.config']); + } else if (fs.existsSync(dataDir + 'objects.jsonl')) { + loadJSONLDB(); + const db = new JSONLDB(dataDir + 'objects.jsonl'); + await db.open(); + + let config = db.get('system.config'); + systemConfig = config || {}; + + await db.close(); + + if (cb) cb(systemConfig); + } else { + console.error('read SystemConfig: No objects file found in datadir ' + dataDir); + } + } catch (err) { + console.error('setupController: ' + err); } - // read system.config object - const dataDir = rootDir + 'tmp/' + appName + '-data/'; + }); +} +async function getSecret() { + var dataDir = rootDir + 'tmp/' + appName + '-data/'; + + if (systemConfig) { + return systemConfig.native.secret; + } + if (fs.existsSync(dataDir + 'objects.json')) { let objs; try { objs = fs.readFileSync(dataDir + 'objects.json'); objs = JSON.parse(objs); } catch (e) { - console.log('ERROR reading/parsing system configuration. Ignore'); - objs = {'system.config': {}}; + console.warn("Could not load secret. Reason: " + e); + return null; } if (!objs || !objs['system.config']) { objs = {'system.config': {}}; } - if (cb) cb(objs['system.config']); - }); -} + return objs['system.config'].native.secre; + } else if (fs.existsSync(dataDir + 'objects.jsonl')) { + loadJSONLDB(); + const db = new JSONLDB(dataDir + 'objects.jsonl'); + await db.open(); -function getSecret() { - var dataDir = rootDir + 'tmp/' + appName + '-data/'; + let config = db.get('system.config'); + config = config || {}; - try { - var objs = fs.readFileSync(dataDir + 'objects.json'); - objs = JSON.parse(objs); + await db.close(); - return objs['system.config'].native.secret; - } catch (e) { - console.warn("Could not load secret. Reason: " + e); - return null; + return config.native.secret; + } else { + console.error('read secret: No objects file found in datadir ' + dataDir); } + } function encrypt (key, value) { @@ -544,100 +716,115 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback console.error('Controller is already started!'); } else { console.log('startController...'); - adapterStarted = false; - let isObjectConnected; - let isStatesConnected; + try { + const config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); - const Objects = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/objects/objectsInMemServer'); - objects = new Objects({ - connection: { - 'type' : 'file', - 'host' : '127.0.0.1', - 'port' : 19001, - 'user' : '', - 'pass' : '', - 'noFileCache': false, - 'connectTimeout': 2000 - }, - logger: { - silly: function (msg) { - console.log(msg); + adapterStarted = false; + let isObjectConnected; + let isStatesConnected; + + // rootDir + 'tmp/node_modules + const objPath = require.resolve(`@iobroker/db-objects-${config.objects.type}`, { + paths: [ rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller'] + }); + console.log('Objects Path: ' + objPath); + const Objects = require(objPath).Server; + objects = new Objects({ + connection: { + 'type': config.objects.type, + 'host': '127.0.0.1', + 'port': 19001, + 'user': '', + 'pass': '', + 'noFileCache': false, + 'connectTimeout': 2000 }, - debug: function (msg) { - console.log(msg); + logger: { + silly: function (msg) { + console.log(msg); + }, + debug: function (msg) { + console.log(msg); + }, + info: function (msg) { + console.log(msg); + }, + warn: function (msg) { + console.warn(msg); + }, + error: function (msg) { + console.error(msg); + } }, - info: function (msg) { - console.log(msg); - }, - warn: function (msg) { - console.warn(msg); - }, - error: function (msg) { - console.error(msg); - } - }, - connected: function () { - isObjectConnected = true; - if (isStatesConnected) { - console.log('startController: started!'); - if (isStartAdapter) { - startAdapter(objects, states, callback); - } else { - if (callback) { - callback(objects, states); - callback = null; + connected: function () { + isObjectConnected = true; + if (isStatesConnected) { + console.log('startController: started!'); + if (isStartAdapter) { + startAdapter(objects, states, callback); + } else { + if (callback) { + callback(objects, states); + callback = null; + } } } - } - }, - change: onObjectChange - }); + }, + change: onObjectChange + }); - // Just open in memory DB itself - const States = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/states/statesInMemServer'); - states = new States({ - connection: { - type: 'file', - host: '127.0.0.1', - port: 19000, - options: { - auth_pass: null, - retry_max_delay: 15000 - } - }, - logger: { - silly: function (msg) { - console.log(msg); + // Just open in memory DB itself + const statePath = require.resolve(`@iobroker/db-states-${config.states.type}`, { + paths: [ rootDir + 'tmp/node_modules', rootDir, rootDir + 'tmp/node_modules/' + appName + '.js-controller'] + }); + console.log('States Path: ' + statePath); + const States = require(statePath).Server; + states = new States({ + connection: { + type: config.states.type, + host: '127.0.0.1', + port: 19000, + options: { + auth_pass: null, + retry_max_delay: 15000 + } }, - debug: function (msg) { - console.log(msg); + logger: { + silly: function (msg) { + console.log(msg); + }, + debug: function (msg) { + console.log(msg); + }, + info: function (msg) { + console.log(msg); + }, + warn: function (msg) { + console.log(msg); + }, + error: function (msg) { + console.log(msg); + } }, - info: function (msg) { - console.log(msg); - }, - warn: function (msg) { - console.log(msg); - }, - error: function (msg) { - console.log(msg); - } - }, - connected: function () { - isStatesConnected = true; - if (isObjectConnected) { - console.log('startController: started!!'); - if (isStartAdapter) { - startAdapter(objects, states, callback); - } else { - if (callback) { - callback(objects, states); - callback = null; + connected: function () { + isStatesConnected = true; + if (isObjectConnected) { + console.log('startController: started!!'); + if (isStartAdapter) { + startAdapter(objects, states, callback); + } else { + if (callback) { + callback(objects, states); + callback = null; + } } } - } - }, - change: onStateChange - }); + }, + change: onStateChange + }); + } catch (err) { + console.log(err); + } } } @@ -721,19 +908,47 @@ function stopController(cb) { } // Setup the adapter -function setAdapterConfig(common, native, instance) { - const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); +async function setAdapterConfig(common, native, instance) { const id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); - if (common) objects[id].common = common; - if (native) objects[id].native = native; - fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects)); + if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.json')) { + const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); + if (common) objects[id].common = common; + if (native) objects[id].native = native; + fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects)); + } else if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.jsonl')) { + loadJSONLDB(); + const db = new JSONLDB(rootDir + 'tmp/' + appName + '-data/objects.jsonl'); + await db.open(); + + let obj = db.get(id); + if (common) obj.common = common; + if (native) obj.native = native; + db.set(id, obj); + + await db.close(); + } else { + console.error('setAdapterConfig: No objects file found in datadir ' + rootDir + 'tmp/' + appName + '-data/'); + } } // Read config of the adapter -function getAdapterConfig(instance) { - const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); - const id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); - return objects[id]; +async function getAdapterConfig(instance) { + const id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); + if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.json')) { + const objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); + return objects[id]; + } else if (fs.existsSync(rootDir + 'tmp/' + appName + '-data/objects.jsonl')) { + loadJSONLDB(); + const db = new JSONLDB(rootDir + 'tmp/' + appName + '-data/objects.jsonl'); + await db.open(); + + let obj = db.get(id); + + await db.close(); + return obj; + } else { + console.error('getAdapterConfig: No objects file found in datadir ' + rootDir + 'tmp/' + appName + '-data/'); + } } if (typeof module !== undefined && module.parent) {