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

Fix validation in the storage modal mount path #3236

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion frontend/src/__mocks__/mockNotebookK8sResource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash-es';
import { KnownLabels, NotebookKind } from '~/k8sTypes';
import { DEFAULT_NOTEBOOK_SIZES } from '~/pages/projects/screens/spawner/const';
import { ContainerResources, TolerationEffect, TolerationOperator } from '~/types';
import { ContainerResources, TolerationEffect, TolerationOperator, VolumeMount } from '~/types';
import { genUID } from '~/__mocks__/mockUtils';
import { RecursivePartial } from '~/typeHelpers';

Expand All @@ -17,6 +17,7 @@ type MockResourceConfigType = {
lastImageSelection?: string;
opts?: RecursivePartial<NotebookKind>;
uid?: string;
additionalVolumeMounts?: VolumeMount[];
};

export const mockNotebookK8sResource = ({
Expand All @@ -31,6 +32,7 @@ export const mockNotebookK8sResource = ({
lastImageSelection = 's2i-minimal-notebook:py3.8-v1',
opts = {},
uid = genUID('notebook'),
additionalVolumeMounts = [],
}: MockResourceConfigType): NotebookKind =>
_.merge(
{
Expand Down Expand Up @@ -144,6 +146,7 @@ export const mockNotebookK8sResource = ({
mountPath: '/opt/app-root/src',
name,
},
...additionalVolumeMounts,
],
workingDir: '/opt/app-root/src',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ClusterStorageModal extends Modal {
return this.find().findByTestId('mount-path-folder-value');
}

findMountFieldHelperText() {
return this.find().findByTestId('mount-path-folder-helper-text');
}

findWorkbenchRestartAlert() {
return this.find().findByTestId('notebook-restart-alert');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ const initInterceptors = ({ isEmpty = false, storageClassName }: HandlersProps)
],
),
);
cy.interceptK8sList(NotebookModel, mockK8sResourceList([mockNotebookK8sResource({})]));
cy.interceptK8sList(
NotebookModel,
mockK8sResourceList([
mockNotebookK8sResource({
additionalVolumeMounts: [
{
mountPath: '/opt/app-root/src/test-dupe',
name: 'test-dupe-pvc-path',
},
],
}),
]),
);
};

const [openshiftDefaultStorageClass, otherStorageClass] = mockStorageClasses;
Expand Down Expand Up @@ -133,6 +145,35 @@ describe('ClusterStorage', () => {
.findWorkbenchConnectionSelect()
.findSelectOption('Test Notebook')
.click();

// don't allow duplicate path
addClusterStorageModal.findMountField().clear();
addClusterStorageModal.findMountField().fill('test-dupe');
addClusterStorageModal
.findMountFieldHelperText()
.should('have.text', 'Mount folder is already in use for this workbench');

// don't allow number in the path
addClusterStorageModal.findMountField().clear();
addClusterStorageModal.findMountField().fill('test2');
addClusterStorageModal
.findMountFieldHelperText()
.should('have.text', 'Must only consist of lower case letters and dashes');

// Allow trailing slash
addClusterStorageModal.findMountField().clear();
addClusterStorageModal.findMountField().fill('test/');
addClusterStorageModal
.findMountFieldHelperText()
.should('have.text', 'Must consist of lower case letters and dashes.');

addClusterStorageModal.findMountField().clear();
addClusterStorageModal
.findMountFieldHelperText()
.should(
'have.text',
'Enter a path to a model or folder. This path cannot point to a root folder.',
);
addClusterStorageModal.findMountField().fill('data');
addClusterStorageModal.findWorkbenchRestartAlert().should('exist');

Expand Down
13 changes: 7 additions & 6 deletions frontend/src/pages/projects/pvc/MountPathField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const MountPathField: React.FC<MountPathFieldProps> = ({
onChange={(e, value) => {
let error = '';
if (value.length === 0) {
error = 'Required';
} else if (!/^[a-z-]+$/.test(value)) {
error = 'Enter a path to a model or folder. This path cannot point to a root folder.';
} else if (!/^[a-z-]+\/?$/.test(value)) {
error = 'Must only consist of lower case letters and dashes';
} else if (inUseMountPaths.includes(`/${value}`)) {
error = 'Mount folder is already in use for this workbench';
Expand All @@ -50,10 +50,11 @@ const MountPathField: React.FC<MountPathFieldProps> = ({
</InputGroup>
<FormHelperText>
<HelperText>
<HelperTextItem variant={mountPath.error ? 'error' : 'default'}>
{mountPath.error
? 'Enter a path to a model or folder. This path cannot point to a root folder.'
: 'Must consist of lower case letters and dashes.'}
<HelperTextItem
variant={mountPath.error ? 'error' : 'default'}
data-testid="mount-path-folder-helper-text"
>
{mountPath.error ? mountPath.error : 'Must consist of lower case letters and dashes.'}
</HelperTextItem>
</HelperText>
</FormHelperText>
Expand Down
Loading