E2E: Add PDF/CSV/Image comparison tasks (#99104)

* E2E: Add tasks for new reporting tests

* try to switch to pdf-parse

* fix pdf comparison

* add log

* increase threshold

* clean up

* apply review feedback

* improve logs + fix local setup for reporting

* format
pull/100278/head
Agnès Toulet 3 months ago committed by GitHub
parent 158c5443b5
commit 0beb7e668b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 94
      e2e/cypress/plugins/smtpTester.js
  2. 2
      package.json
  3. 4
      scripts/grafana-server/custom.ini
  4. 5
      scripts/grafana-server/start-server
  5. 1915
      yarn.lock

@ -1,3 +1,6 @@
const fs = require('fs');
const { Jimp, diff } = require('jimp');
const pdf = require('pdf-parse');
const ms = require('smtp-tester');
const PORT = 7777;
@ -29,6 +32,97 @@ const initialize = (on, config) => {
return lastEmail[email] || null;
},
});
on('task', {
async compareImages({ expectedImageFilepath, newImageContent, updateExpectedImage = false }) {
const inputBuffer = Buffer.from(newImageContent.data);
if (updateExpectedImage) {
fs.writeFileSync(expectedImageFilepath, inputBuffer);
return true;
}
const inputImage = await Jimp.read(inputBuffer);
const expectedImage = await Jimp.read(expectedImageFilepath);
const pixelDiff = diff(expectedImage, inputImage, 0.3);
return pixelDiff.percent <= 0.1;
},
});
on('task', {
async compareCSVs({ expectedCSVFilepath, newCSVContent, updateExpectedCSV = false }) {
const inputBuffer = Buffer.from(newCSVContent.data);
if (updateExpectedCSV) {
await fs.writeFileSync(expectedCSVFilepath, inputBuffer);
return true;
}
const inputCSV = toCSV(inputBuffer);
const expectedCSV = toCSV(fs.readFileSync(expectedCSVFilepath));
if (inputCSV.length !== expectedCSV.length) {
return false;
}
for (let i = 0; i < expectedCSV.length; i++) {
const line = expectedCSV[i];
for (let j = 0; j < line.length; j++) {
if (line[j] !== inputCSV[i][j]) {
return false;
}
}
}
return true;
},
});
on('task', {
async comparePDFs({ expectedPDFFilepath, newPDFContent, updateExpectedPDF = false }) {
const inputBuffer = Buffer.from(newPDFContent.data);
if (updateExpectedPDF) {
fs.writeFileSync(expectedPDFFilepath, inputBuffer);
return true;
}
const inputDoc = await pdf(inputBuffer);
const expectedDoc = await pdf(expectedPDFFilepath);
removePDFGeneratedOnDate(inputDoc);
removePDFGeneratedOnDate(expectedDoc);
if (inputDoc.numpages !== expectedDoc.numpages) {
console.log('PDFs do not contain the same number of pages');
return false;
}
if (inputDoc.text !== expectedDoc.text) {
console.log('PDFs do not contain the same text');
console.log('PDF expected text: ', expectedDoc.text);
console.log('PDF input text: ', inputDoc.text);
return false;
}
return true;
},
});
};
const toCSV = (buffer) => {
return buffer
.toString()
.split('\n')
.map((e) => e.trim())
.map((e) => e.split(',').map((e) => e.trim()));
};
// remove the date part of the "Generated on <date>" header in PDFs as it's too complicated to set it to a fixed date
const removePDFGeneratedOnDate = (pdfDoc) => {
const regex = /(Generated on )(.*)(Data time range)/;
// removes the text in the second set of parenthesis of the regex above
pdfDoc.text = pdfDoc.text.replace(regex, (match, p1, p2, p3) => {
return `${p1} ${p3}`;
});
};
exports.initialize = initialize;

@ -206,6 +206,7 @@
"jest-junit": "16.0.0",
"jest-matcher-utils": "29.7.0",
"jest-watch-typeahead": "^2.2.2",
"jimp": "^1.6.0",
"jsdom-testing-mocks": "^1.13.1",
"knip": "^5.10.0",
"lerna": "8.1.8",
@ -216,6 +217,7 @@
"node-notifier": "10.0.1",
"nx": "19.8.2",
"openapi-types": "^12.1.3",
"pdf-parse": "^1.1.1",
"postcss": "8.5.1",
"postcss-loader": "8.1.1",
"postcss-reporter": "7.1.0",

@ -15,3 +15,7 @@ type=sqlite3
wal=true
max_idle_conn = 2
max_open_conn = 2
[smtp]
enabled = true
host = localhost:7777

@ -48,6 +48,11 @@ elif [ -d "../e2e/test-plugins" ]; then
cp -r "../e2e/test-plugins" "$RUNDIR/data/plugins"
fi
if [ "$INSTALL_IMAGE_RENDERER" ]; then
echo -e "Installing image renderer"
$RUNDIR/bin/"$ARCH"grafana cli --pluginsDir "$RUNDIR/data/plugins" plugins install grafana-image-renderer
fi
echo -e "Copy provisioning setup from devenv"
cp devenv/datasources.yaml $PROV_DIR/datasources

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save