Skip to content

Commit 9177f78

Browse files
authored
fix: factory flow with a devfile that uses a parent (#1320)
* fix: factory flow with a devfile that uses a parent Signed-off-by: Oleksii Orel <oorel@redhat.com>
1 parent b538bd9 commit 9177f78

File tree

4 files changed

+118
-86
lines changed

4 files changed

+118
-86
lines changed

packages/dashboard-frontend/src/components/WorkspaceProgress/CreatingSteps/Apply/Devfile/__tests__/prepareDevfile.spec.ts

Lines changed: 108 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import { dump } from 'js-yaml';
14+
import cloneDeep from 'lodash/cloneDeep';
1415

1516
import { prepareDevfile } from '@/components/WorkspaceProgress/CreatingSteps/Apply/Devfile/prepareDevfile';
1617
import devfileApi from '@/services/devfileApi';
@@ -228,107 +229,140 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
228229
});
229230

230231
describe('has parent', () => {
231-
describe('with registryUrl', () => {
232-
it('with storage-type attribute', () => {
233-
// mute console logs
234-
console.warn = jest.fn();
235-
const devfile = {
236-
schemaVersion: '2.2.0',
237-
metadata: {
238-
name: 'wksp-test',
239-
},
240-
parent: {
241-
id: 'nodejs',
242-
registryUrl: 'https://registry.devfile.io/',
243-
},
244-
} as devfileApi.Devfile;
232+
let devfile: devfileApi.Devfile;
233+
let parentDevfile: devfileApi.Devfile;
234+
// mute console logs
235+
console.warn = jest.fn();
236+
237+
beforeEach(() => {
238+
devfile = {
239+
schemaVersion: '2.2.2',
240+
metadata: {
241+
generateName: 'nodejs',
242+
},
243+
parent: {},
244+
} as devfileApi.Devfile;
245245

246-
const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
247-
schemaVersion: '2.2.2',
248-
metadata: {
249-
generateName: 'nodejs',
246+
parentDevfile = {
247+
schemaVersion: '2.2.0',
248+
metadata: {
249+
name: 'sample-using-parent',
250+
},
251+
components: [
252+
{
253+
name: 'tools',
254+
container: {
255+
env: [
256+
{
257+
name: 'DEVFILE_ENV_VAR',
258+
value: 'true',
259+
},
260+
],
261+
},
250262
},
251-
attributes: {
252-
'controller.devfile.io/storage-type': 'ephemeral',
263+
],
264+
commands: [
265+
{
266+
id: 'parent-command',
267+
exec: {
268+
label: '2. This command from the parent',
269+
component: 'tools',
270+
commandLine: 'echo "Hello from parent"',
271+
},
253272
},
254-
} as devfileApi.Devfile);
273+
],
274+
} as devfileApi.Devfile;
275+
});
255276

277+
describe('with registryUrl', () => {
278+
it('with storage-type attribute', () => {
279+
devfile.parent!.id = 'nodejs';
280+
devfile.parent!.registryUrl = 'https://registry.devfile.io/';
281+
const _devfile = cloneDeep(devfile);
282+
283+
parentDevfile.attributes = {
284+
'controller.devfile.io/storage-type': 'ephemeral',
285+
};
286+
const _parentDevfile = cloneDeep(parentDevfile);
287+
288+
const newDevfile = prepareDevfile(
289+
_devfile,
290+
factoryId,
291+
'ephemeral',
292+
false,
293+
_parentDevfile,
294+
);
295+
296+
expect(_devfile).toEqual(devfile);
297+
expect(_parentDevfile).toEqual(parentDevfile);
256298
expect(console.warn).toHaveBeenCalledWith(
257299
'Unable to apply controller.devfile.io/storage-type attribute.',
258300
);
259301
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toBeUndefined();
260302
});
261303

262304
it('without storage-type attribute', () => {
263-
const devfile = {
264-
schemaVersion: '2.2.0',
265-
metadata: {
266-
name: 'wksp-test',
267-
},
268-
parent: {
269-
id: 'nodejs',
270-
registryUrl: 'https://registry.devfile.io/',
271-
},
272-
} as devfileApi.Devfile;
273-
274-
const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
275-
schemaVersion: '2.2.2',
276-
metadata: {
277-
generateName: 'nodejs',
278-
},
279-
} as devfileApi.Devfile);
305+
devfile.parent!.id = 'nodejs';
306+
devfile.parent!.registryUrl = 'https://registry.devfile.io/';
307+
const _devfile = cloneDeep(devfile);
308+
309+
const _parentDevfile = cloneDeep(parentDevfile);
310+
311+
const newDevfile = prepareDevfile(
312+
_devfile,
313+
factoryId,
314+
'ephemeral',
315+
false,
316+
_parentDevfile,
317+
);
280318

319+
expect(_devfile).toEqual(devfile);
320+
expect(_parentDevfile).toEqual(parentDevfile);
281321
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toEqual('ephemeral');
282322
});
283323
});
284324
describe('with uri', () => {
285325
it('with storage-type attribute', () => {
286-
// mute console logs
287-
console.warn = jest.fn();
288-
const devfile = {
289-
schemaVersion: '2.2.0',
290-
metadata: {
291-
name: 'wksp-test',
292-
},
293-
parent: {
294-
uri: 'https://raw.githubusercontent.com/test/devfile.yaml',
295-
},
296-
} as devfileApi.Devfile;
297-
298-
const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
299-
schemaVersion: '2.2.2',
300-
metadata: {
301-
generateName: 'nodejs',
302-
},
303-
attributes: {
304-
'controller.devfile.io/storage-type': 'ephemeral',
305-
},
306-
} as devfileApi.Devfile);
326+
devfile.parent!.uri = 'https://raw.githubusercontent.com/test/devfile.yaml';
327+
const _devfile = cloneDeep(devfile);
328+
329+
parentDevfile.attributes = {
330+
'controller.devfile.io/storage-type': 'ephemeral',
331+
};
332+
const _parentDevfile = cloneDeep(parentDevfile);
333+
334+
const newDevfile = prepareDevfile(
335+
_devfile,
336+
factoryId,
337+
'ephemeral',
338+
false,
339+
_parentDevfile,
340+
);
307341

342+
expect(_devfile).toEqual(devfile);
343+
expect(_parentDevfile).toEqual(parentDevfile);
308344
expect(console.warn).toHaveBeenCalledWith(
309345
'Unable to apply controller.devfile.io/storage-type attribute.',
310346
);
311347
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toBeUndefined();
312348
});
313349

314350
it('without storage-type attribute', () => {
315-
const devfile = {
316-
schemaVersion: '2.2.0',
317-
metadata: {
318-
name: 'wksp-test',
319-
},
320-
parent: {
321-
uri: 'https://raw.githubusercontent.com/test/devfile.yaml',
322-
},
323-
} as devfileApi.Devfile;
351+
devfile.parent!.uri = 'https://raw.githubusercontent.com/test/devfile.yaml';
352+
const _devfile = cloneDeep(devfile);
324353

325-
const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
326-
schemaVersion: '2.2.2',
327-
metadata: {
328-
generateName: 'nodejs',
329-
},
330-
} as devfileApi.Devfile);
354+
const _parentDevfile = cloneDeep(parentDevfile);
355+
356+
const newDevfile = prepareDevfile(
357+
_devfile,
358+
factoryId,
359+
'ephemeral',
360+
false,
361+
_parentDevfile,
362+
);
331363

364+
expect(_devfile).toEqual(devfile);
365+
expect(_parentDevfile).toEqual(parentDevfile);
332366
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toEqual('ephemeral');
333367
});
334368
});

