Add latest changes from gitlab-org/gitlab@master

This commit is contained in:
GitLab Bot 2023-12-29 15:13:59 +00:00
parent d1fe6b3349
commit 36c4308d16
4 changed files with 77 additions and 2 deletions

View File

@ -464,6 +464,21 @@ module Ci
end
end
def clear_heartbeat
cleared_attributes = {
version: nil,
revision: nil,
platform: nil,
architecture: nil,
ip_address: nil,
executor_type: nil,
config: {},
contacted_at: nil
}
merge_cache_attributes(cleared_attributes)
update_columns(cleared_attributes)
end
def pick_build!(build)
tick_runner_queue if matches_build?(build)
end

View File

@ -20,6 +20,8 @@ module Ci
runner_manager = runner.runner_managers.find_by_system_xid!(system_id)
runner_manager.destroy!
runner.clear_heartbeat if runner.runner_managers.empty?
ServiceResponse.success
end

View File

@ -1198,6 +1198,46 @@ RSpec.describe Ci::Runner, type: :model, feature_category: :runner do
end
end
describe '#clear_heartbeat', :freeze_time do
let!(:runner) { create(:ci_runner, :project, version: '15.0.0') }
let(:heartbeat_values) do
{
version: '15.0.1',
platform: 'darwin',
architecture: '18-bit',
ip_address: '1.1.1.1',
executor: 'shell',
revision: 'sha',
config: { 'gpus' => 'all' }
}
end
let(:expected_attributes) { heartbeat_values.except(:executor).merge(executor_type: 'shell') }
let(:expected_cleared_attributes) { expected_attributes.to_h { |key, _| [key, nil] }.merge(config: {}) }
it 'clears contacted at and other attributes' do
expect do
runner.heartbeat(heartbeat_values)
end.to change { runner.reload.contacted_at }.from(nil).to(Time.current)
.and change { runner.reload.uncached_contacted_at }.from(nil).to(Time.current)
expected_attributes.each do |key, value|
expect(runner.public_send(key)).to eq(value)
expect(runner.read_attribute(key)).to eq(value)
end
expect do
runner.clear_heartbeat
end.to change { runner.reload.contacted_at }.from(Time.current).to(nil)
.and change { runner.reload.uncached_contacted_at }.from(Time.current).to(nil)
expected_cleared_attributes.each do |key, value|
expect(runner.public_send(key)).to eq(value)
expect(runner.read_attribute(key)).to eq(value)
end
end
end
describe '#destroy' do
let(:runner) { create(:ci_runner) }

View File

@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe ::Ci::Runners::UnregisterRunnerManagerService, '#execute', feature_category: :fleet_visibility do
RSpec.describe ::Ci::Runners::UnregisterRunnerManagerService, '#execute', :freeze_time, feature_category: :fleet_visibility do
subject(:execute) { described_class.new(runner, 'some_token', system_id: system_id).execute }
context 'with runner registered with registration token' do
@ -21,7 +21,7 @@ RSpec.describe ::Ci::Runners::UnregisterRunnerManagerService, '#execute', featur
context 'with runner created in UI' do
let!(:runner_manager1) { create(:ci_runner_machine, runner: runner, system_xid: 'system_id_1') }
let!(:runner_manager2) { create(:ci_runner_machine, runner: runner, system_xid: 'system_id_2') }
let!(:runner) { create(:ci_runner, registration_type: :authenticated_user) }
let!(:runner) { create(:ci_runner, registration_type: :authenticated_user, contacted_at: Time.current) }
context 'with system_id specified' do
let(:system_id) { runner_manager1.system_xid }
@ -34,6 +34,24 @@ RSpec.describe ::Ci::Runners::UnregisterRunnerManagerService, '#execute', featur
expect(runner[:errors]).to be_nil
expect(runner.runner_managers).to contain_exactly(runner_manager2)
end
it 'does not clear runner heartbeat' do
expect(runner).not_to receive(:clear_heartbeat)
expect(execute).to be_success
end
context "when there are no runner managers left after deletion" do
let!(:runner_manager2) { nil }
it 'clears the heartbeat attributes' do
expect(runner).to receive(:clear_heartbeat).and_call_original
expect do
expect(execute).to be_success
end.to change { runner.reload.read_attribute(:contacted_at) }.from(Time.current).to(nil)
end
end
end
context 'with unknown system_id' do