Skip to content

Adding more instrumentation. #186

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

Merged
Merged
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
17 changes: 13 additions & 4 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const archiver = require("../helpers/archiver"),
downloadBuildArtifacts = require('../helpers/buildArtifacts').downloadBuildArtifacts,
updateNotifier = require('update-notifier'),
pkg = require('../../package.json');

module.exports = function run(args) {
let bsConfigPath = utils.getConfigPath(args.cf);
//Delete build_results.txt from log folder if already present.
Expand Down Expand Up @@ -96,6 +95,9 @@ module.exports = function run(args) {
//get the number of spec files
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressJson);

// return the number of parallels user specified
let userSpecifiedParallels = utils.getParallels(bsConfig, args);

// accept the number of parallels
utils.setParallels(bsConfig, args, specFiles.length);

Expand Down Expand Up @@ -137,6 +139,11 @@ module.exports = function run(args) {
utils.setProcessHooks(data.build_id, bsConfig, bs_local, args);
let message = `${data.message}! ${Constants.userMessages.BUILD_CREATED} with build id: ${data.build_id}`;
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${data.dashboard_url}`;
let buildReportData = {
'build_id': data.build_id,
'user_id': data.user_id,
'parallels': userSpecifiedParallels
};
utils.exportResults(data.build_id, `${config.dashboardUrl}${data.build_id}`);
if ((utils.isUndefined(bsConfig.run_settings.parallels) && utils.isUndefined(args.parallels)) || (!utils.isUndefined(bsConfig.run_settings.parallels) && bsConfig.run_settings.parallels == Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE)) {
logger.warn(Constants.userMessages.NO_PARALLELS);
Expand Down Expand Up @@ -174,7 +181,7 @@ module.exports = function run(args) {

// Generate custom report!
reportGenerator(bsConfig, data.build_id, args, function(){
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null);
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, buildReportData);
utils.handleSyncExit(exitCode, data.dashboard_url);
});
});
Expand Down Expand Up @@ -202,7 +209,8 @@ module.exports = function run(args) {
dataToSend.used_auto_local = bsConfig.connection_settings.usedAutoLocal;
}
}
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, dataToSend);
buildReportData = { ...buildReportData, ...dataToSend };
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, buildReportData);
return;
}).catch(async function (err) {
// Build creation failed
Expand Down Expand Up @@ -283,7 +291,8 @@ module.exports = function run(args) {
}).catch(function (err) {
logger.error(err);
utils.setUsageReportingFlag(null, args.disableUsageReporting);
utils.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, utils.getErrorCodeFromErr(err));
let bsJsonData = utils.readBsConfigJSON(bsConfigPath);
utils.sendUsageReport(bsJsonData, args, err.message, Constants.messageTypes.ERROR, utils.getErrorCodeFromErr(err));
process.exitCode = Constants.ERROR_EXIT_CODE;
}).finally(function(){
updateNotifier({
Expand Down
10 changes: 9 additions & 1 deletion bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,14 @@ const usageReportingConstants = {

const LATEST_VERSION_SYNTAX_REGEX = /\d*.latest(.\d*)?/gm

const AUTH_REGEX = /"auth" *: *{[\s\S]*?}/g

const ERROR_EXIT_CODE = 1;

const REDACTED = "[REDACTED]";

const REDACTED_AUTH =`auth: { "username": ${REDACTED}, "access_key": ${REDACTED} }`;

module.exports = Object.freeze({
syncCLI,
userMessages,
Expand All @@ -228,5 +234,7 @@ module.exports = Object.freeze({
METADATA_CHAR_BUFFER_PER_SPEC,
usageReportingConstants,
LATEST_VERSION_SYNTAX_REGEX,
ERROR_EXIT_CODE
ERROR_EXIT_CODE,
AUTH_REGEX,
REDACTED_AUTH
});
17 changes: 15 additions & 2 deletions bin/helpers/usageReporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const cp = require("child_process"),
const config = require('./config'),
fileLogger = require('./logger').fileLogger,
utils = require('./utils');

const { AUTH_REGEX, REDACTED_AUTH } = require("./constants");

function get_version(package_name) {
try {
Expand Down Expand Up @@ -173,19 +175,30 @@ function isUsageReportingEnabled() {
function send(args) {
if (isUsageReportingEnabled() === "true") return;

let bsConfig = args.bstack_config;
let bsConfig = JSON.parse(JSON.stringify(args.bstack_config));
let runSettings = "";
let sanitizedbsConfig = "";
let cli_details = cli_version_and_path(bsConfig);
let data = utils.isUndefined(args.data) ? {} : args.data;

if (bsConfig && bsConfig.run_settings) {
data.cypress_version = bsConfig.run_settings.cypress_version
runSettings = bsConfig.run_settings;
data.cypress_version = bsConfig.run_settings.cypress_version;
}

sanitizedbsConfig = `${(typeof bsConfig === 'string') ? bsConfig :
JSON.stringify(bsConfig)}`.replace(AUTH_REGEX, REDACTED_AUTH);

delete args.bstack_config;

const payload = {
event_type: "cypress_cli_stats",
data: {
build_hashed_id: data.build_id,
user_id: data.user_id,
parallels: data.parallels,
bstack_json: sanitizedbsConfig,
run_settings: runSettings,
os: _os(),
os_version: os_version(),
bstack_json_found_in_pwd: bstack_json_found_in_pwd(),
Expand Down
13 changes: 13 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ exports.setUsageReportingFlag = (bsConfig, disableUsageReporting) => {
}
};

exports.getParallels = (bsConfig, args) => {
return args.parallels || bsConfig['run_settings']['parallels'];
}

exports.setParallels = (bsConfig, args, numOfSpecs) => {
if (!this.isUndefined(args.parallels)) {
bsConfig["run_settings"]["parallels"] = args.parallels;
Expand Down Expand Up @@ -908,6 +912,15 @@ exports.setOtherConfigs = (bsConfig, args) => {
}
}

exports.readBsConfigJSON = (bsConfigPath) => {
try {
fs.accessSync(bsConfigPath, fs.constants.R_OK);
return fs.readFileSync(bsConfigPath, 'utf-8');
} catch (err) {
return null;
}
}

exports.getCypressJSON = (bsConfig) => {
let cypressJSON = undefined;
if (bsConfig.run_settings.cypress_config_file && bsConfig.run_settings.cypress_config_filename !== 'false') {
Expand Down
21 changes: 18 additions & 3 deletions test/unit/bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("runs", () => {
});
getErrorCodeFromErrStub = sandbox.stub().returns("random-error-code");
deleteResultsStub = sandbox.stub();
readBsConfigJSONStub = sandbox.stub().returns(null);
});

afterEach(() => {
Expand All @@ -47,7 +48,8 @@ describe("runs", () => {
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub,
deleteResults: deleteResultsStub
deleteResults: deleteResultsStub,
readBsConfigJSON: readBsConfigJSONStub
},
});

Expand Down Expand Up @@ -206,6 +208,7 @@ describe("runs", () => {

beforeEach(() => {
sandbox = sinon.createSandbox();
getParallelsStub = sandbox.stub();
setParallelsStub = sandbox.stub();
warnSpecLimitStub = sandbox.stub();
setUsernameStub = sandbox.stub();
Expand Down Expand Up @@ -257,6 +260,7 @@ describe("runs", () => {
'../helpers/utils': {
validateBstackJson: validateBstackJsonStub,
sendUsageReport: sendUsageReportStub,
getParallels: getParallelsStub,
setParallels: setParallelsStub,
warnSpecLimit: warnSpecLimitStub,
setUsername: setUsernameStub,
Expand Down Expand Up @@ -322,6 +326,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setLocalConfigFileStub);
sinon.assert.calledOnce(setCypressConfigFilenameStub);
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
sinon.assert.calledOnce(getParallelsStub);
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(warnSpecLimitStub);
sinon.assert.calledOnce(setLocalStub);
Expand Down Expand Up @@ -356,6 +361,7 @@ describe("runs", () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
validateBstackJsonStub = sandbox.stub();
getParallelsStub = sandbox.stub();
setParallelsStub = sandbox.stub();
warnSpecLimitStub = sandbox.stub();
setUsernameStub = sandbox.stub();
Expand Down Expand Up @@ -408,6 +414,7 @@ describe("runs", () => {
'../helpers/utils': {
validateBstackJson: validateBstackJsonStub,
sendUsageReport: sendUsageReportStub,
getParallels: getParallelsStub,
setParallels: setParallelsStub,
warnSpecLimit: warnSpecLimitStub,
setUsername: setUsernameStub,
Expand Down Expand Up @@ -476,6 +483,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setLocalModeStub);
sinon.assert.calledOnce(setLocalConfigFileStub);
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
sinon.assert.calledOnce(getParallelsStub);
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(warnSpecLimitStub);
sinon.assert.calledOnce(setLocalStub);
Expand Down Expand Up @@ -513,6 +521,7 @@ describe("runs", () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
validateBstackJsonStub = sandbox.stub();
getParallelsStub = sandbox.stub();
setParallelsStub = sandbox.stub();
warnSpecLimitStub = sandbox.stub();
setUsernameStub = sandbox.stub();
Expand Down Expand Up @@ -567,6 +576,7 @@ describe("runs", () => {
'../helpers/utils': {
validateBstackJson: validateBstackJsonStub,
sendUsageReport: sendUsageReportStub,
getParallels: getParallelsStub,
setParallels: setParallelsStub,
warnSpecLimit: warnSpecLimitStub,
setUsername: setUsernameStub,
Expand Down Expand Up @@ -646,6 +656,7 @@ describe("runs", () => {
sinon.assert.calledOnce(validateBstackJsonStub);
sinon.assert.calledOnce(capabilityValidatorStub);
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
sinon.assert.calledOnce(getParallelsStub);
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(warnSpecLimitStub);
sinon.assert.calledOnce(setLocalStub);
Expand Down Expand Up @@ -683,6 +694,7 @@ describe("runs", () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
validateBstackJsonStub = sandbox.stub();
getParallelsStub = sandbox.stub();
setParallelsStub = sandbox.stub();
warnSpecLimitStub = sandbox.stub()
setUsernameStub = sandbox.stub();
Expand Down Expand Up @@ -748,7 +760,7 @@ describe("runs", () => {
let errorCode = null;
let message = `Success! ${Constants.userMessages.BUILD_CREATED} with build id: random_build_id`;
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${dashboardUrl}`;
let data = {time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id', test_zip_size: 123, npm_zip_size: 123}
let data = { user_id: 1234, parallels: 10, time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id', test_zip_size: 123, npm_zip_size: 123}

const runs = proxyquire('../../../../bin/commands/runs', {
'../helpers/utils': {
Expand All @@ -762,6 +774,7 @@ describe("runs", () => {
setTestEnvs: setTestEnvsStub,
setSystemEnvs: setSystemEnvsStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getParallels: getParallelsStub,
setParallels: setParallelsStub,
warnSpecLimit: warnSpecLimitStub,
getConfigPath: getConfigPathStub,
Expand Down Expand Up @@ -837,8 +850,9 @@ describe("runs", () => {
stopLocalBinaryStub.returns(Promise.resolve("nothing"));
nonEmptyArrayStub.returns(false);
checkErrorStub.returns('test');
getParallelsStub.returns(10);
createBuildStub.returns(Promise.resolve({ message: 'Success', build_id: 'random_build_id', dashboard_url: dashboardUrl, user_id: 1234 }));
fetchZipSizeStub.returns(123);
createBuildStub.returns(Promise.resolve({ message: 'Success', build_id: 'random_build_id', dashboard_url: dashboardUrl }));

return runs(args)
.then(function (_bsConfig) {
Expand All @@ -851,6 +865,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setLocalConfigFileStub);
sinon.assert.calledOnce(capabilityValidatorStub);
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
sinon.assert.calledOnce(getParallelsStub);
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(warnSpecLimitStub);
sinon.assert.calledTwice(fetchZipSizeStub);
Expand Down
32 changes: 32 additions & 0 deletions test/unit/bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,38 @@ describe('utils', () => {
});
});

describe('getparallels', () =>{
it('should return the parallels specified by the user as arguments', ()=>{
let bsConfig = {
run_settings: {
parallels: 100,
},
};
let args = {
parallels: 200
};
expect(utils.getParallels(bsConfig, args)).to.be.eq(200);
});

it('should return the parallels specified by the user in bsconfig if not passed as arguments', ()=>{
let bsConfig = {
run_settings: {
parallels: 100,
},
};
let args = {};
expect(utils.getParallels(bsConfig, args)).to.be.eq(100);
});

it('should return the undefined if no parallels specified in bsconfig and arguments', ()=>{
let bsConfig = {
run_settings: {},
};
let args = {};
expect(utils.getParallels(bsConfig, args)).to.be.eq(undefined);
});
});

describe('checkError', () => {
it('should return error if exists', () => {
expect(utils.checkError({error: "test error"})).to.be.eq("test error");
Expand Down