-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Timothy Asir Jeyasingh <[email protected]>
- Loading branch information
1 parent
d768d0b
commit 72ce5b7
Showing
6 changed files
with
251 additions
and
1 deletion.
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
71 changes: 71 additions & 0 deletions
71
packages/ocs/dashboards/persistent-internal/OptimizeModal.spec.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,71 @@ | ||
import React from 'react'; | ||
import { screen, render, fireEvent, waitFor } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { OSDMigrationModal } from '../../modals/osd-migration/osdMigrationModal'; | ||
import * as migrationStatus from '../../utils/osd-migration'; | ||
|
||
jest.mock('@odf/shared/selectors', () => ({ | ||
getName: jest.fn().mockReturnValue('test'), | ||
})); | ||
|
||
// Mocking getOSDMigrationStatus | ||
jest.mock('../../utils/osd-migration'); | ||
|
||
jest.mock('@odf/shared/hooks/useK8sList', () => ({ | ||
__esModule: true, | ||
useK8sList: () => [ | ||
[ | ||
{ | ||
metadata: { | ||
name: 'test', | ||
}, | ||
}, | ||
], | ||
true, | ||
undefined, | ||
], | ||
})); | ||
|
||
describe('OptimizeModal Component', () => { | ||
test('renders without errors', async () => { | ||
// Mock getOSDMigrationStatus to return 'Completed' for this test | ||
migrationStatus.getOSDMigrationStatus.mockReturnValue('Completed'); | ||
|
||
render(<OSDMigrationModal />); | ||
|
||
// Wait for the component to finish rendering (use async/await) | ||
await waitFor(() => { | ||
expect(screen.queryByText('Optimize')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test('renders with migration pending', async () => { | ||
// Mock getOSDMigrationStatus to return 'Completed' for this test | ||
migrationStatus.getOSDMigrationStatus.mockReturnValue('Pending'); | ||
|
||
render(<OSDMigrationModal />); | ||
|
||
// Wait for the component to finish rendering (use async/await) | ||
await waitFor(() => { | ||
expect(screen.queryByText('Optimize')).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click(screen.getByText(/Optimize/i)); | ||
|
||
// Wait for modal to be visible | ||
await waitFor(() => { | ||
expect( | ||
screen.queryByText('Optimise cluster for Regional-DR?') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click(screen.getByText('Close')); | ||
|
||
// Wait for modal to be closed | ||
await waitFor(() => { | ||
expect( | ||
screen.queryByText('Configure cluster for Regional-DR?') | ||
).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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
113 changes: 113 additions & 0 deletions
113
packages/ocs/modals/osd-migration/osdMigrationModal.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,113 @@ | ||
import * as React from 'react'; | ||
import { DISASTER_RECOVERY_TARGET_ANNOTATION } from '@odf/core/constants'; | ||
import { CephClusterModel } from '@odf/shared/models'; | ||
import { getName, getNamespace } from '@odf/shared/selectors'; | ||
import { CephClusterKind } from '@odf/shared/types'; | ||
import { useCustomTranslation } from '@odf/shared/useCustomTranslationHook'; | ||
import { | ||
RedExclamationCircleIcon, | ||
k8sPatch, | ||
} from '@openshift-console/dynamic-plugin-sdk'; | ||
import { | ||
Modal, | ||
ModalVariant, | ||
Button, | ||
ModalBoxHeader, | ||
ModalBoxBody, | ||
Title, | ||
} from '@patternfly/react-core'; | ||
import { | ||
FAILED, | ||
PENDING, | ||
getOSDMigrationStatus, | ||
} from '../../utils/osd-migration'; | ||
|
||
export const OSDMigrationModal: React.FC<OSDMigrationModalProps> = ({ | ||
cephData, | ||
}) => { | ||
const { t } = useCustomTranslation(); | ||
const dRSetupStatus: string = getOSDMigrationStatus(cephData); | ||
const [isOpen, setOpen] = React.useState(false); | ||
const [errorMessage, setErrorMessage] = React.useState<string | null>(null); | ||
const openModal = () => setOpen(true); | ||
|
||
const closeModal = () => { | ||
setOpen(false); | ||
setErrorMessage(null); | ||
}; | ||
|
||
const handleOptimize = () => { | ||
const patch = [ | ||
{ | ||
op: 'add', | ||
path: `/metadata/annotations/${DISASTER_RECOVERY_TARGET_ANNOTATION}`, | ||
value: 'true', | ||
}, | ||
]; | ||
|
||
k8sPatch({ | ||
model: CephClusterModel, | ||
resource: { | ||
metadata: { | ||
name: getName(cephData), | ||
namespace: getNamespace(cephData), | ||
}, | ||
}, | ||
data: patch, | ||
}) | ||
.then(() => { | ||
closeModal(); | ||
}) | ||
.catch((err) => { | ||
setErrorMessage(err.message); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div style={{ display: 'flex', justifyContent: 'space-between' }}> | ||
<p> | ||
{dRSetupStatus === FAILED && <RedExclamationCircleIcon />} | ||
{t(dRSetupStatus)} | ||
</p> | ||
{dRSetupStatus === PENDING && ( | ||
<a onClick={openModal}>{t('Optimize')}</a> | ||
)} | ||
</div> | ||
<div> | ||
<Modal | ||
variant={ModalVariant.medium} | ||
title={t('Optimise cluster for Regional-DR?')} | ||
isOpen={isOpen} | ||
onClose={closeModal} | ||
actions={[ | ||
<Button key="close" variant="secondary" onClick={closeModal}> | ||
Close | ||
</Button>, | ||
<Button key="optimize" variant="primary" onClick={handleOptimize}> | ||
Optimize | ||
</Button>, | ||
]} | ||
> | ||
<p> | ||
{t( | ||
'Configure the cluster for a Regional-DR setup by migrating OSDs. Migration may take sometime depending on several factors. To learn more about OSDs migration best practices and its consequences refer to the documentation.' | ||
)} | ||
</p> | ||
{errorMessage && ( | ||
<ModalBoxHeader> | ||
<Title headingLevel="h2" size="lg"> | ||
Error | ||
</Title> | ||
</ModalBoxHeader> | ||
)} | ||
<ModalBoxBody>{errorMessage}</ModalBoxBody> | ||
</Modal> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
type OSDMigrationModalProps = { | ||
cephData?: CephClusterKind; | ||
}; |
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,35 @@ | ||
import { DISASTER_RECOVERY_TARGET_ANNOTATION } from '@odf/core/constants'; | ||
import { getAnnotations } from '@odf/shared/selectors'; | ||
import { CephClusterKind } from '@odf/shared/types'; | ||
|
||
export const IN_PROGRESS = 'In Progress'; | ||
export const PENDING = 'Pending'; | ||
export const COMPLETED = 'Completed'; | ||
export const FAILED = 'Failed'; | ||
export const BLUESTORE_RDR = 'bluestore-rdr'; | ||
export const BLUESTORE = 'bluestore'; | ||
|
||
export const getOSDMigrationStatus = (ceph: CephClusterKind) => { | ||
if (!!ceph) { | ||
const bluestoreCount = ceph?.status?.storage?.osd?.storeType?.[BLUESTORE]; | ||
const bluestoreRdrCount = | ||
ceph?.status?.storage?.osd?.storeType?.[BLUESTORE_RDR]; | ||
|
||
const isDisasterRecoveryTarget = | ||
getAnnotations(ceph)?.[DISASTER_RECOVERY_TARGET_ANNOTATION] === 'true'; | ||
|
||
if (bluestoreCount > 0) { | ||
if (bluestoreRdrCount > 0 || isDisasterRecoveryTarget) { | ||
return IN_PROGRESS; | ||
} else { | ||
return PENDING; | ||
} | ||
} else if (bluestoreRdrCount > 0) { | ||
return COMPLETED; | ||
} | ||
} else { | ||
return FAILED; | ||
} // TODO Add condition for migration failure | ||
|
||
return ''; | ||
}; |
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