builds.rb 9.44 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
include ActionDispatch::TestProcess

5
FactoryBot.define do
6
  factory :ci_build, class: 'Ci::Build' do
7 8 9 10 11 12 13
    name { 'test' }
    stage { 'test' }
    stage_idx { 0 }
    ref { 'master' }
    tag { false }
    add_attribute(:protected) { false }
    created_at { 'Di 29. Okt 09:50:00 CET 2013' }
14
    scheduling_type { 'stage' }
15
    pending
16

17 18
    options do
      {
19
        image: 'ruby:2.7',
20 21
        services: ['postgres'],
        script: ['ls -a']
22 23
      }
    end
24

25 26
    yaml_variables do
      [
27
        { key: 'DB_NAME', value: 'postgres', public: true }
28 29
      ]
    end
30

31
    pipeline factory: :ci_pipeline
32
    project { pipeline.project }
33

34
    trait :degenerated do
35 36
      options { nil }
      yaml_variables { nil }
37 38
    end

39
    trait :started do
40
      started_at { 'Di 29. Okt 09:51:28 CET 2013' }
41 42 43 44
    end

    trait :finished do
      started
45
      finished_at { 'Di 29. Okt 09:53:28 CET 2013' }
46 47
    end

48
    trait :success do
49
      finished
50
      status { 'success' }
51 52 53
    end

    trait :failed do
54
      finished
55
      status { 'failed' }
56 57
    end

58
    trait :canceled do
59
      finished
60
      status { 'canceled' }
61 62
    end

63
    trait :skipped do
64
      started
65
      status { 'skipped' }
66 67
    end

68
    trait :running do
69
      started
70
      status { 'running' }
71 72 73
    end

    trait :pending do
74 75
      queued_at { 'Di 29. Okt 09:50:59 CET 2013' }
      status { 'pending' }
76 77
    end

78
    trait :created do
79
      status { 'created' }
80 81
    end

82 83 84 85
    trait :waiting_for_resource do
      status { 'waiting_for_resource' }
    end

Tiger's avatar
Tiger committed
86
    trait :preparing do
87
      status { 'preparing' }
Tiger's avatar
Tiger committed
88 89
    end

90 91
    trait :scheduled do
      schedulable
92
      status { 'scheduled' }
93
      scheduled_at { 1.minute.since }
94 95 96 97
    end

    trait :expired_scheduled do
      schedulable
98
      status { 'scheduled' }
99
      scheduled_at { 1.minute.ago }
100 101
    end

102
    trait :manual do
103 104
      status { 'manual' }
      self.when { 'manual' }
105 106
    end

107
    trait :teardown_environment do
108
      environment { 'staging' }
109 110 111 112
      options do
        {
          script: %w(ls),
          environment: { name: 'staging',
113 114
            action: 'stop',
            url: 'http://staging.example.com/$CI_JOB_NAME' }
115 116
        }
      end
117 118 119
    end

    trait :deploy_to_production do
120
      environment { 'production' }
121

122 123 124 125
      options do
        {
          script: %w(ls),
          environment: { name: 'production',
126
            url: 'http://prd.example.com/$CI_JOB_NAME' }
127 128
        }
      end
129 130 131
    end

    trait :start_review_app do
132
      environment { 'review/$CI_COMMIT_REF_NAME' }
133

134 135 136 137
      options do
        {
          script: %w(ls),
          environment: { name: 'review/$CI_COMMIT_REF_NAME',
138 139
            url: 'http://staging.example.com/$CI_JOB_NAME',
            on_stop: 'stop_review_app' }
140 141
        }
      end
142 143 144
    end

    trait :stop_review_app do
145 146
      name { 'stop_review_app' }
      environment { 'review/$CI_COMMIT_REF_NAME' }
147

148 149 150 151
      options do
        {
          script: %w(ls),
          environment: { name: 'review/$CI_COMMIT_REF_NAME',
152 153
            url: 'http://staging.example.com/$CI_JOB_NAME',
            action: 'stop' }
154 155
        }
      end
