|
| 1 | +require 'spec_helper' |
| 2 | +require 'migrations/helpers/migration_shared_context' |
| 3 | + |
| 4 | +RSpec.describe 'migration to seed WAS_RUNNING events for currently-running app processes', isolation: :truncation, type: :migration do |
| 5 | + include_context 'migration' do |
| 6 | + let(:migration_filename) { '20260428120000_seed_was_running_app_usage_events.rb' } |
| 7 | + end |
| 8 | + |
| 9 | + let(:run_migration) do |
| 10 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) |
| 11 | + end |
| 12 | + |
| 13 | + let(:revert_migration) do |
| 14 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) |
| 15 | + end |
| 16 | + |
| 17 | + describe 'up migration' do |
| 18 | + context 'when there are no processes' do |
| 19 | + it 'inserts no rows' do |
| 20 | + expect { run_migration }.not_to change { db[:app_usage_events].where(state: 'WAS_RUNNING').count }.from(0) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + context 'when there is one STARTED process' do |
| 25 | + let(:parent_app) { VCAP::CloudController::AppModel.make(name: 'my-app') } |
| 26 | + let!(:process) { VCAP::CloudController::ProcessModelFactory.make(app: parent_app, type: 'web', state: 'STARTED', instances: 3, memory: 512) } |
| 27 | + |
| 28 | + it 'inserts one WAS_RUNNING row with the expected fields' do |
| 29 | + expect { run_migration }.to change { db[:app_usage_events].where(state: 'WAS_RUNNING').count }.from(0).to(1) |
| 30 | + |
| 31 | + row = db[:app_usage_events].where(state: 'WAS_RUNNING').first |
| 32 | + expect(row[:guid]).to be_present |
| 33 | + expect(row[:state]).to eq('WAS_RUNNING') |
| 34 | + expect(row[:previous_state]).to be_nil |
| 35 | + expect(row[:app_guid]).to eq(process.guid) |
| 36 | + expect(row[:app_name]).to eq('my-app') |
| 37 | + expect(row[:parent_app_guid]).to eq(parent_app.guid) |
| 38 | + expect(row[:parent_app_name]).to eq('my-app') |
| 39 | + expect(row[:space_guid]).to eq(parent_app.space.guid) |
| 40 | + expect(row[:space_name]).to eq(parent_app.space.name) |
| 41 | + expect(row[:org_guid]).to eq(parent_app.space.organization.guid) |
| 42 | + expect(row[:instance_count]).to eq(3) |
| 43 | + expect(row[:previous_instance_count]).to eq(3) |
| 44 | + expect(row[:memory_in_mb_per_instance]).to eq(512) |
| 45 | + expect(row[:previous_memory_in_mb_per_instance]).to eq(512) |
| 46 | + expect(row[:previous_package_state]).to eq('UNKNOWN') |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context 'when there is a STOPPED process' do |
| 51 | + let(:parent_app) { VCAP::CloudController::AppModel.make } |
| 52 | + let!(:process) { VCAP::CloudController::ProcessModelFactory.make(app: parent_app, state: 'STOPPED') } |
| 53 | + |
| 54 | + it 'does not insert a row for the stopped process' do |
| 55 | + expect { run_migration }.not_to change { db[:app_usage_events].where(state: 'WAS_RUNNING').count }.from(0) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + context 'when there is a mix of STARTED and STOPPED processes' do |
| 60 | + let(:running_app) { VCAP::CloudController::AppModel.make } |
| 61 | + let(:stopped_app) { VCAP::CloudController::AppModel.make } |
| 62 | + let!(:running_process) { VCAP::CloudController::ProcessModelFactory.make(app: running_app, state: 'STARTED') } |
| 63 | + let!(:stopped_process) { VCAP::CloudController::ProcessModelFactory.make(app: stopped_app, state: 'STOPPED') } |
| 64 | + |
| 65 | + it 'inserts a WAS_RUNNING row only for the started process' do |
| 66 | + run_migration |
| 67 | + |
| 68 | + rows = db[:app_usage_events].where(state: 'WAS_RUNNING').all |
| 69 | + expect(rows.size).to eq(1) |
| 70 | + expect(rows.first[:app_guid]).to eq(running_process.guid) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'when there are pre-existing rows in app_usage_events' do |
| 75 | + let(:parent_app) { VCAP::CloudController::AppModel.make } |
| 76 | + let!(:process) { VCAP::CloudController::ProcessModelFactory.make(app: parent_app, state: 'STARTED') } |
| 77 | + let!(:existing_event) { VCAP::CloudController::AppUsageEvent.make(state: 'STARTED', app_guid: parent_app.guid) } |
| 78 | + |
| 79 | + it 'preserves the existing rows (no truncate)' do |
| 80 | + expect { run_migration }.to change(VCAP::CloudController::AppUsageEvent, :count).by(1) |
| 81 | + expect(VCAP::CloudController::AppUsageEvent.where(guid: existing_event.guid).first).to be_present |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when an app has a desired droplet in STAGED state' do |
| 86 | + let(:parent_app) { VCAP::CloudController::AppModel.make } |
| 87 | + let!(:process) { VCAP::CloudController::ProcessModelFactory.make(app: parent_app, state: 'STARTED') } |
| 88 | + |
| 89 | + it 'sets package_state to STAGED' do |
| 90 | + run_migration |
| 91 | + row = db[:app_usage_events].where(state: 'WAS_RUNNING').first |
| 92 | + expect(row[:package_state]).to eq('STAGED') |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context 'when multiple started processes exist' do |
| 97 | + let(:apps) { Array.new(3) { VCAP::CloudController::AppModel.make } } |
| 98 | + |
| 99 | + before do |
| 100 | + apps.each { |app| VCAP::CloudController::ProcessModelFactory.make(app: app, state: 'STARTED') } |
| 101 | + end |
| 102 | + |
| 103 | + it 'inserts one WAS_RUNNING row per running process' do |
| 104 | + expect { run_migration }.to change { db[:app_usage_events].where(state: 'WAS_RUNNING').count }.from(0).to(3) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + describe 'down migration' do |
| 110 | + let(:parent_app) { VCAP::CloudController::AppModel.make } |
| 111 | + let!(:process) { VCAP::CloudController::ProcessModelFactory.make(app: parent_app, state: 'STARTED') } |
| 112 | + let!(:unrelated_event) { VCAP::CloudController::AppUsageEvent.make(state: 'STARTED', app_guid: parent_app.guid) } |
| 113 | + |
| 114 | + before { run_migration } |
| 115 | + |
| 116 | + it 'removes only the WAS_RUNNING rows' do |
| 117 | + expect(db[:app_usage_events].where(state: 'WAS_RUNNING').count).to eq(1) |
| 118 | + |
| 119 | + revert_migration |
| 120 | + |
| 121 | + expect(db[:app_usage_events].where(state: 'WAS_RUNNING').count).to eq(0) |
| 122 | + expect(db[:app_usage_events].where(guid: unrelated_event.guid).count).to eq(1) |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments