From c1ba060e5d3c9e0b071858a298afaed50de1d430 Mon Sep 17 00:00:00 2001 From: Reuben Pereira Date: Wed, 6 Nov 2024 10:51:58 +0530 Subject: [PATCH] Make some methods in AutoDeploy::Tag class methods Methods that only call other class methods should themselves be class methods. This changes product_version, component_ref and omnibus_package methods to be class methods. Also update callers of those methods, and their specs. --- lib/release_tools/auto_deploy/tag.rb | 8 +-- .../auto_deploy/wait_for_package.rb | 2 +- .../tasks/auto_deploy/baking_time.rb | 2 +- .../tasks/auto_deploy/deploy_trigger.rb | 2 +- lib/tasks/auto_deploy.rake | 2 +- .../lib/release_tools/auto_deploy/tag_spec.rb | 26 +++----- .../auto_deploy/wait_for_package_spec.rb | 5 +- .../tasks/auto_deploy/baking_time_spec.rb | 62 +++++++++++++++++++ spec/lib/tasks/auto_deploy_rake_spec.rb | 6 -- .../deploy_trigger_shared_examples.rb | 12 ++-- 10 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 spec/lib/release_tools/tasks/auto_deploy/baking_time_spec.rb diff --git a/lib/release_tools/auto_deploy/tag.rb b/lib/release_tools/auto_deploy/tag.rb index f178a52ed..7d6c9a08c 100644 --- a/lib/release_tools/auto_deploy/tag.rb +++ b/lib/release_tools/auto_deploy/tag.rb @@ -37,16 +37,16 @@ module ReleaseTools NOW.strftime('%Y%m%d%H%M') end - def omnibus_package + def self.omnibus_package component_ref(component: 'omnibus-gitlab-ee').sub('+', '-') end - def component_ref(component:) + def self.component_ref(component:) product_version[component]&.ref end - def product_version - @product_version ||= ProductVersion.new(self.class.current) + def self.product_version + ProductVersion.new(current) end end end diff --git a/lib/release_tools/auto_deploy/wait_for_package.rb b/lib/release_tools/auto_deploy/wait_for_package.rb index c267edb58..4e8715b38 100644 --- a/lib/release_tools/auto_deploy/wait_for_package.rb +++ b/lib/release_tools/auto_deploy/wait_for_package.rb @@ -49,7 +49,7 @@ module ReleaseTools private def find_ref - ReleaseTools::AutoDeploy::Tag.new.component_ref(component: @release_name) + ReleaseTools::AutoDeploy::Tag.component_ref(component: @release_name) end # Get the latest pipeline for a given ref diff --git a/lib/release_tools/tasks/auto_deploy/baking_time.rb b/lib/release_tools/tasks/auto_deploy/baking_time.rb index 601e53c62..ce61c1455 100644 --- a/lib/release_tools/tasks/auto_deploy/baking_time.rb +++ b/lib/release_tools/tasks/auto_deploy/baking_time.rb @@ -7,7 +7,7 @@ module ReleaseTools class BakingTime def execute status = ReleaseTools::Promotion::ProductionStatus.new(:canary_up) - package = ReleaseTools::AutoDeploy::Tag.new.omnibus_package + package = ReleaseTools::AutoDeploy::Tag.omnibus_package foreword = ReleaseTools::Promotion::BakingTimeForeword.new(package) Retriable.retriable do diff --git a/lib/release_tools/tasks/auto_deploy/deploy_trigger.rb b/lib/release_tools/tasks/auto_deploy/deploy_trigger.rb index e58f139a7..77a942ba8 100644 --- a/lib/release_tools/tasks/auto_deploy/deploy_trigger.rb +++ b/lib/release_tools/tasks/auto_deploy/deploy_trigger.rb @@ -70,7 +70,7 @@ module ReleaseTools end def omnibus_package - @omnibus_package ||= ReleaseTools::AutoDeploy::Tag.new.omnibus_package + @omnibus_package ||= ReleaseTools::AutoDeploy::Tag.omnibus_package end def save_pipeline_id_to_deploy_vars(pipeline_id) diff --git a/lib/tasks/auto_deploy.rake b/lib/tasks/auto_deploy.rake index cf8749490..05e72e4dc 100644 --- a/lib/tasks/auto_deploy.rake +++ b/lib/tasks/auto_deploy.rake @@ -8,7 +8,7 @@ namespace :auto_deploy do next end - product_version = ReleaseTools::AutoDeploy::Tag.new.product_version + product_version = ReleaseTools::AutoDeploy::Tag.product_version ReleaseTools::Tasks::AutoDeploy::Start.new(product_version:).execute end diff --git a/spec/lib/release_tools/auto_deploy/tag_spec.rb b/spec/lib/release_tools/auto_deploy/tag_spec.rb index 4fc3e72b8..956581939 100644 --- a/spec/lib/release_tools/auto_deploy/tag_spec.rb +++ b/spec/lib/release_tools/auto_deploy/tag_spec.rb @@ -3,11 +3,11 @@ require 'spec_helper' RSpec.describe ReleaseTools::AutoDeploy::Tag do - def stub_product_version(tag, metadata:) - product_version = ReleaseTools::ProductVersion.new(tag.to_s) + def stub_product_version(version, metadata:) + product_version = ReleaseTools::ProductVersion.new(version) allow(product_version).to receive(:metadata).and_return(metadata) - allow(tag).to receive(:product_version).and_return(product_version) + allow(described_class).to receive(:product_version).and_return(product_version) end describe '.current' do @@ -63,14 +63,11 @@ RSpec.describe ReleaseTools::AutoDeploy::Tag do } } - ClimateControl.modify(AUTO_DEPLOY_BRANCH: '1-2-auto-deploy') do - tag = described_class.new - stub_product_version(tag, metadata: metadata) + stub_product_version('1.2.201501020304', metadata: metadata) - ref = tag.component_ref(component: 'test') + ref = described_class.component_ref(component: 'test') - expect(ref).to eq(test_ref) - end + expect(ref).to eq(test_ref) end end @@ -86,12 +83,9 @@ RSpec.describe ReleaseTools::AutoDeploy::Tag do } } - ClimateControl.modify(AUTO_DEPLOY_BRANCH: '1-2-auto-deploy') do - tag = described_class.new - stub_product_version(tag, metadata: metadata) + stub_product_version('13.11.202103300420', metadata: metadata) - expect(tag.omnibus_package).to eq('13.11.202103300420-08d97919f95.b031a4c4d63') - end + expect(described_class.omnibus_package).to eq('13.11.202103300420-08d97919f95.b031a4c4d63') end end @@ -115,9 +109,7 @@ RSpec.describe ReleaseTools::AutoDeploy::Tag do .to receive(:new) .and_return(product_version) - tag = described_class.new - - expect(tag.product_version.metadata).to eq(metadata) + expect(described_class.product_version.metadata).to eq(metadata) end end end diff --git a/spec/lib/release_tools/auto_deploy/wait_for_package_spec.rb b/spec/lib/release_tools/auto_deploy/wait_for_package_spec.rb index ff905269c..eaa288d9c 100644 --- a/spec/lib/release_tools/auto_deploy/wait_for_package_spec.rb +++ b/spec/lib/release_tools/auto_deploy/wait_for_package_spec.rb @@ -6,13 +6,12 @@ RSpec.describe ReleaseTools::AutoDeploy::WaitForPackage do let!(:fake_client) { stub_const('ReleaseTools::GitlabDevClient', spy) } before do - fake_instance = double(:instance) - + fake_instance = instance_double(ReleaseTools::AutoDeploy::Tag) allow(ReleaseTools::AutoDeploy::Tag) .to receive(:new) .and_return(fake_instance) - allow(fake_instance) + allow(ReleaseTools::AutoDeploy::Tag) .to receive(:component_ref) .with(component: 'test-release') .and_return('1.2.2021010203') diff --git a/spec/lib/release_tools/tasks/auto_deploy/baking_time_spec.rb b/spec/lib/release_tools/tasks/auto_deploy/baking_time_spec.rb new file mode 100644 index 000000000..5b4f14969 --- /dev/null +++ b/spec/lib/release_tools/tasks/auto_deploy/baking_time_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'release_tools/tasks' + +describe ReleaseTools::Tasks::AutoDeploy::BakingTime do + subject(:execute) { described_class.new.execute } + + describe '#execute' do + let(:production_status) do + instance_double(ReleaseTools::Promotion::ProductionStatus, fine?: true, to_slack_blocks: ['rest']) + end + + before do + allow(ReleaseTools::Promotion::ProductionStatus) + .to receive(:new) + .and_return(production_status) + + allow(ReleaseTools::AutoDeploy::Tag).to receive(:omnibus_package).and_return('') + + allow(ReleaseTools::Promotion::BakingTimeForeword) + .to receive(:new) + .and_return(instance_double(ReleaseTools::Promotion::BakingTimeForeword, to_slack_block: 'foreword')) + end + + it 'sends a notification' do + expect(ReleaseTools::Promotion::ProductionStatus).to receive(:new) + + expect(ReleaseTools::AutoDeploy::Tag).to receive(:omnibus_package) + + expect(ReleaseTools::Promotion::BakingTimeForeword).to receive(:new) + + expect(ReleaseTools::Slack::ChatopsNotification) + .to receive(:fire_hook) + .with( + text: "Baking time completed, ready to promote", + channel: ReleaseTools::Slack::F_UPCOMING_RELEASE, + blocks: %w[foreword rest] + ) + + execute + end + + context 'when promotion is blocked' do + let(:production_status) do + instance_double(ReleaseTools::Promotion::ProductionStatus, fine?: false, to_slack_blocks: ['rest']) + end + + it 'responds with promotion blocked' do + expect(ReleaseTools::Slack::ChatopsNotification) + .to receive(:fire_hook) + .with( + text: "Baking time completed, promotion blocked", + channel: ReleaseTools::Slack::F_UPCOMING_RELEASE, + blocks: %w[foreword rest] + ) + + execute + end + end + end +end diff --git a/spec/lib/tasks/auto_deploy_rake_spec.rb b/spec/lib/tasks/auto_deploy_rake_spec.rb index 3ce9e3592..88c2626bf 100644 --- a/spec/lib/tasks/auto_deploy_rake_spec.rb +++ b/spec/lib/tasks/auto_deploy_rake_spec.rb @@ -3,12 +3,6 @@ require 'rake_helper' describe 'auto deploy tasks', :rake do - around do |ex| - ClimateControl.modify(AUTO_DEPLOY_BRANCH: '1-2-auto-deploy') do - ex.run - end - end - describe 'start', task: 'auto_deploy:start' do it 'calls AutoDeploy::Start with version' do expect(ReleaseTools::Tasks::AutoDeploy::Start) diff --git a/spec/support/shared_examples/deploy_trigger_shared_examples.rb b/spec/support/shared_examples/deploy_trigger_shared_examples.rb index 21438eb27..e387fb376 100644 --- a/spec/support/shared_examples/deploy_trigger_shared_examples.rb +++ b/spec/support/shared_examples/deploy_trigger_shared_examples.rb @@ -20,15 +20,13 @@ RSpec.shared_examples 'deploy trigger' do |ref, environment| end before do - fake_instance = double(:instance) + fake_instance = instance_double(ReleaseTools::AutoDeploy::Tag) allow(ReleaseTools::AutoDeploy::Tag) - .to receive(:new) - .and_return(fake_instance) - - allow(fake_instance) - .to receive(:omnibus_package) - .and_return('13.9.202102081220-19f7e30d4cf.3fc3c71e4fc') + .to receive_messages( + new: fake_instance, + omnibus_package: '13.9.202102081220-19f7e30d4cf.3fc3c71e4fc' + ) end after do -- GitLab