summaryrefslogtreecommitdiff
path: root/spec/services/ci/runners/process_runner_version_update_service_spec.rb
blob: d2a7e87b2d5e20ee16af49286ddedd844f26deb2 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::Runners::ProcessRunnerVersionUpdateService, feature_category: :runner_fleet do
  subject(:service) { described_class.new(version) }

  let(:version) { '1.0.0' }
  let(:available_runner_releases) { %w[1.0.0 1.0.1] }

  describe '#execute' do
    subject(:execute) { service.execute }

    context 'with upgrade check returning error' do
      let(:service_double) { instance_double(Gitlab::Ci::RunnerUpgradeCheck) }

      before do
        allow(service_double).to receive(:check_runner_upgrade_suggestion).with(version)
          .and_return([version, :error])
        allow(service).to receive(:upgrade_check_service).and_return(service_double)
      end

      it 'does not update ci_runner_versions records', :aggregate_failures do
        expect do
          expect(execute).to be_error
          expect(execute.message).to eq 'upgrade version check failed'
        end.not_to change(Ci::RunnerVersion, :count).from(0)
        expect(service_double).to have_received(:check_runner_upgrade_suggestion).with(version).once
      end
    end

    context 'with successful result from upgrade check' do
      before do
        url = ::Gitlab::CurrentSettings.current_application_settings.public_runner_releases_url

        WebMock.stub_request(:get, url).to_return(
          body: available_runner_releases.map { |v| { name: v } }.to_json,
          status: 200,
          headers: { 'Content-Type' => 'application/json' }
        )
      end

      context 'with no existing ci_runner_version record' do
        it 'creates ci_runner_versions record', :aggregate_failures do
          expect do
            expect(execute).to be_success
            expect(execute.http_status).to eq :ok
            expect(execute.payload).to eq({ upgrade_status: 'recommended' })
          end.to change(Ci::RunnerVersion, :all).to contain_exactly(
            an_object_having_attributes(version: version, status: 'recommended')
          )
        end
      end

      context 'with existing ci_runner_version record' do
        let!(:runner_version) { create(:ci_runner_version, version: '1.0.0', status: :not_available) }

        it 'updates ci_runner_versions record', :aggregate_failures do
          expect do
            expect(execute).to be_success
            expect(execute.http_status).to eq :ok
            expect(execute.payload).to eq({ upgrade_status: 'recommended' })
          end.to change { runner_version.reload.status }.from('not_available').to('recommended')
        end
      end

      context 'with up-to-date ci_runner_version record' do
        let!(:runner_version) { create(:ci_runner_version, version: '1.0.0', status: :recommended) }

        it 'does not update ci_runner_versions record', :aggregate_failures do
          expect do
            expect(execute).to be_success
            expect(execute.http_status).to eq :ok
            expect(execute.payload).to eq({ upgrade_status: 'recommended' })
          end.not_to change { runner_version.reload.status }
        end
      end
    end
  end
end