diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 42f4c7ad1..d9d9ba482 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -26,7 +26,7 @@ If applicable, add screenshots to help explain your problem. **Version:** - Predator: [e.g. 1.0.8] - Predator-runner [e.g. 1.0.5] - - Database [e.g. cassandra] + - Database [e.g. sqlite] **Additional context** Add any other context about the problem here. diff --git a/README.md b/README.md index 478aa0181..460daf2d9 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ It has a simple, one-click installation, built with support for Kubernetes, DC/O |-------------------------------- |:------------------:|:---------| | Distributed Load | :sparkle: |Predator supports an unlimited number of load generators that produce multiple load runners concurrently. | Rich UI | :sparkle: |Predator offers a rich UI where you can write tests, run them and compare results. -| Reports && Tests Persistence | :sparkle: |Predator provides out-of-the box functionality for persisting data in Cassandra, Postgres, MySQL, MSSQL and SQLITE. +| Reports && Tests Persistence | :sparkle: |Predator provides out-of-the box functionality for persisting data in Postgres, MySQL, MSSQL and SQLITE. | Real time reports | :sparkle: |Predator aggregates all concurrent runs into a single beautiful report in real time (latency, rps, status codes and more). | CSV Datasets | :sparkle: |Predator support uploading files like csv to provide dataset for test inputs | Scheduled runs | :sparkle: |Predator can run recurring tests using cron expressions. diff --git a/docs/devguide/docs/about.md b/docs/devguide/docs/about.md index 01253769d..e8e802363 100644 --- a/docs/devguide/docs/about.md +++ b/docs/devguide/docs/about.md @@ -18,7 +18,7 @@ Predator is a performance platform that can be configured to automatically load - **One click installation**: Predator can be installed with just one click in Kubernetes and DC/OS, or on any other machine running Docker. -- **Supports 5 Different databases**: Predator provides out-of-the box functionality for persisting data in Cassandra, Postgres, MySQL, MSSQL and SQLITE. +- **Supports 5 Different databases**: Predator provides out-of-the box functionality for persisting data in Postgres, MySQL, MSSQL and SQLITE. - **Scheduled jobs**: Predator can run recurring tests using cron expressions. @@ -37,5 +37,3 @@ load engine to fire the requests. The schema for creating tests via the Predator ## Feature Comparison ![Screenshot](images/features.png) - - diff --git a/docs/devguide/docs/configuration.md b/docs/devguide/docs/configuration.md index b06ebfe61..7cc316efc 100644 --- a/docs/devguide/docs/configuration.md +++ b/docs/devguide/docs/configuration.md @@ -22,7 +22,7 @@ Below are variables Predator can be configured with. ## Database | Environment Variable | Description | Configurable from UI/API | Default value | |---------------------- |--------------------------------------------------------------------------------- |-------------------------- |--------------- | -| DATABASE_TYPE | Database to integrate Predator with [Cassandra, Postgres, MySQL, MSSQL, SQLITE] | x | SQLITE | +| DATABASE_TYPE | Database to integrate Predator with [Postgres, MySQL, MSSQL, SQLITE] | x | SQLITE | | DATABASE_NAME | Database/Keyspace name | x | | | DATABASE_ADDRESS | Database address | x | | | DATABASE_USERNAME | Database username | x | | @@ -30,14 +30,6 @@ Below are variables Predator can be configured with. Additional parameters for the following chosen databases: -#### Cassandra -| Environment Variable | Configurable from UI/API | Default value | -|------------------------------ |-------------------------- |---------------- | -| CASSANDRA_REPLICATION_FACTOR | x | 1 | -| CASSANDRA_CONSISTENCY | x | localQuorum | -| CASSANDRA_KEY_SPACE_STRATEGY | x | SimpleStrategy | -| CASSANDRA_LOCAL_DATA_CENTER | x | | - #### SQLITE | Environment Variable | Description | Configurable from UI/API | Default value | |---------------------- |------------------ |-------------------------- |--------------- | @@ -109,4 +101,4 @@ Additional parameters for the following chosen databases: | SMTP_PASSWORD | smtp_server.password | SMTP password | ✓ | | | SMTP_TIMEOUT | smtp_server.timeout | How many milliseconds to wait for the connection to establish to SMTP server | ✓ | 200 | | SMTP_SECURE | smtp_server.secure | if true the connection will use TLS when connecting to server. [Nodemailer SMTP options](https://nodemailer.com/smtp/) | ✓ | false | -| SMTP_REJECT_UNAUTH_CERTS | smtp_server.rejectUnauthCerts | should fail or succeed on unauthorized certificate | ✓ | false | \ No newline at end of file +| SMTP_REJECT_UNAUTH_CERTS | smtp_server.rejectUnauthCerts | should fail or succeed on unauthorized certificate | ✓ | false | diff --git a/docs/devguide/docs/index.html.bk b/docs/devguide/docs/index.html.bk index f5306ecfe..1fb17b15b 100644 --- a/docs/devguide/docs/index.html.bk +++ b/docs/devguide/docs/index.html.bk @@ -28,28 +28,28 @@ - + @@ -162,7 +162,7 @@

Supports 5 storage backends

-

Predator provides out-of-the box functionality for persisting data in Cassandra, Postgres, MySQL, MSSQL and SQLITE.

+

Predator provides out-of-the box functionality for persisting data in Postgres, MySQL, MSSQL and SQLITE.

@@ -273,4 +273,4 @@ - \ No newline at end of file + diff --git a/docs/devguide/docs/project.mobirise b/docs/devguide/docs/project.mobirise index 7590e2c56..abcf3dea8 100644 --- a/docs/devguide/docs/project.mobirise +++ b/docs/devguide/docs/project.mobirise @@ -506,7 +506,7 @@ } }, "_name": "features5", - "_customHTML": "
\n \n \n \n \n \n \n \n \n \n\n
\n \n \n \n
\n \n \n \n \n
\n\n
\n
\n
\n
\n\n
\n
\n \n
\n
\n

\n Built for the cloud

\n

Predator is built to take advantage of Kubernetes and DC/OS. It’s integrated with those platforms and is able to manage the load generator lifecycles by itself.

\n
\n
\n\n
1\" mbr-class=\"{'col-lg-3': cardsAmount == '4',\n 'col-lg-4': cardsAmount == '3'}\">\n
\n \n
\n
\n

\n Distributed load

\n

Predator supports an unlimited number of load generators that produce multiple load runners concurrently. Predator will aggregate the result of those runners in real-time into a beautiful report.

\n
\n
\n\n
2\" mbr-class=\"{'col-lg-3': cardsAmount == '4',\n 'col-lg-4': cardsAmount == '3'}\">\n
\n \n
\n
\n

\n One click installation

\n

Predator can be installed with just one click in Kubernetes and DC/OS, or on any other machine running Docker.

\n
\n
\n\n
3\" mbr-class=\"{'col-lg-3': cardsAmount == '4',\n 'col-lg-4': cardsAmount == '3'}\">\n
\n \n
\n
\n

Supports 5 storage backends

\n

Predator provides out-of-the box functionality for persisting data in Cassandra, Postgres, MySQL, MSSQL and SQLITE.

\n
\n
\n
\n
\n
", + "_customHTML": "
\n \n \n \n \n \n \n \n \n \n\n
\n \n \n \n
\n \n \n \n \n
\n\n
\n
\n
\n
\n\n
\n
\n \n
\n
\n

\n Built for the cloud

\n

Predator is built to take advantage of Kubernetes and DC/OS. It’s integrated with those platforms and is able to manage the load generator lifecycles by itself.

\n
\n
\n\n
1\" mbr-class=\"{'col-lg-3': cardsAmount == '4',\n 'col-lg-4': cardsAmount == '3'}\">\n
\n \n
\n
\n

\n Distributed load

\n

Predator supports an unlimited number of load generators that produce multiple load runners concurrently. Predator will aggregate the result of those runners in real-time into a beautiful report.

\n
\n
\n\n
2\" mbr-class=\"{'col-lg-3': cardsAmount == '4',\n 'col-lg-4': cardsAmount == '3'}\">\n
\n \n
\n
\n

\n One click installation

\n

Predator can be installed with just one click in Kubernetes and DC/OS, or on any other machine running Docker.

\n
\n
\n\n
3\" mbr-class=\"{'col-lg-3': cardsAmount == '4',\n 'col-lg-4': cardsAmount == '3'}\">\n
\n \n
\n
\n

Supports 5 storage backends

\n

Predator provides out-of-the box functionality for persisting data in Postgres, MySQL, MSSQL and SQLITE.

