Skip to content

Commit

Permalink
Rm video processing
Browse files Browse the repository at this point in the history
  • Loading branch information
MitskevichVS committed Aug 28, 2022
1 parent 1bdc353 commit 9551b69
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 528 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ module.exports = {
'consistent-return': 0,
camelcase: 0,
'no-underscore-dangle': 0,
'no-promise-executor-return': 0,
'no-template-curly-in-string': 0,
'new-cap': 0,
},
};
10 changes: 5 additions & 5 deletions lib/cypress-zebrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class ZbrReporter extends Mocha.reporters.Base {
});
this.worker.send({ event: workerEvents.WORKER_INIT, config });

const processConfigEvent = (config) => {
cypressConfig = config;
const processConfigEvent = (cyConfig) => {
cypressConfig = cyConfig;
};
const registerBrowserEvent = (browser) => {
this.worker.send({
Expand All @@ -57,7 +57,7 @@ class ZbrReporter extends Mocha.reporters.Base {
(server) => {
server.off(EVENTS.CONFIG, '*');
server.off(EVENTS.REG_BROWSER, '*');
}
},
);

ZbrReporter.worker = this.worker;
Expand All @@ -75,7 +75,7 @@ class ZbrReporter extends Mocha.reporters.Base {
.on(EVENT_SUITE_BEGIN, (suite) => {
this.worker.send({
event: EVENT_SUITE_BEGIN,
suite: parseSuiteStartObject(suite, this.runner.suite.file)
suite: parseSuiteStartObject(suite, this.runner.suite.file),
});
})
.on(EVENT_SUITE_END, (suite) => {
Expand Down Expand Up @@ -138,7 +138,7 @@ class ZbrReporter extends Mocha.reporters.Base {
})
.once(EVENT_RUN_END, async () => {
this.worker.send({
event: EVENT_RUN_END
event: EVENT_RUN_END,
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/log-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require('winston-daily-rotate-file');

class LogUtil {
constructor(configResolver) {
const myFormat = printf(({ level, message, timestamp }) => `${timestamp} ${level}: ${message}`);
const myFormat = printf(({ level, message, timestamp: myFormatTimestamp }) => `${myFormatTimestamp} ${level}: ${message}`);

const loggingEnabled = configResolver.getLoggingEnabled();

Expand Down
33 changes: 1 addition & 32 deletions lib/object-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,8 @@ const parseSuiteEndObject = (suite) => ({
endTime: new Date().valueOf(),
});

// all tests info for video split parse in this function(EVENT_SUITE_END)
//! not used?
const parseSuiteTestsDurations = (suite, cypressConfig) => {
if (cypressConfig.video) {
if (suite.suites.length && suite.suites[0].tests.length) {
const tests = [];
for (const test of suite.suites[0].tests) {
tests.push({
title: test.title,
state: test.state,
id: test.id,
uniqueId: Buffer.from(`${test.id}-${test.fullTitle()}`, 'utf-8').toString('base64'),
duration: test.wallClockDuration,
});
}

return {
suiteTitle: suite.suites[0].title,
suiteFileName: suite.file,
videoFolder: cypressConfig.videosFolder,
tests,
};
}
// for no tests
return null;
}
// for video disabled
return undefined;
};

const testParse = (suite, arr) => {
suite.map((s) => {
suite.forEach((s) => {
s.tests.forEach((test) => {
arr.push({
title: test.title,
Expand Down Expand Up @@ -119,6 +89,5 @@ module.exports = {
parseSuiteStartObject,
parseSuiteEndObject,
parseTestInfo,
parseSuiteTestsDurations,
testParse,
};
112 changes: 16 additions & 96 deletions lib/plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
const ipc = require('node-ipc');
const { connectToZbrIPC } = require('./../ipc/client');
const { EVENTS } = require('./../ipc/events');
const fs = require('fs');
const { mapJsonReviver, getFilesizeInBytes } = require('../utils');
const { splitFullVideoIntoParts } = require('../video-splitter');
const { HttpClient, jsonHeaders, multipartDataHeaders } = require('../api-client-axios.js');
const { connectToZbrIPC } = require('../ipc/client');
const { EVENTS } = require('../ipc/events');
const { HttpClient, jsonHeaders } = require('../api-client-axios');
const { ConfigResolver } = require('../config-resolver');
const FormData = require('form-data');
const LogUtil = require('../log-util').LogUtil;
const { LogUtil } = require('../log-util');
const { urls, getRefreshToken } = require('../request-builder');
const path = require('path');

const registerZbrPlugin = (on, config) => {
console.log('zbr plugin execution was started');
Expand All @@ -21,13 +16,12 @@ const registerZbrPlugin = (on, config) => {
zbr_registerBrowser(browser) {
connectPromise.then(() => {
ipc.of[ipcConnectionAlias].emit(EVENTS.REG_BROWSER, browser);
})
});
return null;
},
});

on('after:run', async (results) => {
return new Promise(resolve => setTimeout(resolve, process.env.ZBR_RUN_END_TIMEOUT || 10000)).then(async () => {
on('after:run', async (results) => new Promise((resolve) => setTimeout(resolve, process.env.ZBR_RUN_END_TIMEOUT || 10000)).then(async () => {
console.log('--- after:run event:');
console.log('results: ', results);
const configResolver = new ConfigResolver(results.config);
Expand All @@ -37,112 +31,38 @@ const registerZbrPlugin = (on, config) => {

const refreshToken = async () => {
if (!accessToken) {
const res = await httpClient.callPost(urls.URL_REFRESH, getRefreshToken(configResolver.getReportingServerAccessToken()), jsonHeaders.headers, true)
const token = res.data.authTokenType + ' ' + res.data.authToken
const res = await httpClient.callPost(urls.URL_REFRESH, getRefreshToken(configResolver.getReportingServerAccessToken()), jsonHeaders.headers, true);
const token = `${res.data.authTokenType} ${res.data.authToken}`;
accessToken = token;
}
return accessToken;
}
};

const getHeadersWithAuth = async (basicHeaders) => {
const authToken = await refreshToken();
if (authToken) {
let authHeaders = basicHeaders.headers;
authHeaders['Authorization'] = authToken;
const authHeaders = basicHeaders.headers;
authHeaders.Authorization = authToken;
return authHeaders;
}
}
};

const getAllStoredTestsData = async () => {
const headers = await getHeadersWithAuth(jsonHeaders);
return httpClient.callGet(urls.URL_ZE_TESTS_DATA_SAVE, headers);
}

const safelyParseJSON = (json) => {
let parsed;
try {
parsed = JSON.parse(json, mapJsonReviver);
} catch (e) {
console.log('JSON parse error', e);
}

return parsed;
}
};

const response = await getAllStoredTestsData();
const runId = response.data?.items?.find((item) => item.key === `runId${process.env.HOSTNAME || ''}`)?.value;
const testsDataString = response.data?.items?.find((item) => item.key === `${runId}${process.env.HOSTNAME || ''}`)?.value;
const testsData = testsDataString && safelyParseJSON(testsDataString);
const durationsData = response.data?.items?.find((item) => item.key === `zebSuiteTestsData${process.env.HOSTNAME || ''}`)?.value;
const testsDurationData = durationsData && safelyParseJSON(durationsData);

const sendVideo = async (videoFilePath, runId, zbrSessionId) => {
try {
if (fs.existsSync(videoFilePath)) {
const url = urls.URL_SEND_SESSION_ARTIFACTS.replace('${testRunId}', runId).replace('${testSessionId}', zbrSessionId);
const headers = await getHeadersWithAuth(multipartDataHeaders);
const formData = new FormData();
formData.append('video', fs.createReadStream(videoFilePath));
headers['Content-Type'] = formData.getHeaders()['content-type'];
headers['x-zbr-video-content-length'] = getFilesizeInBytes(videoFilePath);

return httpClient.callPost(url, formData, headers, true);
}
} catch (err) {
console.error(err);
return new Promise(resolve => { resolve() });
}
}

let promises = [];
if (testsDurationData) {
await splitFullVideoIntoParts(testsDurationData, logger);
testsDurationData?.forEach(tests => {
tests.forEach(async (test) => {
console.log('test: ', test);
console.log('testsData test: ', testsData.get(test.uniqueId));
if (test.videoFilePath && testsData?.get(test.uniqueId)) {
//video is sent to all tests. check test.state === 'failed' for only failed tests
testsData.get(test.uniqueId).videoFilePath = test.videoFilePath;
promises.push(new Promise((resolve, reject) => {
sendVideo(test.videoFilePath, runId, testsData.get(test.uniqueId).zbrSessionId)
.then((result) => {
console.log('result', result);
return resolve(result);
})
.catch((err) => {
console.log('err', err);
return reject(err);
})
}));
} else {
logger.info(path.basename(__filename), 'video will not be pushed. video not exists');
logger.debug(path.basename(__filename), `unexisted video path: ${test.videoFilePath}`);
}
})
})
}

// finish test run:
const registerTestRunFinish = async () => {
const headers = await getHeadersWithAuth(jsonHeaders);
return httpClient.callPut(urls.URL_FINISH_RUN.concat(runId), { endedAt: new Date( Date.now() - (process.env.ZBR_RUN_END_TIMEOUT || 10000 ))}, headers);
}
return httpClient.callPut(urls.URL_FINISH_RUN.concat(runId), { endedAt: new Date(Date.now() - (process.env.ZBR_RUN_END_TIMEOUT || 10000)) }, headers);
};

await registerTestRunFinish();

if (promises.length > 0) {
console.log('Video is sending');
return await new Promise((resolve, reject) => {
return Promise.all(promises).then((results) => {
console.log(results);
console.log('Completed');
return resolve();
});
});
}
})
})
}));
};

module.exports = registerZbrPlugin;
Loading

0 comments on commit 9551b69

Please sign in to comment.