156 157
    end

158
    trait :allowed_to_fail do
159
      allow_failure { true }
160 161
    end

162 163 164 165
    trait :ignored do
      allowed_to_fail
    end

166 167 168 169
    trait :playable do
      manual
    end

170 171 172 173
    trait :retryable do
      success
    end

174
    trait :schedulable do
175
      self.when { 'delayed' }
176 177 178 179 180 181 182

      options do
        {
          script: ['ls -a'],
          start_in: '1 minute'
        }
      end
183 184 185
    end

    trait :actionable do
186
      self.when { 'manual' }
187 188
    end

189
    trait :retried do
190
      retried { true }
191 192
    end

193 194 195 196 197 198 199 200 201
    trait :cancelable do
      pending
    end

    trait :erasable do
      success
      artifacts
    end

202
    trait :tags do
203 204 205
      tag_list do
        [:docker, :ruby]
      end
206 207
    end

208
    trait :on_tag do
209
      tag { true }
210 211 212
    end

    trait :triggered do
213
      trigger_request factory: :ci_trigger_request
214 215
    end

216 217 218 219 220 221 222 223
    trait :resource_group do
      waiting_for_resource_at { 5.minutes.ago }

      after(:build) do |build, evaluator|
        build.resource_group = create(:ci_resource_group, project: build.project)
      end
    end

224 225 226 227 228 229
    trait :with_deployment do
      after(:build) do |build, evaluator|
        ##
        # Build deployment/environment relations if environment name is set
        # to the job. If `build.deployment` has already been set, it doesn't
        # build a new instance.
230
        environment = Gitlab::Ci::Pipeline::Seed::Environment.new(build).to_resource
231
        build.deployment =
232
          Gitlab::Ci::Pipeline::Seed::Deployment.new(build, environment).to_resource
233 234 235
      end
    end

236
    trait :tag do
237
      tag { true }
238
    end
239

240
    trait :coverage do
241 242
      coverage { 99.9 }
      coverage_regex { '/(d+)/' }
243 244
    end

245
    trait :trace_live do
246
      after(:create) do |build, evaluator|
247
        build.trace.set('BUILD TRACE')
248
      end
249
    end
250

251 252 253 254 255 256
    trait :trace_artifact do
      after(:create) do |build, evaluator|
        create(:ci_job_artifact, :trace, job: build)
      end
    end

257
    trait :trace_with_duplicate_sections do
258 259 260
      after(:create) do |build, evaluator|
        trace = File.binread(
          File.expand_path(
261
            Rails.root.join('spec/fixtures/trace/trace_with_duplicate_sections')))
262 263 264 265 266

        build.trace.set(trace)
      end
    end

267 268 269 270 271 272 273 274 275 276
    trait :trace_with_sections do
      after(:create) do |build, evaluator|
        trace = File.binread(
          File.expand_path(
            Rails.root.join('spec/fixtures/trace/trace_with_sections')))

        build.trace.set(trace)
      end
    end

277
    trait :unicode_trace_live do
278 279 280 281 282 283 284 285 286
      after(:create) do |build, evaluator|
        trace = File.binread(
          File.expand_path(
            Rails.root.join('spec/fixtures/trace/ansi-sequence-and-unicode')))

        build.trace.set(trace)
      end
    end

287
    trait :erased do
288
      erased_at { Time.now }
289 290 291 292
      erased_by factory: :user
    end

    trait :queued do
293
      queued_at { Time.now }
294 295 296
      runner factory: :ci_runner
    end

297
    trait :artifacts do
298
      after(:create) do |build|
299 300
        create(:ci_job_artifact, :archive, job: build, expire_at: build.artifacts_expire_at)
        create(:ci_job_artifact, :metadata, job: build, expire_at: build.artifacts_expire_at)
301
        build.reload
