Skip to content

Commit

Permalink
Wizard: Add validation for kernel step
Browse files Browse the repository at this point in the history
This adds validation for the Kernel step.
  • Loading branch information
regexowl committed Jan 20, 2025
1 parent 4f7f81e commit 37d0e93
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Components/CreateImageWizard/CreateImageWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
useDetailsValidation,
useRegistrationValidation,
useHostnameValidation,
useKernelValidation,
} from './utilities/useValidation';
import {
isAwsAccountIdValid,
Expand Down Expand Up @@ -223,6 +224,8 @@ const CreateImageWizard = ({ isEdit }: CreateImageWizardProps) => {
const fileSystemValidation = useFilesystemValidation();
// Hostname
const hostnameValidation = useHostnameValidation();
// Kernel
const kernelValidation = useKernelValidation();
// Firstboot
const firstBootValidation = useFirstBootValidation();
// Details
Expand Down Expand Up @@ -510,8 +513,12 @@ const CreateImageWizard = ({ isEdit }: CreateImageWizardProps) => {
key="wizard-kernel"
navItem={customStatusNavItem}
isHidden={!isKernelEnabled}
status={kernelValidation.disabledNext ? 'error' : 'default'}
footer={
<CustomWizardFooter disableNext={false} optional={true} />
<CustomWizardFooter
disableNext={kernelValidation.disabledNext}
optional={true}
/>
}
>
<KernelStep />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { FormGroup } from '@patternfly/react-core';
import {
Alert,
Button,
HelperText,
HelperTextItem,
MenuToggle,
MenuToggleElement,
Select,
Expand All @@ -20,6 +22,7 @@ import {
changeKernelName,
selectKernel,
} from '../../../../../store/wizardSlice';
import { useKernelValidation } from '../../../utilities/useValidation';

const initialOptions = ['kernel', 'kernel-debug'];
let kernelOptions = initialOptions;
Expand All @@ -28,6 +31,8 @@ const KernelName = () => {
const dispatch = useAppDispatch();
const kernel = useAppSelector(selectKernel).name;

const stepValidation = useKernelValidation();

const [isOpen, setIsOpen] = useState(false);
const [inputValue, setInputValue] = useState<string>('');
const [filterValue, setFilterValue] = useState<string>('');
Expand Down Expand Up @@ -166,6 +171,13 @@ const KernelName = () => {
))}
</SelectList>
</Select>
{stepValidation.errors.kernel && (
<HelperText>
<HelperTextItem variant={'error'}>
{stepValidation.errors.kernel}
</HelperTextItem>
</HelperText>
)}
</FormGroup>
</>
);
Expand Down
20 changes: 20 additions & 0 deletions src/Components/CreateImageWizard/utilities/useValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
selectActivationKey,
selectRegistrationType,
selectHostname,
selectKernel,
} from '../../../store/wizardSlice';
import {
getDuplicateMountPoints,
Expand All @@ -27,6 +28,7 @@ import {
isMountpointMinSizeValid,
isSnapshotValid,
isHostnameValid,
isKernelNameValid,
} from '../validators';

export type StepValidation = {
Expand All @@ -41,13 +43,15 @@ export function useIsBlueprintValid(): boolean {
const filesystem = useFilesystemValidation();
const snapshot = useSnapshotValidation();
const hostname = useHostnameValidation();
const kernel = useKernelValidation();
const firstBoot = useFirstBootValidation();
const details = useDetailsValidation();
return (
!registration.disabledNext &&
!filesystem.disabledNext &&
!snapshot.disabledNext &&
!hostname.disabledNext &&
!kernel.disabledNext &&
!firstBoot.disabledNext &&
!details.disabledNext
);
Expand Down Expand Up @@ -155,6 +159,22 @@ export function useHostnameValidation(): StepValidation {
return { errors: {}, disabledNext: false };
}

export function useKernelValidation(): StepValidation {
const kernel = useAppSelector(selectKernel);

const errorMessage = 'Invalid format.';

if (!isKernelNameValid(kernel.name)) {
return {
errors: {
kernel: errorMessage,
},
disabledNext: true,
};
}
return { errors: {}, disabledNext: false };
}

export function useDetailsValidation(): StepValidation {
const name = useAppSelector(selectBlueprintName);
const description = useAppSelector(selectBlueprintDescription);
Expand Down
11 changes: 11 additions & 0 deletions src/Components/CreateImageWizard/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ export const isHostnameValid = (hostname: string) => {
);
};

export const isKernelNameValid = (kernelName: string) => {
if (!kernelName) {
return true;
}

return (
kernelName.length < 65 &&
/^[a-z0-9]|[a-z0-9][a-z0-9-_.+]*[a-z0-9]$/.test(kernelName)
);
};

export const isPortValid = (port: string) => {
return /^(\d{1,5}|[a-z]{1,6})(-\d{1,5})?:[a-z]{1,6}$/.test(port);
};
12 changes: 12 additions & 0 deletions src/test/Components/CreateImageWizard/steps/Kernel/Kernel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
clickBack,
clickNext,
enterBlueprintName,
getNextButton,
interceptBlueprintRequest,
openAndDismissSaveAndBuildModal,
verifyCancelButton,
Expand Down Expand Up @@ -121,6 +122,17 @@ describe('Step Kernel', () => {
await openKernelNameOptions(kernelNameDropdown);
await screen.findByText(CUSTOM_NAME);
});

test('disable Next with invalid custom kernel name', async () => {
await renderCreateMode();
await goToKernelStep();

await selectCustomKernelName('-----------');
const nextButton = await getNextButton();
expect(nextButton).toBeDisabled();
await clearKernelName();
expect(nextButton).toBeEnabled();
});
});

describe('Kernel request generated correctly', () => {
Expand Down

0 comments on commit 37d0e93

Please sign in to comment.