|
108 | 108 | expect(VCAP::CloudController::AppUsageEvent.count).to eq(0) |
109 | 109 | end |
110 | 110 |
|
| 111 | + it 'keeps AppUsageEvent WAS_RUNNING record when there is no corresponding stop record' do |
| 112 | + stale_was_running = VCAP::CloudController::AppUsageEvent.make(created_at: 2.days.ago, state: 'WAS_RUNNING', app_guid: 'guid1') |
| 113 | + |
| 114 | + record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::AppUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: false, keep_running_records: true) |
| 115 | + record_cleanup.delete |
| 116 | + |
| 117 | + expect(stale_was_running.reload).to be_present |
| 118 | + expect(VCAP::CloudController::AppUsageEvent.count).to eq(1) |
| 119 | + end |
| 120 | + |
| 121 | + it 'deletes AppUsageEvent WAS_RUNNING record when there is a corresponding stop record' do |
| 122 | + app_guid = 'stopped-app' |
| 123 | + stale_was_running = VCAP::CloudController::AppUsageEvent.make(created_at: 5.days.ago, state: 'WAS_RUNNING', app_guid: app_guid) |
| 124 | + stale_stop = VCAP::CloudController::AppUsageEvent.make(created_at: 4.days.ago, state: 'STOPPED', app_guid: app_guid) |
| 125 | + |
| 126 | + record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::AppUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: false, keep_running_records: true) |
| 127 | + record_cleanup.delete |
| 128 | + |
| 129 | + expect { stale_was_running.reload }.to raise_error(Sequel::NoExistingObject) |
| 130 | + expect { stale_stop.reload }.to raise_error(Sequel::NoExistingObject) |
| 131 | + end |
| 132 | + |
| 133 | + it 'keeps both STARTED and WAS_RUNNING records for the same app while it is running' do |
| 134 | + app_guid = 'running-app' |
| 135 | + stale_started = VCAP::CloudController::AppUsageEvent.make(created_at: 10.days.ago, state: 'STARTED', app_guid: app_guid) |
| 136 | + stale_was_running = VCAP::CloudController::AppUsageEvent.make(created_at: 5.days.ago, state: 'WAS_RUNNING', app_guid: app_guid) |
| 137 | + |
| 138 | + record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::AppUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: false, keep_running_records: true) |
| 139 | + record_cleanup.delete |
| 140 | + |
| 141 | + expect(stale_started.reload).to be_present |
| 142 | + expect(stale_was_running.reload).to be_present |
| 143 | + end |
| 144 | + |
| 145 | + it 'deletes both STARTED and WAS_RUNNING records when a STOPPED record exists' do |
| 146 | + app_guid = 'restarted-app' |
| 147 | + stale_started = VCAP::CloudController::AppUsageEvent.make(created_at: 10.days.ago, state: 'STARTED', app_guid: app_guid) |
| 148 | + stale_was_running = VCAP::CloudController::AppUsageEvent.make(created_at: 8.days.ago, state: 'WAS_RUNNING', app_guid: app_guid) |
| 149 | + stale_stop = VCAP::CloudController::AppUsageEvent.make(created_at: 5.days.ago, state: 'STOPPED', app_guid: app_guid) |
| 150 | + |
| 151 | + record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::AppUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: false, keep_running_records: true) |
| 152 | + record_cleanup.delete |
| 153 | + |
| 154 | + expect { stale_started.reload }.to raise_error(Sequel::NoExistingObject) |
| 155 | + expect { stale_was_running.reload }.to raise_error(Sequel::NoExistingObject) |
| 156 | + expect { stale_stop.reload }.to raise_error(Sequel::NoExistingObject) |
| 157 | + end |
| 158 | + |
111 | 159 | # ==================== KEEP_RUNNING_RECORDS: ServiceUsageEvent ==================== |
112 | 160 |
|
113 | 161 | it 'keeps ServiceUsageEvent create record when there is no corresponding delete record' do |
|
177 | 225 | expect { orphan_delete.reload }.to raise_error(Sequel::NoExistingObject) |
178 | 226 | end |
179 | 227 |
|
| 228 | + it 'keeps ServiceUsageEvent WAS_RUNNING record when there is no corresponding delete record' do |
| 229 | + stale_was_running = VCAP::CloudController::ServiceUsageEvent.make(created_at: 2.days.ago, state: 'WAS_RUNNING', service_instance_guid: 'guid1') |
| 230 | + |
| 231 | + record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::ServiceUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: false, keep_running_records: true) |
| 232 | + record_cleanup.delete |
| 233 | + |
| 234 | + expect(stale_was_running.reload).to be_present |
| 235 | + expect(VCAP::CloudController::ServiceUsageEvent.count).to eq(1) |
| 236 | + end |
| 237 | + |
| 238 | + it 'deletes ServiceUsageEvent WAS_RUNNING record when there is a corresponding delete record' do |
| 239 | + service_guid = 'deleted-instance' |
| 240 | + stale_was_running = VCAP::CloudController::ServiceUsageEvent.make(created_at: 5.days.ago, state: 'WAS_RUNNING', service_instance_guid: service_guid) |
| 241 | + stale_delete = VCAP::CloudController::ServiceUsageEvent.make(created_at: 4.days.ago, state: 'DELETED', service_instance_guid: service_guid) |
| 242 | + |
| 243 | + record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::ServiceUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: false, keep_running_records: true) |
| 244 | + record_cleanup.delete |
| 245 | + |
| 246 | + expect { stale_was_running.reload }.to raise_error(Sequel::NoExistingObject) |
| 247 | + expect { stale_delete.reload }.to raise_error(Sequel::NoExistingObject) |
| 248 | + end |
| 249 | + |
| 250 | + it 'keeps both CREATED and WAS_RUNNING records for the same service instance while it exists' do |
| 251 | + service_guid = 'running-service' |
| 252 | + stale_created = VCAP::CloudController::ServiceUsageEvent.make(created_at: 10.days.ago, state: 'CREATED', service_instance_guid: service_guid) |
| 253 | + stale_was_running = VCAP::CloudController::ServiceUsageEvent.make(created_at: 5.days.ago, state: 'WAS_RUNNING', service_instance_guid: service_guid) |
| 254 | + |
| 255 | + record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::ServiceUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: false, keep_running_records: true) |
| 256 | + record_cleanup.delete |
| 257 | + |
| 258 | + expect(stale_created.reload).to be_present |
| 259 | + expect(stale_was_running.reload).to be_present |
| 260 | + end |
| 261 | + |
180 | 262 | # ==================== EDGE CASES & DATA INTEGRITY ==================== |
181 | 263 |
|
182 | 264 | it 'deletes records with non-lifecycle states when keep_running_records is true' do |
|
0 commit comments