packages/dashboard-frontend/src/components/WorkspaceProgress/CreatingSteps/Apply/Devfile/prepareDevfile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ export function prepareDevfile(
3131
factoryId: string,
3232
storageType: che.WorkspaceStorageType | undefined,
3333
appendSuffix: boolean,
34-
parentDevfile?: devfileApi.Devfile | undefined,
34+
_parentDevfile?: devfileApi.Devfile | undefined,
3535
): devfileApi.Devfile {
3636
const devfile = cloneDeep(_devfile);
37+
const parentDevfile = cloneDeep(_parentDevfile);
3738
const attributes = DevfileAdapter.getAttributes(devfile);
3839
if (
3940
!attributes[DEVWORKSPACE_METADATA_ANNOTATION] ||

packages/dashboard-frontend/src/components/WorkspaceProgress/CreatingSteps/Fetch/Devfile/index.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,9 @@ class CreatingStepFetchDevfile extends ProgressStep<Props, State> {
205205
return true;
206206
}
207207

208-
let resolveDone = false;
209208
try {
210209
// start resolving the devfile
211-
resolveDone = await this.resolveDevfile(sourceUrl);
210+
await this.resolveDevfile(sourceUrl);
212211
} catch (e) {
213212
const errorMessage = common.helpers.errors.getMessage(e);
214213
// check if it is a scheme validation error
@@ -225,9 +224,6 @@ class CreatingStepFetchDevfile extends ProgressStep<Props, State> {
225224
}
226225
throw e;
227226
}
228-
if (!resolveDone) {
229-
return false;
230-
}
231227

232228
// wait for the devfile resolving to complete
233229
return false;

packages/dashboard-frontend/src/services/devfile/adapter.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ export class DevfileAdapter {
2020
}
2121

2222
public static getAttributes(devfile: devfileApi.Devfile) {
23-
const attributes = {};
23+
const attributes = {} as any;
24+
2425
if (devfile.schemaVersion?.startsWith('2.0')) {
2526
if (!devfile.metadata.attributes) {
2627
devfile.metadata.attributes = attributes;
2728
}
2829
return devfile.metadata.attributes;
29-
} else {
30-
if (!devfile.attributes) {
31-
devfile.attributes = attributes;
32-
}
33-
return devfile.attributes;
3430
}
31+
if (!devfile.attributes) {
32+
devfile.attributes = attributes;
33+
}
34+
35+
return devfile.attributes;
3536
}
3637

3738
get attributes() {

0 commit comments

Comments
 (0)