|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +module VCAP::CloudController |
| 4 | + module Jobs::Runtime |
| 5 | + RSpec.describe LifecycleTypeBackfill, job_context: :worker do |
| 6 | + subject(:job) { LifecycleTypeBackfill.new } |
| 7 | + |
| 8 | + let(:db) { Sequel::Model.db } |
| 9 | + |
| 10 | + it { is_expected.to be_a_valid_job } |
| 11 | + |
| 12 | + it 'knows its job name' do |
| 13 | + expect(job.job_name_in_configuration).to eq(:lifecycle_type_backfill) |
| 14 | + end |
| 15 | + |
| 16 | + it 'has max_attempts of 1' do |
| 17 | + expect(job.max_attempts).to eq(1) |
| 18 | + end |
| 19 | + |
| 20 | + describe '#perform' do |
| 21 | + context 'when the lifecycle_type column is missing on every table' do |
| 22 | + let!(:app) { AppModel.make } |
| 23 | + let!(:droplet) { DropletModel.make } |
| 24 | + let!(:build) { BuildModel.make } |
| 25 | + |
| 26 | + before do |
| 27 | + db[:apps].where(guid: app.guid).update(lifecycle_type: nil) |
| 28 | + db[:droplets].where(guid: droplet.guid).update(lifecycle_type: nil) |
| 29 | + db[:builds].where(guid: build.guid).update(lifecycle_type: nil) |
| 30 | + allow(db).to receive(:schema).and_call_original |
| 31 | + %i[apps droplets builds].each do |table| |
| 32 | + allow(db).to receive(:schema).with(table, reload: true).and_return( |
| 33 | + [[:guid, {}], [:name, {}]] |
| 34 | + ) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + it 'does not issue any UPDATE statements' do |
| 39 | + expect { job.perform }.to have_queried_db_times(/update .(apps|droplets|builds). set/i, 0) |
| 40 | + end |
| 41 | + |
| 42 | + it 'leaves NULL rows untouched' do |
| 43 | + job.perform |
| 44 | + expect(db[:apps].where(guid: app.guid).get(:lifecycle_type)).to be_nil |
| 45 | + expect(db[:droplets].where(guid: droplet.guid).get(:lifecycle_type)).to be_nil |
| 46 | + expect(db[:builds].where(guid: build.guid).get(:lifecycle_type)).to be_nil |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context 'when no rows have NULL lifecycle_type on any table' do |
| 51 | + before do |
| 52 | + AppModel.make |
| 53 | + DropletModel.make |
| 54 | + BuildModel.make |
| 55 | + end |
| 56 | + |
| 57 | + it 'does not issue any UPDATE statements' do |
| 58 | + expect { job.perform }.to have_queried_db_times(/update .(apps|droplets|builds). set/i, 0) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context 'when there are apps with NULL lifecycle_type' do |
| 63 | + let(:buildpack_app) { AppModel.make } |
| 64 | + let(:cnb_app) { AppModel.make(:cnb) } |
| 65 | + let(:docker_app) { AppModel.make(:docker) } |
| 66 | + |
| 67 | + before do |
| 68 | + db[:apps].where(guid: [buildpack_app.guid, cnb_app.guid, docker_app.guid]).update(lifecycle_type: nil) |
| 69 | + end |
| 70 | + |
| 71 | + it 'sets lifecycle_type accordingly' do |
| 72 | + job.perform |
| 73 | + expect(db[:apps].where(guid: buildpack_app.guid).get(:lifecycle_type)).to eq(BuildpackLifecycleDataModel::LIFECYCLE_TYPE) |
| 74 | + expect(db[:apps].where(guid: cnb_app.guid).get(:lifecycle_type)).to eq(CNBLifecycleDataModel::LIFECYCLE_TYPE) |
| 75 | + expect(db[:apps].where(guid: docker_app.guid).get(:lifecycle_type)).to eq(DockerLifecycleDataModel::LIFECYCLE_TYPE) |
| 76 | + end |
| 77 | + |
| 78 | + it 'does not touch updated_at' do |
| 79 | + original_updated_at = db[:apps].where(guid: [buildpack_app.guid, cnb_app.guid, docker_app.guid]).select_map(%i[guid updated_at]).to_h |
| 80 | + job.perform |
| 81 | + expect(db[:apps].where(guid: [buildpack_app.guid, cnb_app.guid, docker_app.guid]).select_map(%i[guid updated_at]).to_h).to eq(original_updated_at) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when there are droplets with NULL lifecycle_type' do |
| 86 | + let(:buildpack_droplet) { DropletModel.make } |
| 87 | + let(:cnb_droplet) { DropletModel.make(:cnb) } |
| 88 | + let(:docker_droplet) { DropletModel.make(:docker) } |
| 89 | + |
| 90 | + before do |
| 91 | + db[:droplets].where(guid: [buildpack_droplet.guid, cnb_droplet.guid, docker_droplet.guid]).update(lifecycle_type: nil) |
| 92 | + end |
| 93 | + |
| 94 | + it 'sets lifecycle_type accordingly' do |
| 95 | + job.perform |
| 96 | + expect(db[:droplets].where(guid: buildpack_droplet.guid).get(:lifecycle_type)).to eq(BuildpackLifecycleDataModel::LIFECYCLE_TYPE) |
| 97 | + expect(db[:droplets].where(guid: cnb_droplet.guid).get(:lifecycle_type)).to eq(CNBLifecycleDataModel::LIFECYCLE_TYPE) |
| 98 | + expect(db[:droplets].where(guid: docker_droplet.guid).get(:lifecycle_type)).to eq(DockerLifecycleDataModel::LIFECYCLE_TYPE) |
| 99 | + end |
| 100 | + |
| 101 | + it 'does not touch updated_at' do |
| 102 | + original_updated_at = db[:droplets].where(guid: [buildpack_droplet.guid, cnb_droplet.guid, docker_droplet.guid]).select_map(%i[guid updated_at]).to_h |
| 103 | + job.perform |
| 104 | + expect(db[:droplets].where(guid: [buildpack_droplet.guid, cnb_droplet.guid, docker_droplet.guid]).select_map(%i[guid updated_at]).to_h).to eq(original_updated_at) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + context 'when there are builds with NULL lifecycle_type' do |
| 109 | + let(:buildpack_build) { BuildModel.make } |
| 110 | + let(:cnb_build) { BuildModel.make(:cnb) } |
| 111 | + let(:docker_build) { BuildModel.make(:docker) } |
| 112 | + |
| 113 | + before do |
| 114 | + db[:builds].where(guid: [buildpack_build.guid, cnb_build.guid, docker_build.guid]).update(lifecycle_type: nil) |
| 115 | + end |
| 116 | + |
| 117 | + it 'sets lifecycle_type accordingly' do |
| 118 | + job.perform |
| 119 | + expect(db[:builds].where(guid: buildpack_build.guid).get(:lifecycle_type)).to eq(BuildpackLifecycleDataModel::LIFECYCLE_TYPE) |
| 120 | + expect(db[:builds].where(guid: cnb_build.guid).get(:lifecycle_type)).to eq(CNBLifecycleDataModel::LIFECYCLE_TYPE) |
| 121 | + expect(db[:builds].where(guid: docker_build.guid).get(:lifecycle_type)).to eq(DockerLifecycleDataModel::LIFECYCLE_TYPE) |
| 122 | + end |
| 123 | + |
| 124 | + it 'does not touch updated_at' do |
| 125 | + original_updated_at = db[:builds].where(guid: [buildpack_build.guid, cnb_build.guid, docker_build.guid]).select_map(%i[guid updated_at]).to_h |
| 126 | + job.perform |
| 127 | + expect(db[:builds].where(guid: [buildpack_build.guid, cnb_build.guid, docker_build.guid]).select_map(%i[guid updated_at]).to_h).to eq(original_updated_at) |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + context 'with more rows than batch_size * batches_per_run' do |
| 132 | + subject(:job) { LifecycleTypeBackfill.new(batch_size: 2, batches_per_run: 2) } |
| 133 | + |
| 134 | + before do |
| 135 | + 5.times { AppModel.make } |
| 136 | + db[:apps].update(lifecycle_type: nil) |
| 137 | + end |
| 138 | + |
| 139 | + it 'updates at most batch_size * batches_per_run rows in a single perform' do |
| 140 | + expect { job.perform }.to change { db[:apps].where(lifecycle_type: nil).count }.from(5).to(1) |
| 141 | + end |
| 142 | + |
| 143 | + it 'processes the remainder on the next perform' do |
| 144 | + job.perform |
| 145 | + job.perform |
| 146 | + expect(db[:apps].where(lifecycle_type: nil).count).to eq(0) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + context 'with fewer rows than batch_size' do |
| 151 | + subject(:job) { LifecycleTypeBackfill.new(batch_size: 2, batches_per_run: 2) } |
| 152 | + |
| 153 | + before do |
| 154 | + AppModel.make |
| 155 | + db[:apps].update(lifecycle_type: nil) |
| 156 | + end |
| 157 | + |
| 158 | + it 'updates every NULL row in a single perform' do |
| 159 | + job.perform |
| 160 | + expect(db[:apps].where(lifecycle_type: nil).count).to eq(0) |
| 161 | + end |
| 162 | + |
| 163 | + it 'issues exactly one SELECT for guids, subsequent batch is skipped' do |
| 164 | + expect { job.perform }.to have_queried_db_times(/select .guid. from .apps. where \(.lifecycle_type. is null\)/i, 1) |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + context 'when batches_per_run is -1 (drain mode)' do |
| 169 | + subject(:job) { LifecycleTypeBackfill.new(batch_size: 2, batches_per_run: -1) } |
| 170 | + |
| 171 | + before do |
| 172 | + 5.times { AppModel.make } |
| 173 | + db[:apps].update(lifecycle_type: nil) |
| 174 | + end |
| 175 | + |
| 176 | + it 'keeps batching until no NULL rows remain' do |
| 177 | + job.perform |
| 178 | + expect(db[:apps].where(lifecycle_type: nil).count).to eq(0) |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 182 | + end |
| 183 | + end |
| 184 | +end |
0 commit comments