diff options
Diffstat (limited to 'spec/migrations/change_public_projects_cost_factor_spec.rb')
-rw-r--r-- | spec/migrations/change_public_projects_cost_factor_spec.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/spec/migrations/change_public_projects_cost_factor_spec.rb b/spec/migrations/change_public_projects_cost_factor_spec.rb new file mode 100644 index 00000000000..78030736093 --- /dev/null +++ b/spec/migrations/change_public_projects_cost_factor_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe ChangePublicProjectsCostFactor, :migration do + # This is a workaround to force the migration to run against the + # `gitlab_ci` schema. Otherwise it only runs against `gitlab_main`. + around do |example| # rubocop: disable Style/MultilineIfModifier + with_reestablished_active_record_base do + reconfigure_db_connection(name: :ci) + example.run + end + end if Gitlab::Database.has_config?(:ci) + + let(:runners) { table(:ci_runners) } + + let!(:shared_1) { runners.create!(runner_type: 1, public_projects_minutes_cost_factor: 0) } + let!(:shared_2) { runners.create!(runner_type: 1, public_projects_minutes_cost_factor: 0) } + let!(:shared_3) { runners.create!(runner_type: 1, public_projects_minutes_cost_factor: 1) } + let!(:group_1) { runners.create!(runner_type: 2, public_projects_minutes_cost_factor: 0) } + + describe '#up' do + context 'when on SaaS' do + before do + allow(Gitlab).to receive(:com?).and_return(true) + end + + it 'updates the cost factor from 0 only for shared runners', :aggregate_failures do + migrate! + + expect(shared_1.reload.public_projects_minutes_cost_factor).to eq(0.008) + expect(shared_2.reload.public_projects_minutes_cost_factor).to eq(0.008) + expect(shared_3.reload.public_projects_minutes_cost_factor).to eq(1) + expect(group_1.reload.public_projects_minutes_cost_factor).to eq(0) + end + end + + context 'when on self-managed', :aggregate_failures do + it 'skips the migration' do + migrate! + + expect(shared_1.public_projects_minutes_cost_factor).to eq(0) + expect(shared_2.public_projects_minutes_cost_factor).to eq(0) + expect(shared_3.public_projects_minutes_cost_factor).to eq(1) + expect(group_1.public_projects_minutes_cost_factor).to eq(0) + end + end + end + + describe '#down' do + context 'when on SaaS' do + before do + allow(Gitlab).to receive(:com?).and_return(true) + end + + it 'resets the cost factor to 0 only for shared runners that were updated', :aggregate_failures do + migrate! + schema_migrate_down! + + expect(shared_1.public_projects_minutes_cost_factor).to eq(0) + expect(shared_2.public_projects_minutes_cost_factor).to eq(0) + expect(shared_3.public_projects_minutes_cost_factor).to eq(1) + expect(group_1.public_projects_minutes_cost_factor).to eq(0) + end + end + end +end |