2022-07-16 23:28:13 +05:30
|
|
|
import { createJobsHash, generateJobNeedsDict, getPipelineDefaultTab } from '~/pipelines/utils';
|
|
|
|
import { TAB_QUERY_PARAM, validPipelineTabNames } from '~/pipelines/constants';
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
describe('utils functions', () => {
|
2020-11-24 15:15:51 +05:30
|
|
|
const jobName1 = 'build_1';
|
|
|
|
const jobName2 = 'build_2';
|
|
|
|
const jobName3 = 'test_1';
|
|
|
|
const jobName4 = 'deploy_1';
|
2021-02-22 17:27:13 +05:30
|
|
|
const job1 = { name: jobName1, script: 'echo hello', stage: 'build' };
|
|
|
|
const job2 = { name: jobName2, script: 'echo build', stage: 'build' };
|
|
|
|
const job3 = {
|
|
|
|
name: jobName3,
|
|
|
|
script: 'echo test',
|
|
|
|
stage: 'test',
|
|
|
|
needs: [jobName1, jobName2],
|
|
|
|
};
|
|
|
|
const job4 = {
|
|
|
|
name: jobName4,
|
|
|
|
script: 'echo deploy',
|
|
|
|
stage: 'deploy',
|
|
|
|
needs: [jobName3],
|
|
|
|
};
|
2021-01-03 14:25:43 +05:30
|
|
|
const userDefinedStage = 'myStage';
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
const pipelineGraphData = {
|
|
|
|
stages: [
|
|
|
|
{
|
|
|
|
name: userDefinedStage,
|
|
|
|
groups: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: job4.stage,
|
|
|
|
groups: [
|
2020-11-24 15:15:51 +05:30
|
|
|
{
|
2021-01-03 14:25:43 +05:30
|
|
|
name: jobName4,
|
|
|
|
jobs: [{ ...job4 }],
|
2020-11-24 15:15:51 +05:30
|
|
|
},
|
|
|
|
],
|
2021-01-03 14:25:43 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
name: job1.stage,
|
|
|
|
groups: [
|
2020-11-24 15:15:51 +05:30
|
|
|
{
|
2021-01-03 14:25:43 +05:30
|
|
|
name: jobName1,
|
|
|
|
jobs: [{ ...job1 }],
|
2020-11-24 15:15:51 +05:30
|
|
|
},
|
|
|
|
{
|
2021-01-03 14:25:43 +05:30
|
|
|
name: jobName2,
|
|
|
|
jobs: [{ ...job2 }],
|
2020-11-24 15:15:51 +05:30
|
|
|
},
|
|
|
|
],
|
2021-01-03 14:25:43 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
name: job3.stage,
|
|
|
|
groups: [
|
|
|
|
{
|
|
|
|
name: jobName3,
|
|
|
|
jobs: [{ ...job3 }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe('createJobsHash', () => {
|
|
|
|
it('returns an empty object if there are no jobs received as argument', () => {
|
|
|
|
expect(createJobsHash([])).toEqual({});
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it('returns a hash with the jobname as key and all its data as value', () => {
|
|
|
|
const jobs = {
|
2021-03-08 18:12:59 +05:30
|
|
|
[jobName1]: { jobs: [job1], name: jobName1, needs: [] },
|
|
|
|
[jobName2]: { jobs: [job2], name: jobName2, needs: [] },
|
|
|
|
[jobName3]: { jobs: [job3], name: jobName3, needs: job3.needs },
|
|
|
|
[jobName4]: { jobs: [job4], name: jobName4, needs: job4.needs },
|
2021-02-22 17:27:13 +05:30
|
|
|
};
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(createJobsHash(pipelineGraphData.stages)).toEqual(jobs);
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
describe('generateJobNeedsDict', () => {
|
|
|
|
it('generates an empty object if it receives no jobs', () => {
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(generateJobNeedsDict({})).toEqual({});
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('generates a dict with empty needs if there are no dependencies', () => {
|
|
|
|
const smallGraph = {
|
2021-02-22 17:27:13 +05:30
|
|
|
[jobName1]: job1,
|
|
|
|
[jobName2]: job2,
|
2020-11-24 15:15:51 +05:30
|
|
|
};
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(generateJobNeedsDict(smallGraph)).toEqual({
|
2021-02-22 17:27:13 +05:30
|
|
|
[jobName1]: [],
|
|
|
|
[jobName2]: [],
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('generates a dict where key is the a job and its value is an array of all its needs', () => {
|
2021-02-22 17:27:13 +05:30
|
|
|
const jobsWithNeeds = {
|
|
|
|
[jobName1]: job1,
|
|
|
|
[jobName2]: job2,
|
|
|
|
[jobName3]: job3,
|
|
|
|
[jobName4]: job4,
|
|
|
|
};
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(generateJobNeedsDict(jobsWithNeeds)).toEqual({
|
|
|
|
[jobName1]: [],
|
|
|
|
[jobName2]: [],
|
|
|
|
[jobName3]: [jobName1, jobName2],
|
|
|
|
[jobName4]: [jobName3, jobName1, jobName2],
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it('removes needs which are not in the data', () => {
|
|
|
|
const inexistantJobName = 'job5';
|
|
|
|
const jobsWithNeeds = {
|
|
|
|
[jobName1]: job1,
|
|
|
|
[jobName2]: job2,
|
|
|
|
[jobName3]: job3,
|
|
|
|
[jobName4]: {
|
|
|
|
name: jobName4,
|
|
|
|
script: 'echo deploy',
|
|
|
|
stage: 'deploy',
|
|
|
|
needs: [inexistantJobName],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(generateJobNeedsDict(jobsWithNeeds)).toEqual({
|
|
|
|
[jobName1]: [],
|
|
|
|
[jobName2]: [],
|
|
|
|
[jobName3]: [jobName1, jobName2],
|
|
|
|
[jobName4]: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('handles parallel jobs by adding the group name as a need', () => {
|
|
|
|
const size = 3;
|
|
|
|
const jobOptimize1 = 'optimize_1';
|
|
|
|
const jobPrepareA = 'prepare_a';
|
|
|
|
const jobPrepareA1 = `${jobPrepareA} 1/${size}`;
|
|
|
|
const jobPrepareA2 = `${jobPrepareA} 2/${size}`;
|
|
|
|
const jobPrepareA3 = `${jobPrepareA} 3/${size}`;
|
|
|
|
|
|
|
|
const jobsParallel = {
|
|
|
|
[jobOptimize1]: {
|
|
|
|
jobs: [job1],
|
|
|
|
name: [jobOptimize1],
|
|
|
|
needs: [jobPrepareA1, jobPrepareA2, jobPrepareA3],
|
|
|
|
},
|
|
|
|
[jobPrepareA]: { jobs: [], name: jobPrepareA, needs: [], size },
|
|
|
|
[jobPrepareA1]: { jobs: [], name: jobPrepareA, needs: [], size },
|
|
|
|
[jobPrepareA2]: { jobs: [], name: jobPrepareA, needs: [], size },
|
|
|
|
[jobPrepareA3]: { jobs: [], name: jobPrepareA, needs: [], size },
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(generateJobNeedsDict(jobsParallel)).toEqual({
|
|
|
|
[jobOptimize1]: [
|
|
|
|
jobPrepareA1,
|
|
|
|
// This is the important part, the `jobPrepareA` group name has been
|
|
|
|
// added to our list of needs.
|
|
|
|
jobPrepareA,
|
|
|
|
jobPrepareA2,
|
|
|
|
jobPrepareA3,
|
|
|
|
],
|
|
|
|
[jobPrepareA]: [],
|
|
|
|
[jobPrepareA1]: [],
|
|
|
|
[jobPrepareA2]: [],
|
|
|
|
[jobPrepareA3]: [],
|
|
|
|
});
|
|
|
|
});
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
describe('getPipelineDefaultTab', () => {
|
|
|
|
const baseUrl = 'http://gitlab.com/user/multi-projects-small/-/pipelines/332/';
|
|
|
|
it('returns null if there was no `tab` params', () => {
|
|
|
|
expect(getPipelineDefaultTab(baseUrl)).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if there was no valid tab param', () => {
|
|
|
|
expect(getPipelineDefaultTab(`${baseUrl}?${TAB_QUERY_PARAM}=invalid`)).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the correct tab name if present', () => {
|
|
|
|
validPipelineTabNames.forEach((tabName) => {
|
|
|
|
expect(getPipelineDefaultTab(`${baseUrl}?${TAB_QUERY_PARAM}=${tabName}`)).toBe(tabName);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|