Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run plugins tests in parallel #5530

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"apidoc": "^1.0.1",
"apidoc-template": "^0.0.2",
"docdash": "^2.0.1",
"glob": "^11.0.0",
"grunt-contrib-watch": "1.1.0",
"grunt-eslint": "24.3.0",
"grunt-mocha-nyc": "1.0.3",
Expand Down
40 changes: 31 additions & 9 deletions test/4.plugins/separation/1.setup.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
var request = require('supertest');
var should = require('should');
var testUtils = require("../../testUtils");
var plugins = require("../../../plugins/pluginManager");
let request = require('supertest');
const should = require('should');
const testUtils = require("../../testUtils");
const plugins = require("../../../plugins/pluginManager");
const { saveState, loadState, clearState } = require('./testState');

request = request(testUtils.url);

var TEMP_KEY = "";
var API_KEY_ADMIN = "";
var APP_ID = "";
var APP_KEY = "";
let TEMP_KEY = "";
let API_KEY_ADMIN = "";
let APP_ID = "";
let APP_KEY = "";

describe('Retrieve API-KEY', function() {
before('Create db connection', async function() {
// Clear any existing state
clearState();

testUtils.db = await plugins.dbConnection("countly");
testUtils.client = testUtils.db.client;
});

it('should create user', function(done) {
testUtils.db.collection("members").findOne({global_admin: true}, function(err, member) {
if (err) {
Expand All @@ -30,6 +36,22 @@ describe('Retrieve API-KEY', function() {
});
});

// Save state after each test
afterEach(function() {
const state = {
TEMP_KEY,
API_KEY_ADMIN,
API_KEY_USER: testUtils.get("API_KEY_USER"),
APP_ID,
APP_KEY,
USER_ID: testUtils.get("USER_ID"),
ADMIN_ID: testUtils.get("ADMIN_ID"),
username: testUtils.username,
email: testUtils.email
};
saveState(state);
});

describe('Creating users', function() {
describe('global admin', function() {
it('should create user', function(done) {
Expand Down Expand Up @@ -87,4 +109,4 @@ describe('Create app', function() {
done();
});
});
});
});
130 changes: 78 additions & 52 deletions test/4.plugins/separation/2.teardown.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
var request = require('supertest');
var should = require('should');
var testUtils = require("../../testUtils");
let request = require('supertest');
const should = require('should');
const testUtils = require("../../testUtils");
const { loadState, clearState } = require('./testState');

request = request(testUtils.url);

var API_KEY_ADMIN = "";
var API_KEY_USER = "";
var TEMP_KEY = "";
var APP_ID = "";
var USER_ID = "";
var ADMIN_ID = "";
let API_KEY_ADMIN = "";
let API_KEY_USER = "";
let TEMP_KEY = "";
let APP_ID = "";
let USER_ID = "";
let ADMIN_ID = "";

describe('Deleting app', function() {
it('should delete app', function(done) {
API_KEY_ADMIN = testUtils.get("API_KEY_ADMIN");
API_KEY_USER = testUtils.get("API_KEY_USER");
TEMP_KEY = testUtils.get("TEMP_KEY");
APP_ID = testUtils.get("APP_ID");
USER_ID = testUtils.get("USER_ID");
ADMIN_ID = testUtils.get("ADMIN_ID");
var params = {app_id: APP_ID};
request
.get('/i/apps/delete?api_key=' + API_KEY_ADMIN + "&args=" + JSON.stringify(params))
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
ob.should.have.property('result', 'Success');
done();
});
describe('Teardown', function() {
before(function() {
const state = loadState();
console.log("--------------", state, "--------------");
if (state) {
API_KEY_ADMIN = state.API_KEY_ADMIN;
API_KEY_USER = state.API_KEY_USER;
TEMP_KEY = state.TEMP_KEY;
APP_ID = state.APP_ID;
USER_ID = state.USER_ID;
ADMIN_ID = state.ADMIN_ID;
}
// If any value is still undefined, try to get it from testUtils
API_KEY_ADMIN = API_KEY_ADMIN || testUtils.get("API_KEY_ADMIN");
API_KEY_USER = API_KEY_USER || testUtils.get("API_KEY_USER");
TEMP_KEY = TEMP_KEY || testUtils.get("TEMP_KEY");
APP_ID = APP_ID || testUtils.get("APP_ID");
USER_ID = USER_ID || testUtils.get("USER_ID");
ADMIN_ID = ADMIN_ID || testUtils.get("ADMIN_ID");
});
});

describe('Deleting user', function() {
describe('delete simple user', function() {
it('should delete successfully', function(done) {
var params = {user_ids: [USER_ID]};
describe('Deleting app', function() {
it('should delete app', function(done) {
if (!APP_ID) {
return done();
}
var params = {app_id: APP_ID};
request
.get('/i/users/delete?api_key=' + API_KEY_ADMIN + "&args=" + JSON.stringify(params))
.get('/i/apps/delete?api_key=' + API_KEY_ADMIN + "&args=" + JSON.stringify(params))
.expect(200)
.end(function(err, res) {
if (err) {
Expand All @@ -50,23 +52,47 @@ describe('Deleting user', function() {
});
});
});
describe('delete admin', function() {
it('should delete successfully', function(done) {
var params = {user_ids: [ADMIN_ID]};
request
.get('/i/users/delete?api_key=' + TEMP_KEY + "&args=" + JSON.stringify(params))
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
ob.should.have.property('result', 'Success');
done();
});

describe('Deleting user', function() {
describe('delete simple user', function() {
it('should delete successfully', function(done) {
console.log("-----", USER_ID, "------");
var params = {user_ids: [USER_ID]};
request
.get('/i/users/delete?api_key=' + API_KEY_ADMIN + "&args=" + JSON.stringify(params))
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
ob.should.have.property('result', 'Success');
done();
});
});
});
after('Close db connection', async function() {
testUtils.client.close();
describe('delete admin', function() {
it('should delete successfully', function(done) {
console.log("-------", ADMIN_ID, "------");
var params = {user_ids: [ADMIN_ID]};
request
.get('/i/users/delete?api_key=' + TEMP_KEY + "&args=" + JSON.stringify(params))
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
ob.should.have.property('result', 'Success');
done();
});
});
after('Close db connection and clear state', async function() {
if (testUtils.client) {
await testUtils.client.close();
}
clearState();
});
});
});
});
});
43 changes: 43 additions & 0 deletions test/4.plugins/separation/testState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs');
const path = require('path');

const STATE_FILE = path.join(__dirname, 'testState.json');

function saveState(state) {
const fullState = {
API_KEY_ADMIN: state.API_KEY_ADMIN || '',
API_KEY_USER: state.API_KEY_USER || '',
TEMP_KEY: state.TEMP_KEY || '',
APP_ID: state.APP_ID || '',
USER_ID: state.USER_ID || '',
ADMIN_ID: state.ADMIN_ID || ''
};
fs.writeFileSync(STATE_FILE, JSON.stringify(fullState));
}

function loadState() {
if (fs.existsSync(STATE_FILE)) {
const state = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8'));
return {
API_KEY_ADMIN: state.API_KEY_ADMIN || '',
API_KEY_USER: state.API_KEY_USER || '',
TEMP_KEY: state.TEMP_KEY || '',
APP_ID: state.APP_ID || '',
USER_ID: state.USER_ID || '',
ADMIN_ID: state.ADMIN_ID || ''
};
}
return null;
}

function clearState() {
if (fs.existsSync(STATE_FILE)) {
fs.unlinkSync(STATE_FILE);
}
}

module.exports = {
saveState,
loadState,
clearState
};
Loading
Loading