Welcome to mirror list, hosted at ThFree Co, Russian Federation.

jobs_spec.rb « ci « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 756fcd8b7cdedf719f994e7b7fe1888cb2a9b2ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe 'Query.jobs', feature_category: :continuous_integration do
  include GraphqlHelpers

  let_it_be(:admin) { create(:admin) }
  let_it_be(:project) { create(:project, :repository, :public) }
  let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
  let_it_be(:runner) { create(:ci_runner) }
  let_it_be(:build) do
    create(:ci_build, pipeline: pipeline, name: 'my test job', ref: 'HEAD', tag_list: %w[tag1 tag2], runner: runner)
  end

  let(:query) do
    %(
      query {
        jobs {
          nodes {
            id
            #{fields.join(' ')}
          }
        }
      }
    )
  end

  let(:jobs_graphql_data) { graphql_data_at(:jobs, :nodes) }

  let(:fields) do
    %w[commitPath refPath webPath browseArtifactsPath playPath tags runner{id}]
  end

  it 'returns the paths in each job of a pipeline' do
    post_graphql(query, current_user: admin)

    expect(jobs_graphql_data).to contain_exactly(
      a_graphql_entity_for(
        build,
        commit_path: "/#{project.full_path}/-/commit/#{build.sha}",
        ref_path: "/#{project.full_path}/-/commits/HEAD",
        web_path: "/#{project.full_path}/-/jobs/#{build.id}",
        browse_artifacts_path: "/#{project.full_path}/-/jobs/#{build.id}/artifacts/browse",
        play_path: "/#{project.full_path}/-/jobs/#{build.id}/play",
        tags: build.tag_list,
        runner: a_graphql_entity_for(runner)
      )
    )
  end

  context 'when requesting individual fields' do
    using RSpec::Parameterized::TableSyntax

    let_it_be(:admin2) { create(:admin) }
    let_it_be(:project2) { create(:project) }
    let_it_be(:pipeline2) { create(:ci_pipeline, project: project2) }

    where(:field) { fields }

    with_them do
      let(:fields) do
        [field]
      end

      it 'does not generate N+1 queries', :request_store, :use_sql_query_cache do
        # warm-up cache and so on:
        args = { current_user: admin }
        args2 = { current_user: admin2 }
        post_graphql(query, **args2)

        control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
          post_graphql(query, **args)
        end

        create(:ci_build, pipeline: pipeline2, name: 'my test job2', ref: 'HEAD', tag_list: %w[tag3])
        post_graphql(query, **args)

        expect { post_graphql(query, **args) }.not_to exceed_all_query_limit(control)
      end
    end
  end
end

RSpec.describe 'Query.jobs.runner', feature_category: :continuous_integration do
  include GraphqlHelpers

  let_it_be(:admin) { create(:admin) }

  let(:jobs_runner_graphql_data) { graphql_data_at(:jobs, :nodes, :runner) }
  let(:query) do
    %(
      query {
        jobs {
          nodes {
            runner{
              id
              adminUrl
              description
            }
          }
        }
      }
    )
  end

  context 'when job has no runner' do
    let_it_be(:build) { create(:ci_build) }

    it 'returns nil' do
      post_graphql(query, current_user: admin)

      expect(jobs_runner_graphql_data).to eq([nil])
    end
  end

  context 'when job has runner' do
    let_it_be(:runner) { create(:ci_runner) }
    let_it_be(:build_with_runner) { create(:ci_build, runner: runner) }

    it 'returns runner attributes' do
      post_graphql(query, current_user: admin)

      expect(jobs_runner_graphql_data).to contain_exactly(a_graphql_entity_for(runner, :description, 'adminUrl' => "http://localhost/admin/runners/#{runner.id}"))
    end
  end
end

