Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit f243916

Browse files
author
Yaacov Zamir
committed
Render VmDetails with deleted template curreclty.
1 parent 6937cc0 commit f243916

File tree

6 files changed

+261
-3
lines changed

6 files changed

+261
-3
lines changed

src/components/Details/Flavor/Flavor.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ export class Flavor extends React.Component {
3232
};
3333

3434
componentDidMount() {
35+
// If not in edit mode, just init state and exit.
36+
if (!this.props.editing) {
37+
this.setState({
38+
loadingTemplate: false,
39+
template: null,
40+
});
41+
42+
return;
43+
}
44+
3545
this.setState({
3646
loadingTemplate: true,
3747
});

src/components/Details/VmDetails/VmDetails.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
prefixedId,
2929
} from '../../../utils';
3030
import { VirtualMachineModel } from '../../../models';
31-
import { CUSTOM_FLAVOR, DASHES } from '../../../constants';
31+
import { CUSTOM_FLAVOR, DASHES, MISSING_STR, PENDING_STR } from '../../../constants';
3232
import { settingsValue, selectVm } from '../../../k8s/selectors';
3333
import { Flavor } from '../Flavor';
3434
import { Description } from '../Description';
@@ -45,6 +45,7 @@ export class VmDetails extends React.Component {
4545
updating: false,
4646
k8sError: null,
4747
form: {},
48+
templateError: PENDING_STR,
4849
};
4950
}
5051

@@ -53,6 +54,11 @@ export class VmDetails extends React.Component {
5354
editing,
5455
});
5556

