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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::ResourceGroups::AssignResourceFromResourceGroupService do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let(:service) { described_class.new(project, user) }
describe '#execute' do
subject { service.execute(resource_group) }
let(:resource_group) { create(:ci_resource_group, project: project) }
let!(:build) { create(:ci_build, :waiting_for_resource, project: project, user: user, resource_group: resource_group) }
context 'when there is an available resource' do
it 'requests resource' do
subject
expect(build.reload).to be_pending
expect(build.resource).to be_present
end
context 'when failed to request resource' do
before do
allow_next_instance_of(Ci::Build) do |build|
allow(build).to receive(:enqueue_waiting_for_resource) { false }
end
end
it 'has a build waiting for resource' do
subject
expect(build).to be_waiting_for_resource
end
end
context 'when the build has already retained a resource' do
before do
resource_group.assign_resource_to(build)
build.update_column(:status, :pending)
end
it 'has a pending build' do
subject
expect(build).to be_pending
end
end
context 'when process mode is oldest_first' do
let(:resource_group) { create(:ci_resource_group, process_mode: :oldest_first, project: project) }
it 'requests resource' do
subject
expect(build.reload).to be_pending
expect(build.resource).to be_present
end
context 'when the other job exists in the newer pipeline' do
let!(:build_2) { create(:ci_build, :waiting_for_resource, project: project, user: user, resource_group: resource_group) }
it 'requests resource for the job in the oldest pipeline' do
subject
expect(build.reload).to be_pending
expect(build.resource).to be_present
expect(build_2.reload).to be_waiting_for_resource
expect(build_2.resource).to be_nil
end
end
context 'when build is not `waiting_for_resource` state' do
let!(:build) { create(:ci_build, :created, project: project, user: user, resource_group: resource_group) }
it 'attempts to request a resource' do
expect_next_found_instance_of(Ci::Build) do |job|
expect(job).to receive(:enqueue_waiting_for_resource).and_call_original
end
subject
end
it 'does not change the job status' do
subject
expect(build.reload).to be_created
expect(build.resource).to be_nil
end
end
end
context 'when process mode is newest_first' do
let(:resource_group) { create(:ci_resource_group, process_mode: :newest_first, project: project) }
it 'requests resource' do
subject
expect(build.reload).to be_pending
expect(build.resource).to be_present
end
context 'when the other job exists in the newer pipeline' do
let!(:build_2) { create(:ci_build, :waiting_for_resource, project: project, user: user, resource_group: resource_group) }
it 'requests resource for the job in the newest pipeline' do
subject
expect(build.reload).to be_waiting_for_resource
expect(build.resource).to be_nil
expect(build_2.reload).to be_pending
expect(build_2.resource).to be_present
end
end
context 'when build is not `waiting_for_resource` state' do
let!(:build) { create(:ci_build, :created, project: project, user: user, resource_group: resource_group) }
it 'attempts to request a resource' do
expect_next_found_instance_of(Ci::Build) do |job|
expect(job).to receive(:enqueue_waiting_for_resource).and_call_original
end
subject
end
it 'does not change the job status' do
subject
expect(build.reload).to be_created
expect(build.resource).to be_nil
end
end
end
end
context 'when there are no available resources' do
let!(:other_build) { create(:ci_build) }
before do
resource_group.assign_resource_to(other_build)
end
it 'does not request resource' do
expect_any_instance_of(Ci::Build).not_to receive(:enqueue_waiting_for_resource)
subject
expect(build.reload).to be_waiting_for_resource
end
context 'when there is a stale build assigned to a resource' do
before do
other_build.doom!
other_build.update_column(:updated_at, 10.minutes.ago)
end
it 'releases the resource from the stale build and assignes to the waiting build' do
subject
expect(build.reload).to be_pending
expect(build.resource).to be_present
end
end
end
end
end
|