diff options
Diffstat (limited to 'spec/frontend/pipelines/pipeline_graph')
-rw-r--r-- | spec/frontend/pipelines/pipeline_graph/mock_data.js | 21 | ||||
-rw-r--r-- | spec/frontend/pipelines/pipeline_graph/utils_spec.js | 307 |
2 files changed, 201 insertions, 127 deletions
diff --git a/spec/frontend/pipelines/pipeline_graph/mock_data.js b/spec/frontend/pipelines/pipeline_graph/mock_data.js index 5a5d6c021a6..b50932deec6 100644 --- a/spec/frontend/pipelines/pipeline_graph/mock_data.js +++ b/spec/frontend/pipelines/pipeline_graph/mock_data.js @@ -1,3 +1,5 @@ +import { createUniqueJobId } from '~/pipelines/utils'; + export const yamlString = `stages: - empty - build @@ -39,18 +41,20 @@ deploy_a: script: echo hello `; +const jobId1 = createUniqueJobId('build', 'build_1'); +const jobId2 = createUniqueJobId('test', 'test_1'); +const jobId3 = createUniqueJobId('test', 'test_2'); +const jobId4 = createUniqueJobId('deploy', 'deploy_1'); + export const pipelineData = { stages: [ { name: 'build', - groups: [], - }, - { - name: 'build', groups: [ { name: 'build_1', jobs: [{ script: 'echo hello', stage: 'build' }], + id: jobId1, }, ], }, @@ -60,10 +64,12 @@ export const pipelineData = { { name: 'test_1', jobs: [{ script: 'yarn test', stage: 'test' }], + id: jobId2, }, { name: 'test_2', jobs: [{ script: 'yarn karma', stage: 'test' }], + id: jobId3, }, ], }, @@ -73,8 +79,15 @@ export const pipelineData = { { name: 'deploy_1', jobs: [{ script: 'yarn magick', stage: 'deploy' }], + id: jobId4, }, ], }, ], + jobs: { + [jobId1]: {}, + [jobId2]: {}, + [jobId3]: {}, + [jobId4]: {}, + }, }; diff --git a/spec/frontend/pipelines/pipeline_graph/utils_spec.js b/spec/frontend/pipelines/pipeline_graph/utils_spec.js index dd85c8c2bd0..ade026c7053 100644 --- a/spec/frontend/pipelines/pipeline_graph/utils_spec.js +++ b/spec/frontend/pipelines/pipeline_graph/utils_spec.js @@ -1,150 +1,211 @@ -import { preparePipelineGraphData } from '~/pipelines/utils'; +import { + preparePipelineGraphData, + createUniqueJobId, + generateJobNeedsDict, +} from '~/pipelines/utils'; -describe('preparePipelineGraphData', () => { - const emptyResponse = { stages: [] }; +describe('utils functions', () => { + const emptyResponse = { stages: [], jobs: {} }; const jobName1 = 'build_1'; const jobName2 = 'build_2'; const jobName3 = 'test_1'; const jobName4 = 'deploy_1'; - const job1 = { [jobName1]: { script: 'echo hello', stage: 'build' } }; - const job2 = { [jobName2]: { script: 'echo build', stage: 'build' } }; - const job3 = { [jobName3]: { script: 'echo test', stage: 'test' } }; - const job4 = { [jobName4]: { script: 'echo deploy', stage: 'deploy' } }; - - describe('returns an object with an empty array of stages if', () => { - it('no data is passed', () => { - expect(preparePipelineGraphData({})).toEqual(emptyResponse); - }); + const job1 = { script: 'echo hello', stage: 'build' }; + const job2 = { script: 'echo build', stage: 'build' }; + const job3 = { script: 'echo test', stage: 'test', needs: [jobName1, jobName2] }; + const job4 = { script: 'echo deploy', stage: 'deploy', needs: [jobName3] }; + const userDefinedStage = 'myStage'; - it('no stages are found', () => { - expect(preparePipelineGraphData({ includes: 'template/myTemplate.gitlab-ci.yml' })).toEqual( - emptyResponse, - ); - }); - }); - - describe('returns the correct array of stages', () => { - it('when multiple jobs are in the same stage', () => { - const expectedData = { - stages: [ + const pipelineGraphData = { + stages: [ + { + name: userDefinedStage, + groups: [], + }, + { + name: job4.stage, + groups: [ { - name: job1[jobName1].stage, - groups: [ - { - name: jobName1, - jobs: [{ script: job1[jobName1].script, stage: job1[jobName1].stage }], - }, - { - name: jobName2, - jobs: [{ script: job2[jobName2].script, stage: job2[jobName2].stage }], - }, - ], + name: jobName4, + jobs: [{ ...job4 }], + id: createUniqueJobId(job4.stage, jobName4), }, ], - }; - - expect(preparePipelineGraphData({ ...job1, ...job2 })).toEqual(expectedData); - }); - - it('when stages are defined by the user', () => { - const userDefinedStage = 'myStage'; - const userDefinedStage2 = 'myStage2'; - - const expectedData = { - stages: [ + }, + { + name: job1.stage, + groups: [ { - name: userDefinedStage, - groups: [], + name: jobName1, + jobs: [{ ...job1 }], + id: createUniqueJobId(job1.stage, jobName1), }, { - name: userDefinedStage2, - groups: [], + name: jobName2, + jobs: [{ ...job2 }], + id: createUniqueJobId(job2.stage, jobName2), }, ], - }; + }, + { + name: job3.stage, + groups: [ + { + name: jobName3, + jobs: [{ ...job3 }], + id: createUniqueJobId(job3.stage, jobName3), + }, + ], + }, + ], + jobs: { + [jobName1]: { ...job1, id: createUniqueJobId(job1.stage, jobName1) }, + [jobName2]: { ...job2, id: createUniqueJobId(job2.stage, jobName2) }, + [jobName3]: { ...job3, id: createUniqueJobId(job3.stage, jobName3) }, + [jobName4]: { ...job4, id: createUniqueJobId(job4.stage, jobName4) }, + }, + }; - expect(preparePipelineGraphData({ stages: [userDefinedStage, userDefinedStage2] })).toEqual( - expectedData, - ); - }); + describe('preparePipelineGraphData', () => { + describe('returns an empty array of stages and empty job objects if', () => { + it('no data is passed', () => { + expect(preparePipelineGraphData({})).toEqual(emptyResponse); + }); - it('by combining user defined stage and job stages, it preserves user defined order', () => { - const userDefinedStage = 'myStage'; - const userDefinedStageThatOverlaps = 'deploy'; + it('no stages are found', () => { + expect(preparePipelineGraphData({ includes: 'template/myTemplate.gitlab-ci.yml' })).toEqual( + emptyResponse, + ); + }); + }); - const expectedData = { - stages: [ - { - name: userDefinedStage, - groups: [], - }, - { - name: job4[jobName4].stage, - groups: [ - { - name: jobName4, - jobs: [{ script: job4[jobName4].script, stage: job4[jobName4].stage }], - }, - ], - }, - { - name: job1[jobName1].stage, - groups: [ - { - name: jobName1, - jobs: [{ script: job1[jobName1].script, stage: job1[jobName1].stage }], - }, - { - name: jobName2, - jobs: [{ script: job2[jobName2].script, stage: job2[jobName2].stage }], - }, - ], + describe('returns the correct array of stages and object of jobs', () => { + it('when multiple jobs are in the same stage', () => { + const expectedData = { + stages: [ + { + name: job1.stage, + groups: [ + { + name: jobName1, + jobs: [{ ...job1 }], + id: createUniqueJobId(job1.stage, jobName1), + }, + { + name: jobName2, + jobs: [{ ...job2 }], + id: createUniqueJobId(job2.stage, jobName2), + }, + ], + }, + ], + jobs: { + [jobName1]: { ...job1, id: createUniqueJobId(job1.stage, jobName1) }, + [jobName2]: { ...job2, id: createUniqueJobId(job2.stage, jobName2) }, }, - { - name: job3[jobName3].stage, - groups: [ - { - name: jobName3, - jobs: [{ script: job3[jobName3].script, stage: job3[jobName3].stage }], - }, - ], + }; + expect( + preparePipelineGraphData({ [jobName1]: { ...job1 }, [jobName2]: { ...job2 } }), + ).toEqual(expectedData); + }); + + it('when stages are defined by the user', () => { + const userDefinedStage2 = 'myStage2'; + + const expectedData = { + stages: [ + { + name: userDefinedStage, + groups: [], + }, + { + name: userDefinedStage2, + groups: [], + }, + ], + jobs: {}, + }; + + expect(preparePipelineGraphData({ stages: [userDefinedStage, userDefinedStage2] })).toEqual( + expectedData, + ); + }); + + it('by combining user defined stage and job stages, it preserves user defined order', () => { + const userDefinedStageThatOverlaps = 'deploy'; + + expect( + preparePipelineGraphData({ + stages: [userDefinedStage, userDefinedStageThatOverlaps], + [jobName1]: { ...job1 }, + [jobName2]: { ...job2 }, + [jobName3]: { ...job3 }, + [jobName4]: { ...job4 }, + }), + ).toEqual(pipelineGraphData); + }); + + it('with only unique values', () => { + const expectedData = { + stages: [ + { + name: job1.stage, + groups: [ + { + name: jobName1, + jobs: [{ ...job1 }], + id: createUniqueJobId(job1.stage, jobName1), + }, + ], + }, + ], + jobs: { + [jobName1]: { ...job1, id: createUniqueJobId(job1.stage, jobName1) }, }, - ], - }; + }; - expect( - preparePipelineGraphData({ - stages: [userDefinedStage, userDefinedStageThatOverlaps], - ...job1, - ...job2, - ...job3, - ...job4, - }), - ).toEqual(expectedData); + expect( + preparePipelineGraphData({ + stages: ['build'], + [jobName1]: { ...job1 }, + [jobName1]: { ...job1 }, + }), + ).toEqual(expectedData); + }); }); + }); - it('with only unique values', () => { - const expectedData = { - stages: [ - { - name: job1[jobName1].stage, - groups: [ - { - name: jobName1, - jobs: [{ script: job1[jobName1].script, stage: job1[jobName1].stage }], - }, - ], - }, - ], + describe('generateJobNeedsDict', () => { + it('generates an empty object if it receives no jobs', () => { + expect(generateJobNeedsDict({ jobs: {} })).toEqual({}); + }); + + it('generates a dict with empty needs if there are no dependencies', () => { + const smallGraph = { + jobs: { + [jobName1]: { ...job1, id: createUniqueJobId(job1.stage, jobName1) }, + [jobName2]: { ...job2, id: createUniqueJobId(job2.stage, jobName2) }, + }, }; - expect( - preparePipelineGraphData({ - stages: ['build'], - ...job1, - ...job1, - }), - ).toEqual(expectedData); + expect(generateJobNeedsDict(smallGraph)).toEqual({ + [pipelineGraphData.jobs[jobName1].id]: [], + [pipelineGraphData.jobs[jobName2].id]: [], + }); + }); + + it('generates a dict where key is the a job and its value is an array of all its needs', () => { + const uniqueJobName1 = pipelineGraphData.jobs[jobName1].id; + const uniqueJobName2 = pipelineGraphData.jobs[jobName2].id; + const uniqueJobName3 = pipelineGraphData.jobs[jobName3].id; + const uniqueJobName4 = pipelineGraphData.jobs[jobName4].id; + + expect(generateJobNeedsDict(pipelineGraphData)).toEqual({ + [uniqueJobName1]: [], + [uniqueJobName2]: [], + [uniqueJobName3]: [uniqueJobName1, uniqueJobName2], + [uniqueJobName4]: [uniqueJobName3, uniqueJobName1, uniqueJobName2], + }); }); }); }); |