57+
setTemplateError = error =>
58+
this.setState({
59+
templateError: error,
60+
});
61+
5662
onFormChange = (formKey, newValue, key, valid) =>
5763
this.setState(state => ({
5864
form: {
@@ -123,10 +129,17 @@ export class VmDetails extends React.Component {
123129
isFormValid = () => Object.keys(this.state.form).every(key => this.state.form[key].valid);
124130

125131
componentDidUpdate() {
126-
const { launcherPod, importerPods, migration, vm } = this.props;
132+
const { launcherPod, importerPods, migration, k8sGet, vm } = this.props;
127133
if (this.state.editing && !this.isVmOff(vm, launcherPod, importerPods, migration)) {
128134
this.setEditing(false);
129135
}
136+
137+
// Check template, if template is missing, update view.
138+
if (getVmTemplate(vm) != null && this.state.templateError === PENDING_STR) {
139+
retrieveVmTemplate(k8sGet, vm)
140+
.then(() => this.setTemplateError(''))
141+
.catch(() => this.setTemplateError(MISSING_STR));
142+
}
130143
}
131144

132145
isVmOff = (vm, launcherPod, importerPods, migration) => {
@@ -155,6 +168,8 @@ export class VmDetails extends React.Component {
155168
const hostName = getHostName(launcherPod);
156169
const fqdn = vmIsOff || !hostName ? DASHES : hostName;
157170
const template = getVmTemplate(vm);
171+
const templateIsMissing = this.state.templateError === MISSING_STR ? ` ${MISSING_STR}` : '';
172+
const templateLabel = template ? `${template.namespace}/${template.name}${templateIsMissing}` : DASHES;
158173
const id = getId(vm);
159174
const sortedBootableDevices = getBootableDevicesInOrder(vm);
160175
const editButton = (
@@ -238,7 +253,7 @@ export class VmDetails extends React.Component {
238253
<dd id={prefixedId(id, 'workload-profile')}>{getWorkloadProfile(vm) || DASHES}</dd>
239254

240255
<dt>Template</dt>
241-
<dd id={prefixedId(id, 'template')}>{template ? `${template.namespace}/${template.name}` : DASHES}</dd>
256+
<dd id={prefixedId(id, 'template')}>{templateLabel}</dd>
242257
</dl>
243258
<div className="kubevirt-vm-details__other-details">
244259
<dl className="kubevirt-vm-details__details-list">

src/components/Details/VmDetails/fixtures/VmDetails.fixture.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,35 @@ export const vmFixtures = {
8787
running: false,
8888
},
8989
},
90+
vmWithDeletedTemplate: {
91+
metadata: {
92+
...metadata,
93+
labels: {
94+
'flavor.template.cnv.io/small': 'true',
95+
'os.template.cnv.io/fedora29': 'true',
96+
[LABEL_USED_TEMPLATE_NAME]: 'deleted-template',
97+
[LABEL_USED_TEMPLATE_NAMESPACE]: 'default',
98+
'workload.template.cnv.io/generic': 'true',
99+
},
100+
},
101+
spec: {
102+
template: {
103+
spec: {
104+
domain: {
105+
cpu: {
106+
cores: 2,
107+
},
108+
resources: {
109+
requests: {
110+
memory: '2G',
111+
},
112+
},
113+
},
114+
},
115+
},
116+
running: false,
117+
},
118+
},
90119
customVm: {
91120
metadata: {
92121
...metadata,
@@ -204,4 +233,14 @@ export default [
204233
overview: true,
205234
},
206235
},
236+
{
237+
component: VmDetails,
238+
name: 'VM detail with deleted template',
239+
props: {
240+
vm: vmFixtures.vmWithDeletedTemplate,
241+
k8sPatch,
242+
k8sGet,
243+
NodeLink: () => true,
244+
},
245+
},
207246
];

src/components/Details/VmDetails/tests/VmDetails.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { getId } from '../../../../selectors';
1515

1616
const testVmDetails = (vm, otherProps) => <VmDetails {...VmDetailsFixture[0].props} vm={vm} {...otherProps} />;
1717

18+
const flushAllPromises = () => new Promise(resolve => setImmediate(resolve));
19+
1820
describe('<VmDetails />', () => {
1921
it('renders correctly', () => {
2022
const component = render(testVmDetails(vmFixtures.downVm));
@@ -73,6 +75,19 @@ describe('<VmDetails /> enzyme', () => {
7375
return disablesEditOnCancel(component);
7476
});
7577

78+
it('renders deleted template correctly', async () => {
79+
const component = mount(testVmDetails(vmFixtures.vmWithDeletedTemplate));
80+
81+
// Wait for all promisses to terminate.
82+
//
83+
// Testing for deleted template is done using a promise
84+
// we need to wait for.
85+
await flushAllPromises();
86+
component.update();
87+
88+
expect(component.render()).toMatchSnapshot();
89+
});
90+
7691
it('updates VM description after clicking save button', () => {
7792
const k8sPatchMock = jest.fn().mockReturnValue(
7893
new Promise(resolve => {

src/components/Details/VmDetails/tests/__snapshots__/VmDetails.test.js.snap

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,183 @@ Array [
175175
]
176176
`;
177177

178+
exports[`<VmDetails /> enzyme renders deleted template correctly 1`] = `
179+
Array [
180+
<h1
181+
class="co-m-pane__heading"
182+
>
183+
Virtual Machine Overview
184+
<div>
185+
<button
186+
class="btn btn-default"
187+
type="button"
188+
>
189+
Edit
190+
</button>
191+
</div>
192+
</h1>,
193+
<div
194+
class="kubevirt-vm-details__content"
195+
>
196+
<dl
197+
class="kubevirt-vm-details__name-list"
198+
>
199+
<dt
200+
class=""
201+
>
202+
Name
203+
</dt>
204+
<dd
205+
class=""
206+
id="my-namespace-my-vm-name"
207+
>
208+
my-vm
209+
</dd>
210+
<dt
211+
class=""
212+
>
213+
Description
214+
</dt>
215+
<dd>
216+
<div
217+
class="kubevirt-vm-details__description"
218+
>
219+
<div
220+
id="my-namespace-my-vm-description"
221+
>
222+
---
223+
</div>
224+
</div>
225+
</dd>
226+
</dl>
227+
<div
228+
class="kubevirt-vm-details__details"
229+
>
230+
<dl
231+
class="kubevirt-vm-details__details-list"
232+
>
233+
<dt>
234+
Status
235+
</dt>
236+
<dd>
237+
<div>
238+
<span
239+
aria-hidden="true"
240+
class="kubevirt-vm-status__icon pficon pficon-off"
241+
/>
242+
Off
243+
</div>
244+
</dd>
245+
<dt>
246+
Operating System
247+
</dt>
248+
<dd
249+
id="my-namespace-my-vm-os"
250+
>
251+
fedora29
252+
</dd>
253+
<dt>
254+
IP Addresses
255+
</dt>
256+
<dd
257+
id="my-namespace-my-vm-ip-addresses"
258+
>
259+
---
260+
</dd>
261+
<dt>
262+
Workload Profile
263+
</dt>
264+
<dd
265+
id="my-namespace-my-vm-workload-profile"
266+
>
267+
generic
268+
</dd>
269+
<dt>
270+
Template
271+
</dt>
272+
<dd
273+
id="my-namespace-my-vm-template"
274+
>
275+
default/deleted-template (Missing)
276+
</dd>
277+
</dl>
278+
<div
279+
class="kubevirt-vm-details__other-details"
280+
>
281+
<dl
282+
class="kubevirt-vm-details__details-list"
283+
>
284+
<dt>
285+
FQDN
286+
</dt>
287+
<dd
288+
id="my-namespace-my-vm-fqdn"
289+
>
290+
---
291+
</dd>
292+
<dt>
293+
Namespace
294+
</dt>
295+
<dd
296+
id="my-namespace-my-vm-namespace"
297+
>
298+
---
299+
</dd>
300+
<dt>
301+
Pod
302+
</dt>
303+
<dd
304+
id="my-namespace-my-vm-pod"
305+
>
306+
---
307+
</dd>
308+
<dd>
309+
---
310+
</dd>
311+
<dt>
312+
Boot Order
313+
</dt>
314+
<dd
315+
id="my-namespace-my-vm-boot-order"
316+
>
317+
---
318+
</dd>
319+
</dl>
320+
<dl
321+
class="kubevirt-vm-details__details-list"
322+
>
323+
<dt>
324+
Node
325+
</dt>
326+
<dd
327+
id="my-namespace-my-vm-node"
328+
>
329+
---
330+
</dd>
331+
<dt>
332+
Flavor
333+
</dt>
334+
<dd>
335+
<div>
336+
<div
337+
id="my-namespace-my-vm-flavor"
338+
>
339+
small
340+
</div>
341+
<div
342+
id="my-namespace-my-vm-flavor-description"
343+
>
344+
2 CPU, 2G Memory
345+
</div>
346+
</div>
347+
</dd>
348+
</dl>
349+
</div>
350+
</div>
351+
</div>,
352+
]
353+
`;
354+
178355
exports[`<VmDetails /> renders CPU and Memory settings for VM with custom flavor 1`] = `
179356
Array [
180357
<h1

src/constants/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export const OS_WINDOWS_PREFIX = 'win';
5959

6060
export const DEFAULT_RDP_PORT = 3389;
6161
export const DASHES = '---';
62+
export const MISSING_STR = '(Missing)';
63+
export const PENDING_STR = '(Pending)';
6264

6365
export const CDI_KUBEVIRT_IO = 'cdi.kubevirt.io';
6466
export const STORAGE_IMPORT_PVC_NAME = 'storage.import.importPvcName';

0 commit comments

Comments
 (0)