RSpec.describe 'Query.project.pipeline', feature_category: :continuous_integration do
  include GraphqlHelpers

  let_it_be(:project) { create(:project, :repository, :public) }
  let_it_be(:user) { create(:user) }

  def all(*fields)
    fields.flat_map { |f| [f, :nodes] }
  end

  describe '.stages.groups.jobs' do
    let(:pipeline) do
      pipeline = create(:ci_pipeline, project: project, user: user)
      stage = create(:ci_stage, project: project, pipeline: pipeline, name: 'first', position: 1)
      create(:ci_build, stage_id: stage.id, pipeline: pipeline, name: 'my test job', scheduling_type: :stage)

      pipeline
    end

    let(:jobs_graphql_data) { graphql_data_at(:project, :pipeline, *all(:stages, :groups, :jobs)) }

    let(:first_n) { var('Int') }

    let(:query) do
      with_signature([first_n], wrap_fields(query_graphql_path(
                                              [
                                                [:project, { full_path: project.full_path }],
                                                [:pipeline, { iid: pipeline.iid.to_s }],
                                                [:stages,   { first: first_n }]
                                              ], stage_fields)))
    end

    let(:stage_fields) do
      <<~FIELDS
      nodes {
        name
        groups {
          nodes {
            detailedStatus {
              id
            }
            name
            jobs {
              nodes {
                downstreamPipeline {
                  id
                  path
                }
                name
                needs {
                  nodes { #{all_graphql_fields_for('CiBuildNeed')} }
                }
                previousStageJobsOrNeeds {
                  nodes {
                      ... on CiBuildNeed {
                        #{all_graphql_fields_for('CiBuildNeed')}
                      }
                      ... on CiJob {
                        #{all_graphql_fields_for('CiJob', excluded: %w[aiFailureAnalysis])}
                      }
                    }
                }
                detailedStatus {
                  id
                }
                pipeline {
                  id
                }
              }
            }
          }
        }
      }
      FIELDS
    end

    it 'returns the jobs of a pipeline stage' do
      post_graphql(query, current_user: user)

      expect(jobs_graphql_data).to contain_exactly(a_hash_including('name' => 'my test job'))
    end

    context 'when there is more than one stage and job needs' do
      before do
        build_stage = create(:ci_stage, position: 2, name: 'build', project: project, pipeline: pipeline)
        test_stage = create(:ci_stage, position: 3, name: 'test', project: project, pipeline: pipeline)

        create(:ci_build, pipeline: pipeline, name: 'docker 1 2', scheduling_type: :stage, ci_stage: build_stage, stage_idx: build_stage.position)
        create(:ci_build, pipeline: pipeline, name: 'docker 2 2', ci_stage: build_stage, stage_idx: build_stage.position, scheduling_type: :dag)
        create(:ci_build, pipeline: pipeline, name: 'rspec 1 2', scheduling_type: :stage, ci_stage: test_stage, stage_idx: test_stage.position)
        test_job = create(:ci_build, pipeline: pipeline, name: 'rspec 2 2', scheduling_type: :dag, ci_stage: test_stage, stage_idx: test_stage.position)

        create(:ci_build_need, build: test_job, name: 'my test job')
      end

      it 'reports the build needs and execution requirements' do
        post_graphql(query, current_user: user)

        expect(jobs_graphql_data).to contain_exactly(
          a_hash_including(
            'name' => 'my test job',
            'needs' => { 'nodes' => [] },
            'previousStageJobsOrNeeds' => { 'nodes' => [] }
          ),
          a_hash_including(
            'name' => 'docker 1 2',
            'needs' => { 'nodes' => [] },
            'previousStageJobsOrNeeds' => { 'nodes' => [
              a_hash_including('name' => 'my test job')
            ] }
          ),
          a_hash_including(
            'name' => 'docker 2 2',
            'needs' => { 'nodes' => [] },
            'previousStageJobsOrNeeds' => { 'nodes' => [] }
          ),
          a_hash_including(
            'name' => 'rspec 1 2',
            'needs' => { 'nodes' => [] },
            'previousStageJobsOrNeeds' => { 'nodes' => [
              a_hash_including('name' => 'docker 1 2'),
              a_hash_including('name' => 'docker 2 2')
            ] }
          ),
          a_hash_including(
            'name' => 'rspec 2 2',
            'needs' => { 'nodes' => [a_hash_including('name' => 'my test job')] },
            'previousStageJobsOrNeeds' => { 'nodes' => [
              a_hash_including('name' => 'my test job')
            ] }
          )
        )
      end

      it 'does not generate N+1 queries', :request_store, :use_sql_query_cache do
        create(:ci_bridge, name: 'bridge-1', pipeline: pipeline, downstream_pipeline: create(:ci_pipeline))

        post_graphql(query, current_user: user)

        control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
          post_graphql(query, current_user: user)
        end

        create(:ci_build, name: 'test-a', pipeline: pipeline)
        create(:ci_build, name: 'test-b', pipeline: pipeline)
        create(:ci_bridge, name: 'bridge-2', pipeline: pipeline, downstream_pipeline: create(:ci_pipeline))
        create(:ci_bridge, name: 'bridge-3', pipeline: pipeline, downstream_pipeline: create(:ci_pipeline))

        expect do
          post_graphql(query, current_user: user)
        end.not_to exceed_all_query_limit(control)
      end
    end
  end

  describe '.jobs.kind' do
    let_it_be(:pipeline) { create(:ci_pipeline, project: project) }

    let(:query) do
      %(
        query {
          project(fullPath: "#{project.full_path}") {
            pipeline(iid: "#{pipeline.iid}") {
              stages {
                nodes {
                  groups{
                    nodes {
                      jobs {
                        nodes {
                          kind
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      )
    end

    context 'when the job is a build' do
      it 'returns BUILD' do
        create(:ci_build, pipeline: pipeline)

        post_graphql(query, current_user: user)

        job_data = graphql_data_at(:project, :pipeline, :stages, :nodes, :groups, :nodes, :jobs, :nodes).first
        expect(job_data['kind']).to eq 'BUILD'
      end
    end

    context 'when the job is a bridge' do
      it 'returns BRIDGE' do
        create(:ci_bridge, pipeline: pipeline)

        post_graphql(query, current_user: user)

        job_data = graphql_data_at(:project, :pipeline, :stages, :nodes, :groups, :nodes, :jobs, :nodes).first
        expect(job_data['kind']).to eq 'BRIDGE'
      end
    end
  end

  describe '.jobs.artifacts' do
    let_it_be(:pipeline) { create(:ci_pipeline, project: project) }

    let(:query) do
      %(
        query {
          project(fullPath: "#{project.full_path}") {
            pipeline(iid: "#{pipeline.iid}") {
              stages {
                nodes {
                  groups{
                    nodes {
                      jobs {
                        nodes {
                          artifacts {
                            nodes {
                              downloadPath
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      )
    end

    context 'when the job is a build' do
      it "returns the build's artifacts" do
        create(:ci_build, :artifacts, pipeline: pipeline)

        post_graphql(query, current_user: user)

        job_data = graphql_data_at(:project, :pipeline, :stages, :nodes, :groups, :nodes, :jobs, :nodes).first
        expect(job_data.dig('artifacts', 'nodes').count).to be(2)
      end
    end

    context 'when the job is not a build' do
      it 'returns nil' do
        create(:ci_bridge, pipeline: pipeline)

        post_graphql(query, current_user: user)

        job_data = graphql_data_at(:project, :pipeline, :stages, :nodes, :groups, :nodes, :jobs, :nodes).first
        expect(job_data['artifacts']).to be_nil
      end
    end
  end

  describe '.jobs.runnerManager' do
    let_it_be(:admin) { create(:admin) }
    let_it_be(:runner_manager) { create(:ci_runner_machine, created_at: Time.current, contacted_at: Time.current) }
    let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
    let_it_be(:build) do
      create(:ci_build, pipeline: pipeline, name: 'my test job', runner_manager: runner_manager)
    end

    let(:query) do
      %(
        query {
          project(fullPath: "#{project.full_path}") {
            pipeline(iid: "#{pipeline.iid}") {
              jobs {
                nodes {
                  id
                  name
                  runnerManager {
                    #{all_graphql_fields_for('CiRunnerManager', excluded: [:runner], max_depth: 1)}
                  }
                }
              }
            }
          }
        }
      )
    end

    let(:jobs_graphql_data) { graphql_data_at(:project, :pipeline, :jobs, :nodes) }

    it 'returns the runner manager in each job of a pipeline' do
      post_graphql(query, current_user: admin)

      expect(jobs_graphql_data).to contain_exactly(
        a_graphql_entity_for(
          build,
          name: build.name,
          runner_manager: a_graphql_entity_for(
            runner_manager,
            system_id: runner_manager.system_xid,
            created_at: runner_manager.created_at.iso8601,
            contacted_at: runner_manager.contacted_at.iso8601,
            status: runner_manager.status.to_s.upcase
          )
        )
      )
    end

    it 'does not generate N+1 queries', :request_store, :use_sql_query_cache do
      control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
        post_graphql(query, current_user: admin)
      end

      runner_manager2 = create(:ci_runner_machine)
      create(:ci_build, pipeline: pipeline, name: 'my test job2', runner_manager: runner_manager2)

      expect { post_graphql(query, current_user: admin) }.not_to exceed_all_query_limit(control)
    end
  end

  describe '.jobs.count' do
    let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
    let_it_be(:successful_job) { create(:ci_build, :success, pipeline: pipeline) }
    let_it_be(:pending_job) { create(:ci_build, :pending, pipeline: pipeline) }
    let_it_be(:failed_job) { create(:ci_build, :failed, pipeline: pipeline) }

    let(:query) do
      %(
        query {
          project(fullPath: "#{project.full_path}") {
            pipeline(iid: "#{pipeline.iid}") {
              jobs {
                count
              }
            }
          }
        }
      )
    end

    before do
      post_graphql(query, current_user: user)
    end

    it 'returns the number of jobs' do
      expect(graphql_data_at(:project, :pipeline, :jobs, :count)).to eq(3)
    end

    context 'with limit value' do
      let(:limit) { 1 }

      let(:query) do
        %(
          query {
            project(fullPath: "#{project.full_path}") {
              pipeline(iid: "#{pipeline.iid}") {
                jobs {
                  count(limit: #{limit})
                }
              }
            }
          }
        )
      end

      it 'returns a limited number of jobs' do
        expect(graphql_data_at(:project, :pipeline, :jobs, :count)).to eq(2)
      end

      context 'with invalid value' do
        let(:limit) { 1500 }

        it 'returns a validation error' do
          expect(graphql_errors).to include(a_hash_including('message' => 'limit must be less than or equal to 1000'))
        end
      end
    end

    context 'with jobs filter' do
      let(:query) do
        %(
          query {
            project(fullPath: "#{project.full_path}") {
              jobs(statuses: FAILED) {
                count
              }
            }
          }
        )
      end

      it 'returns the number of failed jobs' do
        expect(graphql_data_at(:project, :jobs, :count)).to eq(1)
      end
    end
  end

  context 'when querying jobs for multiple projects' do
    let(:query) do
      %(
        query {
          projects {
            nodes {
              jobs {
                nodes {
                  name
                }
              }
            }
          }
        }
      )
    end

    before do
      create_list(:project, 2).each do |project|
        project.add_developer(user)
        create(:ci_build, project: project)
      end
    end

    it 'returns an error' do
      post_graphql(query, current_user: user)

      expect_graphql_errors_to_include [/"jobs" field can be requested only for 1 Project\(s\) at a time./]
    end
  end

  context 'when batched querying jobs for multiple projects' do
    let(:batched) do
      [
        { query: query_1 },
        { query: query_2 }
      ]
    end

    let(:query_1) do
      %(
        query Page1 {
          projects {
            nodes {
              jobs {
                nodes {
                  name
                }
              }
            }
          }
        }
      )
    end

    let(:query_2) do
      %(
        query Page2 {
          projects {
            nodes {
              jobs {
                nodes {
                  name
                }
              }
            }
          }
        }
      )
    end

    before do
      create_list(:project, 2).each do |project|
        project.add_developer(user)
        create(:ci_build, project: project)
      end
    end

    it 'limits the specific field evaluation per query' do
      get_multiplex(batched, current_user: user)

      resp = json_response

      expect(resp.first.dig('data', 'projects', 'nodes').first.dig('jobs', 'nodes').first['name']).to eq('test')
      expect(resp.first['errors'].first['message'])
        .to match(/"jobs" field can be requested only for 1 Project\(s\) at a time./)
      expect(resp.second.dig('data', 'projects', 'nodes').first.dig('jobs', 'nodes').first['name']).to eq('test')
      expect(resp.second['errors'].first['message'])
        .to match(/"jobs" field can be requested only for 1 Project\(s\) at a time./)
    end
  end
end