-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Warning Message About Custom Result Index on Production Clusters …
…Despite Existing Indices (#759) Customers were receiving a warning message about the detectors we have created on a production cluster. The message incorrectly warned that the result index for the detector is custom and is not present on the cluster, and would be recreated when real-time or historical detection starts for the detector. However, real-time detection had already been started for these detectors, and the result index did exist. Customers could consistently reproduce the bug, although I only succeeded once when creating a detector immediately after a cluster started. The issue may arise from a potential race condition in the code when the statement finishes before the cat indices call completes (see this code segment https://github.com/opensearch-project/anomaly-detection-dashboards-plugin/blob/main/public/pages/DetectorDetail/containers/DetectorDetail.tsx#L136-L140). This PR adds a check to ensure the cat indices call has finished before concluding that the index does not exist. Additionally, we check if the visible indices are empty. If they are, it likely indicates an issue retrieving existing indices. To be cautious, we choose not to show the error message and consider the result index as not missing. This PR also resolves a warning message encountered during test runs due to the failure to mock HTMLCanvasElement. This was addressed by following the solution provided here: https://stackoverflow.com/questions/48828759/unit-test-raises-error-because-of-getcontext-is-not-implemented console.error Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package) at module.exports (/Users/kaituo/code/github/OpenSearch-Dashboards/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17) at HTMLCanvasElementImpl.getContext (/Users/kaituo/code/github/OpenSearch-Dashboards/node_modules/jsdom/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js:42:5) at HTMLCanvasElement.getContext (/Users/kaituo/code/github/OpenSearch-Dashboards/node_modules/jsdom/lib/jsdom/living/generated/HTMLCanvasElement.js:131:58) at Object.23352 (/Users/kaituo/code/github/OpenSearch-Dashboards/plugins/anomaly-detection-dashboards-plugin/node_modules/plotly.js-dist/plotly.js:201984:42) Testing Done: * Conducted end-to-end testing to confirm there is no regression due to these changes. * Added unit tests. Signed-off-by: Kaituo Li <[email protected]>
- Loading branch information
Showing
6 changed files
with
313 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
275 changes: 275 additions & 0 deletions
275
public/pages/DetectorDetail/containers/__tests__/CustomIndexErrorMsg.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,275 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { DetectorDetail, DetectorRouterProps } from '../DetectorDetail'; | ||
import { Provider } from 'react-redux'; | ||
import { | ||
HashRouter as Router, | ||
RouteComponentProps, | ||
Route, | ||
Switch, | ||
Redirect, | ||
} from 'react-router-dom'; | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import { httpClientMock, coreServicesMock } from '../../../../../test/mocks'; | ||
import { CoreServicesContext } from '../../../../components/CoreServices/CoreServices'; | ||
import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils'; | ||
import { useFetchDetectorInfo } from '../../../CreateDetectorSteps/hooks/useFetchDetectorInfo'; | ||
|
||
jest.mock('../../hooks/useFetchMonitorInfo'); | ||
|
||
//jest.mock('../../../CreateDetectorSteps/hooks/useFetchDetectorInfo'); | ||
jest.mock('../../../CreateDetectorSteps/hooks/useFetchDetectorInfo', () => ({ | ||
// The jest.mock function is used at the top level of the test file to mock the entire module. | ||
// Within each test, the mock implementation for useFetchDetectorInfo is set using jest.Mock. | ||
// This ensures that the hook returns the desired values for each test case. | ||
useFetchDetectorInfo: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../../services', () => ({ | ||
...jest.requireActual('../../../../services'), | ||
|
||
getDataSourceEnabled: () => ({ | ||
enabled: false, | ||
}), | ||
})); | ||
|
||
const detectorId = '4QY4YHEB5W9C7vlb3Mou'; | ||
|
||
// Configure the mock store | ||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
|
||
const renderWithRouter = (detectorId: string, initialState: any) => ({ | ||
...render( | ||
<Provider store={mockStore(initialState)}> | ||
<Router> | ||
<Switch> | ||
<Route | ||
path={`/detectors/${detectorId}/results`} | ||
render={(props: RouteComponentProps<DetectorRouterProps>) => { | ||
const testProps = { | ||
...props, | ||
match: { | ||
params: { detectorId: detectorId }, | ||
isExact: false, | ||
path: '', | ||
url: '', | ||
}, | ||
}; | ||
return ( | ||
<CoreServicesContext.Provider value={coreServicesMock}> | ||
<DetectorDetail {...testProps} /> | ||
</CoreServicesContext.Provider> | ||
); | ||
}} | ||
/> | ||
<Redirect from="/" to={`/detectors/${detectorId}/results`} /> | ||
</Switch> | ||
</Router> | ||
</Provider> | ||
), | ||
}); | ||
|
||
const resultIndex = 'opensearch-ad-plugin-result-test-query2'; | ||
|
||
describe('detector detail', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('detector info still loading', () => { | ||
const detectorInfo = { | ||
detector: getRandomDetector(true, resultIndex), | ||
hasError: false, | ||
isLoadingDetector: true, | ||
errorMessage: undefined, | ||
}; | ||
|
||
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); | ||
|
||
const initialState = { | ||
opensearch: { | ||
indices: [{ health: 'green', index: resultIndex }], | ||
requesting: false, | ||
}, | ||
ad: { | ||
detectors: {}, | ||
}, | ||
alerting: { | ||
monitors: {}, | ||
}, | ||
}; | ||
|
||
renderWithRouter(detectorId, initialState); | ||
const element = screen.queryByTestId('missingResultIndexCallOut'); | ||
|
||
// Assert that the element is not in the document | ||
expect(element).toBeNull(); | ||
}); | ||
|
||
test('detector has no result index', () => { | ||
const detectorInfo = { | ||
detector: getRandomDetector(true, undefined), | ||
hasError: false, | ||
isLoadingDetector: true, | ||
errorMessage: undefined, | ||
}; | ||
|
||
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); | ||
|
||
const initialState = { | ||
opensearch: { | ||
indices: [{ health: 'green', index: resultIndex }], | ||
requesting: false, | ||
}, | ||
ad: { | ||
detectors: {}, | ||
}, | ||
alerting: { | ||
monitors: {}, | ||
}, | ||
}; | ||
|
||
renderWithRouter(detectorId, initialState); | ||
const element = screen.queryByTestId('missingResultIndexCallOut'); | ||
|
||
// Assert that the element is not in the document | ||
expect(element).toBeNull(); | ||
}); | ||
|
||
test('cat indices are being requested', () => { | ||
const detectorInfo = { | ||
detector: getRandomDetector(true, resultIndex), | ||
hasError: false, | ||
isLoadingDetector: false, | ||
errorMessage: undefined, | ||
}; | ||
|
||
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); | ||
|
||
const initialState = { | ||
opensearch: { | ||
indices: [], | ||
requesting: true, | ||
}, | ||
ad: { | ||
detectors: {}, | ||
}, | ||
alerting: { | ||
monitors: {}, | ||
}, | ||
}; | ||
|
||
renderWithRouter(detectorId, initialState); | ||
const element = screen.queryByTestId('missingResultIndexCallOut'); | ||
|
||
// Assert that the element is not in the document | ||
expect(element).toBeNull(); | ||
}); | ||
|
||
test('visible indices are empty', () => { | ||
const detectorInfo = { | ||
detector: getRandomDetector(true, resultIndex), | ||
hasError: false, | ||
isLoadingDetector: false, | ||
errorMessage: undefined, | ||
}; | ||
|
||
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); | ||
|
||
const initialState = { | ||
opensearch: { | ||
indices: [], | ||
requesting: false, | ||
}, | ||
ad: { | ||
detectors: {}, | ||
}, | ||
alerting: { | ||
monitors: {}, | ||
}, | ||
}; | ||
|
||
renderWithRouter(detectorId, initialState); | ||
const element = screen.queryByTestId('missingResultIndexCallOut'); | ||
|
||
// Assert that the element is not in the document | ||
expect(element).toBeNull(); | ||
}); | ||
|
||
test('the result index is not found in the visible indices', () => { | ||
const detectorInfo = { | ||
detector: getRandomDetector(true, resultIndex), | ||
hasError: false, | ||
isLoadingDetector: false, | ||
errorMessage: undefined, | ||
}; | ||
|
||
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); | ||
|
||
const initialState = { | ||
opensearch: { | ||
indices: [{ health: 'green', index: '.kibana_-962704462_v992471_1' }], | ||
requesting: false, | ||
}, | ||
ad: { | ||
detectors: {}, | ||
}, | ||
alerting: { | ||
monitors: {}, | ||
}, | ||
}; | ||
|
||
renderWithRouter(detectorId, initialState); | ||
const element = screen.queryByTestId('missingResultIndexCallOut'); | ||
|
||
// Assert that the element is in the document | ||
expect(element).not.toBeNull(); | ||
}); | ||
|
||
test('the result index is found in the visible indices', () => { | ||
const detector = getRandomDetector(true, resultIndex); | ||
|
||
// Set up the mock implementation for useFetchDetectorInfo | ||
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => ({ | ||
detector: detector, | ||
hasError: false, | ||
isLoadingDetector: false, | ||
errorMessage: undefined, | ||
})); | ||
|
||
const initialState = { | ||
opensearch: { | ||
indices: [ | ||
{ health: 'green', index: '.kibana_-962704462_v992471_1' }, | ||
{ health: 'green', index: resultIndex }, | ||
], | ||
requesting: false, | ||
}, | ||
ad: { | ||
detectors: {}, | ||
}, | ||
alerting: { | ||
monitors: {}, | ||
}, | ||
}; | ||
|
||
renderWithRouter(detectorId, initialState); | ||
const element = screen.queryByTestId('missingResultIndexCallOut'); | ||
|
||
// Assert that the element is not in the document | ||
expect(element).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
*/ | ||
|
||
require('babel-polyfill'); | ||
import 'jest-canvas-mock'; |