302
      end
303
    end
304

305
    trait :test_reports do
306 307
      after(:build) do |build|
        build.job_artifacts << create(:ci_job_artifact, :junit, job: build)
308 309 310
      end
    end

311 312 313 314 315 316
    trait :test_reports_with_attachment do
      after(:build) do |build|
        build.job_artifacts << create(:ci_job_artifact, :junit_with_attachment, job: build)
      end
    end

317 318 319 320 321 322
    trait :coverage_reports do
      after(:build) do |build|
        build.job_artifacts << create(:ci_job_artifact, :cobertura, job: build)
      end
    end

323
    trait :expired do
324
      artifacts_expire_at { 1.minute.ago }
325
    end
326 327 328 329 330 331 332 333 334 335 336 337

    trait :with_commit do
      after(:build) do |build|
        allow(build).to receive(:commit).and_return build(:commit, :without_author)
      end
    end

    trait :with_commit_and_author do
      after(:build) do |build|
        allow(build).to receive(:commit).and_return build(:commit)
      end
    end
338 339 340 341

    trait :extended_options do
      options do
        {
342
          image: { name: 'ruby:2.7', entrypoint: '/bin/sh' },
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
          services: ['postgres', { name: 'docker:stable-dind', entrypoint: '/bin/sh', command: 'sleep 30', alias: 'docker' }],
          script: %w(echo),
          after_script: %w(ls date),
          artifacts: {
            name: 'artifacts_file',
            untracked: false,
            paths: ['out/'],
            when: 'always',
            expire_in: '7d'
          },
          cache: {
            key: 'cache_key',
            untracked: false,
            paths: ['vendor/*'],
            policy: 'pull-push'
          }
359 360 361
        }
      end
    end
362 363 364 365

    trait :no_options do
      options { {} }
    end
Filipa Lacerda's avatar
Filipa Lacerda committed
366

367 368
    # TODO: move Security traits to ee_ci_build
    # https://gitlab.com/gitlab-org/gitlab/-/issues/210486
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
    trait :dast do
      options do
        {
            artifacts: { reports: { dast: 'gl-dast-report.json' } }
        }
      end
    end

    trait :sast do
      options do
        {
            artifacts: { reports: { sast: 'gl-sast-report.json' } }
        }
      end
    end

    trait :dependency_scanning do
      options do
        {
            artifacts: { reports: { dependency_scanning: 'gl-dependency-scanning-report.json' } }
        }
      end
    end

    trait :container_scanning do
      options do
        {
            artifacts: { reports: { container_scanning: 'gl-container-scanning-report.json' } }
        }
      end
    end

401 402 403 404 405 406 407 408
    trait :license_management do
      options do
        {
            artifacts: { reports: { license_management: 'gl-license-management-report.json' } }
        }
      end
    end

409 410 411 412 413 414 415 416
    trait :license_scanning do
      options do
        {
          artifacts: { reports: { license_management: 'gl-license-scanning-report.json' } }
        }
      end
    end

Filipa Lacerda's avatar
Filipa Lacerda committed
417
    trait :non_playable do
418 419
      status { 'created' }
      self.when { 'manual' }
Filipa Lacerda's avatar
Filipa Lacerda committed
420
    end
Shinya Maeda's avatar
Shinya Maeda committed
421

422
    trait :protected do
423
      add_attribute(:protected) { true }
Shinya Maeda's avatar
Shinya Maeda committed
424
    end
425 426 427

    trait :script_failure do
      failed
428
      failure_reason { 1 }
429
    end
430 431 432

    trait :api_failure do
      failed
433
      failure_reason { 2 }
434
    end
435

436 437
    trait :prerequisite_failure do
      failed
438
      failure_reason { 10 }
439 440
    end

441 442
    trait :with_runner_session do
      after(:build) do |build|
443
        build.build_runner_session(url: 'https://localhost')
444 445
      end
    end
446 447
  end
end