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

[Cases] Fix cases flaky tests #176863

Merged
merged 19 commits into from
Feb 22, 2024
Merged

[Cases] Fix cases flaky tests #176863

merged 19 commits into from
Feb 22, 2024

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Feb 13, 2024

Summary

With the help of @umbopepato, @js-jankisalvi, and @adcoelho, we realized that one possible source of flakiness is the CasesContextProvider that every test uses under the hood. In this PR I refactor the CasesContextProvider to render immediately its children and not wait for the appId and appTitle to be defined. I make them optional and make all changes needed in the code to accommodate the presence of an optional appId. Also, I refactored some components that I believed could introduce flakiness.

Issues

Fixes #175570
Fixes #175956
Fixes #175935
Fixes #175934
Fixes #175655
Fixes #176643
Fixes #175204

Flaky test runner: https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5255, https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5261, https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5264, https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5267

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@cnasikas cnasikas requested a review from a team as a code owner February 13, 2024 19:40
@cnasikas cnasikas self-assigned this Feb 13, 2024
@cnasikas cnasikas added Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.14.0 labels Feb 13, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

@cnasikas cnasikas added v8.13.0 and removed v8.14.0 labels Feb 13, 2024
@cnasikas cnasikas added the release_note:skip Skip the PR/issue when compiling release notes label Feb 13, 2024
* `appId` and `appTitle` are dynamically retrieved from kibana context.
* We need to update the state if any of these values change, the rest of props are never updated.
*/
useEffect(() => {
Copy link
Member Author

@cnasikas cnasikas Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the useEffects to save re-render in the hope of making tests more resilient as all of them rely on the CasesProvider. Also, they are redundant.

@@ -41,7 +41,6 @@ const initialCaseValue: FormProps = {
connectorId: NONE_CONNECTOR_ID,
fields: null,
syncAlerts: true,
selectedOwner: null,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is set by the component that renders the solution selection.

@@ -61,16 +61,6 @@ const OwnerSelector = ({

const onChange = useCallback((val: string) => field.setValue(val), [field]);

useEffect(() => {
Copy link
Member Author

@cnasikas cnasikas Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant. defaultValue can produce the same results.

@@ -20,33 +20,38 @@ interface Props {
isLoading: boolean;
}

const SeverityFieldFormComponent = ({ isLoading }: { isLoading: boolean }) => {
Copy link
Member Author

@cnasikas cnasikas Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved inside UseField and remove all the watch logic.

Copy link
Contributor

@js-jankisalvi js-jankisalvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactoring 👍
Should we loop all tests just to verify once?

@cnasikas
Copy link
Member Author

Nice refactoring 👍 Should we loop all tests just to verify once?

Good point. Added here 775c87e (#176863).

externalReferenceAttachmentTypeRegistry,
features,
owner,
permissions,
Copy link
Contributor

@adcoelho adcoelho Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you end up checking if this was fine for the permissions? useMemo only uses Object.is for comparisons.

Edit: after reading the PR further I think this might be fine. It seems all we cared about here seemed to be whether or not the permissions were null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is fine as it is at the moment. If we see unnecessary renders we can optimize further.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am ok with leaving it at is but just wanted to highlight that

If we see unnecessary renders we can optimize further.

The issue is rather the permission changing in a way that does not trigger a rerender when it should be cause we only look for superficial changes to the permissions object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. This could happen if someone does permissions.create = true instead of replacing the whole object. Also, most applications initiate the ContextProvider with the permissions being returned by our own helper function here https://github.com/elastic/kibana/blob/56887ac1f85dd06bf89453637eb1a0fb562a43b9/x-pack/plugins/cases/public/client/helpers/can_use_cases.ts. The function will always return a new object. Given all that, you are right. Let's take this into account and make it more explicit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -34,12 +33,12 @@ describe.skip('UserToolTip', () => {
</UserToolTip>
);

fireEvent.mouseOver(screen.getByText('case user'));
fireEvent.mouseOver(await screen.findByText('case user'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: userEvent is the recommended class but I wonder if it takes some extra milliseconds so I don't mind keeping fireEvent.

@@ -148,8 +137,8 @@ export const CasesProvider: React.FC<{ value: CasesContextProps }> = ({
[getFilesClient, owner]
);

return isCasesContextValue(value) ? (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end, what needed to be changed to remove this check?

I hadn't realized before that isCasesContextValue also checked for value.permissions != null.

Missing appId or appTitle might be ok but can't this have worse consequences when rendering cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end, what needed to be changed to remove this check?

Some changes in the PR take into account undefined appId, mostly in navigation and when we construct the local storage keys. In practice, the appId will never be undefined when the cases app is rendered.

I hadn't realized before that isCasesContextValue also checked for value.permissions != null

The permissions is coming as a prop and it is required. I am not sure why we had the check but it will never be undefined.

@cnasikas cnasikas enabled auto-merge (squash) February 22, 2024 10:17
Copy link
Contributor

@js-jankisalvi js-jankisalvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, Great cleanup 🎉

@cnasikas cnasikas disabled auto-merge February 22, 2024 10:59
@cnasikas cnasikas enabled auto-merge (squash) February 22, 2024 12:45
@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #48 / Actions and Triggers app Rules list rules list bulk actions should apply filters to bulk actions when using the select all button

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
cases 457.0KB 456.8KB -198.0B

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
cases 170.6KB 170.8KB +168.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
cases 57 58 +1

Total ESLint disabled count

id before after diff
cases 73 74 +1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @cnasikas

@cnasikas cnasikas merged commit 0dd21e5 into elastic:main Feb 22, 2024
21 checks passed
@cnasikas cnasikas deleted the fix_various_tests branch February 22, 2024 13:24
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Feb 22, 2024
cnasikas added a commit that referenced this pull request Feb 22, 2024
## Summary

PR #176863 improved the cases
context provided which we assumed caused some flakiness in our tests. I
am unskipping a couple of tests to test if the changes on that PR
stabilized the tests.

Fixes #176805
Fixes #174528
Fixes #174527
Fixes #174526
Fixes #174525
Fixes #176600
Fixes #146394


### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
js-jankisalvi added a commit that referenced this pull request Feb 22, 2024
## Summary

As a follow-up to #176863
unskipping filterPopOver and propertyActions tests

Fixes #176679
Fixes #176680
Fixes #176681
Fixes #176682
Fixes #176683
Fixes #176684
Fixes #176685
Fixes #176686
Fixes #174384
Fixes #174667
Fixes #175310
adcoelho added a commit that referenced this pull request Feb 22, 2024
## Summary
As a follow-up to #176863 I am unskipping these severity tests to check
if the changes on that PR stabilized them.

Fixes #176336
jbudz added a commit that referenced this pull request Feb 22, 2024
cnasikas added a commit to cnasikas/kibana that referenced this pull request Feb 26, 2024
fkanout pushed a commit to fkanout/kibana that referenced this pull request Mar 4, 2024
## Summary

With the help of @umbopepato, @js-jankisalvi, and @adcoelho, we realized
that one possible source of flakiness is the `CasesContextProvider` that
every test uses under the hood. In this PR I refactor the
`CasesContextProvider` to render immediately its children and not wait
for the `appId` and `appTitle` to be defined. I make them optional and
make all changes needed in the code to accommodate the presence of an
optional `appId`. Also, I refactored some components that I believed
could introduce flakiness.

## Issues

Fixes elastic#175570
Fixes elastic#175956
Fixes elastic#175935
Fixes elastic#175934
Fixes elastic#175655
Fixes elastic#176643
Fixes elastic#175204

Flaky test runner:
https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5255,
https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5261,
https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5264,
https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5267

### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenario

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Umberto Pepato <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
fkanout pushed a commit to fkanout/kibana that referenced this pull request Mar 4, 2024
## Summary

PR elastic#176863 improved the cases
context provided which we assumed caused some flakiness in our tests. I
am unskipping a couple of tests to test if the changes on that PR
stabilized the tests.

Fixes elastic#176805
Fixes elastic#174528
Fixes elastic#174527
Fixes elastic#174526
Fixes elastic#174525
Fixes elastic#176600
Fixes elastic#146394


### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
fkanout pushed a commit to fkanout/kibana that referenced this pull request Mar 4, 2024
fkanout pushed a commit to fkanout/kibana that referenced this pull request Mar 4, 2024
## Summary
As a follow-up to elastic#176863 I am unskipping these severity tests to check
if the changes on that PR stabilized them.

Fixes elastic#176336
fkanout pushed a commit to fkanout/kibana that referenced this pull request Mar 4, 2024
cnasikas added a commit that referenced this pull request Mar 9, 2024
## Summary

This reverts commit cf942f2. It also moves the `appId` and `appTitle`
outside the cases context. The `useApplication` hook is used when needed
to get the `appId` and `appTitle`. Lastly, a new hook called
`useCasesLocalStorage` was created to be used when interacting with the
local storage. It will use the `owner` to namespace the keys of the
local storage and fallback to the `appId` if the `owner` is empty.

Fixes #175204
Fixes #175570

### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Kibana Machine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment