Skip to content
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

Remove file-type dependency via jimp #137214

Merged
merged 30 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d2e6fca
Removed file-type from package.json deps.
jeramysoucy Jul 26, 2022
d94a7e5
Replaced jimp with sharp and pixelmatch. Eliminates several deps, inc…
jeramysoucy Jul 28, 2022
229e032
Fixed copy paste error, which lead to pass positives and errors.
jeramysoucy Jul 28, 2022
14f18b1
Integrated updateBaselines flag into dashboards report testing.
jeramysoucy Aug 2, 2022
c159935
Removed accidental '.only'
jeramysoucy Aug 2, 2022
49d2edf
Reverted area chart baseline.
jeramysoucy Aug 2, 2022
23bfa8d
Removed explicit declaration of sharp from package.json.
jeramysoucy Aug 2, 2022
ba4699e
Merge branch 'main' into remove-file-type-dep
jeramysoucy Aug 2, 2022
3c93a29
Adjusted dashboard screenshot size in attempt to eliminate layout sca…
jeramysoucy Aug 3, 2022
a080e82
Reverted dashboard screenshot size. Lowered tolerance in dashboard PN…
jeramysoucy Aug 3, 2022
e23c946
Changed session save location for dashboard report images for better …
jeramysoucy Aug 3, 2022
ca03c91
Fixed small dashboard baseline screenshot with correct aspect ratio.
jeramysoucy Aug 3, 2022
c1b3f41
Updated 2 basline screenshots from CI. Lowered PNG match tolerance fo…
jeramysoucy Aug 4, 2022
b830b9c
Update of dashboard controls light mode baseline.
jeramysoucy Aug 4, 2022
8a20c05
Updated dashboard controls dark mode baseline screenshot.
jeramysoucy Aug 4, 2022
59ada75
Lowering tolerance of PNG compare tests from 9% to 1% for troubleshoo…
jeramysoucy Aug 4, 2022
13897c0
Reverting tolerance of PNG compares to 0.09. Will create GH issues fo…
jeramysoucy Aug 11, 2022
f6bfe88
Implemented PngService to provide an accessible single reference to P…
jeramysoucy Aug 11, 2022
759aa2e
Updated service reference and call to png compare function left out o…
jeramysoucy Aug 11, 2022
f6a4d8c
Updated another missed PNG reference.
jeramysoucy Aug 11, 2022
ee00e7f
Removed superfluous parameter.
jeramysoucy Aug 11, 2022
41751ca
Merge remote-tracking branch 'upstream/main' into remove-file-type-dep
jeramysoucy Aug 12, 2022
e1546be
Update after bootstrap
jeramysoucy Aug 12, 2022
c2669d0
Updated area chart baseline to match new render with panel filters.
jeramysoucy Aug 12, 2022
080a9ea
Reverted limits in dashboard snapshot tests to original values.
jeramysoucy Aug 12, 2022
7a1a18d
Merge branch 'main' into remove-file-type-dep
kibanamachine Aug 14, 2022
64467a4
Merge remote-tracking branch 'upstream/main' into remove-file-type-dep
jeramysoucy Aug 24, 2022
ac8ccec
Merge and rebuild.
jeramysoucy Aug 24, 2022
7cf3519
Merge branch 'remove-file-type-dep' of github.com:jeramysoucy/kibana …
jeramysoucy Aug 24, 2022
ebc5505
Rebuild after merge.
jeramysoucy Aug 24, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ __tmp__
/x-pack/test/*/screenshots/failure
/x-pack/test/*/screenshots/session
/x-pack/test/*/screenshots/visual_regression_gallery.html
/x-pack/test/functional/apps/*/*/reporting/reports/failure

