summaryrefslogtreecommitdiff
path: root/spec/models/ci/build_metadata_spec.rb
blob: 588e5872cc85ce2f0d04787f253fe0fbae6baa3d (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
# frozen_string_literal: true

require 'spec_helper'

describe Ci::BuildMetadata do
  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, :repository, group: group, build_timeout: 2000) }

  let_it_be(:pipeline) do
    create(:ci_pipeline, project: project,
                         sha: project.commit.id,
                         ref: project.default_branch,
                         status: 'success')
  end

  let(:build) { create(:ci_build, pipeline: pipeline) }
  let(:metadata) { build.metadata }

  it_behaves_like 'having unique enum values'

  describe '#update_timeout_state' do
    subject { metadata }

    shared_examples 'sets timeout' do |source, timeout|
      it 'sets project_timeout_source' do
        expect { subject.update_timeout_state }.to change { subject.reload.timeout_source }.to(source)
      end

      it 'sets project timeout' do
        expect { subject.update_timeout_state }.to change { subject.reload.timeout }.to(timeout)
      end
    end

    context 'when project timeout is set' do
      context 'when runner is assigned to the job' do
        before do
          build.update!(runner: runner)
        end

        context 'when runner timeout is not set' do
          let(:runner) { create(:ci_runner, maximum_timeout: nil) }

          it_behaves_like 'sets timeout', 'project_timeout_source', 2000
        end

        context 'when runner timeout is lower than project timeout' do
          let(:runner) { create(:ci_runner, maximum_timeout: 1900) }

          it_behaves_like 'sets timeout', 'runner_timeout_source', 1900
        end

        context 'when runner timeout is higher than project timeout' do
          let(:runner) { create(:ci_runner, maximum_timeout: 2100) }

          it_behaves_like 'sets timeout', 'project_timeout_source', 2000
        end
      end

      context 'when job timeout is set' do
        context 'when job timeout is higher than project timeout' do
          let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 3000 }) }

          it_behaves_like 'sets timeout', 'job_timeout_source', 3000
        end

        context 'when job timeout is lower than project timeout' do
          let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 1000 }) }

          it_behaves_like 'sets timeout', 'job_timeout_source', 1000
        end
      end

      context 'when both runner and job timeouts are set' do
        before do
          build.update(runner: runner)
        end

        context 'when job timeout is higher than runner timeout' do
          let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 3000 }) }
          let(:runner) { create(:ci_runner, maximum_timeout: 2100) }

          it_behaves_like 'sets timeout', 'runner_timeout_source', 2100
        end

        context 'when job timeout is lower than runner timeout' do
          let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 1900 }) }
          let(:runner) { create(:ci_runner, maximum_timeout: 2100) }

          it_behaves_like 'sets timeout', 'job_timeout_source', 1900
        end
      end
    end
  end
end