Skip to content

Commit

Permalink
Volume Snapshot basic crud test
Browse files Browse the repository at this point in the history
Signed-off-by: Ankush Behl <cloudbehl@gmail.com>
  • Loading branch information
cloudbehl committed Sep 11, 2020
1 parent 6e01ad9 commit 75574b9
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { Helmet } from 'react-helmet';
import { Link } from 'react-router-dom';
import { match } from 'react-router';

import { Grid, GridItem, ActionGroup, Button, Alert } from '@patternfly/react-core';
Expand Down Expand Up @@ -142,7 +143,15 @@ const PVCSummary: React.FC<PVCSummaryProps> = ({ persistentVolumeClaim }) => {
};

const CreateSnapshotForm = withHandlePromise<SnapshotResourceProps>((props) => {
const { resourceName, namespace, kindObj, handlePromise, inProgress, errorMessage } = props;
const {
resourceName,
plural,
namespace,
kindObj,
handlePromise,
inProgress,
errorMessage,
} = props;

const [pvcName, setPVCName] = React.useState(resourceName);
const [pvcObj, setPVCObj] = React.useState<PersistentVolumeClaimKind>(null);
Expand Down Expand Up @@ -201,84 +210,97 @@ const CreateSnapshotForm = withHandlePromise<SnapshotResourceProps>((props) => {
};

return (
<div className="co-m-pane__body co-volume-snapshot__body">
<Helmet>
<title>{title}</title>
</Helmet>
<form className="co-m-pane__body-group co-m-pane__form" onSubmit={create}>
<h1 className="co-m-pane__heading">{title}</h1>
{kindObj.kind === PersistentVolumeClaimModel.kind && (
<p>
Creating snapshot for claim <strong>{resourceName}</strong>
</p>
)}
{pvcName && pvcObj && pvcObj?.status?.phase !== 'Bound' && (
<Alert
className="co-alert co-volume-snapshot__alert-body"
variant="warning"
title="Snapshot creation for unbound claim is not recommended."
isInline
/>
)}
{kindObj.kind === VolumeSnapshotModel.kind && (
/* eslint-disable jsx-a11y/label-has-associated-control */
<>
<label className="control-label co-required" html-for="claimName">
{PersistentVolumeModel.label}
</label>
<PVCDropdown
dataTest="pvc-dropdown"
namespace={namespace}
onChange={handlePVCName}
selectedKey={pvcName}
desc={`Persistent Volume Claim in ${namespace} namespace`}
<div className="co-volume-snapshot__body">
<div className="co-m-pane__body co-m-pane__form">
<Helmet>
<title>{title}</title>
</Helmet>
<h1 className="co-m-pane__heading co-m-pane__heading--baseline">
<div className="co-m-pane__name">{title}</div>
<div className="co-m-pane__heading-link">
<Link
to={`/k8s/ns/${namespace || 'default'}/${plural}/~new`}
id="yaml-link"
data-test="yaml-link"
replace
>
Edit YAML
</Link>
</div>
</h1>
<form className="co-m-pane__body-group" onSubmit={create}>
{kindObj.kind === PersistentVolumeClaimModel.kind && (
<p>
Creating snapshot for claim <strong>{resourceName}</strong>
</p>
)}
{pvcName && pvcObj && pvcObj?.status?.phase !== 'Bound' && (
<Alert
className="co-alert co-volume-snapshot__alert-body"
variant="warning"
title="Snapshot creation for unbound claim is not recommended."
isInline
/>
</>
)}
<div className="form-group co-volume-snapshot__form">
<label className="control-label co-required" htmlFor="snapshot-name">
Name
</label>
<input
className="pf-c-form-control"
type="text"
onChange={handleSnapshotName}
name="snapshotName"
id="snapshot-name"
value={snapshotName}
required
/>
</div>
{pvcObj && (
)}
{kindObj.kind === VolumeSnapshotModel.kind && (
/* eslint-disable jsx-a11y/label-has-associated-control */
<>
<label className="control-label co-required" html-for="claimName">
{PersistentVolumeModel.label}
</label>
<PVCDropdown
dataTest="pvc-dropdown"
namespace={namespace}
onChange={handlePVCName}
selectedKey={pvcName}
desc={`Persistent Volume Claim in ${namespace} namespace`}
/>
</>
)}
<div className="form-group co-volume-snapshot__form">
<label className="control-label co-required" htmlFor="snapshot-class">
Volume Snapshot Class
<label className="control-label co-required" htmlFor="snapshot-name">
Name
</label>
<SnapshotClassDropdown
dataTest="snapshot-dropdown"
onChange={setSnapshotClassName}
selectedKey={snapshotClassName}
pvcSC={pvcObj?.spec?.storageClassName}
<input
className="pf-c-form-control"
type="text"
onChange={handleSnapshotName}
name="snapshotName"
id="snapshot-name"
value={snapshotName}
required
/>
</div>
)}

<ButtonBar errorMessage={errorMessage || loadError} inProgress={inProgress}>
<ActionGroup className="pf-c-form">
<Button
type="submit"
variant="primary"
id="save-changes"
isDisabled={!snapshotClassName || !snapshotName || !pvcName}
>
Create
</Button>
<Button type="button" variant="secondary" onClick={history.goBack}>
Cancel
</Button>
</ActionGroup>
</ButtonBar>
</form>
{pvcObj && (
<div className="form-group co-volume-snapshot__form">
<label className="control-label co-required" htmlFor="snapshot-class">
Snapshot Class
</label>
<SnapshotClassDropdown
onChange={setSnapshotClassName}
dataTest="snapshot-dropdown"
selectedKey={snapshotClassName}
pvcSC={pvcObj?.spec?.storageClassName}
/>
</div>
)}
<ButtonBar errorMessage={errorMessage || loadError} inProgress={inProgress}>
<ActionGroup className="pf-c-form">
<Button
type="submit"
variant="primary"
id="save-changes"
isDisabled={!snapshotClassName || !snapshotName || !pvcName}
>
Create
</Button>
<Button type="button" variant="secondary" onClick={history.goBack}>
Cancel
</Button>
</ActionGroup>
</ButtonBar>
</form>
</div>
<div className="co-volume-snapshot__info">
<Grid hasGutter>
<GridItem span={1} />
Expand All @@ -302,7 +324,14 @@ const VolumeSnapshotComponent: React.FC<VolumeSnapshotComponentProps> = (props)
if (!kindObj && kindsInFlight) {
return <LoadingBox />;
}
return <CreateSnapshotForm namespace={params.ns} resourceName={params.name} kindObj={kindObj} />;
return (
<CreateSnapshotForm
plural={params.plural}
namespace={params.ns}
resourceName={params.name}
kindObj={kindObj}
/>
);
};

export const VolumeSnapshot = connectToPlural(VolumeSnapshotComponent);
Expand All @@ -319,6 +348,7 @@ type SnapshotResourceProps = HandlePromiseProps & {
namespace: string;
resourceName: string;
kindObj: K8sKind;
plural: string;
};

type PVCSummaryProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const Row: RowFunction<VolumeSnapshotContentKind> = ({ key, obj, style, index })
</TableData>
<TableData className={tableColumnClasses[6]}>
<ResourceKebab
kind={referenceForModel(VolumeSnapshotModel)}
kind={referenceForModel(VolumeSnapshotContentModel)}
resource={obj}
actions={Kebab.factory.common}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ describe('Kubernetes resource CRUD operations', () => {
.set('limitranges', { kind: 'LimitRange' })
.set('horizontalpodautoscalers', { kind: 'HorizontalPodAutoscaler' })
.set('networkpolicies', { kind: 'NetworkPolicy' })
.set('roles', { kind: 'Role' });
.set('roles', { kind: 'Role' })
.set('snapshot.storage.k8s.io~v1beta1~VolumeSnapshot', {
kind: 'snapshot.storage.k8s.io~v1beta1~VolumeSnapshot',
})
.set('snapshot.storage.k8s.io~v1beta1~VolumeSnapshotClass', {
kind: 'snapshot.storage.k8s.io~v1beta1~VolumeSnapshotClass',
namespaced: false,
})
.set('snapshot.storage.k8s.io~v1beta1~VolumeSnapshotContent', {
kind: 'snapshot.storage.k8s.io~v1beta1~VolumeSnapshotContent',
namespaced: false,
});
const openshiftObjs = OrderedMap<string, { kind: string; namespaced?: boolean }>()
.set('deploymentconfigs', { kind: 'DeploymentConfig' })
.set('buildconfigs', { kind: 'BuildConfig' })
Expand All @@ -63,7 +74,12 @@ describe('Kubernetes resource CRUD operations', () => {
let testObjs = Cypress.env('openshift') === true ? k8sObjs.merge(openshiftObjs) : k8sObjs;
testObjs = Cypress.env('servicecatalog') === true ? testObjs.merge(serviceCatalogObjs) : testObjs;
const testLabel = 'automated-test-name';
const resourcesWithCreationForm = new Set(['StorageClass', 'Route', 'PersistentVolumeClaim']);
const resourcesWithCreationForm = new Set([
'StorageClass',
'Route',
'PersistentVolumeClaim',
'snapshot.storage.k8s.io~v1beta1~VolumeSnapshot',
]);

testObjs.forEach(({ kind, namespaced = true }, resource) => {
describe(kind, () => {
Expand Down
42 changes: 42 additions & 0 deletions frontend/public/models/yaml-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,48 @@ spec:
- "-c"
- "for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done"
restartPolicy: Never
`,
)
.setIn(
[referenceForModel(k8sModels.VolumeSnapshotModel), 'default'],
`
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
name: new-snapshot-test
spec:
volumeSnapshotClassName: csi-hostpath-snapclass
source:
persistentVolumeClaimName: pvc-test
`,
)
.setIn(
[referenceForModel(k8sModels.VolumeSnapshotClassModel), 'default'],
`
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
name: csi-hostpath-snapclass
driver: hostpath.csi.k8s.io #csi-hostpath
deletionPolicy: Delete
`,
)
.setIn(
[referenceForModel(k8sModels.VolumeSnapshotContentModel), 'default'],
`
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotContent
metadata:
name: new-snapshot-content-test
spec:
deletionPolicy: Delete
driver: hostpath.csi.k8s.io
source:
snapshotHandle: 7bdd0de3-aaeb-11e8-9aae-0242ac110002
volumeSnapshotClassName: csi-hostpath-snapclass
volumeSnapshotRef:
name: new-snapshot-test
namespace: default
`,
);

Expand Down

0 comments on commit 75574b9

Please sign in to comment.