/html_docs
.eslintcache
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@
"fast-deep-equal": "^3.1.1",
"fflate": "^0.6.9",
"file-saver": "^1.3.8",
"file-type": "^10.9.0",
"font-awesome": "4.7.0",
"formik": "^2.2.9",
"fp-ts": "^2.3.1",
Expand Down Expand Up @@ -1071,6 +1070,7 @@
"@types/pbf": "3.0.2",
"@types/pdfmake": "^0.2.0",
"@types/pegjs": "^0.10.1",
"@types/pixelmatch": "^5.2.4",
"@types/pngjs": "^3.4.0",
"@types/prettier": "^2.3.2",
"@types/pretty-ms": "^5.0.0",
Expand Down Expand Up @@ -1099,6 +1099,7 @@
"@types/selenium-webdriver": "^4.1.2",
"@types/semver": "^7",
"@types/set-value": "^2.0.0",
"@types/sharp": "^0.30.4",
"@types/sinon": "^7.0.13",
"@types/source-map-support": "^0.5.3",
"@types/stats-lite": "^2.2.0",
Expand Down Expand Up @@ -1229,7 +1230,6 @@
"jest-snapshot": "^26.6.2",
"jest-specific-snapshot": "^4.0.0",
"jest-styled-components": "^7.0.3",
"jimp": "^0.14.0",
"jsdom": "^16.4.0",
"json-schema-typed": "^8.0.1",
"json5": "^1.0.1",
Expand Down Expand Up @@ -1259,6 +1259,7 @@
"peggy": "^1.2.0",
"pirates": "^4.0.1",
"piscina": "^3.2.0",
"pixelmatch": "^5.3.0",
"playwright": "^1.17.1",
"pngjs": "^3.4.0",
"postcss": "^8.4.14",
Expand Down
Binary file modified test/functional/screenshots/baseline/area_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/functional/screenshots/baseline/tsvb_dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 29 additions & 12 deletions test/functional/services/common/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { cloneDeepWith } from 'lodash';
import { Key, Origin, WebDriver } from 'selenium-webdriver';
import { modifyUrl } from '@kbn/std';

