Skip to content

Commit

Permalink
PMM-13205 line_count tests (#812)
Browse files Browse the repository at this point in the history
* PMM-13205 line_count tests

* PMM-13205 rename line_count tests to be executed at the end

* PMM-13205 tweak line_count tests to be executed as part of FB
  • Loading branch information
yurkovychv authored Jul 12, 2024
1 parent 5cb1084 commit 31e12ec
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
55 changes: 54 additions & 1 deletion tests/custom_steps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const assert = require('assert');
const AdmZip = require('adm-zip');
const buildUrl = require('build-url');
const fs = require('fs');
const path = require('path');
const axios = require('axios');

const systemMessageText = '.page-alert-list div[data-testid^="data-testid Alert"] > div';
const systemMessageButtonClose = '.page-alert-list button';
Expand Down Expand Up @@ -51,10 +54,40 @@ module.exports = () => actor({
this.seeAttributesOnElements(locator, { disabled: null });
},

async readZipArchive(filepath) {
/**
* Downloads a zip file from the given URL and saves it to the downloads directory using REST helper.
* @param {string} url - The URL of the zip file to download.
* @param {string} filename - The name to save the downloaded file as.
* @returns {string} - The path to the downloaded file.
*/
async downloadZipFile(url, filename) {
const downloadsDir = path.join(__dirname, '..', 'downloads');

if (!fs.existsSync(downloadsDir)) {
fs.mkdirSync(downloadsDir, { recursive: true });
}

const outputPath = path.join(downloadsDir, filename);

const authHeader = `Basic ${await this.getAuth()}`;
const response = await axios.get(url, {
headers: {
Authorization: authHeader,
},
responseType: 'arraybuffer',
});

fs.writeFileSync(outputPath, response.data);

return outputPath;
},

readZipArchive(filepath, getZip = false) {
try {
const zip = new AdmZip(filepath);

if (getZip) return zip;

return zip.getEntries().map(({ name }) => name);
} catch (e) {
return Error(`Something went wrong when reading a zip file ${filepath}. ${e}`);
Expand Down Expand Up @@ -102,6 +135,26 @@ module.exports = () => actor({
});
},

/**
* Returns the number of lines in the specified file within the zip archive.
* @param {string} zipPath - The path to the zip file.
* @param {string} fileName - The name of the file to check.
* @returns {number} - The number of lines in the file.
*/
getFileLineCount(zipPath, fileName) {
const zip = this.readZipArchive(zipPath, true);

const zipEntry = zip.getEntry(fileName);

if (!zipEntry) {
throw new Error(`File ${fileName} not found in the ZIP`);
}

const fileContent = zipEntry.getData().toString('utf8');

return fileContent.split('\n').length;
},

/**
* Fluent wait for the specified callable. Callable should be async and return bool value
* Fails test if timeout exceeded.
Expand Down
4 changes: 3 additions & 1 deletion tests/helper/file_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class FileHelper extends Helper {

async fileSize(path, failOnError = true) {
try {
let stats = fs.statSync(path);
const stats = fs.statSync(path);

return stats.size;
} catch (e) {
if (!failOnError) assert.ok(false, `Could not get file size: ${path}, because of error: ${e}`);
}

return -1;
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/serverLogs_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { codeceptjsConfig } = inject();
const assert = require('assert');

Feature('Logs tests');

const filename = 'logs.zip';
const fileNameToCheck = 'pmm-managed.log';
const baseUrl = codeceptjsConfig.config.helpers.Playwright.url;

BeforeSuite(async ({ locationsAPI }) => {
// Simple request to generate 51k lines in logs
for (let i = 0; i < 10000; i++) {
await locationsAPI.getLocationsList();
}
});

// @settings-fb tag added in order to execute these tests on FB

Scenario('Verify no line_count parameter when downloading logs @settings-fb', async ({ I }) => {
const outputPath = await I.downloadZipFile(`${baseUrl}/logs.zip`, filename);
const actualLineCount = I.getFileLineCount(outputPath, fileNameToCheck);

assert.ok(actualLineCount === 50001, `File ${fileNameToCheck} has ${actualLineCount} lines, but expected 50001`);
});

Scenario('Verify line_count=10 parameter when downloading logs @settings-fb', async ({ I }) => {
const outputPath = await I.downloadZipFile(`${baseUrl}/logs.zip?line-count=10`, filename);
const actualLineCount = I.getFileLineCount(outputPath, fileNameToCheck);

assert.ok(actualLineCount === 11, `File ${fileNameToCheck} has ${actualLineCount} lines, but expected 11`);
});

Scenario('Verify line_count=-1 parameter when downloading logs @settings-fb', async ({ I }) => {
const outputPath = await I.downloadZipFile(`${baseUrl}/logs.zip?line-count=-1`, filename);
const actualLineCount = I.getFileLineCount(outputPath, fileNameToCheck);

assert.ok(actualLineCount > 50001, `File ${fileNameToCheck} has ${actualLineCount} lines, but expected more than 50001`);
});

0 comments on commit 31e12ec

Please sign in to comment.