File: stage_entity_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (168 lines) | stat: -rw-r--r-- 5,458 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe StageEntity, feature_category: :continuous_integration do
  let(:pipeline) { create(:ci_pipeline) }
  let(:request) { double('request') }
  let(:user) { create(:user) }

  let(:entity) do
    described_class.new(stage, request: request)
  end

  let(:stage) do
    create(:ci_stage, pipeline: pipeline, status: :success)
  end

  before do
    allow(request).to receive(:current_user).and_return(user)
    create(:ci_build, :success, pipeline: pipeline, stage_id: stage.id)
  end

  describe '#as_json' do
    subject { entity.as_json }

    it 'contains relevant fields' do
      expect(subject).to include :name, :status, :path
    end

    it 'contains detailed status' do
      expect(subject[:status]).to include :text, :label, :group, :icon, :tooltip
      expect(subject[:status][:label]).to eq s_('CiStatusLabel|passed')
    end

    it 'contains valid name' do
      expect(subject[:name]).to eq 'test'
    end

    it 'contains valid id' do
      expect(subject[:id]).to eq stage.id
    end

    it 'contains path to the stage' do
      expect(subject[:path])
        .to include "pipelines/#{pipeline.id}##{stage.name}"
    end

    it 'contains path to the stage dropdown' do
      expect(subject[:dropdown_path])
        .to include "pipelines/#{pipeline.id}/stage.json?stage=test"
    end

    it 'contains stage title' do
      expect(subject[:title]).to eq "test: #{s_('CiStatusLabel|passed')}"
    end

    it 'does not contain play_details info' do
      expect(subject[:status][:action]).not_to be_present
    end

    context 'when the jobs should be grouped' do
      let(:entity) { described_class.new(stage, request: request, grouped: true) }

      it 'exposes the group key' do
        expect(subject).to include :groups
      end

      context 'and contains commit status' do
        before do
          create(:generic_commit_status, pipeline: pipeline, ci_stage: stage)
        end

        it 'contains commit status' do
          groups = subject[:groups].map { |group| group[:name] }
          expect(groups).to include('generic')
        end
      end
    end

    context 'with a skipped stage ' do
      let(:stage) { create(:ci_stage, status: 'skipped') }

      it 'contains play_all_manual' do
        expect(subject[:status][:action]).to be_present
      end
    end

    context 'with a scheduled stage ' do
      let(:stage) { create(:ci_stage, status: 'scheduled') }

      it 'contains play_all_manual' do
        expect(subject[:status][:action]).to be_present
      end
    end

    context 'with a manual stage ' do
      let(:stage) { create(:ci_stage, status: 'manual') }

      it 'contains play_all_manual' do
        expect(subject[:status][:action]).to be_present
      end
    end

    context 'when details: true' do
      def serialize(stage)
        described_class.new(stage, request: request, details: true).as_json
      end

      it 'avoids N+1 queries on latest_statuses', :use_sql_query_cache, :request_store do
        pipeline = create(:ci_pipeline)
        stage = create(:ci_stage, pipeline: pipeline, status: :success)

        serialize(stage) # Warm up O(1) queries

        # Prepare control
        create(:ci_build, :tags, ci_stage: stage, pipeline: pipeline)
        create(:ci_bridge, ci_stage: stage, pipeline: pipeline)
        create(:generic_commit_status, ci_stage: stage, pipeline: pipeline)

        control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
          serialize(stage)
        end

        # Prepare sample using a generous number to counteract any caches from
        # the control
        create_list(:ci_build, 10, :tags, ci_stage: stage, pipeline: pipeline)
        create_list(:ci_bridge, 10, ci_stage: stage, pipeline: pipeline)
        create_list(:generic_commit_status, 10, ci_stage: stage, pipeline: pipeline)

        expect { serialize(stage) }.not_to exceed_query_limit(control)
      end
    end

    context 'when details: true and retried: true' do
      let(:pipeline) { create(:ci_pipeline) }
      let(:stage) { create(:ci_stage, pipeline: pipeline, status: :success) }
      let(:entity) { described_class.new(stage, request: request, details: true, retried: true) }

      before do
        create(:ci_build, :success, pipeline: pipeline, stage_id: stage.id, name: 'latest_job')
        create(:ci_build, :retried, pipeline: pipeline, stage_id: stage.id, name: 'retried_job')
        create(:ci_build, :failed, pipeline: pipeline, stage_id: stage.id, name: 'failed_job')
      end

      it 'exposes latest_statuses and retried' do
        result = entity.as_json

        expect(result).to include(:latest_statuses, :retried)
        expect(result[:latest_statuses].map { |job| job[:name] }).to include('failed_job', 'latest_job')
        expect(result[:retried].map { |job| job[:name] }).to eq(['retried_job'])
      end

      it 'does not expose latest_statuses when details is false' do
        result = described_class.new(stage, request: request, retried: true).as_json

        expect(result).not_to include(:latest_statuses)
        expect(result).to include(:retried)
      end

      it 'does not expose retried when retried is false' do
        result = described_class.new(stage, request: request, details: true).as_json

        expect(result).to include(:latest_statuses)
        expect(result).not_to include(:retried)
      end
    end
  end
end