import Jimp from 'jimp';
import sharp from 'sharp';
import { WebElementWrapper } from '../lib/web_element_wrapper';
import { FtrProviderContext, FtrService } from '../../ftr_provider_context';
import { Browsers } from '../remote/browsers';
Expand Down Expand Up @@ -87,8 +87,9 @@ class BrowserService extends FtrService {
public async getScreenshotAsBitmap() {
const screenshot = await this.takeScreenshot();
const buffer = Buffer.from(screenshot, 'base64');
const session = (await Jimp.read(buffer)).clone();
return session.bitmap;

const session = sharp(buffer).png({ quality: 100, progressive: true }).clone();
return session;
}

/**
Expand All @@ -103,23 +104,36 @@ class BrowserService extends FtrService {
// We really want to set the Kibana app to a specific size without regard to the browser chrome (borders)
// But that means we first need to figure out the display scaling factor.
// NOTE: None of this is required when running Chrome headless because there's no scaling and no borders.
await this.setWindowSize(1200, 800);
const largeWidth = 1200;
const largeHeight = 800;
const smallWidth = 600;
const smallHeight = 400;

await this.setWindowSize(largeWidth, largeHeight);
const bitmap1 = await this.getScreenshotAsBitmap();
const bm1Data = await bitmap1.metadata();
this.log.debug(
`======browser======== actual initial screenshot size width=${bitmap1.width}, height=${bitmap1.height}`
`======browser======== actual initial screenshot size width=${bm1Data.width}, height=${bm1Data.height}`
);

// drasticly change the window size so we can calculate the scaling
await this.setWindowSize(600, 400);
await this.setWindowSize(smallWidth, smallHeight);
const bitmap2 = await this.getScreenshotAsBitmap();
const bm2Data = await bitmap2.metadata();
this.log.debug(
`======browser======== actual second screenshot size width= ${bitmap2.width}, height=${bitmap2.height}`
`======browser======== actual second screenshot size width= ${bm2Data.width}, height=${bm2Data.height}`
);

const xScaling = (bitmap1.width - bitmap2.width) / 600;
const yScaling = (bitmap1.height - bitmap2.height) / 400;
const xBorder = Math.round(600 - bitmap2.width / xScaling);
const yBorder = Math.round(400 - bitmap2.height / yScaling);
const bm1Width = bm1Data.width ?? smallWidth;
const bm1Height = bm1Data.height ?? smallHeight;
const bm2Width = bm2Data.width ?? smallWidth;
const bm2Height = bm2Data.height ?? smallHeight;

const xScaling = (bm1Width - bm2Width) / smallWidth;
const yScaling = (bm1Height - bm2Height) / smallHeight;
const xBorder = Math.round(600 - bm2Width / xScaling);
const yBorder = Math.round(400 - bm2Height / yScaling);

this.log.debug(
`======browser======== calculated values xBorder= ${xBorder}, yBorder=${yBorder}, xScaling=${xScaling}, yScaling=${yScaling}`
);
Expand All @@ -129,9 +143,12 @@ class BrowserService extends FtrService {
await this.setWindowSize(width + xBorder, height + yBorder);

const bitmap3 = await this.getScreenshotAsBitmap();
const bm3Data = await bitmap3.metadata();
const bm3Width = bm3Data.width ?? width;
const bm3Height = bm3Data.height ?? height;
// when there is display scaling this won't show the expected size. It will show expected size * scaling factor
this.log.debug(
`======browser======== final screenshot size width=${bitmap3.width}, height=${bitmap3.height}`
`======browser======== final screenshot size width=${bm3Width}, height=${bm3Height}`
);
}

Expand Down
1 change: 1 addition & 0 deletions test/functional/services/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type { Browser } from './browser';
export { BrowserProvider } from './browser';
export { FailureDebuggingProvider } from './failure_debugging';
export { FindProvider } from './find';
export { PngService } from './png';
export { ScreenshotsService } from './screenshots';
export { SnapshotsService } from './snapshots';
export { TestSubjects } from './test_subjects';
96 changes: 96 additions & 0 deletions test/functional/services/common/png.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { dirname } from 'path';
import { promisify } from 'util';
import { promises as fs, writeFile, readFileSync, mkdir } from 'fs';
import path from 'path';
import { comparePngs, PngDescriptor } from '../lib/compare_pngs';
import { FtrProviderContext, FtrService } from '../../ftr_provider_context';

const mkdirAsync = promisify(mkdir);
const writeFileAsync = promisify(writeFile);
jeramysoucy marked this conversation as resolved.
Show resolved Hide resolved

export class PngService extends FtrService {
private readonly log = this.ctx.getService('log');

constructor(ctx: FtrProviderContext) {
super(ctx);
}

async comparePngs(
sessionInfo: string | PngDescriptor,
baselineInfo: string | PngDescriptor,
diffPath: string,
sessionDirectory: string
) {
return await comparePngs(sessionInfo, baselineInfo, diffPath, sessionDirectory, this.log);
}

async checkIfPngsMatch(actualpngPath: string, baselinepngPath: string, folder: string) {
this.log.debug(`checkIfpngsMatch: ${actualpngPath} vs ${baselinepngPath}`);
// Copy the pngs into the session directory, as that's where the generated pngs will automatically be
// stored.
const sessionDirectoryPath = path.resolve(folder, 'session');
const failureDirectoryPath = path.resolve(folder, 'failure');

await fs.mkdir(sessionDirectoryPath, { recursive: true });
await fs.mkdir(failureDirectoryPath, { recursive: true });

const actualpngFileName = path.basename(actualpngPath, '.png');
const baselinepngFileName = path.basename(baselinepngPath, '.png');

const baselineCopyPath = path.resolve(
sessionDirectoryPath,
`${baselinepngFileName}_baseline.png`
);
const actualCopyPath = path.resolve(sessionDirectoryPath, `${actualpngFileName}_actual.png`);

// Don't cause a test failure if the baseline snapshot doesn't exist - we don't have all OS's covered and we
// don't want to start causing failures for other devs working on OS's which are lacking snapshots. We have
// mac and linux covered which is better than nothing for now.
try {
this.log.debug(`writeFile: ${baselineCopyPath}`);
await fs.writeFile(baselineCopyPath, await fs.readFile(baselinepngPath));
} catch (error) {
throw new Error(`No baseline png found at ${baselinepngPath}`);
}
this.log.debug(`writeFile: ${actualCopyPath}`);
await fs.writeFile(actualCopyPath, await fs.readFile(actualpngPath));

let diffTotal = 0;

const diffPngPath = path.resolve(failureDirectoryPath, `${baselinepngFileName}-${1}.png`);
diffTotal += await this.comparePngs(
actualCopyPath,
baselineCopyPath,
diffPngPath,
sessionDirectoryPath
);

return diffTotal;
}

async compareAgainstBaseline(
sessionPath: string,
baselinePath: string,
folder: string,
updateBaselines: boolean
) {
this.log.debug(`compareAgainstBaseline: ${sessionPath} vs ${baselinePath}`);

if (updateBaselines) {
this.log.debug('Updating baseline PNG');
await mkdirAsync(dirname(baselinePath), { recursive: true });
await writeFileAsync(baselinePath, readFileSync(sessionPath));
return 0;
} else {
return await this.checkIfPngsMatch(sessionPath, baselinePath, folder);
}
}
}
2 changes: 2 additions & 0 deletions test/functional/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BrowserProvider,
FailureDebuggingProvider,
FindProvider,
PngService,
ScreenshotsService,
SnapshotsService,
TestSubjects,
Expand Down Expand Up @@ -58,6 +59,7 @@ export const services = {
find: FindProvider,
testSubjects: TestSubjects,
docTable: DocTableService,
png: PngService,
screenshots: ScreenshotsService,
snapshots: SnapshotsService,
failureDebugging: FailureDebuggingProvider,
Expand Down
Loading