From 31e12ec570123681b3849c169cbcd23391cd7afa Mon Sep 17 00:00:00 2001 From: Vasyl Yurkovych <59879559+yurkovychv@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:52:21 +0300 Subject: [PATCH] PMM-13205 line_count tests (#812) * 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 --- tests/custom_steps.js | 55 ++++++++++++++++++++++++++++++++++++- tests/helper/file_helper.js | 4 ++- tests/serverLogs_test.js | 38 +++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tests/serverLogs_test.js diff --git a/tests/custom_steps.js b/tests/custom_steps.js index 078776a75..8a35bde2d 100644 --- a/tests/custom_steps.js +++ b/tests/custom_steps.js @@ -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'; @@ -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}`); @@ -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. diff --git a/tests/helper/file_helper.js b/tests/helper/file_helper.js index e5d7b1ced..fb4ce00fa 100644 --- a/tests/helper/file_helper.js +++ b/tests/helper/file_helper.js @@ -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; } } diff --git a/tests/serverLogs_test.js b/tests/serverLogs_test.js new file mode 100644 index 000000000..412262b82 --- /dev/null +++ b/tests/serverLogs_test.js @@ -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`); +});