\n
\n
\n
\n
\n
", "_cid": "rqyVDU3s7P", "_anchor": "features5-7", "_protectedParams": [], @@ -1309,4 +1309,4 @@ ] } } -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index 387e5138f..e3f9f874c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -951,11 +951,6 @@ "resolved": "http://npm.zooz.co:8083/@types%2fcolor-name/-/color-name-1.1.1.tgz", "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==" }, - "@types/long": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.1.tgz", - "integrity": "sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==" - }, "@types/minimist": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.0.tgz", @@ -1056,11 +1051,6 @@ "integrity": "sha1-anmQQ3ynNtXhKI25K9MmbV9csqo=", "dev": true }, - "adm-zip": { - "version": "0.4.14", - "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.4.14.tgz", - "integrity": "sha512-/9aQCnQHF+0IiCl0qhXoK7qs//SwYE7zX8lsr/DNk1BRAHYxeLZPL4pguwK29gUEqasYQjqPtEpDRSWEkdHn9g==" - }, "after": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/after/-/after-0.8.2.tgz", @@ -1856,7 +1846,7 @@ }, "busboy": { "version": "0.3.1", - "resolved": "https://registry.npmjs.org/busboy/-/busboy-0.3.1.tgz", + "resolved": "http://npm.zooz.co:8083/busboy/-/busboy-0.3.1.tgz", "integrity": "sha512-y7tTxhGKXcyBxRKAni+awqx8uqaJKrSFSNFSeRG5CsWNdmy2BIK+6VGWEW7TZnIO/533mtMEA4rOevQV815YJw==", "requires": { "dicer": "0.3.0" @@ -2005,39 +1995,6 @@ "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" }, - "cassandra-driver": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/cassandra-driver/-/cassandra-driver-4.3.1.tgz", - "integrity": "sha512-4Yuf9UkmwidiXKdAb4AgkC92CcUhP4hNUNaMNnKMGA3MRMQj2ZCEL6BR0/PrMPkYGC5faMPHJ4YzWAu3FFvM6g==", - "requires": { - "@types/long": "^4.0.0", - "@types/node": ">=4", - "adm-zip": "^0.4.13", - "long": "^2.2.0" - } - }, - "cassandra-migration": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/cassandra-migration/-/cassandra-migration-2.7.0.tgz", - "integrity": "sha512-TVOLANJnURNXxLgSyoyKeYoAXqrbUJdCizOsh8riCqe5wwYM6m504lOwPgPMP9rnOVx8ImX1w96GpiQwWdLB3Q==", - "requires": { - "cassandra-driver": "^3.3.0", - "commander": "^2.9", - "durations": "^3.4.1", - "lodash": "^4.13.1", - "q": "^1.4" - }, - "dependencies": { - "cassandra-driver": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/cassandra-driver/-/cassandra-driver-3.6.0.tgz", - "integrity": "sha512-CkN3V+oPaF5RvakUjD3uUjEm8f6U8S0aT1+YqeQsVT3UDpPT2K8SOdNDEHA1KjamakHch6zkDgHph1xWyqBGGw==", - "requires": { - "long": "^2.2.0" - } - } - } - }, "chai": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz", @@ -2351,6 +2308,21 @@ "supports-color": "^5.3.0" } }, + "cz-conventional-changelog": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/cz-conventional-changelog/-/cz-conventional-changelog-3.2.0.tgz", + "integrity": "sha512-yAYxeGpVi27hqIilG1nh4A9Bnx4J3Ov+eXy4koL3drrR+IO9GaWPsKjik20ht608Asqi8TQPf0mczhEeyAtMzg==", + "dev": true, + "requires": { + "@commitlint/load": ">6.1.1", + "chalk": "^2.4.1", + "commitizen": "^4.0.3", + "conventional-commit-types": "^3.0.0", + "lodash.map": "^4.5.1", + "longest": "^2.0.1", + "word-wrap": "^1.0.3" + } + }, "figures": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/figures/-/figures-2.0.0.tgz", @@ -3684,9 +3656,9 @@ } }, "cz-conventional-changelog": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/cz-conventional-changelog/-/cz-conventional-changelog-3.2.0.tgz", - "integrity": "sha512-yAYxeGpVi27hqIilG1nh4A9Bnx4J3Ov+eXy4koL3drrR+IO9GaWPsKjik20ht608Asqi8TQPf0mczhEeyAtMzg==", + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/cz-conventional-changelog/-/cz-conventional-changelog-3.3.0.tgz", + "integrity": "sha512-U466fIzU5U22eES5lTNiNbZ+d8dfcHcssH4o7QsdWaCcRs/feIPCxKYSWkYBNs5mny7MvEfwpTLWjvbm94hecw==", "dev": true, "requires": { "@commitlint/load": ">6.1.1", @@ -3974,7 +3946,7 @@ }, "dicer": { "version": "0.3.0", - "resolved": "https://registry.npmjs.org/dicer/-/dicer-0.3.0.tgz", + "resolved": "http://npm.zooz.co:8083/dicer/-/dicer-0.3.0.tgz", "integrity": "sha512-MdceRRWqltEG2dZqO769g27N/3PXfcKl04VhYnBlo2YhH7zPi88VebsjTKclaOyiuMaGU72hTfw3VkUitGcVCA==", "requires": { "streamsearch": "0.1.2" @@ -4123,11 +4095,6 @@ "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=" }, - "durations": { - "version": "3.4.2", - "resolved": "https://registry.npmjs.org/durations/-/durations-3.4.2.tgz", - "integrity": "sha512-V/lf7y33dGaypZZetVI1eu7BmvkbC4dItq12OElLRpKuaU5JxQstV2zHwLv8P7cNbQ+KL1WD80zMCTx5dNC4dg==" - }, "ecc-jsbn": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", @@ -7403,11 +7370,6 @@ "integrity": "sha512-l9x0+1offnKKIzYVjyXU2SiwhXDLekRzKyhnbyldPHvC7BvLPVpdNUNR2KeMAiCN2D/kLNttZgQD5WjSxuBx3Q==", "dev": true }, - "long": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/long/-/long-2.4.0.tgz", - "integrity": "sha1-n6GAux2VAM3CnEFWdmoZleH0Uk8=" - }, "longest": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/longest/-/longest-2.0.1.tgz", @@ -9295,7 +9257,8 @@ "q": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz", - "integrity": "sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=" + "integrity": "sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=", + "dev": true }, "qs": { "version": "6.5.2", diff --git a/package.json b/package.json index 90913f243..01429a1bb 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,6 @@ "artillery": "^1.6.0-29", "bluebird": "^3.7.2", "body-parser": "^1.19.0", - "cassandra-driver": "4.3.1", - "cassandra-migration": "^2.7.0", "copy-dir": "^0.3.0", "cron": "1.7.2", "dockerode": "^2.5.8", diff --git a/src/config/databaseConfig.js b/src/config/databaseConfig.js index 0fdb48f0f..001d66312 100644 --- a/src/config/databaseConfig.js +++ b/src/config/databaseConfig.js @@ -1,23 +1,10 @@ -let cassandra = require('cassandra-driver'); const config = { type: (process.env.DATABASE_TYPE || 'SQLITE').toUpperCase(), name: process.env.DATABASE_NAME || 'predator', address: process.env.DATABASE_ADDRESS, username: process.env.DATABASE_USERNAME, password: process.env.DATABASE_PASSWORD, - cassandraReplicationFactor: process.env.CASSANDRA_REPLICATION_FACTOR || 1, - cassandraConsistency: getCassandraConsistencyByName(process.env.CASSANDRA_CONSISTENCY), - cassandraKeyspaceStrategy: process.env.CASSANDRA_KEY_SPACE_STRATEGY || 'SimpleStrategy', - cassandraLocalDataCenter: process.env.CASSANDRA_LOCAL_DATA_CENTER || 'datacenter1', sqliteStorage: process.env.SQLITE_STORAGE || 'predator' }; -function getCassandraConsistencyByName(cassandraConsistencyName) { - let consistency = cassandra.types.consistencies[cassandraConsistencyName]; - if (!consistency) { - consistency = cassandra.types.consistencies.localQuorum; - } - return consistency; -} - module.exports = config; diff --git a/src/configManager/models/database/cassandra/cassandraConnector.js b/src/configManager/models/database/cassandra/cassandraConnector.js deleted file mode 100644 index aba330533..000000000 --- a/src/configManager/models/database/cassandra/cassandraConnector.js +++ /dev/null @@ -1,75 +0,0 @@ -const logger = require('../../../../common/logger'); -const databaseConfig = require('../../../../config/databaseConfig'); -const GET_CONFIG_VALUE = 'SELECT* FROM config WHERE key= ?'; -const GET_CONFIG = 'SELECT* FROM config'; -const INSERT_DATA = 'INSERT INTO config(key, value) values(?,?)'; -const DELETE_CONFIG = 'DELETE FROM config WHERE key=?;'; - -let client; - -module.exports = { - init, - updateConfig, - getConfig, - deleteConfig, - getConfigValue -}; -function init(cassandraClient) { - client = cassandraClient; -} - -const queryOptions = { - consistency: databaseConfig.cassandraConsistency, - prepare: true -}; - -function updateConfig(updateValues) { - let queriesArr = []; - Object.keys(updateValues).forEach(key => { - let value = updateValues[key] instanceof Object ? JSON.stringify(updateValues[key]) : updateValues[key] + ''; - queriesArr.push({ query: INSERT_DATA, params: [key, value] }); - }); - return batchUpsert(queriesArr, queryOptions); -} - -function deleteConfig(key) { - return executeQuery(DELETE_CONFIG, [key]); -} - -function getConfigValue(configValue) { - return executeQuery(GET_CONFIG_VALUE, [configValue]); -} - -function getConfig() { - return executeQuery(GET_CONFIG); -} - -async function batchUpsert(queriesArr, queryOptions) { - try { - const result = await client.batch(queriesArr, queryOptions); - logger.trace('Query result', { - queryArr: queriesArr, - queryOptions: queryOptions, - rows_returned: result.rowLength - }); - return Promise.resolve(result.rows ? result.rows : []); - } catch (exception) { - logger.error(`Cassandra batch failed \n ${JSON.stringify({ queriesArr, queryOptions })}`, exception); - return Promise.reject(new Error('Error occurred in communication with cassandra')); - } -} - -async function executeQuery(query, params) { - try { - let result = await client.execute(query, params, { prepare: true }, queryOptions); - logger.trace('Query result', { - query: query, - params: params, - rows_returned: result.rowLength - }); - return Promise.resolve(result.rows ? result.rows : []); - } catch (exception) { - logger.error(`Cassandra query failed \n ${JSON.stringify({ query, params, queryOptions })}`, exception); - return Promise.reject(new Error('Error occurred in communication with cassandra')); - } -} \ No newline at end of file diff --git a/src/configManager/models/database/databaseConnector.js b/src/configManager/models/database/databaseConnector.js index 26b5b591b..b9a45e859 100644 --- a/src/configManager/models/database/databaseConnector.js +++ b/src/configManager/models/database/databaseConnector.js @@ -1,10 +1,5 @@ 'use strict'; - -const databaseConfig = require('../../../config/databaseConfig'); -const cassandraConnector = require('./cassandra/cassandraConnector'); -const sequelizeConnector = require('./sequelize/sequelizeConnector'); - -const databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; +const databaseConnector = require('./sequelize/sequelizeConnector'); module.exports = { init, diff --git a/src/database/cassandra-handler/cassandra.js b/src/database/cassandra-handler/cassandra.js deleted file mode 100644 index ace1d11f4..000000000 --- a/src/database/cassandra-handler/cassandra.js +++ /dev/null @@ -1,71 +0,0 @@ -'use strict'; - -const schedulerCassandraConnector = require('../../jobs/models/database/cassandra/cassandraConnector'); -const reportsCassandraConnector = require('../../reports/models/database/cassandra/cassandraConnector'); -const testsCassandraConnector = require('../../tests/models/database/cassandra/cassandraConnector'); -const configCassandraConnector = require('../../configManager/models/database/cassandra/cassandraConnector'); -const processorsCassandraConnector = require('../../processors/models/database/cassandra/cassandraConnector'); -const files = require('../../files/models/database/cassandra/cassandraConnector'); -const databaseConfig = require('../../config/databaseConfig'); -const cassandraMigration = require('./cassandraMigration'); -const logger = require('../../common/logger'); -const cassandra = require('cassandra-driver'); -let cassandraClient; - -module.exports.init = async () => { - cassandraClient = await createClient(); - await cassandraMigration.runMigration(); - await reportsCassandraConnector.init(cassandraClient); - await schedulerCassandraConnector.init(cassandraClient); - await testsCassandraConnector.init(cassandraClient); - await configCassandraConnector.init(cassandraClient); - await processorsCassandraConnector.init(cassandraClient); - await files.init(cassandraClient); - logger.info('cassandra client initialized'); -}; - -module.exports.ping = () => { - const query = 'SELECT * FROM system_schema.keyspaces where keyspace_name=?'; - let queryParams = [databaseConfig.name]; - return cassandraClient.execute(query, queryParams) - .then(function (results) { - if (!results.rows || results.rows.length <= 0) { - return Promise.reject(new Error('Key space wasn\'t found')); - } else { - return Promise.resolve(true); - } - }).catch(function () { - return Promise.reject(new Error('Error occurred in communication with cassandra')); - }); -}; - -module.exports.closeConnection = () => { - return new Promise((resolve, reject) => { - if (cassandraClient) { - try { - cassandraClient.shutdown(); - logger.info('Cassandra client shutdown successful'); - return resolve(); - } catch (exception) { - logger.error('Failed to close Cassandra connections' + exception); - return reject(exception); - } - } else { - logger.info('Cassandra client shutdown successful'); - return resolve(); - } - }); -}; - -async function createClient() { - const authProvider = new cassandra.auth.PlainTextAuthProvider(databaseConfig.username, databaseConfig.password); - const config = { - contactPoints: String(databaseConfig.address).split(','), - keyspace: databaseConfig.name, - authProvider, - localDataCenter: databaseConfig.cassandraLocalDataCenter - }; - - let cassandraClient = new cassandra.Client(config); - return cassandraClient; -} diff --git a/src/database/cassandra-handler/cassandraMigration.js b/src/database/cassandra-handler/cassandraMigration.js deleted file mode 100644 index e98e226d5..000000000 --- a/src/database/cassandra-handler/cassandraMigration.js +++ /dev/null @@ -1,247 +0,0 @@ -'use strict'; - -let cassandra = require('cassandra-driver'), - cassandraConfig = require('../../config/databaseConfig'), - client; -let args; -let fs = require('fs-extra'); -let cmd = require('node-cmd'); -let logger = require('../../common/logger'); -let path = require('path'); -let CREATE_KEY_SPACE_QUERY; -const CONSISTENCY_POLICY = cassandra.types.consistencies.localQuorum; -const MAX_FETCH_SIZE = 1000; -const isDevMode = process.env.DEV_MODE === 'true'; - -let initFileNameTemplate = 'cassandra_config_template.json', - initFileName = 'cassandra_config.json'; - -let cassandraHandlerLogContext = { - key_space_name: cassandraConfig.name, - initFileNameTemplate: initFileNameTemplate, - init_file_name: initFileName -}; - -const options = { - prepare: true, - consistency: cassandra.types.consistencies.localQuorum -}; - -module.exports.initArgs = initArgs; -module.exports.closeCassandraConnection = closeCassandraConnection; -module.exports.initCassandraConnection = initCassandraConnection; - -module.exports.runMigration = function () { - initArgs(); - - return initCassandraConnection() - .then(function () { - if (args.cassandra_keyspace_strategy === 'SimpleStrategy') { - CREATE_KEY_SPACE_QUERY = 'CREATE KEYSPACE IF NOT EXISTS ' + - args.key_space_name + - " WITH replication = {'class': 'SimpleStrategy', 'replication_factor':" + - args.replication_factor + '}'; - } else if (args.cassandra_keyspace_strategy === 'NetworkTopologyStrategy') { - CREATE_KEY_SPACE_QUERY = 'CREATE KEYSPACE IF NOT EXISTS ' + - args.key_space_name + - " WITH replication = {'class': 'NetworkTopologyStrategy', '" + args.cassandra_local_data_center + "' :" + - args.replication_factor + '}'; - } else { - throw new Error(args.cassandra_keyspace_strategy + ' is not supported cassandra keyspace strategy'); - } - return createKeySpaceIfNeeded(); - }) - .then(function () { - return closeCassandraConnection(); - }) - .then(function () { - return createConfigTemplateFile(); - }) - .then(function () { - if (!isDevMode) { - return runCassandraScripts(); - } - }) - .then(function () { - return removeConfigFile(); - }) - .then(function () { - initKeyspaceCassandraConnection(args.key_space_name); - cassandraHandlerLogContext = { - key_space_name: cassandraConfig.name - }; - }) - .catch(function (error) { - cassandraHandlerLogContext.initialize_cassandra_environment_error = error; - logger.error(cassandraHandlerLogContext, 'Cassandra handler: error occurred while trying to init cassandra credentials'); - process.exit(1); - }); -}; - -module.exports.ping = function (keyspace) { - return new Promise(function (resolve, reject) { - const query = 'SELECT * FROM system_schema.keyspaces where keyspace_name=?'; - let queryParams = [keyspace]; - - client.execute(query, queryParams, options) - .then(function (results) { - if (!results.rows || results.rows.length <= 0) { - return reject(new Error('Key space doesn\'t found')); - } else { - return resolve(true); - } - }).catch(function (error) { - return reject(error); - }); - }); -}; - -function initArgs() { - args = { - key_space_name: cassandraConfig.name, - cassandra_url: cassandraConfig.address, - replication_factor: cassandraConfig.cassandraReplicationFactor, - cassandra_username: cassandraConfig.username, - cassandra_password: cassandraConfig.password, - cassandra_keyspace_strategy: cassandraConfig.cassandraKeyspaceStrategy, - cassandra_local_data_center: cassandraConfig.cassandraLocalDataCenter, - root_dir: path.join(__dirname, '../../') - }; -} - -function closeCassandraConnection() { - logger.info(cassandraHandlerLogContext, 'Cassandra handler: closing cassandra connection'); - return new Promise(function (resolve, reject) { - if (client) { - client.shutdown(function (err) { - if (err) { - cassandraHandlerLogContext.client_shutdown_err = err; - logger.error(cassandraHandlerLogContext, 'Cassandra handler: failed to close Cassandra connection.'); - reject(err); - } else { - logger.info(cassandraHandlerLogContext, 'Cassandra handler: connection was closed successfully'); - resolve(); - } - }); - } else { - resolve(); - } - }); -} - -function runCassandraScripts() { - return new Promise(function (resolve, reject) { - let initCmd = path.join(args.root_dir, '../node_modules/.bin/cassandra-migration'); - let initConfigPath = path.join(args.root_dir, '/database/cassandra-handler', initFileName); - logger.info(cassandraHandlerLogContext, 'Cassandra handler: running migration scripts'); - - cmd.get( - initCmd + ' ' + initConfigPath, - function (err, data, stderr) { - if (err) { - cassandraHandlerLogContext.run_cassandra_scripts_err = err || stderr; - cassandraHandlerLogContext.run_cassandra_scripts_stderr = stderr; - logger.error(cassandraHandlerLogContext, 'Cassandra handler: failed running cassandra migration scripts'); - reject(err || stderr); - } - cassandraHandlerLogContext.run_cassandra_scripts_output = data; - logger.info(cassandraHandlerLogContext, 'Cassandra handler: successfully ran cassandra migration scripts'); - delete cassandraHandlerLogContext.run_cassandra_scripts_output; - resolve(); - } - ); - }); -} - -function removeConfigFile() { - return new Promise(function (resolve, reject) { - logger.trace(cassandraHandlerLogContext, 'Cassandra handler: removiung cassandra migration config file'); - fs.remove(path.join(args.root_dir, '/database/cassandra-handler/', initFileName), function (err) { - if (err) { - cassandraHandlerLogContext.remove_config_file_err = err; - logger.error(cassandraHandlerLogContext, 'Cassandra handler: could not remove cassandra migration config file'); - reject(err); - } - - logger.info(cassandraHandlerLogContext, 'Cassandra handler: successfully removed cassandra migration config file'); - resolve(); - }); - }); -} - -function createConfigTemplateFile() { - return new Promise(function (resolve, reject) { - let templateJsonFile = require('./' + initFileNameTemplate); - - templateJsonFile.migrationsDir = path.join(args.root_dir, '/database/cassandra-handler/init-scripts'); - templateJsonFile.cassandra.contactPoints = args.cassandra_url.split(','); - templateJsonFile.cassandra.keyspace = args.key_space_name; - templateJsonFile.auth.username = args.cassandra_username; - templateJsonFile.auth.password = args.cassandra_password; - - logger.trace(cassandraHandlerLogContext, 'Cassandra handler: set init templateJsonFile'); - - fs.writeFile(path.join(args.root_dir, 'database/cassandra-handler/', initFileName), JSON.stringify(templateJsonFile, null, 2), function (err) { - if (err) { - cassandraHandlerLogContext.remove_config_template_file_err = err; - logger.error(cassandraHandlerLogContext, 'Cassandra handler: could not write to cassandra init file'); - reject(err); - } - logger.info(cassandraHandlerLogContext, 'Cassandra handler: successfully wrote to cassandra init file'); - resolve(); - }); - }); -} - -function initCassandraConnection() { - return new Promise(function (resolve, reject) { - client = new cassandra.Client(buildClient(undefined)); - resolve(); - }); -} - -function initKeyspaceCassandraConnection(keyspace) { - return new Promise(function (resolve, reject) { - client = new cassandra.Client(buildClient(keyspace)); - - logger.info(cassandraHandlerLogContext, 'Cassandra Handler: Successfully connected to cassandra keyspace'); - resolve(); - }); -} - -function buildClient(keyspace) { - let authProvider = new cassandra.auth.PlainTextAuthProvider(args.cassandra_username, args.cassandra_password); - let cassandraClient = { - contactPoints: args.cassandra_url.split(','), - authProvider: authProvider, - localDataCenter: args.cassandra_local_data_center - }; - - if (keyspace) { - cassandraClient['keyspace'] = keyspace; - } else { - cassandraClient['queryOptions'] = { - consistency: CONSISTENCY_POLICY, - fetchSize: MAX_FETCH_SIZE - }; - } - - logger.trace(cassandraHandlerLogContext, 'Cassandra Handler: set client configuration'); - return cassandraClient; -} - -function createKeySpaceIfNeeded() { - return new Promise(function (resolve, reject) { - client.execute(CREATE_KEY_SPACE_QUERY, null, { prepare: true }, function (err) { - if (err) { - cassandraHandlerLogContext.create_key_space_query_err = err; - cassandraHandlerLogContext.create_key_space_query_inner_err = err.innerErrors; - logger.error(cassandraHandlerLogContext, 'Cassandra handler: could not create keyspace'); - reject(err); - } else { - logger.info(cassandraHandlerLogContext, 'Cassandra handler: CREATE_KEY_SPACE_QUERY executed successfully!'); - resolve(); - } - }); - }); -} diff --git a/src/database/cassandra-handler/cassandra_config_template.json b/src/database/cassandra-handler/cassandra_config_template.json deleted file mode 100644 index 6cb357ee5..000000000 --- a/src/database/cassandra-handler/cassandra_config_template.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "migrationsDir": "./migration-scripts", - "quiet": false, - "cassandra": { - "contactPoints": [""], - "keyspace": "", - "protocolOptions": { - "port": 9042 - }, - "socketOptions": { - "connectTimeout": 15000 - } - }, - "auth": { - "username": "", - "password": "" - } - } \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/10__add_file_id_test_table.cql b/src/database/cassandra-handler/init-scripts/10__add_file_id_test_table.cql deleted file mode 100644 index 811c7b914..000000000 --- a/src/database/cassandra-handler/init-scripts/10__add_file_id_test_table.cql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE tests ADD file_id uuid; diff --git a/src/database/cassandra-handler/init-scripts/11__create_files_table.cql b/src/database/cassandra-handler/init-scripts/11__create_files_table.cql deleted file mode 100644 index 4fe6dd733..000000000 --- a/src/database/cassandra-handler/init-scripts/11__create_files_table.cql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE IF NOT EXISTS files( -id text, -file text, -PRIMARY KEY (id)); \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/12__drop_last_reports_view.cql b/src/database/cassandra-handler/init-scripts/12__drop_last_reports_view.cql deleted file mode 100644 index c780e78d8..000000000 --- a/src/database/cassandra-handler/init-scripts/12__drop_last_reports_view.cql +++ /dev/null @@ -1 +0,0 @@ -DROP MATERIALIZED VIEW IF EXISTS last_reports diff --git a/src/database/cassandra-handler/init-scripts/13__create_last_reports_table.cql b/src/database/cassandra-handler/init-scripts/13__create_last_reports_table.cql deleted file mode 100644 index 14146e707..000000000 --- a/src/database/cassandra-handler/init-scripts/13__create_last_reports_table.cql +++ /dev/null @@ -1,17 +0,0 @@ -CREATE TABLE IF NOT EXISTS last_reports( -test_id uuid, -start_time_year int, -start_time_month int, -revision_id uuid, -report_id text, -test_configuration text, -last_updated_at timestamp, -start_time timestamp, -test_name text, -test_description text, -job_id text, -test_type text, -notes text, -phase text, -PRIMARY KEY ((start_time_year,start_time_month),start_time ,report_id, test_id)) -WITH CLUSTERING ORDER BY (start_time DESC); diff --git a/src/database/cassandra-handler/init-scripts/14__add_enabled_jobs_table_.cql b/src/database/cassandra-handler/init-scripts/14__add_enabled_jobs_table_.cql deleted file mode 100644 index e248f8b58..000000000 --- a/src/database/cassandra-handler/init-scripts/14__add_enabled_jobs_table_.cql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE jobs ADD enabled boolean; \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/14__create_processors_table.cql b/src/database/cassandra-handler/init-scripts/14__create_processors_table.cql deleted file mode 100644 index bce252e2f..000000000 --- a/src/database/cassandra-handler/init-scripts/14__create_processors_table.cql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE IF NOT EXISTS processors -( - id uuid, - name text, - description text, - javascript text, - created_at timestamp, - updated_at timestamp, - PRIMARY KEY (id, created_at)) - WITH CLUSTERING ORDER BY (created_at DESC); ---- -ALTER TABLE tests ADD processor_id uuid; diff --git a/src/database/cassandra-handler/init-scripts/15__create_processors_mapping_table.cql b/src/database/cassandra-handler/init-scripts/15__create_processors_mapping_table.cql deleted file mode 100644 index 5f491997c..000000000 --- a/src/database/cassandra-handler/init-scripts/15__create_processors_mapping_table.cql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE IF NOT EXISTS processors_mapping -( - name text, - id uuid, - PRIMARY KEY (name) -); diff --git a/src/database/cassandra-handler/init-scripts/17__processors_exported_functions.cql b/src/database/cassandra-handler/init-scripts/17__processors_exported_functions.cql deleted file mode 100644 index 1eac2dda1..000000000 --- a/src/database/cassandra-handler/init-scripts/17__processors_exported_functions.cql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE processors ADD exported_functions list; diff --git a/src/database/cassandra-handler/init-scripts/18__bench_mark_data_tests.cql b/src/database/cassandra-handler/init-scripts/18__bench_mark_data_tests.cql deleted file mode 100644 index e8e2c444f..000000000 --- a/src/database/cassandra-handler/init-scripts/18__bench_mark_data_tests.cql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE IF NOT EXISTS benchmarks( -test_id uuid, -data text, -PRIMARY KEY (test_id)); diff --git a/src/database/cassandra-handler/init-scripts/19__bench_mark_data_reports.cql b/src/database/cassandra-handler/init-scripts/19__bench_mark_data_reports.cql deleted file mode 100644 index 1017357c3..000000000 --- a/src/database/cassandra-handler/init-scripts/19__bench_mark_data_reports.cql +++ /dev/null @@ -1,8 +0,0 @@ -ALTER TABLE reports_summary ADD benchmark_weights_data text; ---- -ALTER TABLE reports_summary ADD score float; ---- -ALTER TABLE last_reports ADD benchmark_weights_data text; ---- -ALTER TABLE last_reports ADD score float; - diff --git a/src/database/cassandra-handler/init-scripts/1__create_jobs_table.cql b/src/database/cassandra-handler/init-scripts/1__create_jobs_table.cql deleted file mode 100644 index 62843574a..000000000 --- a/src/database/cassandra-handler/init-scripts/1__create_jobs_table.cql +++ /dev/null @@ -1,15 +0,0 @@ -CREATE TABLE IF NOT EXISTS jobs( -id uuid, -test_id uuid, -environment text, -cron_expression text, -arrival_rate int, -duration int, -ramp_to int, -webhooks list, -emails list, -parallelism int, -max_virtual_users int, -notes text, -PRIMARY KEY (id) -); \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/20__csv_file.cql b/src/database/cassandra-handler/init-scripts/20__csv_file.cql deleted file mode 100644 index d170cdb89..000000000 --- a/src/database/cassandra-handler/init-scripts/20__csv_file.cql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE files ADD name text; ---- -ALTER TABLE tests ADD csv_file_id uuid; diff --git a/src/database/cassandra-handler/init-scripts/2__create_reports_summary_table.cql b/src/database/cassandra-handler/init-scripts/2__create_reports_summary_table.cql deleted file mode 100644 index 30a43ab6b..000000000 --- a/src/database/cassandra-handler/init-scripts/2__create_reports_summary_table.cql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE IF NOT EXISTS reports_summary( -test_id uuid, -revision_id uuid, -report_id text, -test_configuration text, -last_updated_at timestamp, -start_time timestamp, -test_name text, -test_description text, -job_id text, -test_type text, -notes text, -phase text, -PRIMARY KEY (test_id, report_id)); diff --git a/src/database/cassandra-handler/init-scripts/3__create_reports_stats_table.cql b/src/database/cassandra-handler/init-scripts/3__create_reports_stats_table.cql deleted file mode 100644 index e8e5413e5..000000000 --- a/src/database/cassandra-handler/init-scripts/3__create_reports_stats_table.cql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE IF NOT EXISTS reports_stats( -runner_id text, -test_id uuid, -report_id text, -stats_id uuid, -stats_time timestamp, -phase_status text, -data text, -phase_index text, -PRIMARY KEY ((test_id, report_id), stats_time)) WITH CLUSTERING ORDER BY (stats_time ASC); \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/4__create_reports_subscribers_table.cql b/src/database/cassandra-handler/init-scripts/4__create_reports_subscribers_table.cql deleted file mode 100644 index 7f144c34b..000000000 --- a/src/database/cassandra-handler/init-scripts/4__create_reports_subscribers_table.cql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS report_subscribers( -test_id uuid, -report_id text, -runner_id text, -phase_status text, -last_stats text, -PRIMARY KEY (test_id, report_id, runner_id)); \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/5__create_last_reports_view_deprecated.cql b/src/database/cassandra-handler/init-scripts/5__create_last_reports_view_deprecated.cql deleted file mode 100644 index 2e654974d..000000000 --- a/src/database/cassandra-handler/init-scripts/5__create_last_reports_view_deprecated.cql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE MATERIALIZED VIEW last_reports_deprecated AS -SELECT * FROM reports_summary -WHERE report_id IS NOT NULL AND start_time IS NOT NULL AND test_id IS NOT NULL -PRIMARY KEY (start_time, report_id, test_id) -WITH CLUSTERING ORDER BY (start_time DESC); diff --git a/src/database/cassandra-handler/init-scripts/6__create_tests.cql b/src/database/cassandra-handler/init-scripts/6__create_tests.cql deleted file mode 100644 index fb45a58a6..000000000 --- a/src/database/cassandra-handler/init-scripts/6__create_tests.cql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE IF NOT EXISTS tests( -id uuid, -updated_at timestamp, -raw_data text, // The request as is that came from the client -artillery_json text, // The json that has been generated for the Artillery -revision_id uuid, -name text, -type text, -description text, -PRIMARY KEY (id, updated_at) -) -WITH compression = { 'sstable_compression' : 'LZ4Compressor' }; diff --git a/src/database/cassandra-handler/init-scripts/7__create_config_table.cql b/src/database/cassandra-handler/init-scripts/7__create_config_table.cql deleted file mode 100644 index dd311f31c..000000000 --- a/src/database/cassandra-handler/init-scripts/7__create_config_table.cql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE IF NOT EXISTS config( -key text, -value text, -PRIMARY KEY (key)); \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/8__dsl_table.cql b/src/database/cassandra-handler/init-scripts/8__dsl_table.cql deleted file mode 100644 index 888e157c3..000000000 --- a/src/database/cassandra-handler/init-scripts/8__dsl_table.cql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS dsl( -dsl_name text, -definition_name text, -artillery_json text, // The json that has been generated for the Artillery -PRIMARY KEY (dsl_name, definition_name) -) -WITH compression = { 'sstable_compression' : 'LZ4Compressor' }; \ No newline at end of file diff --git a/src/database/cassandra-handler/init-scripts/9__add_proxy_and_debug_jobs_table_.cql b/src/database/cassandra-handler/init-scripts/9__add_proxy_and_debug_jobs_table_.cql deleted file mode 100644 index 09bad07ef..000000000 --- a/src/database/cassandra-handler/init-scripts/9__add_proxy_and_debug_jobs_table_.cql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE jobs ADD proxy_url text; ---- -ALTER TABLE jobs ADD debug text; \ No newline at end of file diff --git a/src/database/database.js b/src/database/database.js index 6ed24fc74..27e96acf8 100644 --- a/src/database/database.js +++ b/src/database/database.js @@ -1,9 +1,8 @@ 'use strict'; -let databaseConfig = require('../config/databaseConfig'); -let cassandraConnector = require('./cassandra-handler/cassandra'); let sequelizeConnector = require('./sequlize-handler/sequlize'); -let databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; +let databaseConnector = sequelizeConnector; + module.exports.init = () => { return databaseConnector.init(); }; @@ -14,4 +13,4 @@ module.exports.ping = () => { module.exports.closeConnection = () => { return databaseConnector.closeConnection(); -}; \ No newline at end of file +}; diff --git a/src/env.js b/src/env.js index 3dadca0a6..4b2fd9c74 100644 --- a/src/env.js +++ b/src/env.js @@ -8,9 +8,7 @@ const BY_PLATFORM_MANDATORY_VARS = { }; const SUPPORTED_PLATFORMS = Object.keys(BY_PLATFORM_MANDATORY_VARS); -const SUPPORTED_CASSANDRA_STRATEGY = ['SimpleStrategy', 'NetworkTopologyStrategy']; const BY_DATABASE_MANDATORY_VARS = { - CASSANDRA: ['DATABASE_NAME', 'DATABASE_ADDRESS', 'DATABASE_USERNAME', 'DATABASE_PASSWORD'], MYSQL: ['DATABASE_NAME', 'DATABASE_ADDRESS', 'DATABASE_USERNAME', 'DATABASE_PASSWORD'], POSTGRES: ['DATABASE_NAME', 'DATABASE_ADDRESS', 'DATABASE_USERNAME', 'DATABASE_PASSWORD'], MSSQL: ['DATABASE_NAME', 'DATABASE_ADDRESS', 'DATABASE_USERNAME', 'DATABASE_PASSWORD'], @@ -49,13 +47,6 @@ env.init = function () { logger.error(missingFields, 'Missing mandatory environment variables'); process.exit(1); } - - if (process.env.CASSANDRA_KEY_SPACE_STRATEGY && !SUPPORTED_CASSANDRA_STRATEGY.includes(process.env.CASSANDRA_KEY_SPACE_STRATEGY)) { - throw new Error('CASSANDRA_KEY_SPACE_STRATEGY not one of the supported values: ' + SUPPORTED_CASSANDRA_STRATEGY); - } - if (process.env.CASSANDRA_KEY_SPACE_STRATEGY === 'NetworkTopologyStrategy' && !process.env.CASSANDRA_LOCAL_DATA_CENTER) { - throw new Error('When using CASSANDRA_KEY_SPACE_STRATEGY: NetworkTopologyStrategy, CASSANDRA_LOCAL_DATA_CENTER is mandatory'); - } }; -module.exports = env; \ No newline at end of file +module.exports = env; diff --git a/src/files/models/database.js b/src/files/models/database.js index e70b7423b..21704ee63 100644 --- a/src/files/models/database.js +++ b/src/files/models/database.js @@ -1,7 +1,6 @@ -const cassandraConnector = require('./database/cassandra/cassandraConnector'), - sequelizeConnector = require('./database/sequelize/sequelizeConnector'), - databaseConfig = require('../../config/databaseConfig'), - databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; +const sequelizeConnector = require('./database/sequelize/sequelizeConnector'); + +const databaseConnector = sequelizeConnector; module.exports = { saveFile, diff --git a/src/files/models/database/cassandra/cassandraConnector.js b/src/files/models/database/cassandra/cassandraConnector.js deleted file mode 100644 index 11e14c1ca..000000000 --- a/src/files/models/database/cassandra/cassandraConnector.js +++ /dev/null @@ -1,48 +0,0 @@ -let logger = require('../../../../common/logger'); -let cassandra = require('cassandra-driver'); -let client = {}; - -const INSERT_FILE = 'INSERT INTO files(id,name,file) values(?,?,?)'; -const GET_FILE_WITH_CONTENT = 'SELECT * FROM files WHERE id = ?'; -const GET_FILE_METADATA = 'SELECT id, name FROM files WHERE id = ?'; - -module.exports = { - init, - saveFile, - getFile -}; - -let queryOptions = { - consistency: cassandra.types.consistencies.localQuorum, - prepare: true -}; - -function init(cassandraClient) { - client = cassandraClient; -} - -async function executeQuery(query, params, queryOptions) { - try { - const result = await client.execute(query, params, queryOptions); - logger.trace('Query result', { - query: query, - params: params, - rows_returned: result.rowLength - }); - return result; - } catch (err){ - logger.error(`Cassandra query failed \n ${JSON.stringify({ query, params, queryOptions })}`, err); - throw new Error('Error occurred in communication with cassandra'); - } -} - -async function saveFile(id, fileName, fileContent) { - let params = [id, fileName, fileContent]; - const result = await executeQuery(INSERT_FILE, params, queryOptions); - return result; -} - -async function getFile(id, isIncludeContent) { - const result = await executeQuery(isIncludeContent ? GET_FILE_WITH_CONTENT : GET_FILE_METADATA, [id], queryOptions); - return result.rows[0] ? result.rows[0] : undefined; -} diff --git a/src/jobs/models/database/cassandra/cassandraConnector.js b/src/jobs/models/database/cassandra/cassandraConnector.js deleted file mode 100644 index 28984dc46..000000000 --- a/src/jobs/models/database/cassandra/cassandraConnector.js +++ /dev/null @@ -1,98 +0,0 @@ -let logger = require('../../../../common/logger'); -let databaseConfig = require('../../../../config/databaseConfig'); -let client; - -const INSERT_JOB = 'INSERT INTO jobs(id, test_id, arrival_rate, cron_expression, duration, emails, environment, ramp_to, webhooks, parallelism, max_virtual_users, notes, proxy_url, debug, enabled) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'; -const GET_JOBS = 'SELECT * FROM jobs'; -const DELETE_JOB = 'DELETE FROM jobs WHERE id=?'; -const GET_JOB = 'SELECT * FROM jobs WHERE id=?'; -const GET_COLUMNS = 'SELECT * FROM system_schema.columns WHERE keyspace_name = ? AND table_name = \'jobs\''; - -let columns; - -module.exports = { - init, - insertJob, - getJobs, - getJob, - deleteJob, - updateJob -}; - -let queryOptions = { - consistency: databaseConfig.cassandraConsistency, - prepare: true -}; - -async function init(cassandraClient) { - client = cassandraClient; -} - -function deleteJob(jobId) { - return executeQuery(DELETE_JOB, [jobId]); -} - -function getJobs() { - return executeQuery(GET_JOBS, []); -} - -function getJob(jobId) { - return executeQuery(GET_JOB, [jobId]); -} - -function insertJob(jobId, jobInfo) { - let params = [jobId, jobInfo.test_id, jobInfo.arrival_rate, jobInfo.cron_expression, jobInfo.duration, jobInfo.emails, jobInfo.environment, jobInfo.ramp_to, jobInfo.webhooks, jobInfo.parallelism, jobInfo.max_virtual_users, jobInfo.notes, jobInfo.proxy_url, jobInfo.debug, jobInfo.enabled]; - return executeQuery(INSERT_JOB, params, queryOptions); -} - -async function updateJob(jobId, jobInfo) { - let params = []; - let updateQuery = 'UPDATE jobs SET '; - - if (!columns) { - columns = await getColumns(); - } - - let error; - - Object.keys(jobInfo).forEach(function (key) { - if (!(columns.indexOf(key) <= -1)) { - updateQuery += key + '=?, '; - params.push(jobInfo[key]); - } - }); - - if (error) { - return Promise.reject(error); - } - - if (params.length > 0) { - updateQuery = updateQuery.substring(0, updateQuery.length - 2); - updateQuery += ' WHERE id=? IF EXISTS'; - params.push(jobId); - return executeQuery(updateQuery, params); - } -} - -async function getColumns() { - let getColumnsResponse = await executeQuery(GET_COLUMNS, [databaseConfig.name]); - let columns = getColumnsResponse.map(row => { - return row.column_name; - }); - columns = columns.filter(column => !(columns === 'job_id' || column === 'id')); - return columns; -} - -function executeQuery(query, params, queryOptions) { - return client.execute(query, params, { prepare: true }, queryOptions).then((result) => { - logger.trace('Query result', { - query: query, - params: params, - rows_returned: result.rowLength - }); - return Promise.resolve(result.rows ? result.rows : []); - }).catch((exception) => { - logger.error(`Cassandra query failed \n ${JSON.stringify({ query, params, queryOptions })}`, exception); - return Promise.reject(new Error('Error occurred in communication with cassandra')); - }); -} diff --git a/src/jobs/models/database/databaseConnector.js b/src/jobs/models/database/databaseConnector.js index 913ad8670..d4d301b6c 100644 --- a/src/jobs/models/database/databaseConnector.js +++ b/src/jobs/models/database/databaseConnector.js @@ -1,9 +1,6 @@ 'use strict'; -let databaseConfig = require('../../../config/databaseConfig'); -let cassandraConnector = require('./cassandra/cassandraConnector'); -let sequelizeConnector = require('./sequelize/sequelizeConnector'); -let databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; +let databaseConnector = require('./sequelize/sequelizeConnector'); module.exports = { init, @@ -41,4 +38,4 @@ async function init() { function closeConnection() { return databaseConnector.closeConnection(); -} \ No newline at end of file +} diff --git a/src/processors/models/database/cassandra/cassandraConnector.js b/src/processors/models/database/cassandra/cassandraConnector.js deleted file mode 100644 index c06f49cf1..000000000 --- a/src/processors/models/database/cassandra/cassandraConnector.js +++ /dev/null @@ -1,114 +0,0 @@ -let logger = require('../../../../common/logger'); -let databaseConfig = require('../../../../config/databaseConfig'); -let _ = require('lodash'); -let client; - -const JAVASCRIPT = 'javascript'; -const INSERT_PROCESSOR = 'INSERT INTO processors(id, name, description, javascript, exported_functions, created_at, updated_at) values(?,?,?,?,?,?,?)'; -const GET_ALL_PROCESSORS = 'SELECT * FROM processors'; -const GET_ALL_PROCESSORS_NO_JAVASCRIPT = 'SELECT id, name, description, created_at, updated_at, exported_functions FROM processors'; - -const GET_PROCESSOR_BY_ID = 'SELECT * FROM processors WHERE id=?'; -const DELETE_PROCESSOR = 'DELETE FROM processors WHERE id=?'; -const UPDATE_PROCESSOR = 'UPDATE processors SET name=?, description=?, javascript=?, exported_functions=?, updated_at=? WHERE id=? AND created_at=? IF EXISTS'; - -const INSERT_PROCESSOR_MAPPING = 'INSERT INTO processors_mapping(name, id) VALUES(?, ?)'; -const DELETE_PROCESSOR_MAPPING = 'DELETE FROM processors_mapping WHERE name=?'; -const GET_PROCESSOR_MAPPING = 'SELECT * FROM processors_mapping WHERE name=?'; - -module.exports = { - init, - insertProcessor, - getAllProcessors, - getProcessorById, - getProcessorByName, - deleteProcessor, - updateProcessor, - _queries: { - INSERT_PROCESSOR_MAPPING, - DELETE_PROCESSOR_MAPPING, - GET_PROCESSOR_MAPPING, - INSERT_PROCESSOR, - GET_ALL_PROCESSORS, - GET_PROCESSOR_BY_ID, - DELETE_PROCESSOR, - UPDATE_PROCESSOR - } -}; - -let queryOptions = { - consistency: databaseConfig.cassandraConsistency, - prepare: true -}; - -async function init(cassandraClient) { - client = cassandraClient; -} - -async function getAllProcessors(from, limit, exclude) { - let query = GET_ALL_PROCESSORS; - if (exclude && (exclude === JAVASCRIPT || exclude.includes(JAVASCRIPT))) { - query = GET_ALL_PROCESSORS_NO_JAVASCRIPT; - } - const resultRows = await executeQuery(query, [], queryOptions); - return _(resultRows).slice(from).take(limit).value(); -} - -async function getProcessorByName(processorName) { - const [processorMapping] = await executeQuery(GET_PROCESSOR_MAPPING, [processorName], queryOptions); - if (processorMapping) { - return getProcessorById(processorMapping.id); - } -} - -async function getProcessorById(processorId) { - const processor = await executeQuery(GET_PROCESSOR_BY_ID, [processorId], queryOptions); - return processor[0]; -} - -async function deleteProcessor(processorId) { - let params = [processorId]; - let processor = await getProcessorById(processorId); - if (processor) { - let mappingParams = [processor.name]; - return Promise.all([ - executeQuery(DELETE_PROCESSOR, params, queryOptions), - executeQuery(DELETE_PROCESSOR_MAPPING, mappingParams, queryOptions) - ]); - } -} - -async function insertProcessor(processorId, processorInfo) { - let params = [processorId, processorInfo.name, processorInfo.description, processorInfo.javascript, processorInfo.exported_functions, Date.now(), Date.now()]; - let mappingParams = [processorInfo.name, processorId]; - const [processor] = await Promise.all([ - executeQuery(INSERT_PROCESSOR, params, queryOptions), - executeQuery(INSERT_PROCESSOR_MAPPING, mappingParams, queryOptions) - ]); - return processor; -} - -async function updateProcessor(processorId, updatedProcessor) { - const { name, description, javascript, exported_functions, created_at: createdAt } = updatedProcessor; - const processor = await getProcessorById(processorId); - const params = [ name, description, javascript, exported_functions, Date.now(), processorId, createdAt.getTime() ]; - return Promise.all([ - executeQuery(UPDATE_PROCESSOR, params, queryOptions), - executeQuery(INSERT_PROCESSOR_MAPPING, [updatedProcessor.name, processorId]), - executeQuery(DELETE_PROCESSOR_MAPPING, [processor.name]) - ]); -} - -function executeQuery(query, params, queryOptions) { - return client.execute(query, params, { prepare: true }, queryOptions).then((result) => { - logger.trace('Query result', { - query: query, - params: params, - rows_returned: result.rowLength - }); - return Promise.resolve(result.rows ? result.rows : []); - }).catch((exception) => { - logger.error(`Cassandra query failed \n ${JSON.stringify({ query, params, queryOptions })}`, exception); - return Promise.reject(new Error('Error occurred in communication with cassandra')); - }); -} diff --git a/src/processors/models/database/databaseConnector.js b/src/processors/models/database/databaseConnector.js index d9bccd187..68e703217 100644 --- a/src/processors/models/database/databaseConnector.js +++ b/src/processors/models/database/databaseConnector.js @@ -1,7 +1,5 @@ -let databaseConfig = require('../../../config/databaseConfig'); -let cassandraConnector = require('./cassandra/cassandraConnector'); let sequelizeConnector = require('./sequelize/sequelizeConnector'); -let databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; +let databaseConnector = sequelizeConnector; module.exports = { init, getAllProcessors, diff --git a/src/reports/models/database/cassandra/cassandraConnector.js b/src/reports/models/database/cassandra/cassandraConnector.js deleted file mode 100644 index 34766aa72..000000000 --- a/src/reports/models/database/cassandra/cassandraConnector.js +++ /dev/null @@ -1,244 +0,0 @@ -'use strict'; -const databaseConfig = require('../../../../config/databaseConfig'), - dateUtil = require('../../../utils/dateUtil'), - _ = require('lodash'), - constants = require('../../../utils/constants'); -const logger = require('../../../../common/logger'); -let client; -const isRowAppliedField = '[applied]'; -const INSERT_REPORT_SUMMARY = 'INSERT INTO reports_summary(test_id, revision_id, report_id, job_id, test_type, phase, start_time, test_name, test_description, test_configuration, notes, last_updated_at) values(?,?,?,?,?,?,?,?,?,?,?,?) IF NOT EXISTS'; -const INSERT_LAST_REPORT_SUMMARY = 'INSERT INTO last_reports(start_time_year,start_time_month,test_id, revision_id, report_id, job_id, test_type, phase, start_time, test_name, test_description, test_configuration, notes, last_updated_at) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?) IF NOT EXISTS'; -const UPDATE_REPORT_BENCHMARK = 'UPDATE reports_summary SET score=?, benchmark_weights_data=? WHERE test_id=? AND report_id=?'; -const DELETE_REPORT_SUMMARY = 'DELETE from reports_summary WHERE test_id=? AND report_id=?'; -const GET_REPORT_SUMMARY = 'SELECT * FROM reports_summary WHERE test_id=? AND report_id=?'; -const GET_REPORTS_SUMMARIES = 'SELECT * FROM reports_summary WHERE test_id=?'; -const GET_LAST_SUMMARIES = 'SELECT * FROM last_reports WHERE start_time_year=? AND start_time_month=? LIMIT ?'; -const INSERT_REPORT_STATS = 'INSERT INTO reports_stats(runner_id, test_id, report_id, stats_id, stats_time, phase_index, phase_status, data) values(?,?,?,?,?,?,?,?)'; -const GET_REPORT_STATS = 'SELECT * FROM reports_stats WHERE test_id=? AND report_id=?'; -const SUBSCRIBE_RUNNER = 'INSERT INTO report_subscribers(test_id, report_id, runner_id, phase_status) values(?,?,?,?)'; -const UPDATE_SUBSCRIBER_WITH_STATS = 'UPDATE report_subscribers SET phase_status=?, last_stats=? WHERE test_id=? AND report_id=? AND runner_id=?'; -const UPDATE_SUBSCRIBER = 'UPDATE report_subscribers SET phase_status=? WHERE test_id=? AND report_id=? AND runner_id=?'; -const GET_REPORT_SUBSCRIBERS = 'SELECT * FROM report_subscribers WHERE test_id=? AND report_id=?'; - -module.exports = { - init, - insertReport, - updateReport, - deleteReport, - getReport, - getReports, - getLastReports, - insertStats, - getStats, - subscribeRunner, - updateSubscriberWithStats, - updateSubscriber, - updateReportBenchmark -}; - -let queryOptions = { - consistency: databaseConfig.cassandraConsistency, - prepare: true -}; - -async function init(cassandraClient) { - client = cassandraClient; -} - -async function insertReport(testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, notes, lastUpdatedAt) { - let params; - const testNotes = notes || ''; - params = [testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, testNotes, lastUpdatedAt]; - const result = await executeQuery(INSERT_REPORT_SUMMARY, params, queryOptions); - if (result[0][isRowAppliedField]) { - insertLastReportAsync(testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, notes, lastUpdatedAt); - } - return result; -} - -function insertLastReportAsync(testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, notes, lastUpdatedAt) { - let params; - const testNotes = notes || ''; - const startTimeDate = new Date(startTime); - const startTimeYear = startTimeDate.getFullYear(); - const startTimeMonth = startTimeDate.getMonth() + 1; - params = [startTimeYear, startTimeMonth, testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, testNotes, lastUpdatedAt]; - return executeQuery(INSERT_LAST_REPORT_SUMMARY, params, queryOptions) - .catch(err => logger.error(`Cassandra insertLastReportAsync failed \n ${JSON.stringify({ - INSERT_LAST_REPORT_SUMMARY, - params, - queryOptions - })}`, err)); -} - -async function updateReport(testId, reportId, reportData) { - const UPDATE_REPORT_SUMMARY = 'UPDATE reports_summary'; - const where = 'WHERE test_id=? AND report_id=?'; - const queryData = buildUpdateQuery(UPDATE_REPORT_SUMMARY, reportData, where, [testId, reportId]); - - updateLastReportAsync(testId, reportId, reportData); - return executeQuery(queryData.query, queryData.params, queryOptions); -} - -async function deleteReport(testId, reportId) { - const reportToDelete = await executeQuery(GET_REPORT_SUMMARY, [testId, reportId], queryOptions); - - const startTime = reportToDelete[0].start_time; - const startTimeDate = new Date(startTime); - const startTimeYear = startTimeDate.getFullYear(); - const startTimeMonth = startTimeDate.getMonth() + 1; - const where = 'WHERE start_time_year=? AND start_time_month=? AND start_time=? AND test_id=? AND report_id=?'; - const whereParams = [startTimeYear, startTimeMonth, startTime, testId, reportId]; - const deleteLastReport = 'DELETE from last_reports'; - - await executeQuery(`${deleteLastReport} ${where}`, whereParams, queryOptions); - await executeQuery(DELETE_REPORT_SUMMARY, [testId, reportId], queryOptions); -} - -function buildUpdateQuery(baseQuery, values, where, whereDataArray) { - const entriesValues = Object.entries(values); - const params = entriesValues.map((entry) => entry[1]).concat(whereDataArray); - const setStatement = `SET ${entriesValues.map((entry) => `${entry[0]}=?`).join(', ')}`; - const query = `${baseQuery} ${setStatement} ${where}`; - - return { - query, - params - }; -} - -async function updateReportBenchmark(testId, reportId, score, benchmarkData) { - const reportData = { - score, - benchmark_weights_data: benchmarkData - }; - updateLastReportAsync(testId, reportId, reportData); - const res = await executeQuery(UPDATE_REPORT_BENCHMARK, [score, benchmarkData, testId, reportId], { prepare: true }); - return res; -} - -async function updateLastReportAsync(testId, reportId, reportData) { - let queryData = {}; - try { - const reportToUpdate = await executeQuery(GET_REPORT_SUMMARY, [testId, reportId], queryOptions); - const startTime = reportToUpdate[0].start_time; - const startTimeDate = new Date(startTime); - const startTimeYear = startTimeDate.getFullYear(); - const startTimeMonth = startTimeDate.getMonth() + 1; - - const where = 'WHERE start_time_year=? AND start_time_month=? AND start_time=? AND test_id=? AND report_id=?'; - const whereParams = [startTimeYear, startTimeMonth, startTime, testId, reportId]; - const UPDATE_LAST_REPORT_SUMMARY = 'UPDATE last_reports'; - - queryData = buildUpdateQuery(UPDATE_LAST_REPORT_SUMMARY, reportData, where, whereParams); - - await executeQuery(queryData.query, queryData.params, queryOptions); - } catch (err) { - logger.error(`Cassandra updateLastReportAsync failed \n ${JSON.stringify({ - query: queryData.query, - params: queryData.params, - queryOptions - })}`, err); - } -} - -function getReport(testId, reportId) { - let params; - params = [testId, reportId]; - return getReportsWIthSubscribers(GET_REPORT_SUMMARY, params, queryOptions); -} - -function getReports(testId) { - let params; - params = [testId]; - return getReportsWIthSubscribers(GET_REPORTS_SUMMARIES, params, queryOptions); -} - -function getLastReports(limit) { - return getLastReportsWIthSubscribers(limit); -} - -function insertStats(runnerId, testId, reportId, statId, statsTime, phaseIndex, phaseStatus, data) { - let params; - params = [runnerId, testId, reportId, statId, statsTime, phaseIndex, phaseStatus, data]; - return executeQuery(INSERT_REPORT_STATS, params, queryOptions); -} - -function getStats(testId, reportId) { - let params; - params = [testId, reportId]; - return executeQuery(GET_REPORT_STATS, params, queryOptions); -} - -function subscribeRunner(testId, reportId, runnerId, phaseStatus) { - let params; - params = [testId, reportId, runnerId, phaseStatus]; - return executeQuery(SUBSCRIBE_RUNNER, params, queryOptions); -} - -async function updateSubscriberWithStats(testId, reportId, runnerId, phaseStatus, lastStats) { - let params; - params = [phaseStatus, lastStats, testId, reportId, runnerId]; - return executeQuery(UPDATE_SUBSCRIBER_WITH_STATS, params, queryOptions); -} - -async function updateSubscriber(testId, reportId, runnerId, phaseStatus) { - let params; - params = [phaseStatus, testId, reportId, runnerId]; - return executeQuery(UPDATE_SUBSCRIBER, params, queryOptions); -} - -function getReportSubscribers(testId, reportId) { - let params; - params = [testId, reportId]; - return executeQuery(GET_REPORT_SUBSCRIBERS, params, queryOptions); -} - -function executeQuery(query, params, queryOptions) { - return client.execute(query, params, queryOptions).then((result) => { - logger.trace('Query result', { - query: query, - params: params, - rows_returned: result.rowLength - }); - return Promise.resolve(result.rows ? result.rows : []); - }).catch((exception) => { - logger.error(`Cassandra query failed \n ${JSON.stringify({ query, params, queryOptions })}`, exception); - return Promise.reject(new Error('Error occurred in communication with cassandra')); - }); -} - -async function getReportsWIthSubscribers(query, params, queryOptions) { - const reports = await executeQuery(query, params, queryOptions); - let reportsWithSubscribers = joinReportsWIthSubscribers(reports); - return reportsWithSubscribers; -} - -async function getLastReportsWIthSubscribers(limit) { - let lastReportsPromise = []; - for (let i = 0; i < constants.MAX_MONTH_OF_LAST_REPORTS; i++) { - const date = dateUtil.dateXMonthAgo(i); - lastReportsPromise.push(executeQuery(GET_LAST_SUMMARIES, [date.year, date.month, limit], queryOptions)); - } - const reportsResult = await Promise.all(lastReportsPromise); - const allReports = _(reportsResult).flatMap(value => value).value().slice(0, limit); - let reportsWIthSubscribers = joinReportsWIthSubscribers(allReports); - return reportsWIthSubscribers; -} - -async function joinReportsWIthSubscribers(reports) { - let subscribers, report; - for (let reportIndex = 0; reportIndex < reports.length; reportIndex++) { - report = reports[reportIndex]; - subscribers = await getReportSubscribers(report.test_id, report.report_id); - subscribers = subscribers.map((subscriber) => { - return { - runner_id: subscriber.runner_id, - phase_status: subscriber.phase_status, - last_stats: JSON.parse(subscriber.last_stats) - }; - }); - report.subscribers = subscribers; - } - return reports; -} diff --git a/src/reports/models/databaseConnector.js b/src/reports/models/databaseConnector.js index 4d20dcf2c..fe8dad43e 100644 --- a/src/reports/models/databaseConnector.js +++ b/src/reports/models/databaseConnector.js @@ -1,9 +1,7 @@ 'use strict'; -let databaseConfig = require('../../config/databaseConfig'); -let cassandraConnector = require('./database/cassandra/cassandraConnector'); let sequelizeConnector = require('./database/sequelize/sequelizeConnector'); -let databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; +let databaseConnector = sequelizeConnector; module.exports = { insertReport, diff --git a/src/tests/models/database.js b/src/tests/models/database.js index 1d4c85f61..ffea7beb8 100644 --- a/src/tests/models/database.js +++ b/src/tests/models/database.js @@ -1,7 +1,6 @@ -const cassandraConnector = require('./database/cassandra/cassandraConnector'), - sequelizeConnector = require('./database/sequelize/sequelizeConnector'), - databaseConfig = require('../../config/databaseConfig'), - databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; +const sequelizeConnector = require('./database/sequelize/sequelizeConnector'); + +const databaseConnector = sequelizeConnector; module.exports = { insertTest, diff --git a/src/tests/models/database/cassandra/cassandraConnector.js b/src/tests/models/database/cassandra/cassandraConnector.js deleted file mode 100644 index 9deac8520..000000000 --- a/src/tests/models/database/cassandra/cassandraConnector.js +++ /dev/null @@ -1,153 +0,0 @@ -let logger = require('../../../../common/logger'); -let cassandra = require('cassandra-driver'); -let client = {}; -let uuid = require('cassandra-driver').types.Uuid; -const sanitizeHelper = require('../../../helpers/sanitizeHelper'); - -const INSERT_TEST_DETAILS = 'INSERT INTO tests(id, name, description, type, updated_at, raw_data, artillery_json, revision_id, file_id, csv_file_id, processor_id) values(?,?,?,?,?,?,?,?,?,?,?)'; -const GET_TEST = 'SELECT * FROM tests WHERE id = ? ORDER BY updated_at DESC limit 1'; -const GET_TEST_REVISIONS = 'SELECT * FROM tests WHERE id = ?'; -const GET_TESTS = 'SELECT * FROM tests'; -const DELETE_TEST = 'DELETE FROM tests WHERE id=?'; -const INSERT_BENCHMARK_DATA_TEST = 'INSERT INTO benchmarks(test_id,data) values(?,?)'; -const GET_BENCHMARK_DATA_TEST = 'SELECT * FROM benchmarks WHERE test_id=?'; - -const INSERT_DSL_DEFINITION_IF_NOT_EXIST = 'INSERT INTO dsl(dsl_name, definition_name, artillery_json) values(?,?,?) IF NOT EXISTS'; -const UPDATE_DSL_DEFINITION = 'UPDATE dsl SET artillery_json= ? WHERE dsl_name = ? AND definition_name = ? IF EXISTS;'; -const DELETE_DSL_DEFINITION = 'DELETE FROM dsl WHERE dsl_name = ? AND definition_name = ? IF EXISTS;'; -const GET_DSL_DEFINITION = 'SELECT * FROM dsl WHERE dsl_name = ? AND definition_name = ? limit 1'; -const GET_DSL_DEFINITIONS = 'SELECT * FROM dsl WHERE dsl_name = ?'; - -module.exports = { - init, - insertTest, - getAllTestRevisions, - getTest, - getTests, - deleteTest, - insertDslDefinition, - getDslDefinition, - getDslDefinitions, - updateDslDefinition, - deleteDefinition, - insertTestBenchmark, - getTestBenchmark -}; - -let queryOptions = { - consistency: cassandra.types.consistencies.localQuorum, - prepare: true -}; - -function init(cassandraClient) { - client = cassandraClient; -} - -async function getTest(id) { - id = uuid.fromString(id); - const result = await executeQuery(GET_TEST, [id], queryOptions); - const sanitizedResult = sanitizeTestResult(result.rows)[0]; - return sanitizedResult; -} - -async function getTests() { - const result = await executeQuery(GET_TESTS, [], queryOptions); - const sanitizedResult = sanitizeTestResult(result.rows); - return sanitizedResult; -} - -async function deleteTest(testId){ - const result = await executeQuery(DELETE_TEST, [testId]); - return result; -} - -async function insertTestBenchmark(testId, benchmarkData) { - testId = uuid.fromString(testId); - const result = await executeQuery(INSERT_BENCHMARK_DATA_TEST, [testId, benchmarkData]); - return result; -} - -async function getTestBenchmark(testId) { - const result = await executeQuery(GET_BENCHMARK_DATA_TEST, [testId]); - return result.rows.length > 0 ? result.rows[0].data : undefined; -} - -async function getAllTestRevisions(id) { - id = uuid.fromString(id); - const result = await executeQuery(GET_TEST_REVISIONS, [id], queryOptions); - const sanitizedResult = await sanitizeTestResult(result.rows); - return sanitizedResult; -} - -async function insertTest(testInfo, testJson, id, revisionId, processorFileId) { - let params; - params = [id, testInfo.name, testInfo.description, testInfo.type, Date.now(), JSON.stringify(testInfo), JSON.stringify(testJson), revisionId, processorFileId, testInfo.csv_file_id, testInfo.processor_id]; - const result = await executeQuery(INSERT_TEST_DETAILS, params, queryOptions); - return result; -} - -async function getDslDefinition(dslName, definitionName) { - const params = [dslName, definitionName]; - const result = await executeQuery(GET_DSL_DEFINITION, params, queryOptions); - const sanitizedResult = sanitizeDslResult(result.rows); - return sanitizedResult[0]; -} -async function getDslDefinitions(dslName) { - const params = [dslName]; - const result = await executeQuery(GET_DSL_DEFINITIONS, params, queryOptions); - const sanitizedResult = sanitizeDslResult(result.rows); - return sanitizedResult; -} - -async function insertDslDefinition(dslName, definitionName, data) { - const params = [dslName, definitionName, JSON.stringify(data)]; - const result = await executeQuery(INSERT_DSL_DEFINITION_IF_NOT_EXIST, params, queryOptions); - return result.rows[0]['[applied]']; -} - -async function updateDslDefinition(dslName, definitionName, data) { - const params = [JSON.stringify(data), dslName, definitionName]; - const result = await executeQuery(UPDATE_DSL_DEFINITION, params, queryOptions); - return result.rows[0]['[applied]']; -} -async function deleteDefinition(dslName, definitionName) { - const params = [dslName, definitionName]; - const result = await executeQuery(DELETE_DSL_DEFINITION, params, queryOptions); - return result.rows[0]['[applied]']; -} - -async function executeQuery(query, params, queryOptions) { - try { - const result = await client.execute(query, params, queryOptions); - logger.trace('Query result', { - query: query, - params: params, - rows_returned: result.rowLength - }); - return result; - } catch (err){ - logger.error(`Cassandra query failed \n ${JSON.stringify({ query, params, queryOptions })}`, err); - throw new Error('Error occurred in communication with cassandra'); - } -} - -function sanitizeTestResult(data) { - const result = data.map(function (row) { - const dslDataObject = sanitizeHelper.extractDslRootData(row.raw_data); - row.artillery_json = row.artillery_json ? JSON.parse(row.artillery_json) : undefined; - row.file_id = row.file_id || undefined; - row.csv_file_id = row.csv_file_id || undefined; - row.processor_id = row.processor_id || undefined; - delete row.raw_data; - return Object.assign(row, dslDataObject); - }); - return result; -} - -function sanitizeDslResult(data) { - const result = data.map(function (row) { - row.artillery_json = JSON.parse(row.artillery_json); - return row; - }); - return result; -} diff --git a/src/tests/models/dsl.js b/src/tests/models/dsl.js index 24c2c91f3..4f7dc18e4 100644 --- a/src/tests/models/dsl.js +++ b/src/tests/models/dsl.js @@ -38,7 +38,7 @@ async function createDefinition(dslName, body) { utils.addDefaultsToStep(body.request); const result = await database.insertDslDefinition(dslName, body.name, body.request); if (result){ - logger.info(body, 'Definition created successfully and saved to Cassandra'); + logger.info(body, 'Definition created successfully and saved to db'); return { name: body.name, request: body.request @@ -54,7 +54,7 @@ async function updateDefinition(dslName, definitionName, body) { utils.addDefaultsToStep(body.request); const result = await database.updateDslDefinition(dslName, definitionName, body.request); if (result){ - logger.info(body, 'Definition updated successfully and saved to Cassandra'); + logger.info(body, 'Definition updated successfully and saved to db'); return { name: body.name, request: body.request @@ -68,7 +68,7 @@ async function updateDefinition(dslName, definitionName, body) { async function deleteDefinition(dslName, definitionName, body) { const result = await database.deleteDefinition(dslName, definitionName); if (result){ - logger.info(body, 'Definition deleted successfully and saved to Cassandra'); + logger.info(body, 'Definition deleted successfully and saved to db'); } else { const error = new Error(ERROR_MESSAGES.NOT_FOUND); error.statusCode = 404; diff --git a/tests/configurations/cassandraConfiguration.sh b/tests/configurations/cassandraConfiguration.sh deleted file mode 100755 index 7668de4ec..000000000 --- a/tests/configurations/cassandraConfiguration.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -e - -export DATABASE_ADDRESS=$RUNNER_IP:9042 -export DATABASE_NAME=cassandra_keyspace -export DATABASE_USERNAME=root -export DATABASE_PASSWORD=password -export REPLICATION_FACTOR=1 -export DATABASE_TYPE=CASSANDRA \ No newline at end of file diff --git a/tests/configurations/dockerRun.sh b/tests/configurations/dockerRun.sh index 5b01362b3..4bef518a3 100755 --- a/tests/configurations/dockerRun.sh +++ b/tests/configurations/dockerRun.sh @@ -72,26 +72,6 @@ function postgres() { echo "$APP is ready" } -function cassandra() { - IMAGE_NAME=cassandra:3.11 - APP=cassandra - stop $APP - COMMAND="docker run \ - -d \ - --name $APP \ - -p 9042:9042 \ - $IMAGE_NAME" - echo -e "Starting $APP\n"${COMMAND/\s+/ } - $COMMAND - COMMAND_EXIT_CODE=$? - if [ ${COMMAND_EXIT_CODE} != 0 ]; then - printf "Error when executing: '${APP}'\n" - exit ${COMMAND_EXIT_CODE} - fi - waitForApp $APP "Created default superuser role 'cassandra'" - echo "$APP is ready" -} - function mailhog() { IMAGE_NAME=mailhog/mailhog APP=mailhog @@ -130,9 +110,6 @@ for option in ${@}; do postgres) postgres ;; - cassandra) - cassandra - ;; reporter) reporter ;; @@ -146,7 +123,7 @@ for option in ${@}; do stop ;; *) - echo "Usage: ./dockerRun.sh " + echo "Usage: ./dockerRun.sh " ;; esac done diff --git a/tests/integration-tests/runLocal.sh b/tests/integration-tests/runLocal.sh index d189d4f9f..9865edd15 100755 --- a/tests/integration-tests/runLocal.sh +++ b/tests/integration-tests/runLocal.sh @@ -4,5 +4,3 @@ LOCAL_TEST=true DATABASE_TYPE=mysql JOB_PLATFORM=kubernetes ./tests/integration- LOCAL_TEST=true DATABASE_TYPE=sqlite JOB_PLATFORM=kubernetes ./tests/integration-tests/run.sh LOCAL_TEST=true DATABASE_TYPE=postgres JOB_PLATFORM=metronome ./tests/integration-tests/run.sh LOCAL_TEST=true DATABASE_TYPE=sqlite JOB_PLATFORM=docker ./tests/integration-tests/run.sh - - diff --git a/tests/unit-tests/cassandra-handler/cassandraHandler-test.js b/tests/unit-tests/cassandra-handler/cassandraHandler-test.js deleted file mode 100644 index a1cc9b12b..000000000 --- a/tests/unit-tests/cassandra-handler/cassandraHandler-test.js +++ /dev/null @@ -1,509 +0,0 @@ -// 'use strict'; -// -// let sinon = require('sinon'); -// let rewire = require('rewire'); -// let chai = require('chai'); -// let expect = chai.expect; -// let chaiSinon = require('chai-sinon'); -// chai.use(chaiSinon); -// let logger = require('../../../src/helpers/logger'); -// let cassandra = require('cassandra-driver'); -// let cassandraConfig = require('../../../src/config/databaseConfig'); -// let cassandraHandler = rewire('../../../src/cassandra-handler/cassandraHandler'); -// -// describe('cassandra handler tests', function () { -// let sandbox; -// cassandraConfig.address = 'localhost:9042'; -// cassandraConfig.username = 'username'; -// cassandraConfig.password = 'password'; -// cassandraConfig.name = 'predator'; -// -// before(function () { -// sandbox = sinon.sandbox.create(); -// }); -// -// afterEach(function () { -// sandbox.restore(); -// }); -// -// describe('cassandra ping tests', function () { -// let clientExecuteStub; -// -// beforeEach(function () { -// sandbox.reset(); -// let client = { -// execute: sinon.stub() -// }; -// -// let stubClient = sandbox.stub(cassandra, 'Client'); -// stubClient.returns(client); -// clientExecuteStub = client.execute; -// }); -// -// it('ping is ok, cassandra is up', function (done) { -// clientExecuteStub.resolves({rows: [{}]}); -// -// cassandraHandler.initArgs(); -// cassandraHandler.initCassandraConnection() -// .then(function () { -// return cassandraHandler.ping('keyspace'); -// }).then(function (result) { -// expect(result).to.be.true; -// done(); -// }).catch(function (error) { -// done(error); -// }); -// }); -// -// it('ping rejects as no schema, cassandra is down', function (done) { -// clientExecuteStub.resolves({rows: []}); -// -// cassandraHandler.initArgs(); -// cassandraHandler.initCassandraConnection() -// .then(function () { -// return cassandraHandler.ping('keyspace'); -// }).then(function () { -// done('Expecting test to fail'); -// }).catch(function (error) { -// expect(error.message).to.eql('Key space doesn\'t found'); -// done(); -// }); -// }); -// -// it('cassandra rejects with error, cassandra is down', function (done) { -// clientExecuteStub.rejects({message: 'failure'}); -// -// cassandraHandler.initArgs(); -// cassandraHandler.initCassandraConnection() -// .then(function () { -// return cassandraHandler.ping('keyspace'); -// }).then(function () { -// done('Expecting test to fail'); -// }).catch(function (error) { -// expect(error.message).to.eql('failure'); -// done(); -// }); -// }); -// }); -// -// describe('initializeCassandraEnvironment tests', function () { -// beforeEach(function () { -// cassandraConfig.address = 'localhost:9042'; -// cassandraConfig.username = 'cassandra'; -// cassandraConfig.password = 'cassandra'; -// let cassandraHandlerLogContext = { -// 'x-zooz-request-id': 'service-startup', -// 'key_space_name': cassandraConfig.name, -// 'init_file_name_template': 'cassandra_config_template.json', -// 'init_file_name': 'cassandra_config.json' -// }; -// cassandraHandler.__set__('cassandraHandlerLogContext', cassandraHandlerLogContext); -// }); -// -// afterEach(function () { -// sandbox.restore(); -// }); -// -// describe('createKeySpaceIfNeeded rejects', function () { -// it('Should exit the process and log the error', function (done) { -// let stubAuthProvider = sandbox.stub(cassandra.auth, 'PlainTextAuthProvider'); -// stubAuthProvider.returns({ -// 'test': 'test' -// }); -// -// let client = { -// execute: sinon.stub(), -// shutdown: sinon.stub() -// }; -// -// let clientExecuteStub = client.execute; -// let error = { -// innerErrors: 'client execute error' -// }; -// clientExecuteStub.yields(error); -// -// let clientShutdownStub = client.shutdown; -// clientShutdownStub.yields(undefined); -// -// let stubClient = sandbox.stub(cassandra, 'Client'); -// stubClient.returns(client); -// -// let fs = { -// writeFile: sinon.stub(), -// remove: sinon.stub() -// }; -// -// let fsStub = fs.writeFile; -// cassandraHandler.__set__('fs', fs); -// fsStub.yields(null); -// -// let fsRemoveStub = fs.remove; -// fsRemoveStub.yields(null); -// -// let cmd = { -// get: sinon.stub() -// }; -// -// let cmdStub = cmd.get; -// cassandraHandler.__set__('cmd', cmd); -// cmdStub.yields(null, null, null); -// -// let stubErrorLogger = sinon.stub(logger, 'error'); -// -// let processStub = { -// exit: sinon.stub() -// }; -// -// let exitStub = processStub.exit; -// exitStub.resolves(); -// cassandraHandler.__set__('process', processStub); -// -// let initializeCassandraEnvironmentError = { -// 'x-zooz-request-id': 'service-startup', -// 'key_space_name': 'predator', -// 'init_file_name_template': 'cassandra_config_template.json', -// 'init_file_name': 'cassandra_config.json', -// 'create_key_space_query_err': { -// 'innerErrors': 'client execute error' -// }, -// 'create_key_space_query_inner_err': 'client execute error', -// 'initialize_cassandra_environment_error': { -// 'innerErrors': 'client execute error' -// } -// }; -// -// cassandraHandler.initializeCassandraEnvironment().then(function () { -// expect(stubAuthProvider.calledWith('cassandra', 'cassandra')).equal(true); -// expect(stubAuthProvider).to.have.been.calledOnce; -// expect(stubClient).to.have.been.calledOnce; -// expect(fsStub).to.have.not.been.calledOnce; -// expect(fsRemoveStub).to.have.not.been.calledOnce; -// expect(cmdStub).to.have.not.been.calledOnce; -// expect(clientExecuteStub).to.have.been.calledOnce; -// expect(clientShutdownStub).to.have.not.been.calledOnce; -// expect(stubErrorLogger.calledWith(initializeCassandraEnvironmentError, 'Cassandra handler: could not create keyspace')).to.be.true; -// expect(stubErrorLogger.calledWith(initializeCassandraEnvironmentError, 'Cassandra handler: error occurred while trying to init cassandra credentials')).to.be.true; -// expect(exitStub).to.have.been.calledOnce; -// stubErrorLogger.restore(); -// done(); -// }).catch(function (error) { -// done(error); -// }); -// }); -// }); -// describe('closeCassandraConnection rejects', function () { -// it('Should exit the process and log the error', function (done) { -// let stubAuthProvider = sandbox.stub(cassandra.auth, 'PlainTextAuthProvider'); -// stubAuthProvider.returns({ -// 'test': 'test' -// }); -// -// let client = { -// execute: sinon.stub(), -// shutdown: sinon.stub() -// }; -// -// let clientExecuteStub = client.execute; -// let error = { -// innerErrors: 'client execute error' -// }; -// clientExecuteStub.yields(undefined); -// -// let clientShutdownStub = client.shutdown; -// clientShutdownStub.yields(error); -// -// let stubClient = sandbox.stub(cassandra, 'Client'); -// stubClient.returns(client); -// -// let fs = { -// writeFile: sinon.stub(), -// remove: sinon.stub() -// }; -// -// let fsStub = fs.writeFile; -// cassandraHandler.__set__('fs', fs); -// fsStub.yields(null); -// -// let fsRemoveStub = fs.remove; -// fsRemoveStub.yields(null); -// -// let cmd = { -// get: sinon.stub() -// }; -// -// let cmdStub = cmd.get; -// cassandraHandler.__set__('cmd', cmd); -// cmdStub.yields(null, null, null); -// -// let stubErrorLogger = sinon.stub(logger, 'error'); -// -// let processStub = { -// exit: sinon.stub() -// }; -// -// let exitStub = processStub.exit; -// exitStub.resolves(); -// cassandraHandler.__set__('process', processStub); -// let initializeCassandraEnvironmentError = { -// 'x-zooz-request-id': 'service-startup', -// 'key_space_name': 'predator', -// 'init_file_name_template': 'cassandra_config_template.json', -// 'init_file_name': 'cassandra_config.json', -// 'client_shutdown_err': { -// 'innerErrors': 'client execute error' -// }, -// 'initialize_cassandra_environment_error': { -// 'innerErrors': 'client execute error' -// } -// }; -// -// cassandraHandler.initializeCassandraEnvironment().then(function () { -// expect(stubAuthProvider.calledWith('cassandra', 'cassandra')).equal(true); -// expect(stubAuthProvider).to.have.been.calledOnce; -// expect(stubClient).to.have.been.calledOnce; -// expect(fsStub).to.have.not.been.calledOnce; -// expect(fsRemoveStub).to.have.not.been.calledOnce; -// expect(cmdStub).to.have.not.been.calledOnce; -// expect(clientExecuteStub).to.have.been.calledOnce; -// expect(clientShutdownStub).to.have.been.calledOnce; -// expect(stubErrorLogger.calledWith(initializeCassandraEnvironmentError, 'Cassandra handler: failed to close Cassandra connection.')).to.be.true; -// expect(exitStub).to.have.been.calledOnce; -// stubErrorLogger.restore(); -// done(); -// }); -// }); -// }); -// describe('createConfigTemplateFile rejects', function () { -// it('Should exit the process and log the error', function (done) { -// let stubAuthProvider = sandbox.stub(cassandra.auth, 'PlainTextAuthProvider'); -// stubAuthProvider.returns({ -// 'test': 'test' -// }); -// -// let client = { -// execute: sinon.stub(), -// shutdown: sinon.stub() -// }; -// -// let clientExecuteStub = client.execute; -// let error = 'error_message'; -// clientExecuteStub.yields(undefined); -// -// let clientShutdownStub = client.shutdown; -// clientShutdownStub.yields(undefined); -// -// let stubClient = sandbox.stub(cassandra, 'Client'); -// stubClient.returns(client); -// -// let fs = { -// writeFile: sinon.stub(), -// remove: sinon.stub() -// }; -// -// let fsStub = fs.writeFile; -// cassandraHandler.__set__('fs', fs); -// fsStub.yields(error); -// -// let fsRemoveStub = fs.remove; -// fsRemoveStub.yields(null); -// -// let cmd = { -// get: sinon.stub() -// }; -// -// let cmdStub = cmd.get; -// cassandraHandler.__set__('cmd', cmd); -// cmdStub.yields(null, null, null); -// -// let stubErrorLogger = sinon.stub(logger, 'error'); -// -// let processStub = { -// exit: sinon.stub() -// }; -// -// let exitStub = processStub.exit; -// exitStub.resolves(); -// cassandraHandler.__set__('process', processStub); -// -// let initializeCassandraEnvironmentError = { -// 'x-zooz-request-id': 'service-startup', -// 'key_space_name': 'predator', -// 'init_file_name_template': 'cassandra_config_template.json', -// 'init_file_name': 'cassandra_config.json', -// 'remove_config_template_file_err': 'error_message', -// 'initialize_cassandra_environment_error': 'error_message' -// }; -// -// cassandraHandler.initializeCassandraEnvironment().then(function () { -// expect(stubAuthProvider.calledWith('cassandra', 'cassandra')).equal(true); -// expect(stubAuthProvider).to.have.been.calledOnce; -// expect(stubClient).to.have.been.calledOnce; -// expect(fsStub).to.have.been.calledOnce; -// expect(fsRemoveStub).to.have.not.been.calledOnce; -// expect(cmdStub).to.have.not.been.calledOnce; -// expect(clientExecuteStub).to.have.been.calledOnce; -// expect(clientShutdownStub).to.have.been.calledOnce; -// expect(stubErrorLogger.calledWith(initializeCassandraEnvironmentError, 'Cassandra handler: could not write to cassandra init file')).to.be.true; -// expect(exitStub).to.have.been.calledOnce; -// stubErrorLogger.restore(); -// done(); -// }); -// }); -// }); -// describe('runCassandraScripts rejects', function () { -// it('Should exit the process and log the error', function (done) { -// let stubAuthProvider = sandbox.stub(cassandra.auth, 'PlainTextAuthProvider'); -// stubAuthProvider.returns({ -// 'test': 'test' -// }); -// -// let client = { -// execute: sinon.stub(), -// shutdown: sinon.stub() -// }; -// -// let clientExecuteStub = client.execute; -// let error = 'error_message'; -// clientExecuteStub.yields(undefined); -// -// let clientShutdownStub = client.shutdown; -// clientShutdownStub.yields(undefined); -// -// let stubClient = sandbox.stub(cassandra, 'Client'); -// stubClient.returns(client); -// -// let fs = { -// writeFile: sinon.stub(), -// remove: sinon.stub() -// }; -// -// let fsStub = fs.writeFile; -// cassandraHandler.__set__('fs', fs); -// fsStub.yields(undefined); -// -// let fsRemoveStub = fs.remove; -// fsRemoveStub.yields(null); -// -// let cmd = { -// get: sinon.stub() -// }; -// -// let cmdStub = cmd.get; -// cassandraHandler.__set__('cmd', cmd); -// cmdStub.yields(error, null, null); -// -// let stubErrorLogger = sinon.stub(logger, 'error'); -// -// let processStub = { -// exit: sinon.stub() -// }; -// -// let exitStub = processStub.exit; -// exitStub.resolves(); -// cassandraHandler.__set__('process', processStub); -// -// let initializeCassandraEnvironmentError = { -// 'x-zooz-request-id': 'service-startup', -// 'key_space_name': 'predator', -// 'init_file_name_template': 'cassandra_config_template.json', -// 'init_file_name': 'cassandra_config.json', -// 'run_cassandra_scripts_err': 'error_message', -// 'run_cassandra_scripts_stderr': null, -// 'initialize_cassandra_environment_error': 'error_message' -// }; -// -// cassandraHandler.initializeCassandraEnvironment().then(function () { -// expect(stubAuthProvider.calledWith('cassandra', 'cassandra')).equal(true); -// expect(stubAuthProvider).to.have.been.calledOnce; -// expect(stubClient).to.have.been.calledOnce; -// expect(fsStub).to.have.been.calledOnce; -// expect(fsRemoveStub).to.have.not.been.calledOnce; -// expect(cmdStub).to.have.been.calledOnce; -// expect(clientExecuteStub).to.have.been.calledOnce; -// expect(clientShutdownStub).to.have.been.calledOnce; -// expect(stubErrorLogger.calledWith(initializeCassandraEnvironmentError, 'Cassandra handler: failed running cassandra migration scripts')).to.be.true; -// expect(exitStub).to.have.been.calledOnce; -// stubErrorLogger.restore(); -// done(); -// }); -// }); -// }); -// describe('removeConfigFile rejects', function () { -// it('Should exit the process and log the error', function (done) { -// let stubAuthProvider = sandbox.stub(cassandra.auth, 'PlainTextAuthProvider'); -// stubAuthProvider.returns({ -// 'test': 'test' -// }); -// -// let client = { -// execute: sinon.stub(), -// shutdown: sinon.stub() -// }; -// -// let clientExecuteStub = client.execute; -// let error = 'error_message'; -// clientExecuteStub.yields(undefined); -// -// let clientShutdownStub = client.shutdown; -// clientShutdownStub.yields(undefined); -// -// let stubClient = sandbox.stub(cassandra, 'Client'); -// stubClient.returns(client); -// -// let fs = { -// writeFile: sinon.stub(), -// remove: sinon.stub() -// }; -// -// let fsStub = fs.writeFile; -// cassandraHandler.__set__('fs', fs); -// fsStub.yields(undefined); -// -// let fsRemoveStub = fs.remove; -// fsRemoveStub.yields(error); -// -// let cmd = { -// get: sinon.stub() -// }; -// -// let cmdStub = cmd.get; -// cassandraHandler.__set__('cmd', cmd); -// cmdStub.yields(null, null, null); -// -// let stubErrorLogger = sinon.stub(logger, 'error'); -// -// let processStub = { -// exit: sinon.stub() -// }; -// -// let exitStub = processStub.exit; -// exitStub.resolves(); -// cassandraHandler.__set__('process', processStub); -// -// let initializeCassandraEnvironmentError = { -// 'x-zooz-request-id': 'service-startup', -// 'key_space_name': 'predator', -// 'init_file_name_template': 'cassandra_config_template.json', -// 'init_file_name': 'cassandra_config.json', -// 'remove_config_file_err': 'error_message', -// 'initialize_cassandra_environment_error': 'error_message' -// }; -// -// cassandraHandler.initializeCassandraEnvironment().then(function () { -// expect(stubAuthProvider.calledWith('cassandra', 'cassandra')).equal(true); -// expect(stubAuthProvider).to.have.been.calledOnce; -// expect(stubClient).to.have.been.calledOnce; -// expect(fsStub).to.have.been.calledOnce; -// expect(fsRemoveStub).to.have.been.calledOnce; -// expect(cmdStub).to.have.been.calledOnce; -// expect(clientExecuteStub).to.have.been.calledOnce; -// expect(clientShutdownStub).to.have.been.calledOnce; -// expect(stubErrorLogger.calledWith(initializeCassandraEnvironmentError, 'Cassandra handler: could not remove cassandra migration configManager file')).to.be.true; -// expect(exitStub).to.have.been.calledOnce; -// stubErrorLogger.restore(); -// done(); -// }); -// }); -// }); -// }); -// }); diff --git a/tests/unit-tests/configManager/cassandra/cassandra-test.js b/tests/unit-tests/configManager/cassandra/cassandra-test.js deleted file mode 100644 index 94b2fe3a1..000000000 --- a/tests/unit-tests/configManager/cassandra/cassandra-test.js +++ /dev/null @@ -1,128 +0,0 @@ -'use strict'; -let sinon = require('sinon'); -let driver = require('cassandra-driver'); -let rewire = require('rewire'); -let should = require('should'); -let cassandraClient = rewire('../../../../src/configManager/models/database/cassandra/cassandraConnector'); - -describe('Cassandra client tests', function() { - let sandbox; - let clientBatchStub; - let clientExecuteStub; - let revert; - - before(() => { - sandbox = sinon.sandbox.create(); - clientBatchStub = sandbox.stub(driver.Client.prototype, 'batch'); - clientExecuteStub = sandbox.stub(driver.Client.prototype, 'execute'); - revert = cassandraClient.__set__('client', { batch: clientBatchStub, execute: clientExecuteStub }); - }); - - afterEach(() => { - sandbox.resetHistory(); - }); - - after(() => { - sandbox.restore(); - revert(); - }); - - describe('Upsert new config record', () => { - it('should succeed simple update', async () => { - clientBatchStub.resolves({ result: { rowLength: 0 } }); - let query = 'INSERT INTO config(key, value) values(?,?)'; - await cassandraClient.updateConfig({ key: 'test_key' }); - - clientBatchStub.getCall(0).args[0][0].query.should.eql(query); - clientBatchStub.getCall(0).args[0][0].params[0].should.eql('key'); - clientBatchStub.getCall(0).args[0][0].params[1].should.eql('test_key'); - }); - }); - - describe('Upsert new config record object as value', () => { - it('should succeed object value update', async () => { - clientBatchStub.resolves({ result: { rowLength: 0 } }); - let query = 'INSERT INTO config(key, value) values(?,?)'; - let objectToSave = { test_json: 'json_value' }; - await cassandraClient.updateConfig({ key: objectToSave }); - - clientBatchStub.getCall(0).args[0][0].query.should.eql(query); - clientBatchStub.getCall(0).args[0][0].params[0].should.eql('key'); - clientBatchStub.getCall(0).args[0][0].params[1].should.eql(JSON.stringify(objectToSave)); - }); - }); - - describe('Upsert new config multiple records object and strings as value', () => { - it('should succeed object value update', async () => { - clientBatchStub.resolves({ result: { rowLength: 0 } }); - let query = 'INSERT INTO config(key, value) values(?,?)'; - let objectToSave = { object_key: 'test_key' }; - await cassandraClient.updateConfig({ stringValue: 'test_string', objectValue: objectToSave }); - - clientBatchStub.getCall(0).args[0][0].query.should.eql(query); - clientBatchStub.getCall(0).args[0][0].params[0].should.eql('stringValue'); - clientBatchStub.getCall(0).args[0][0].params[1].should.eql('test_string'); - clientBatchStub.getCall(0).args[0][1].params[0].should.eql('objectValue'); - clientBatchStub.getCall(0).args[0][1].params[1].should.eql(JSON.stringify(objectToSave)); - }); - }); - - describe('get all config', () => { - it('should succeed get config', async () => { - clientExecuteStub.resolves(new Promise((resolve, reject) => { - resolve({}); - })); - let query = 'SELECT* FROM config'; - await cassandraClient.getConfig(); - - clientExecuteStub.getCall(0).args[0].should.eql(query); - }); - }); - describe('handle cassandra delete ', () => { - it('should succeed delete', async () => { - const query = 'DELETE FROM config WHERE key=?;'; - clientExecuteStub.resolves([]); - await cassandraClient.deleteConfig('delete_key'); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql('delete_key'); - }); - }); - - describe('get config by value multple ', () => { - it('should succeed get config', async () => { - clientExecuteStub.resolves(new Promise((resolve, reject) => { - resolve({}); - })); - let query = 'SELECT* FROM config WHERE key= ?'; - await cassandraClient.getConfigValue('value_test'); - - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql('value_test'); - }); - }); - - describe('handle cassandra execute error ', () => { - it('should reject request with error', async () => { - clientExecuteStub.throws(); - let errorText = 'Error occurred in communication with cassandra'; - - cassandraClient.getConfigValue('value_test').then(() => { - throw new Error('Expected to catch error!'); - }, (err) => { - should(err.message).eql(errorText); - }); - }); - }); - describe('handle cassandra batch error ', () => { - it('should reject request with error', async () => { - clientBatchStub.throws(); - let errorText = 'Error occurred in communication with cassandra'; - - cassandraClient.updateConfig({}).then(() => { - throw new Error('Expected to catch error!'); - }, (err) => { - should(err.message).eql(errorText); - }); - }); - }); -}); \ No newline at end of file diff --git a/tests/unit-tests/configManager/configHandler-test.js b/tests/unit-tests/configManager/configHandler-test.js index e0bd8b52c..d8ddd08c3 100644 --- a/tests/unit-tests/configManager/configHandler-test.js +++ b/tests/unit-tests/configManager/configHandler-test.js @@ -5,6 +5,7 @@ process.env.JOB_PLATFORM = 'DOCKER'; const should = require('should'); const rewire = require('rewire'); const sinon = require('sinon'); + const databaseConnector = require('../../../src/configManager/models/database/databaseConnector'); const configConstants = require('../../../src/common/consts').CONFIG; @@ -118,15 +119,15 @@ const resultAfterConvert = { describe('Manager config', function () { let sandbox; - let cassandraGetStub; - let cassandraGetValueStub; - let cassandraUpdateStub; + let databaseConnectorGetStub; + let databaseConnectorGetValueStub; + let databaseConnectorUpdateStub; before(() => { sandbox = sinon.sandbox.create(); - cassandraGetStub = sandbox.stub(databaseConnector, 'getConfigAsObject'); - cassandraGetValueStub = sandbox.stub(databaseConnector, 'getConfigValue'); - cassandraUpdateStub = sandbox.stub(databaseConnector, 'updateConfig'); + databaseConnectorGetStub = sandbox.stub(databaseConnector, 'getConfigAsObject'); + databaseConnectorGetValueStub = sandbox.stub(databaseConnector, 'getConfigValue'); + databaseConnectorUpdateStub = sandbox.stub(databaseConnector, 'updateConfig'); manager = rewire('../../../src/configManager/models/configHandler'); }); @@ -140,7 +141,7 @@ describe('Manager config', function () { describe('get default config', function () { it('get default config success', async () => { - cassandraGetStub.resolves([]); + databaseConnectorGetStub.resolves([]); let result = await manager.getConfig(); @@ -152,7 +153,7 @@ describe('Manager config', function () { describe('get config from default and DB', function () { it('get config success', async () => { - cassandraGetStub.resolves({ 'runner_cpu': 2 }); + databaseConnectorGetStub.resolves({ 'runner_cpu': 2 }); let result = await manager.getConfig(); should(Object.keys(result).length).eql(Object.keys(configConstants).length); Object.keys(result).forEach(key => { @@ -166,7 +167,7 @@ describe('Manager config', function () { describe('get config with corrupted data from DB', function () { it('get config success', async () => { - cassandraGetStub.resolves({ 'key_not_valid': 2 }); + databaseConnectorGetStub.resolves({ 'key_not_valid': 2 }); let result = await manager.getConfig(); const resultEscapedUndefined = escapeUndefinedValues(result); should(resultEscapedUndefined).eql(defaultConfig); @@ -175,7 +176,7 @@ describe('Manager config', function () { describe('get config and parse types, types are valid', function () { it('get config success', async () => { - cassandraGetStub.resolves(configResponseParseObject); + databaseConnectorGetStub.resolves(configResponseParseObject); let result = await manager.getConfig(); @@ -186,7 +187,7 @@ describe('Manager config', function () { describe('get config value from env variables', function () { it('get config value success', async () => { - cassandraGetValueStub.resolves(undefined); + databaseConnectorGetValueStub.resolves(undefined); let result = await manager.getConfigValue('runner_cpu'); should(result).eql(1); @@ -195,7 +196,7 @@ describe('Manager config', function () { describe('update config ', function () { it('update config success', async () => { - cassandraUpdateStub.resolves([]); + databaseConnectorUpdateStub.resolves([]); let result = await manager.updateConfig({ runner_cpu: 'test_runner_cpu' }); should(result).eql([]); diff --git a/tests/unit-tests/configManager/configHandlerEnvVaribles-test.js b/tests/unit-tests/configManager/configHandlerEnvVaribles-test.js index 5b6e164be..fe3c21c0e 100644 --- a/tests/unit-tests/configManager/configHandlerEnvVaribles-test.js +++ b/tests/unit-tests/configManager/configHandlerEnvVaribles-test.js @@ -11,11 +11,11 @@ const configConstants = require('../../../src/common/consts').CONFIG; describe('Manager config with env variables', function () { let sandbox; let manager; - let cassandraGetStub; + let databaseConnectorGetStub; before(() => { sandbox = sinon.sandbox.create(); - cassandraGetStub = sandbox.stub(databaseConnector, 'getConfigAsObject'); + databaseConnectorGetStub = sandbox.stub(databaseConnector, 'getConfigAsObject'); process.env.SMTP_FROM = 'smtp_from_test'; process.env.SMTP_PORT = 'smtp_port_test'; @@ -50,7 +50,7 @@ describe('Manager config with env variables', function () { sandbox.restore(); }); it('get config for from env varibles in the right types (json,int,float,string)', async () => { - cassandraGetStub.resolves([]); + databaseConnectorGetStub.resolves([]); let result = await manager.getConfig(); should(Object.keys(result).length).eql(Object.keys(configConstants).length); should(result.grafana_url).eql('url_test'); diff --git a/tests/unit-tests/configManager/sequelize/sequelizeConnector-test.js b/tests/unit-tests/configManager/sequelize/sequelizeConnector-test.js index 4e9eba8ab..8bc604344 100644 --- a/tests/unit-tests/configManager/sequelize/sequelizeConnector-test.js +++ b/tests/unit-tests/configManager/sequelize/sequelizeConnector-test.js @@ -4,7 +4,7 @@ const sinon = require('sinon'), databaseConfig = require('../../../../src/config/databaseConfig'), sequelizeConnector = require('../../../../src/configManager/models/database/sequelize/sequelizeConnector'); -describe('Cassandra client tests', function () { +describe('Sequelize client tests', function () { let sandbox, sequelizeModelStub, sequelizeUpsertStub, @@ -123,4 +123,4 @@ describe('Cassandra client tests', function () { should(sequelizeGeValueetStub.args[0][0].where.key).eql('key_value'); }); }); -}); \ No newline at end of file +}); diff --git a/tests/unit-tests/env-test.js b/tests/unit-tests/env-test.js index be029e400..6c9d4934e 100644 --- a/tests/unit-tests/env-test.js +++ b/tests/unit-tests/env-test.js @@ -60,7 +60,7 @@ describe.skip('Env Suite', function () { it('Should Failed - missing all mandatory env', () => { should(logErrorStub.called).eql(true); - should(logErrorStub.args[0][0]).eql('DATABASE_TYPE should be one of: CASSANDRA,MYSQL,POSTGRES,MSSQL,SQLITE'); + should(logErrorStub.args[0][0]).eql('DATABASE_TYPE should be one of: MYSQL,POSTGRES,MSSQL,SQLITE'); }); MANDATORY_VARS.forEach(function (varb) { diff --git a/tests/unit-tests/files/models/cassandraConnector-test.js b/tests/unit-tests/files/models/cassandraConnector-test.js deleted file mode 100644 index 06f476db2..000000000 --- a/tests/unit-tests/files/models/cassandraConnector-test.js +++ /dev/null @@ -1,79 +0,0 @@ -'use strict'; -let sinon = require('sinon'); -let logger = require('../../../../src/common/logger'); -let should = require('should'); -let cassandraClient = require('../../../../src/files/models/database/cassandra/cassandraConnector'); -let uuid = require('uuid'); - -describe('Cassandra client tests', function () { - let sandbox; - let clientExecuteStub; - - before(() => { - sandbox = sinon.sandbox.create(); - clientExecuteStub = sandbox.stub(); - cassandraClient.init({ execute: clientExecuteStub }); - }); - - afterEach(() => { - sandbox.resetHistory(); - }); - - after(() => { - sandbox.restore(); - }); - - describe('Create and get files', function () { - it('should succeed simple insert of file', async () => { - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let id = uuid.v4(); - - let query = 'INSERT INTO files(id,name,file) values(?,?,?)'; - await cassandraClient.saveFile(id, 'some_file.txt', 'contentcontentcontent'); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - clientExecuteStub.getCall(0).args[1][1].should.eql('some_file.txt'); - clientExecuteStub.getCall(0).args[1][2].should.eql('contentcontentcontent'); - }); - it('should succeed simple get of file', async () => { - clientExecuteStub.resolves({ rows: [ { name: 'file.txt', file: 'abcdef' }] }); - let id = uuid.v4(); - - let query = 'SELECT id, name FROM files WHERE id = ?'; - let file = await cassandraClient.getFile(id); - - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - - file.name.should.eql('file.txt'); - file.file.should.eql('abcdef'); - }); - - it('should succeed simple get of file with content', async () => { - clientExecuteStub.resolves({ rows: [ { name: 'file.txt', file: 'abcdef' }] }); - let id = uuid.v4(); - - let query = 'SELECT * FROM files WHERE id = ?'; - let file = await cassandraClient.getFile(id, true); - - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - - file.name.should.eql('file.txt'); - file.file.should.eql('abcdef'); - }); - - it('should return undefined when file not found', async () => { - clientExecuteStub.resolves({ rows: [] }); - let id = uuid.v4(); - - let query = 'SELECT id, name FROM files WHERE id = ?'; - let file = await cassandraClient.getFile(id); - - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - - should(file).eql(undefined); - }); - }); -}); diff --git a/tests/unit-tests/files/models/database-test.js b/tests/unit-tests/files/models/database-test.js deleted file mode 100644 index d32555e36..000000000 --- a/tests/unit-tests/files/models/database-test.js +++ /dev/null @@ -1,61 +0,0 @@ - -const should = require('should'), - sinon = require('sinon'), - cassandra = require('../../../../src/files/models/database/cassandra/cassandraConnector'), - sequelizeConnector = require('../../../../src/files/models/database/sequelize/sequelizeConnector'), - rewire = require('rewire'), - databaseConfig = require('../../../../src/config/databaseConfig'); -let database = require('../../../../src/files/models/database'); -const functions = [ - { - functionName: 'saveFile', - args: ['id', 'name', 'file'] - }, - { - functionName: 'getFile', - args: ['id', true] - }, -]; - -describe('Testing database', function () { - let sandbox; - before(function () { - process.env.DATABASE_TYPE = 'CASSANDRA'; - sandbox = sinon.sandbox.create(); - functions.forEach(function (func) { - sandbox.stub(cassandra, func.functionName); - sandbox.stub(sequelizeConnector, func.functionName); - }); - }); - beforeEach(function () { - sandbox.resetHistory(); - }); - after(function () { - sandbox.restore(); - }); - - describe('when database type is cassandra - should applied functions on cassandra client', function () { - before(async function () { - databaseConfig.type = 'cassandra'; - database = rewire('../../../../src/files/models/database'); - }); - functions.forEach(function (func) { - it(`checking func: ${func.functionName}`, async function () { - await database[func.functionName](...func.args); - should(cassandra[func.functionName].args).eql([func.args]); - }); - }); - }); - describe('when database type is not cassandra - should applied functions on sequlize client', function () { - before(async function () { - databaseConfig.type = 'not-cassandra'; - database = rewire('../../../../src/files/models/database'); - }); - functions.forEach(function (func) { - it(`checking func: ${func.functionName}`, async function () { - await database[func.functionName](...func.args); - should(sequelizeConnector[func.functionName].args).eql([func.args]); - }); - }); - }); -}); diff --git a/tests/unit-tests/jobs/cassandra/cassandra-test.js b/tests/unit-tests/jobs/cassandra/cassandra-test.js deleted file mode 100644 index 3f07ce2e2..000000000 --- a/tests/unit-tests/jobs/cassandra/cassandra-test.js +++ /dev/null @@ -1,230 +0,0 @@ -'use strict'; -let sinon = require('sinon'); -let logger = require('../../../../src/common/logger'); -let driver = require('cassandra-driver'); -let rewire = require('rewire'); -let should = require('should'); -let cassandraClient = rewire('../../../../src/jobs/models/database/cassandra/cassandraConnector'); - -let uuid = require('uuid'); - -describe('Cassandra client tests', function() { - let sandbox; - let clientExecuteStub; - let revert; - let loggerErrorStub; - - before(() => { - sandbox = sinon.sandbox.create(); - clientExecuteStub = sandbox.stub(driver.Client.prototype, 'execute'); - revert = cassandraClient.__set__('client', { execute: clientExecuteStub }); - loggerErrorStub = sandbox.stub(logger, 'error'); - }); - - afterEach(() => { - sandbox.resetHistory(); - }); - - after(() => { - sandbox.restore(); - revert(); - }); - - describe('Init and shutdown tests', function(){ - it('it should initialize cassandra client successfully', (done) => { - try { - cassandraClient.init({ execute: clientExecuteStub }); - } catch (e) { - e.should.be.equal(undefined); - e.should.not.be.instanceOf(Error); - } - done(); - }); - }); - - describe('Insert new test tests', function(){ - it('should succeed simple insert', function(){ - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let id = uuid.v4(); - let testId = uuid.v4(); - - let query = 'INSERT INTO jobs(id, test_id, arrival_rate, cron_expression, duration, emails, environment, ramp_to, webhooks, parallelism, max_virtual_users, notes, proxy_url, debug, enabled) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'; - return cassandraClient.insertJob(id, { test_id: testId, arrival_rate: 1, duration: 1, cron_expression: '* * * *', emails: {}, environment: 'test', ramp_to: '1', webhooks: 1, parallelism: 3, max_virtual_users: 500, notes: 'hello' }) - .then(function(){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - clientExecuteStub.getCall(0).args[1][1].should.eql(testId); - }); - }); - - it('should log error for failing inserting new test', function(){ - clientExecuteStub.rejects(); - return cassandraClient.insertJob(uuid.v4(), { test_id: uuid.v4(), arrival_rate: 1, duration: 1, cron_expression: '* * * *', emails: {}, environment: 'test', ramp_to: '1', webhooks: 1, notes: 'hello' }) - .catch(function(){ - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - - describe('Get jobs', function(){ - it('should get multiple jobs', function(){ - let cassandraResponse = { rows: [{ id: 'id', test_id: 'test_id', arrival_rate: 1, duration: 1, cron_expression: null, emails: null, webhooks: null, ramp_to: '1' }] }; - clientExecuteStub.resolves(cassandraResponse); - - let query = 'SELECT * FROM jobs'; - return cassandraClient.getJobs() - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - result.should.eql(cassandraResponse.rows); - }); - }); - - it('should get failure from cassandra', function(){ - clientExecuteStub.rejects(new Error('error')); - - let query = 'SELECT * FROM jobs'; - return cassandraClient.getJobs() - .then(function(result){ - return Promise.reject(new Error('should not get here')); - }).catch(function(err) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); - - describe('Get job', function(){ - it('should get single job', function(){ - clientExecuteStub.resolves({ rows: [{ id: 'id', test_id: 'test_id', arrival_rate: 1, duration: 1, cron_expression: null, emails: null, webhooks: null, ramp_to: '1' }] }); - let jobId = uuid.v4(); - let query = 'SELECT * FROM jobs WHERE id=?'; - return cassandraClient.getJob(jobId) - .then(function(result){ - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(jobId); - }); - }); - }); - - describe('Delete job', function(){ - it('should delete single job', function(){ - clientExecuteStub.resolves({ rows: [] }); - let jobId = uuid.v4(); - let query = 'DELETE FROM jobs WHERE id=?'; - return cassandraClient.deleteJob(jobId) - .then(function(result){ - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(jobId); - result.should.eql([]); - }); - }); - - it('should get failure from cassandra', function(){ - clientExecuteStub.rejects(new Error('error')); - let jobId = uuid.v4(); - let query = 'DELETE FROM jobs WHERE id=?'; - return cassandraClient.deleteJob(jobId) - .then(function(){ - return Promise.reject(new Error('should not get here')); - }).catch(function(err) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); - - describe('Update job', function(){ - it('should succeed update of one parameter', function(){ - clientExecuteStub.onCall(0).resolves({ rows: [{ column_name: 'test_id' }] }); - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - cassandraClient.__set__('databaseConfig', { name: 'keyspace' }); - let id = uuid.v4(); - let testId = uuid.v4(); - - let updateQuery = 'UPDATE jobs SET test_id=? WHERE id=? IF EXISTS'; - let columnQuery = 'SELECT * FROM system_schema.columns WHERE keyspace_name = ? AND table_name = \'jobs\''; - return cassandraClient.updateJob(id, { test_id: testId }) - .then(function(){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(columnQuery); - clientExecuteStub.getCall(0).args[1][0].should.eql('keyspace'); - clientExecuteStub.getCall(1).args[0].should.eql(updateQuery); - clientExecuteStub.getCall(1).args[1][1].should.eql(id); - clientExecuteStub.getCall(1).args[1][0].should.eql(testId); - }); - }); - - it('should succeed update more than one parameter', function(){ - cassandraClient.__set__('columns', undefined); - clientExecuteStub.onCall(0).resolves({ rows: [{ column_name: 'test_id' }, { column_name: 'duration' }] }); - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let id = uuid.v4(); - let testId = uuid.v4(); - - let query = 'UPDATE jobs SET test_id=?, duration=? WHERE id=? IF EXISTS'; - let columnQuery = 'SELECT * FROM system_schema.columns WHERE keyspace_name = ? AND table_name = \'jobs\''; - return cassandraClient.updateJob(id, { test_id: testId, duration: 4 }) - .then(function(){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(columnQuery); - clientExecuteStub.getCall(0).args[1][0].should.eql('keyspace'); - clientExecuteStub.getCall(1).args[0].should.eql(query); - clientExecuteStub.getCall(1).args[1][2].should.eql(id); - clientExecuteStub.getCall(1).args[1][1].should.eql(4); - clientExecuteStub.getCall(1).args[1][0].should.eql(testId); - }); - }); - - it('should ignore none existing parameter in the update', function(){ - cassandraClient.__set__('columns', undefined); - clientExecuteStub.onCall(0).resolves({ rows: [{ column_name: 'test_id' }] }); - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let id = uuid.v4(); - let testId = uuid.v4(); - - let query = 'UPDATE jobs SET test_id=? WHERE id=? IF EXISTS'; - let columnQuery = 'SELECT * FROM system_schema.columns WHERE keyspace_name = ? AND table_name = \'jobs\''; - return cassandraClient.updateJob(id, { test_id: testId, duration: 4 }) - .then(function(){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(columnQuery); - clientExecuteStub.getCall(0).args[1][0].should.eql('keyspace'); - clientExecuteStub.getCall(1).args[0].should.eql(query); - clientExecuteStub.getCall(1).args[1][1].should.eql(id); - clientExecuteStub.getCall(1).args[1][0].should.eql(testId); - }); - }); - - it('should log error for failing updating new test', function(){ - clientExecuteStub.onCall(0).resolves({ rows: [{ column_name: 'test_id' }] }); - clientExecuteStub.rejects(); - return cassandraClient.updateJob(uuid.v4(), { test_id: uuid.v4() }) - .catch(function(){ - loggerErrorStub.callCount.should.eql(1); - }); - }); - - ['id', 'job_id'].forEach(function(idName){ - it('should reject an error for trying to update ' + idName, function(){ - cassandraClient.__set__('columns', undefined); - clientExecuteStub.onCall(0).resolves({ rows: [{ column_name: 'test_id' }] }); - cassandraClient.__set__('databaseConfig', { name: 'keyspace' }); - - let query = 'SELECT * FROM system_schema.columns WHERE keyspace_name = ? AND table_name = \'jobs\''; - return cassandraClient.updateJob(uuid.v4(), { [idName]: 'something' }) - .catch(function(err){ - err.statusCode.should.eql(400); - err.message.should.eql('Job id can not be updated'); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql('keyspace'); - }); - }); - }); - }); -}); \ No newline at end of file diff --git a/tests/unit-tests/processors/cassandra/cassandra-test.js b/tests/unit-tests/processors/cassandra/cassandra-test.js deleted file mode 100644 index 1b39a2881..000000000 --- a/tests/unit-tests/processors/cassandra/cassandra-test.js +++ /dev/null @@ -1,242 +0,0 @@ -'use strict'; -let sinon = require('sinon'); -let logger = require('../../../../src/common/logger'); -let driver = require('cassandra-driver'); -let rewire = require('rewire'); -let should = require('should'); -let cassandraClient = rewire('../../../../src/processors/models/database/cassandra/cassandraConnector'); - -let uuid = require('uuid'); - -describe('Cassandra processors tests', function() { - let sandbox; - let clientExecuteStub; - let revert; - let loggerErrorStub; - - before(() => { - sandbox = sinon.sandbox.create(); - clientExecuteStub = sandbox.stub(driver.Client.prototype, 'execute'); - revert = cassandraClient.__set__('client', { execute: clientExecuteStub }); - loggerErrorStub = sandbox.stub(logger, 'error'); - }); - - afterEach(() => { - sandbox.resetHistory(); - }); - - after(() => { - sandbox.restore(); - revert(); - }); - - describe('init', function() { - it('should assign the cassandra client successfully', function () { - const prevClient = cassandraClient.__get__('client'); - const newClient = { clientId: 'fake-client' }; - cassandraClient.init(newClient); - const updatedClient = cassandraClient.__get__('client'); - updatedClient.should.equal(newClient); - cassandraClient.__set__('client', prevClient); - }); - }); - - describe('Insert new processor', function(){ - it('should succeed simple insert', function(){ - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let id = uuid.v4(); - - let query = cassandraClient._queries.INSERT_PROCESSOR; - return cassandraClient.insertProcessor(id, { name: 'mick', description: 'some processor', javascript: 'module.exports.mick = \'ey\'' }) - .then(function(){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - }); - }); - - it('should log error for failing inserting new processor', function(){ - clientExecuteStub.rejects(); - return cassandraClient.insertProcessor('id', { name: 'mick', description: 'some processor', javascript: 'module.exports.mick = \'ey\'' }) - .catch(function(){ - loggerErrorStub.callCount.should.eql(2); - }); - }); - }); - - describe('Get processors', function(){ - it('should get multiple processors', function(){ - let cassandraResponse = { rows: [{ id: 'id', name: 'mick', description: 'some processor', javascript: 'module.exports.mick = \'ey\'', created_at: Date.now(), updated_at: Date.now() }] }; - clientExecuteStub.resolves(cassandraResponse); - - let query = 'SELECT * FROM processors'; - return cassandraClient.getAllProcessors() - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - result.should.eql(cassandraResponse.rows); - }); - }); - - it('should get multiple processors while excluding javascript', function(){ - let cassandraResponse = { rows: [{ id: 'id', name: 'mick', description: 'some processor', javascript: 'module.exports.mick = \'ey\'', created_at: Date.now(), updated_at: Date.now() }] }; - clientExecuteStub.resolves(cassandraResponse); - - let query = 'SELECT id, name, description, created_at, updated_at, exported_functions FROM processors'; - return cassandraClient.getAllProcessors(undefined, undefined, 'javascript') - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - result.should.eql(cassandraResponse.rows); - }); - }); - - it('should get failure from cassandra', function(){ - clientExecuteStub.rejects(new Error('error')); - - let query = cassandraClient._queries.GET_ALL_PROCESSORS; - return cassandraClient.getAllProcessors() - .then(function(result){ - return Promise.reject(new Error('should not get here')); - }).catch(function(err) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); - - describe('Get processor', function() { - describe('getProcessorById', function() { - it('should get a single processor', function(){ - clientExecuteStub.resolves({ rows: [{ id: 'id', name: 'mick', description: 'some processor', javascript: 'module.exports.mick = \'ey\'', created_at: Date.now(), updated_at: Date.now() }] }); - let proccesorId = uuid.v4(); - let query = cassandraClient._queries.GET_PROCESSOR_BY_ID; - return cassandraClient.getProcessorById(proccesorId) - .then(function(result){ - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(proccesorId); - should(result).containDeep({ id: 'id', name: 'mick', description: 'some processor', javascript: 'module.exports.mick = \'ey\'' }); - }); - }); - }); - - describe('updateProcessor', function() { - it('should update the execute the update query sucessfully', async function() { - const getProcessorQuery = cassandraClient._queries.GET_PROCESSOR_BY_ID; - const updateProcessorQuery = cassandraClient._queries.UPDATE_PROCESSOR; - const deleteProcessorMapping = cassandraClient._queries.DELETE_PROCESSOR_MAPPING; - const insertProcessorMapping = cassandraClient._queries.INSERT_PROCESSOR_MAPPING; - const processorId = uuid.v4(); - const processor = { - id: processorId, - name: 'updated processor name', - description: 'some processor', - javascript: 'module.exports.mick = \'ey\'', - created_at: Date.now() - }; - const processorMapping = { - name: processor.name, - id: processorId - }; - clientExecuteStub.withArgs(getProcessorQuery).resolves({ rows: [processor] }); - clientExecuteStub.withArgs(updateProcessorQuery).resolves({ rows: [processor] }); - clientExecuteStub.withArgs(insertProcessorMapping).resolves({ rows: [processorMapping] }); - clientExecuteStub.withArgs(deleteProcessorMapping).resolves({ rows: [] }); - cassandraClient.updateProcessor(processorId, processor).then(result => { - clientExecuteStub.getCall(0).args[0].should.eql(getProcessorQuery); - clientExecuteStub.getCall(0).args[1][0].should.eql(processorId); - clientExecuteStub.getCall(1).args[0].should.eql(updateProcessorQuery); - clientExecuteStub.getCall(1).args[1][0].should.eql(processor.name); - clientExecuteStub.getCall(2).args[0].should.eql(insertProcessorMapping); - clientExecuteStub.getCall(2).args[1][0].should.eql(processor.name); - clientExecuteStub.getCall(2).args[1][1].should.eql(processor.id); - clientExecuteStub.getCall(3).args[0].should.eql(deleteProcessorMapping); - clientExecuteStub.getCall(3).args[1][0].should.eql(processor.name); - should(result).containDeep(processor); - }); - }); - }); - - describe('getProcessorByName', function() { - it('should get a single processor', function() { - let processorId = uuid.v4(); - let processorName = 'Generate Random Kitty Name'; - const processor = { - id: processorId, - name: processorName, - description: 'some processor', - javascript: 'module.exports.mick = \'ey\'', - created_at: Date.now(), - updated_at: Date.now() - }; - const processorMapping = { - name: processorName, - id: processorId - }; - let query = cassandraClient._queries.GET_PROCESSOR_BY_ID; - let mappingQuery = cassandraClient._queries.GET_PROCESSOR_MAPPING; - clientExecuteStub.withArgs(mappingQuery).resolves({ rows: [processorMapping] }); - clientExecuteStub.withArgs(query).resolves({ rows: [processor] }); - return cassandraClient.getProcessorByName(processorName) - .then(function(result) { - clientExecuteStub.getCall(0).args[0].should.eql(mappingQuery); - clientExecuteStub.getCall(0).args[1][0].should.eql(processorName); - clientExecuteStub.getCall(1).args[0].should.eql(query); - clientExecuteStub.getCall(1).args[1][0].should.eql(processorId); - should(result).containDeep(processor); - }); - }); - }); - }); - - describe('Delete processor', function(){ - it('should delete single processor', function(){ - const processorId = uuid.v4(); - const processorMapping = { - name: 'mick', - id: processorId - }; - const getProcessorQuery = cassandraClient._queries.GET_PROCESSOR_BY_ID; - const deleteProcessorQuery = cassandraClient._queries.DELETE_PROCESSOR; - const deleteMappingQuery = cassandraClient._queries.DELETE_PROCESSOR_MAPPING; - clientExecuteStub.withArgs(getProcessorQuery).resolves({ rows: [processorMapping] }); - clientExecuteStub.withArgs(deleteProcessorQuery).resolves({ rows: [] }); - clientExecuteStub.withArgs(deleteMappingQuery).resolves({ rows: [] }); - - return cassandraClient.deleteProcessor(processorId) - .then(function(result){ - clientExecuteStub.callCount.should.eql(3); - clientExecuteStub.getCall(0).args[0].should.eql(getProcessorQuery); - clientExecuteStub.getCall(0).args[1][0].should.eql(processorMapping.id); - clientExecuteStub.getCall(1).args[0].should.eql(deleteProcessorQuery); - clientExecuteStub.getCall(1).args[1][0].should.eql(processorId); - clientExecuteStub.getCall(2).args[0].should.eql(deleteMappingQuery); - clientExecuteStub.getCall(2).args[1][0].should.eql(processorMapping.name); - result.should.eql([[], []]); - }); - }); - - it('should get failure from cassandra', function(){ - let processorId = uuid.v4(); - const processorMapping = { - id: processorId, - name: 'mick' - }; - let getProcessorMapping = cassandraClient._queries.GET_PROCESSOR_BY_ID; - let deleteProcessorQuery = cassandraClient._queries.DELETE_PROCESSOR; - clientExecuteStub.withArgs(getProcessorMapping).resolves({ rows: [processorMapping] }); - clientExecuteStub.withArgs(deleteProcessorQuery).rejects(new Error('error')); - return cassandraClient.deleteProcessor(processorId) - .then(function(){ - return Promise.reject(new Error('should not get here')); - }).catch(function(err) { - clientExecuteStub.getCall(0).args[0].should.eql(getProcessorMapping); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); -}); diff --git a/tests/unit-tests/reporter/cassandra/cassandra-test.js b/tests/unit-tests/reporter/cassandra/cassandra-test.js deleted file mode 100644 index aa91fdccf..000000000 --- a/tests/unit-tests/reporter/cassandra/cassandra-test.js +++ /dev/null @@ -1,385 +0,0 @@ -'use strict'; -let sinon = require('sinon'); -let logger = require('../../../../src/common/logger'); -let driver = require('cassandra-driver'); -let rewire = require('rewire'); -let cassandraClient = rewire('../../../../src/reports/models/database/cassandra/cassandraConnector'); - -let uuid = require('uuid'); - -const REPORT = { - 'test_id': 'test id', - 'revision_id': 'revision_id', - 'report_id': 'report_id', - 'test_name': 'test name', - 'report_url': 'http://www.zooz.com', - 'last_stats': JSON.stringify({ - 'timestamp': '2018-05-28T15:40:10.044Z', - 'scenariosCreated': 289448, - 'scenariosCompleted': 289447, - 'requestsCompleted': 694611, - 'latency': { - 'min': 6.3, - 'max': 3822.8, - 'median': 58.8, - 'p95': 115.5, - 'p99': 189.4 - }, - 'rps': { - 'count': 694611, - 'mean': 178.61 - }, - 'scenarioDuration': { - 'min': 80.4, - 'max': 5251.7, - 'median': 146.8, - 'p95': 244.4, - 'p99': 366.6 - }, - 'scenarioCounts': { - 'Create token and get token': 173732, - 'Create token, create customer and assign token to customer': 115716 - }, - 'errors': { EAI_AGAIN: 112, NOTREACH: 123 }, - 'codes': { - '200': 173732, - '201': 520878, - '503': 1 - }, - 'matches': 0, - 'customStats': {}, - 'concurrency': 1510, - 'pendingRequests': 1471 - }), - 'end_time': 1527533519591, - 'start_time': 1527533459591 -}; - -describe('Cassandra client tests', function() { - let sandbox; - let clientExecuteStub; - let revert; - let loggerErrorStub; - - let testId, revisionId, reportId, jobId, testType, startTime, testName, testDescription, testConfiguration, notes, lastUpdatedAt, phase; - - before(() => { - sandbox = sinon.sandbox.create(); - clientExecuteStub = sandbox.stub(driver.Client.prototype, 'execute'); - revert = cassandraClient.__set__('client', { execute: clientExecuteStub }); - loggerErrorStub = sandbox.stub(logger, 'error'); - - testId = uuid(); - revisionId = uuid(); - reportId = uuid(); - jobId = uuid(); - testType = 'testType'; - startTime = new Date('1/10/2017'); - testName = 'testName'; - testDescription = 'testDescription'; - testConfiguration = 'testConfiguration'; - notes = 'notes'; - lastUpdatedAt = Date.now(); - phase = '0'; - }); - - afterEach(() => { - sandbox.resetHistory(); - clientExecuteStub.reset(); - }); - - after(() => { - sandbox.restore(); - revert(); - }); - - describe('Init and shutdown tests', function(){ - it('it should initialize cassandra client successfully', (done) => { - try { - cassandraClient.init({ execute: clientExecuteStub }); - } catch (e) { - e.should.be.equal(undefined); - e.should.not.be.instanceOf(Error); - } - done(); - }); - }); - - describe('Insert new report', function () { - it('should succeed simple insert', function () { - clientExecuteStub.resolves({ rowLength: 1, rows: [{ '[applied]': true }] }); - let queryReport = 'INSERT INTO reports_summary(test_id, revision_id, report_id, job_id, test_type, phase, start_time, test_name, test_description, test_configuration, notes, last_updated_at) values(?,?,?,?,?,?,?,?,?,?,?,?) IF NOT EXISTS'; - let queryLastReport = 'INSERT INTO last_reports(start_time_year,start_time_month,test_id, revision_id, report_id, job_id, test_type, phase, start_time, test_name, test_description, test_configuration, notes, last_updated_at) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?) IF NOT EXISTS'; - return cassandraClient.insertReport(testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, notes, lastUpdatedAt) - .then(function () { - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(queryReport); - clientExecuteStub.getCall(0).args[1][0].should.eql(testId); - clientExecuteStub.getCall(0).args[1][1].should.eql(revisionId); - clientExecuteStub.getCall(0).args[1][2].should.eql(reportId); - clientExecuteStub.getCall(1).args[0].should.eql(queryLastReport); - clientExecuteStub.getCall(1).args[1][0].should.eql(2017); - clientExecuteStub.getCall(1).args[1][1].should.eql(1); - clientExecuteStub.getCall(1).args[1][2].should.eql(testId); - clientExecuteStub.getCall(1).args[1][3].should.eql(revisionId); - clientExecuteStub.getCall(1).args[1][4].should.eql(reportId); - }); - }); - - it('should log error for failing inserting new report', function(){ - clientExecuteStub.rejects(); - return cassandraClient.insertReport(testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, notes, lastUpdatedAt) - .catch(function(){ - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - describe('should update report with benchmark tests ', async () => { - it('should update report with benchmark tests ', async () => { - clientExecuteStub.resolves({ rowLength: 1, rows: [{ '[applied]': false }] }); - let queryReport = 'UPDATE reports_summary SET score=?, benchmark_weights_data=? WHERE test_id=? AND report_id=?'; - await cassandraClient.updateReportBenchmark(testId, reportId, 5.3, 'some data'); - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.callCount.should.eql(3); // query last report should be trig - clientExecuteStub.getCall(1).args[0].should.eql(queryReport); - clientExecuteStub.getCall(1).args[1][0].should.eql(5.3); - clientExecuteStub.getCall(1).args[1][1].should.eql('some data'); - clientExecuteStub.getCall(1).args[1][2].should.eql(testId); - clientExecuteStub.getCall(1).args[1][3].should.eql(reportId); - }); - }); - - describe('Insert report that already exist', function () { - it('should succeed simple insert', function () { - clientExecuteStub.resolves({ rowLength: 1, rows: [{ '[applied]': false }] }); - let queryReport = 'INSERT INTO reports_summary(test_id, revision_id, report_id, job_id, test_type, phase, start_time, test_name, test_description, test_configuration, notes, last_updated_at) values(?,?,?,?,?,?,?,?,?,?,?,?) IF NOT EXISTS'; - return cassandraClient.insertReport(testId, revisionId, reportId, jobId, testType, phase, startTime, testName, testDescription, testConfiguration, notes, lastUpdatedAt) - .then(function () { - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.callCount.should.eql(1); // query last report should not be trig - clientExecuteStub.getCall(0).args[0].should.eql(queryReport); - clientExecuteStub.getCall(0).args[1][0].should.eql(testId); - clientExecuteStub.getCall(0).args[1][1].should.eql(revisionId); - clientExecuteStub.getCall(0).args[1][2].should.eql(reportId); - }); - }); - }); - - describe('Update report and verify last report updated', function () { - it('should succeed simple insert', async function () { - const phase = uuid(); - const cassandraClientLastReport = cassandraClient.__get__('updateLastReportAsync'); - clientExecuteStub.onCall(0).resolves({ rowLength: 1, rows: [{ 'start_time': '01/22/2017' }] }); - clientExecuteStub.onCall(1).resolves({ rowLength: 1 }); - let queryLastReport = 'UPDATE last_reports SET phase=?, last_updated_at=? WHERE start_time_year=? AND start_time_month=? AND start_time=? AND test_id=? AND report_id=?'; - await cassandraClientLastReport(testId, reportId, { phase, last_updated_at: lastUpdatedAt }); - - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(1).args[0].should.eql(queryLastReport); - clientExecuteStub.getCall(1).args[1][0].should.eql(phase); - clientExecuteStub.getCall(1).args[1][1].should.eql(lastUpdatedAt); - clientExecuteStub.getCall(1).args[1][2].should.eql(2017); - clientExecuteStub.getCall(1).args[1][3].should.eql(1); - clientExecuteStub.getCall(1).args[1][4].should.eql('01/22/2017'); - clientExecuteStub.getCall(1).args[1][5].should.eql(testId); - clientExecuteStub.getCall(1).args[1][6].should.eql(reportId); - }); - }); - describe('Get report', function(){ - it('should get single report', function(){ - let cassandraResponse = { rows: [REPORT] }; - clientExecuteStub.resolves(cassandraResponse); - - let query = 'SELECT * FROM reports_summary WHERE test_id=? AND report_id=?'; - return cassandraClient.getReport() - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - result.should.eql(cassandraResponse.rows); - }); - }); - - it('should get failure from cassandra', function(){ - clientExecuteStub.rejects(new Error('error')); - - let query = 'SELECT * FROM reports_summary WHERE test_id=? AND report_id=?'; - return cassandraClient.getReport() - .then(function(result){ - return Promise.reject(new Error('should not get here')); - }).catch(function(err) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); - - describe('Get last report success', function(){ - it('should get last reports', function(){ - let cassandraResponse = { rows: [REPORT, REPORT, REPORT] }; - clientExecuteStub.onCall(0).resolves({ rows: [REPORT] }); - clientExecuteStub.onCall(1).resolves({ rows: [REPORT] }); - clientExecuteStub.onCall(2).resolves({ rows: [REPORT] }); - clientExecuteStub.resolves({ rows: [REPORT] }); - - let query = 'SELECT * FROM last_reports WHERE start_time_year=? AND start_time_month=? LIMIT ?'; - return cassandraClient.getLastReports(3) - .then(function (result) { - const date = new Date(); - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(date.getFullYear()); - clientExecuteStub.getCall(0).args[1][1].should.eql(date.getMonth() + 1); - clientExecuteStub.getCall(0).args[1][2].should.eql(3); - result.should.eql(cassandraResponse.rows); - sandbox.resetHistory(); - }); - }); - }); - - describe('Get last report fail', function () { - it('should get failure from cassandra', function () { - clientExecuteStub.rejects(new Error('error')); - - let query = 'SELECT * FROM reports_summary WHERE test_id=? AND report_id=?'; - return cassandraClient.getReport() - .then(function (result) { - return Promise.reject(new Error('should not get here')); - }).catch(function (err) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); - - describe('Get reports', function(){ - it('should get multiple reports', function(){ - let cassandraResponse = { rows: [REPORT, REPORT] }; - clientExecuteStub.resolves(cassandraResponse); - - let query = 'SELECT * FROM reports_summary WHERE test_id=?'; - return cassandraClient.getReports() - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - result.should.eql(cassandraResponse.rows); - }); - }); - - it('should get failure from cassandra', function(){ - clientExecuteStub.rejects(new Error('error')); - - let query = 'SELECT * FROM reports_summary WHERE test_id=?'; - return cassandraClient.getReports() - .then(function(result){ - return Promise.reject(new Error('should not get here')); - }).catch(function(err) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); - - describe('Insert new stats', function(){ - const runnerId = uuid(); - const statId = uuid(); - const statsTime = new Date().getTime(); - const phaseIndex = uuid(0); - const phaseStatus = uuid('started'); - const data = JSON.stringify({ median: 5 }); - - it('should succeed simple insert', function(){ - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let query = 'INSERT INTO reports_stats(runner_id, test_id, report_id, stats_id, stats_time, phase_index, phase_status, data) values(?,?,?,?,?,?,?,?)'; - return cassandraClient.insertStats(runnerId, testId, reportId, statId, statsTime, phaseIndex, phaseStatus, data) - .then(function(){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(runnerId); - clientExecuteStub.getCall(0).args[1][1].should.eql(testId); - clientExecuteStub.getCall(0).args[1][2].should.eql(reportId); - clientExecuteStub.getCall(0).args[1][3].should.eql(statId); - }); - }); - - it('should log error for failing inserting new report', function(){ - clientExecuteStub.rejects(); - return cassandraClient.insertStats(runnerId, testId, reportId, statId, statsTime, phaseIndex, phaseStatus, data) - .catch(function(){ - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - - describe('Subscribe Runner', function(){ - it('should subscribe runner to report', function(){ - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - - let query = 'INSERT INTO report_subscribers(test_id, report_id, runner_id, phase_status) values(?,?,?,?)'; - return cassandraClient.subscribeRunner('test_id', 'report_id', 'runner_id', 'initializing') - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql('test_id'); - clientExecuteStub.getCall(0).args[1][1].should.eql('report_id'); - clientExecuteStub.getCall(0).args[1][2].should.eql('runner_id'); - clientExecuteStub.getCall(0).args[1][3].should.eql('initializing'); - }); - }); - - it('should get failure from cassandra', function(){ - clientExecuteStub.rejects(new Error('error')); - - let query = 'SELECT * FROM reports_summary WHERE test_id=?'; - return cassandraClient.getReports() - .then(function(result){ - return Promise.reject(new Error('should not get here')); - }).catch(function(err) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - loggerErrorStub.args[0][1].message.should.eql('error'); - err.message.should.eql('Error occurred in communication with cassandra'); - }); - }); - }); - - describe('Update Subscriber', function(){ - it('should update subscriber stage in report without stats', function(){ - let cassandraResponse = { rows: [REPORT] }; - clientExecuteStub.resolves(cassandraResponse); - - let query = 'UPDATE report_subscribers SET phase_status=? WHERE test_id=? AND report_id=? AND runner_id=?'; - return cassandraClient.updateSubscriber('test_id', 'report_id', 'runner_id', 'intermediate') - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql('intermediate'); - clientExecuteStub.getCall(0).args[1][1].should.eql('test_id'); - clientExecuteStub.getCall(0).args[1][2].should.eql('report_id'); - clientExecuteStub.getCall(0).args[1][3].should.eql('runner_id'); - }); - }); - - it('should update subscriber stage in report with stats', function(){ - let cassandraResponse = { rows: [REPORT] }; - clientExecuteStub.resolves(cassandraResponse); - - let query = 'UPDATE report_subscribers SET phase_status=?, last_stats=? WHERE test_id=? AND report_id=? AND runner_id=?'; - return cassandraClient.updateSubscriberWithStats('test_id', 'report_id', 'runner_id', 'intermediate', 'last_stats') - .then(function(result){ - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql('intermediate'); - clientExecuteStub.getCall(0).args[1][1].should.eql('last_stats'); - clientExecuteStub.getCall(0).args[1][2].should.eql('test_id'); - clientExecuteStub.getCall(0).args[1][3].should.eql('report_id'); - clientExecuteStub.getCall(0).args[1][4].should.eql('runner_id'); - }); - }); - }); -}); diff --git a/tests/unit-tests/reporter/models/finalReportGenerator-test.js b/tests/unit-tests/reporter/models/finalReportGenerator-test.js index a8f119080..590c9e692 100644 --- a/tests/unit-tests/reporter/models/finalReportGenerator-test.js +++ b/tests/unit-tests/reporter/models/finalReportGenerator-test.js @@ -201,7 +201,7 @@ describe('Artillery report generator test', () => { }); describe('Bad flows - With parallelism', function () { - it('create final report fails when cassandra returns error', async () => { + it('create final report fails when sequelize returns error', async () => { databaseConnectorGetStatsStub.rejects(new Error('Database failure')); reportsManagerGetReportStub.resolves(REPORT); @@ -216,7 +216,7 @@ describe('Artillery report generator test', () => { testShouldFail.should.eql(false, 'Test action was supposed to get exception'); }); - it('create final report fails when no rows returned from cassandra ', async () => { + it('create final report fails when no rows returned from sequelize ', async () => { databaseConnectorGetStatsStub.resolves([]); reportsManagerGetReportStub.resolves(REPORT); @@ -336,4 +336,4 @@ const PARALLEL_INTERMEDIATE_ROWS = [ 'phase_index': '0', 'data': '{"timestamp":"2019-03-10T17:24:33.043Z","scenariosCreated":300,"scenariosCompleted":300,"requestsCompleted":300,"latency":{"min":59.5,"max":98.3,"median":61.3,"p95":72.9,"p99":84},"rps":{"count":300,"mean":20},"scenarioDuration":{"min":60,"max":98.9,"median":61.9,"p95":73.5,"p99":84.5},"scenarioCounts":{"Get response code 200":300},"errors":{},"codes":{"200":300},"matches":0,"customStats":{},"counters":{},"concurrency":1,"pendingRequests":1,"scenariosAvoided":0}' } -]; \ No newline at end of file +]; diff --git a/tests/unit-tests/tests/models/cassandraConnector-test.js b/tests/unit-tests/tests/models/cassandraConnector-test.js deleted file mode 100644 index d70b5b96a..000000000 --- a/tests/unit-tests/tests/models/cassandraConnector-test.js +++ /dev/null @@ -1,566 +0,0 @@ -'use strict'; -let sinon = require('sinon'); -let logger = require('../../../../src/common/logger'); -let should = require('should'); -let cassandraClient = require('../../../../src/tests/models/database/cassandra/cassandraConnector'); -let uuid = require('uuid'); -let uuidCassandraDriver = require('cassandra-driver').types.Uuid; - -describe('Cassandra client tests', function () { - let sandbox; - let clientExecuteStub; - let loggerErrorStub; - - before(() => { - sandbox = sinon.sandbox.create(); - clientExecuteStub = sandbox.stub(); - cassandraClient.init({ execute: clientExecuteStub }); - loggerErrorStub = sandbox.stub(logger, 'error'); - }); - - afterEach(() => { - sandbox.resetHistory(); - }); - - after(() => { - sandbox.restore(); - }); - - describe('Insert new test tests', function () { - it('should succeed simple insert', function () { - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let id = uuid.v4(); - let revisionId = uuid.v4(); - - let query = 'INSERT INTO tests(id, name, description, type, updated_at, raw_data, artillery_json, revision_id, file_id, csv_file_id, processor_id) values(?,?,?,?,?,?,?,?,?,?,?)'; - return cassandraClient.insertTest({ scenarios: { raw_data: 'raw' } }, { json: 'artillery' }, id, revisionId) - .then(function () { - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - clientExecuteStub.getCall(0).args[1][7].should.eql(revisionId); - clientExecuteStub.getCall(0).args[1][5].should.eql(JSON.stringify({ 'scenarios': { 'raw_data': 'raw' } })); - clientExecuteStub.getCall(0).args[1][6].should.eql(JSON.stringify({ json: 'artillery' })); - }); - }); - - it('should log error for failing inserting new test', function () { - clientExecuteStub.rejects(); - return cassandraClient.insertTest({ data: 'raw' }, { json: 'artillery' }, uuid.v4(), uuid.v4()) - .catch(function () { - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - - describe('Get single test', function () { - it('Should get single test', function () { - let query = 'SELECT * FROM tests WHERE id = ? ORDER BY updated_at DESC limit 1'; - let date = new Date(); - let cassandraResponse = { - rows: [ - { - id: 'c1656c48-e028-11e7-80c1-9a214cf093aa', - updated_at: date, - raw_data: '{"data":"raw"}', - artillery_json: '{"json":"artillery"}', - revision_id: 'c1656c48-e028-11e7-80c1-9a214cf093ab' - } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.getTest('c1656c48-e028-11e7-80c1-9a214cf093aa') - .then(function (res) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - should(clientExecuteStub.getCall(0).args[1]).eql([uuidCassandraDriver.fromString('c1656c48-e028-11e7-80c1-9a214cf093aa')]); - should(JSON.stringify(res)).eql(JSON.stringify(cassandraResponse.rows[0])); - }); - }); - - it('Should get error because of cassandra error', function () { - let query = 'SELECT * FROM tests WHERE id = ? ORDER BY updated_at DESC limit 1'; - clientExecuteStub.rejects(); - return cassandraClient.getTest('c1656c48-e028-11e7-80c1-9a214cf093aa') - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].toString().should.eql('c1656c48-e028-11e7-80c1-9a214cf093aa'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - describe('benchmark tests', () => { - it('should succeed insert benchmark', async () => { - clientExecuteStub.resolves({ result: { rowLength: 0 } }); - let id = uuid.v4(); - - let query = 'INSERT INTO benchmarks(test_id,data) values(?,?)'; - await cassandraClient.insertTestBenchmark(id, JSON.stringify({ data: 'some data' })); - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(uuidCassandraDriver.fromString(id)); - clientExecuteStub.getCall(0).args[1][1].should.eql(JSON.stringify({ data: 'some data' })); - }); - it('should get benchmark', async () => { - clientExecuteStub.resolves({ rows: [{ data: 'some data' }] }); - let id = uuid.v4(); - - let query = 'SELECT * FROM benchmarks WHERE test_id=?'; - await cassandraClient.getTestBenchmark(id); - loggerErrorStub.callCount.should.eql(0); - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].should.eql(id); - }); - }); - - describe('Delete test', function () { - it('Should delete single test successfully', () => { - let query = 'DELETE FROM tests WHERE id=?'; - let cassandraResponse = {}; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.deleteTest('c1656c48-e028-11e7-80c1-9a214cf093aa') - .then(function (res) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1].should.eql(['c1656c48-e028-11e7-80c1-9a214cf093aa']); - res.should.eql(cassandraResponse); - }); - }); - it('Should get error because of cassandra error', function () { - let query = 'DELETE FROM tests WHERE id=?'; - clientExecuteStub.rejects(); - return cassandraClient.deleteTest('c1656c48-e028-11e7-80c1-9a214cf093aa') - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].toString().should.eql('c1656c48-e028-11e7-80c1-9a214cf093aa'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - - describe('Get all test revisions', function () { - it('Should get test revisions', function () { - let query = 'SELECT * FROM tests WHERE id = ?'; - let date = new Date(); - let laterDate = new Date(); - let cassandraResponse = { - rows: [ - { - id: 'c1656c48-e028-11e7-80c1-9a214cf093aa', - updated_at: date, - raw_data: '{"data":"raw"}', - artillery_json: '{"json":"artillery"}', - revision_id: 'c1656c48-e028-11e7-80c1-9a214cf093ab' - }, - { - id: 'c1656c48-e028-11e7-80c1-9a214cf093aa', - updated_at: laterDate, - raw_data: '{"data":"raw"}', - artillery_json: '{"json":"artillery"}', - revision_id: 'c1656c48-e028-11e7-80c1-9a214cf093ac' - } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.getAllTestRevisions('c1656c48-e028-11e7-80c1-9a214cf093aa') - .then(function (res) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].toString().should.eql('c1656c48-e028-11e7-80c1-9a214cf093aa'); - res.should.eql(cassandraResponse.rows); - }).catch(function (err) { - throw new Error('Should not get here: ' + err); - }); - }); - - it('Should get error because of cassandra error', function () { - let query = 'SELECT * FROM tests WHERE id = ?'; - clientExecuteStub.rejects(); - return cassandraClient.getAllTestRevisions('c1656c48-e028-11e7-80c1-9a214cf093aa') - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql(query); - clientExecuteStub.getCall(0).args[1][0].toString().should.eql('c1656c48-e028-11e7-80c1-9a214cf093aa'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - - describe('Get all tests', function () { - it('Should get all tests', function () { - let query = 'SELECT * FROM tests'; - let date = new Date(); - let laterDate = new Date(); - let cassandraResponse = { - rows: [ - { - id: 'c1656c48-e028-11e7-80c1-9a214cf093aa', - updated_at: date, - raw_data: '{"name":"Test1","description":"Test1"}', - artillery_json: '{"json":"artillery"}', - revision_id: 'c1656c48-e028-11e7-80c1-9a214cf093ab' - }, - { - id: 'c1656c48-e028-11e7-80c1-9a214cf093ab', - updated_at: laterDate, - raw_data: '{"name":"Test2","description":"Test2"}', - artillery_json: '{"json":"artillery"}', - revision_id: 'c1656c48-e028-11e7-80c1-9a214cf093ac' - } - ] - }; - - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.getTests() - .then(function (res) { - clientExecuteStub.getCall(0).args[0].should.eql(query); - res.should.eql(cassandraResponse.rows); - }).catch(function (err) { - throw new Error('Should not get here: ' + err); - }); - }); - - it('Should get error because of cassandra error', function () { - let query = 'SELECT * FROM tests'; - clientExecuteStub.rejects(); - return cassandraClient.getTests() - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql(query); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - describe('Get dsl definition', function () { - it('Should get definition object', function () { - const cassandraResponse = { - rows: [ - { artillery_json: '{"json":"artillery"}' } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.getDslDefinition('dslName', 'definitionName') - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'SELECT * FROM dsl WHERE dsl_name = ? AND definition_name = ? limit 1', - [ - 'dslName', - 'definitionName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql({ - 'artillery_json': { - 'json': 'artillery' - } - }); - }); - }); - it('Should get definition undefined when there is no result', function () { - const cassandraResponse = { - rows: [] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.getDslDefinition('dslName', 'definitionName') - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'SELECT * FROM dsl WHERE dsl_name = ? AND definition_name = ? limit 1', - [ - 'dslName', - 'definitionName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql(undefined); - }); - }); - - it('Should get error because of cassandra error', function () { - clientExecuteStub.rejects(); - return cassandraClient.getDslDefinition('dslName', 'definitionName') - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql('SELECT * FROM dsl WHERE dsl_name = ? AND definition_name = ? limit 1'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - describe('Get dsl definitions', function () { - it('Should get array of definition object', function () { - const cassandraResponse = { - rows: [ - { artillery_json: '{"json":"artillery"}' }, - { artillery_json: '{"json":"artillery2"}' } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.getDslDefinitions('dslName') - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'SELECT * FROM dsl WHERE dsl_name = ?', - [ - 'dslName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql([ - { - 'artillery_json': { - 'json': 'artillery' - } - }, - { - 'artillery_json': { - 'json': 'artillery2' - } - } - ]); - }); - }); - it('Should get empty array when there is no result', function () { - const cassandraResponse = { - rows: [] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.getDslDefinitions('dslName') - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'SELECT * FROM dsl WHERE dsl_name = ?', - [ - 'dslName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql([]); - }); - }); - - it('Should get error because of cassandra error', function () { - clientExecuteStub.rejects(); - return cassandraClient.getDslDefinitions('dslName') - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql('SELECT * FROM dsl WHERE dsl_name = ?'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - describe('update dsl definition', function () { - it('Should get true when update applied', function () { - const cassandraResponse = { - rows: [ - { '[applied]': true } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.updateDslDefinition('dslName', 'definitionName', { json: 'artillery' }) - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'UPDATE dsl SET artillery_json= ? WHERE dsl_name = ? AND definition_name = ? IF EXISTS;', - [ - '{"json":"artillery"}', - 'dslName', - 'definitionName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql(true); - }); - }); - it('Should get false when update does not applied', function () { - const cassandraResponse = { - rows: [ - { '[applied]': false } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.updateDslDefinition('dslName', 'definitionName', { json: 'artillery' }) - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'UPDATE dsl SET artillery_json= ? WHERE dsl_name = ? AND definition_name = ? IF EXISTS;', - [ - '{"json":"artillery"}', - 'dslName', - 'definitionName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql(false); - }); - }); - - it('Should get error because of cassandra error', function () { - clientExecuteStub.rejects(); - return cassandraClient.updateDslDefinition('dslName', 'definitionName', { json: 'artillery' }) - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql('UPDATE dsl SET artillery_json= ? WHERE dsl_name = ? AND definition_name = ? IF EXISTS;'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - describe('delete dsl definition', function () { - it('Should get true when delete applied', function () { - const cassandraResponse = { - rows: [ - { '[applied]': true } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.deleteDefinition('dslName', 'definitionName') - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'DELETE FROM dsl WHERE dsl_name = ? AND definition_name = ? IF EXISTS;', - [ - 'dslName', - 'definitionName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql(true); - }); - }); - it('Should get false when delete does not applied', function () { - const cassandraResponse = { - rows: [ - { '[applied]': false } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.deleteDefinition('dslName', 'definitionName') - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'DELETE FROM dsl WHERE dsl_name = ? AND definition_name = ? IF EXISTS;', - [ - 'dslName', - 'definitionName' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql(false); - }); - }); - - it('Should get error because of cassandra error', function () { - clientExecuteStub.rejects(); - return cassandraClient.deleteDefinition('dslName', 'definitionName') - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql('DELETE FROM dsl WHERE dsl_name = ? AND definition_name = ? IF EXISTS;'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); - describe('insertDslDefinition definition', function () { - it('Should get true when insert applied', function () { - const cassandraResponse = { - rows: [ - { '[applied]': true } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.insertDslDefinition('dslName', 'definitionName', { data: 'data' }) - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'INSERT INTO dsl(dsl_name, definition_name, artillery_json) values(?,?,?) IF NOT EXISTS', - [ - 'dslName', - 'definitionName', - '{"data":"data"}' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql(true); - }); - }); - it('Should get false when insert applied does not applied', function () { - const cassandraResponse = { - rows: [ - { '[applied]': false } - ] - }; - clientExecuteStub.resolves(cassandraResponse); - return cassandraClient.insertDslDefinition('dslName', 'definitionName', { data: 'data' }) - .then(function (res) { - should(clientExecuteStub.args).eql([ - [ - 'INSERT INTO dsl(dsl_name, definition_name, artillery_json) values(?,?,?) IF NOT EXISTS', - [ - 'dslName', - 'definitionName', - '{"data":"data"}' - ], - { - 'consistency': 6, - 'prepare': true - } - ] - ]); - should(res).eql(false); - }); - }); - - it('Should get error because of cassandra error', function () { - clientExecuteStub.rejects(); - return cassandraClient.insertDslDefinition('dslName', 'definitionName', { data: 'data' }) - .then(function () { - throw new Error('Should not get here'); - }).catch(function () { - clientExecuteStub.getCall(0).args[0].should.eql('INSERT INTO dsl(dsl_name, definition_name, artillery_json) values(?,?,?) IF NOT EXISTS'); - loggerErrorStub.callCount.should.eql(1); - }); - }); - }); -}); diff --git a/tests/unit-tests/tests/models/database-test.js b/tests/unit-tests/tests/models/database-test.js deleted file mode 100644 index 60f4a54e3..000000000 --- a/tests/unit-tests/tests/models/database-test.js +++ /dev/null @@ -1,95 +0,0 @@ - -const should = require('should'), - sinon = require('sinon'), - cassandra = require('../../../../src/tests/models/database/cassandra/cassandraConnector'), - sequelizeConnector = require('../../../../src/tests/models/database/sequelize/sequelizeConnector'), - rewire = require('rewire'), - databaseConfig = require('../../../../src/config/databaseConfig'); -let database = require('../../../../src/tests/models/database'); -const functions = [ - { - functionName: 'insertTest', - args: ['testInfo', 'testJson', 'id', 'revisionId', 'processorFileId', 'csvFileId'] - }, - { - functionName: 'insertTestBenchmark', - args: ['testId', 'benchmarkData'] - }, - { - functionName: 'getTestBenchmark', - args: ['testId'] - }, - { - functionName: 'getTest', - args: ['id'] - }, - { - functionName: 'getTests', - args: [] - }, - { - functionName: 'deleteTest', - args: ['id'] - }, - { - functionName: 'insertDslDefinition', - args: ['dslName', 'definitionName', 'data'] - }, { - functionName: 'getDslDefinition', - args: ['dslName', 'definitionName'] - }, { - functionName: 'getDslDefinitions', - args: ['dslName'] - }, - { - functionName: 'updateDslDefinition', - args: ['dslName', 'definitionName', 'data'] - }, - { - functionName: 'deleteDefinition', - args: ['dslName', 'definitionName'] - } -]; - -describe('Testing database', function () { - let sandbox; - before(function () { - process.env.DATABASE_TYPE = 'CASSANDRA'; - sandbox = sinon.sandbox.create(); - functions.forEach(function (func) { - sandbox.stub(cassandra, func.functionName); - sandbox.stub(sequelizeConnector, func.functionName); - }); - }); - beforeEach(function () { - sandbox.resetHistory(); - }); - after(function () { - sandbox.restore(); - }); - - describe('when database type is cassandra - should applied functions on cassandra client', function () { - before(async function () { - databaseConfig.type = 'cassandra'; - database = rewire('../../../../src/tests/models/database'); - }); - functions.forEach(function (func) { - it(`checking func: ${func.functionName}`, async function () { - await database[func.functionName](...func.args); - should(cassandra[func.functionName].args).eql([func.args]); - }); - }); - }); - describe('when database type is not cassandra - should applied functions on sequlize client', function () { - before(async function () { - databaseConfig.type = 'not-cassandra'; - database = rewire('../../../../src/tests/models/database'); - }); - functions.forEach(function (func) { - it(`checking func: ${func.functionName}`, async function () { - await database[func.functionName](...func.args); - should(sequelizeConnector[func.functionName].args).eql([func.args]); - }); - }); - }); -});