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

generate_message_to_run_e2e_pipeline_spec.rb « scripts « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b19121573984bd08e39719174c670eacec82e3c (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
# frozen_string_literal: true

# rubocop:disable RSpec/VerifiedDoubles

require 'fast_spec_helper'
require 'gitlab/rspec/all'
require_relative '../../scripts/generate-message-to-run-e2e-pipeline'

RSpec.describe GenerateMessageToRunE2ePipeline, feature_category: :tooling do
  include StubENV

  let(:options) do
    {
      project: '13083',
      pipeline_id: '13083',
      api_token: 'asdf1234',
      endpoint: 'https://gitlab.com/api/v4'
    }
  end

  let(:client) { double('Gitlab::Client') }

  let(:note_content) do
    <<~MARKDOWN
      <!-- Run e2e warning begin -->
      Some note
      <!-- Run e2e warning end -->
    MARKDOWN
  end

  before do
    allow(Gitlab).to receive(:client)
                       .with(endpoint: options[:endpoint], private_token: options[:api_token])
                       .and_return(client)
  end

  subject { described_class.new(options) }

  describe '#execute' do
    let(:commit_merge_request) do
      Struct.new(:author, :iid).new(
        Struct.new(:id, :username).new(
          '2',
          'test_user'
        ),
        '123'
      )
    end

    let(:merge_request) { instance_double(CommitMergeRequests, execute: [commit_merge_request]) }
    let(:merge_request_note_client) { instance_double(CreateMergeRequestNote, execute: true) }

    before do
      stub_env(
        'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA' => 'bfcd2b9b5cad0b889494ce830697392c8ca11257'
      )

      allow(CommitMergeRequests).to receive(:new)
                                      .with(options.merge(sha: ENV['CI_MERGE_REQUEST_SOURCE_BRANCH_SHA']))
                                      .and_return(merge_request)
    end

    context 'when there are qa_test_folders' do
      before do
        allow(subject).to receive(:qa_tests_folders?).and_return(true)
      end

      context 'when there is no existing note' do
        before do
          allow(subject).to receive(:existing_note).and_return(nil)
          allow(subject).to receive(:content).and_return(note_content)

          allow(client).to receive(:create_merge_request_comment)
                             .with(options[:project], '123', note_content)
        end

        it 'adds a new note' do
          expect(CreateMergeRequestNote).to receive(:new)
                                              .with(options.merge(merge_request: commit_merge_request))
                                              .and_return(merge_request_note_client)

          expect(merge_request_note_client).to receive(:execute).with(note_content)

          subject.execute
        end
      end

      context 'when there is existing note' do
        before do
          allow(subject).to receive(:existing_note).and_return(true)
        end

        it 'does not add a new note' do
          expect(CreateMergeRequestNote).not_to receive(:new)

          subject.execute
        end
      end
    end

    context 'when there are no qa_test_folders' do
      before do
        allow(subject).to receive(:qa_tests_folders?).and_return(false)
      end

      it 'does not add a new note' do
        expect(CreateMergeRequestNote).not_to receive(:new)

        subject.execute
      end
    end
  end

  describe '#qa_tests_folders?' do
    before do
      allow(File).to receive(:exist?).with(any_args).and_return(true)
      allow(File).to receive(:open).with(any_args).and_return(file_contents)
    end

    context 'when QA_TESTS is empty' do
      let(:file_contents) do
        %w[
          QA_SUITES='QA::Scenario::Test::Instance::All'
          QA_TESTS=''
          QA_FEATURE_FLAGS=''
        ]
      end

      it 'returns false' do
        expect(subject.send(:qa_tests_folders?)).to be_falsy
      end
    end

    context 'when QA_TESTS has a spec file' do
      let(:file_contents) do
        %w[
          QA_SUITES='QA::Scenario::Test::Instance::All'
          QA_TESTS='qa/specs/features/browser_ui/1_manage/login/log_in_spec.rb'
          QA_FEATURE_FLAGS=''
        ]
      end

      it 'returns false' do
        expect(subject.send(:qa_tests_folders?)).to be_falsy
      end
    end

    context 'when QA_TESTS has folders' do
      let(:file_contents) do
        [
          "QA_SUITES='QA::Scenario::Test::Instance::All'",
          "QA_TESTS='qa/specs/features/browser_ui/1_manage/ qa/specs/features/browser_ui/2_plan'",
          "QA_FEATURE_FLAGS=''"
        ]
      end

      it 'returns true' do
        expect(subject.send(:qa_tests_folders?)).to be_truthy
      end
    end
  end

  describe '#match?' do
    it 'returns true for a note that matches NOTE_PATTERN' do
      expect(subject.send(:match?, note_content)).to be_truthy
    end

    it 'returns false for a note that does not match NOTE_PATTERN' do
      expect(subject.send(:match?, 'Some random text')).to be_falsy
    end
  end

  describe '#existing_note' do
    let(:mr_comments_response) do
      [
        double(:mr_comment, id: 1, body: 'foo'),
        double(:mr_comment, id: 2, body: 'bar'),
        existing_note
      ]
    end

    before do
      allow(client)
        .to receive(:merge_request_comments)
              .with(any_args)
              .and_return(double(auto_paginate: mr_comments_response))
      allow(subject).to receive(:merge_request).and_return(double(:merge_request, id: 2, iid: 123))
    end

    context 'when note exists' do
      let(:existing_note) do
        double(
          :mr_comment,
          id: 3,
          body: note_content
        )
      end

      it 'returns the existing note' do
        expect(subject.send(:existing_note)).to eq existing_note
      end
    end

    context 'when note doesnt exists' do
      let(:existing_note) do
        double(
          :mr_comment,
          id: 3,
          body: 'random content'
        )
      end

      it 'returns nil' do
        expect(subject.send(:existing_note)).to eq nil
      end
    end
  end

  describe '#content' do
    let(:author_username) { 'sam_smith' }

    let(:expected_content) do
      <<~MARKDOWN
      <!-- Run e2e warning begin -->
      @#{author_username} Some end-to-end (E2E) tests should run based on the stage label.

      Please start the `trigger-omnibus-and-follow-up-e2e` job in the `qa` stage and wait for the tests in the `follow-up-e2e:package-and-test-ee` pipeline
      to pass **before merging this MR**. Do not use **Auto-merge**, unless these tests have already completed successfully, because a failure in these tests do not block the auto-merge.
      (E2E tests are computationally intensive and don't run automatically for every push/rebase, so we ask you to run this job manually at least once.)

      To run all E2E tests, apply the ~"pipeline:run-all-e2e" label and run a new pipeline.

      E2E test jobs are allowed to fail due to [flakiness](https://about.gitlab.com/handbook/engineering/quality/quality-engineering/test-metrics-dashboards/#package-and-test).
      See current failures at the latest [pipeline triage issue](https://gitlab.com/gitlab-org/quality/pipeline-triage/-/issues).

      Once done, apply the ✅ emoji on this comment.

      **Team members only:** for any questions or help, reach out on the internal `#test-platform` Slack channel.
      <!-- Run e2e warning end -->
      MARKDOWN
    end

    before do
      allow(subject).to receive(:merge_request).and_return(double(:merge_request,
        author: double(username: author_username)))
    end

    it 'returns content text with author username' do
      expect(subject.send(:content)).to eq expected_content
    end
  end

  describe '#author_username' do
    let(:author_username) { 'sam_smith' }

    before do
      allow(subject).to receive(:merge_request).and_return(double(:merge_request,
        author: double(username: author_username)))
    end

    it 'returns nil' do
      expect(subject.send(:author_username)).to eq author_username
    end
  end

  describe '#env' do
    before do
      stub_env(
        'VAR_WITH_VALUE' => 'bfcd2b9b5cad0b889494ce830697392c8ca11257',
        'EMPTY_VAR' => ' '
      )
    end

    it 'returns env var when not empty' do
      expect(subject.send(:env, 'VAR_WITH_VALUE')).to eq 'bfcd2b9b5cad0b889494ce830697392c8ca11257'
    end

    it 'returns nil when env var is empty' do
      expect(subject.send(:env, 'EMPTY_VAR')).to be_nil
    end
  end
end

# rubocop:enable RSpec/VerifiedDoubles