PromucFlow_constructor/app/client/cypress/scripts/cypress-split.ts
Saroj 9895ee217e
ci: Split spec improvements for cypress ci runs (#26774)
> Pull Request Template
>
> Use this template to quickly create a well written pull request.
Delete all quotes before creating the pull request.
>
## Description
> Add a TL;DR when description is extra long (helps content team)
>
> Please include a summary of the changes and which issue has been
fixed. Please also include relevant motivation
> and context. List any dependencies that are required for this change
>
> Links to Notion, Figma or any other documents that might be relevant
to the PR
>
>
#### PR fixes following issue(s)
Fixes # (issue number)
> if no issue exists, please create an issue and ask the maintainers
about this first
>
>
#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [ ] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-09-25 09:49:21 +05:30

326 lines
9.7 KiB
TypeScript

/* eslint-disable no-console */
import type { DataItem } from "./util";
import util from "./util";
import globby from "globby";
import minimatch from "minimatch";
import cypress from "cypress";
export class cypressSplit {
util = new util();
dbClient = this.util.configureDbClient();
// This function will get all the spec paths using the pattern
private async getSpecFilePaths(
specPattern: any,
ignoreTestFiles: any,
): Promise<string[]> {
const files = globby.sync(specPattern, {
ignore: ignoreTestFiles,
});
// ignore the files that doesn't match
const ignorePatterns = [...(ignoreTestFiles || [])];
// a function which returns true if the file does NOT match
const doesNotMatchAllIgnoredPatterns = (file: string) => {
// using {dot: true} here so that folders with a '.' in them are matched
const MINIMATCH_OPTIONS = { dot: true, matchBase: true };
return ignorePatterns.every((pattern) => {
return !minimatch(file, pattern, MINIMATCH_OPTIONS);
});
};
const filtered = files.filter(doesNotMatchAllIgnoredPatterns);
return filtered;
}
private async getSpecsWithTime(specs: string[], attemptId: number) {
const client = await this.dbClient.connect();
const defaultDuration = 180000;
const specsMap = new Map();
try {
const queryRes = await client.query(
'SELECT * FROM public."spec_avg_duration" ORDER BY duration DESC',
);
queryRes.rows.forEach((obj) => {
specsMap.set(obj.name, obj);
});
const allSpecsWithDuration = specs.map((spec) => {
const match = specsMap.get(spec);
return match ? match : { name: spec, duration: defaultDuration };
});
const activeRunners = await this.util.getActiveRunners();
const activeRunnersFromDb = await this.getActiveRunnersFromDb(attemptId);
return await this.util.divideSpecsIntoBalancedGroups(
allSpecsWithDuration,
Number(activeRunners) - Number(activeRunnersFromDb),
);
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
// This function will finally get the specs as a comma separated string to pass the specs to the command
private async getSpecsToRun(
specPattern: string | string[] = "cypress/e2e/**/**/*.{js,ts}",
ignorePattern: string | string[],
attemptId: number,
): Promise<string[]> {
try {
const specFilePaths = await this.getSpecFilePaths(
specPattern,
ignorePattern,
);
const specsToRun = await this.getSpecsWithTime(specFilePaths, attemptId);
return specsToRun === undefined
? []
: specsToRun[0].map((spec) => spec.name);
} catch (err) {
console.error(err);
process.exit(1);
}
}
private async getActiveRunnersFromDb(attemptId: number) {
const client = await this.dbClient.connect();
try {
const matrixRes = await client.query(
`SELECT * FROM public."matrix" WHERE "attemptId" = $1`,
[attemptId],
);
return matrixRes.rowCount;
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private async getAlreadyRunningSpecs() {
const client = await this.dbClient.connect();
try {
const dbRes = await client.query(
`SELECT name FROM public."specs"
WHERE "matrixId" IN
(SELECT id FROM public."matrix"
WHERE "attemptId" = (
SELECT id FROM public."attempt" WHERE "workflowId" = $1 and "attempt" = $2
)
)`,
[this.util.getVars().runId, this.util.getVars().attempt_number],
);
const specs: string[] =
dbRes.rows.length > 0 ? dbRes.rows.map((row) => row.name) : [];
return specs;
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private async createAttempt() {
const client = await this.dbClient.connect();
try {
const runResponse = await client.query(
`INSERT INTO public."attempt" ("workflowId", "attempt", "repo", "committer", "type", "commitMsg", "branch")
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT ("workflowId", attempt) DO NOTHING
RETURNING id;`,
[
this.util.getVars().runId,
this.util.getVars().attempt_number,
this.util.getVars().repository,
this.util.getVars().committer,
this.util.getVars().tag,
this.util.getVars().commitMsg,
this.util.getVars().branch,
],
);
if (runResponse.rows.length > 0) {
return runResponse.rows[0].id;
} else {
const res = await client.query(
`SELECT id FROM public."attempt" WHERE "workflowId" = $1 AND attempt = $2`,
[this.util.getVars().runId, this.util.getVars().attempt_number],
);
return res.rows[0].id;
}
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private async createMatrix(attemptId: number) {
const client = await this.dbClient.connect();
try {
const matrixResponse = await client.query(
`INSERT INTO public."matrix" ("workflowId", "matrixId", "status", "attemptId")
VALUES ($1, $2, $3, $4)
ON CONFLICT ("matrixId", "attemptId") DO NOTHING
RETURNING id;`,
[
this.util.getVars().runId,
this.util.getVars().thisRunner,
"started",
attemptId,
],
);
return matrixResponse.rows[0].id;
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private async getFailedSpecsFromPreviousRun(
workflowId = Number(this.util.getVars().runId),
attempt_number = Number(this.util.getVars().attempt_number) - 1,
) {
const client = await this.dbClient.connect();
try {
const dbRes = await client.query(
`SELECT name FROM public."specs"
WHERE "matrixId" IN
(SELECT id FROM public."matrix"
WHERE "attemptId" = (
SELECT id FROM public."attempt" WHERE "workflowId" = $1 and "attempt" = $2
)
) AND status IN ('fail', 'queued', 'in-progress')`,
[workflowId, attempt_number],
);
const specs: string[] =
dbRes.rows.length > 0 ? dbRes.rows.map((row) => row.name) : [];
return specs;
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private async addSpecsToMatrix(matrixId: number, specs: string[]) {
const client = await this.dbClient.connect();
try {
for (const spec of specs) {
const res = await client.query(
`INSERT INTO public."specs" ("name", "matrixId", "status") VALUES ($1, $2, $3) RETURNING id`,
[spec, matrixId, "queued"],
);
}
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private async addLockGetTheSpecs(
attemptId: number,
specPattern: string | string[],
ignorePattern: string | string[],
) {
const client = await this.dbClient.connect();
let specs: string[] = [];
let locked = false;
try {
while (!locked) {
const result = await client.query(
`UPDATE public."attempt" SET is_locked = true WHERE id = $1 AND is_locked = false RETURNING id`,
[attemptId],
);
if (result.rows.length === 1) {
locked = true;
let runningSpecs: string[] =
(await this.getAlreadyRunningSpecs()) ?? [];
if (typeof ignorePattern === "string") {
runningSpecs.push(ignorePattern);
ignorePattern = runningSpecs;
} else {
ignorePattern = runningSpecs.concat(ignorePattern);
}
specs = await this.getSpecsToRun(
specPattern,
ignorePattern,
attemptId,
);
return specs;
} else {
await this.sleep(5000);
}
}
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private async updateTheSpecsAndReleaseLock(
attemptId: number,
specs: string[],
) {
const client = await this.dbClient.connect();
try {
const matrixRes = await this.createMatrix(attemptId);
await this.addSpecsToMatrix(matrixRes, specs);
await client.query(
`UPDATE public."attempt" SET is_locked = false WHERE id = $1 AND is_locked = true RETURNING id`,
[attemptId],
);
} catch (err) {
console.log(err);
} finally {
client.release();
}
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
public async splitSpecs(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions,
) {
try {
let specPattern = config.specPattern;
let ignorePattern: string | string[] = config.excludeSpecPattern;
const cypressSpecs = this.util.getVars().cypressSpecs;
const defaultSpec = "cypress/scripts/no_spec.ts";
if (cypressSpecs != "")
specPattern = cypressSpecs?.split(",").filter((val) => val !== "");
if (this.util.getVars().cypressRerun === "true") {
specPattern =
(await this.getFailedSpecsFromPreviousRun()) ?? defaultSpec;
}
const attempt = await this.createAttempt();
const specs =
(await this.addLockGetTheSpecs(attempt, specPattern, ignorePattern)) ??
[];
if (specs.length > 0 && !specs.includes(defaultSpec)) {
config.specPattern = specs.length == 1 ? specs[0] : specs;
await this.updateTheSpecsAndReleaseLock(attempt, specs);
} else {
config.specPattern = defaultSpec;
}
return config;
} catch (err) {
console.log(err);
} finally {
this.dbClient.end();
}
}
}