Skip to content

Commit 133546b

Browse files
pbuskomodulo11
authored andcommitted
ensure the default lifecycle is set for buildpack model
Co-authored-by: Johannes Dillmann <[email protected]>
1 parent 9e9b978 commit 133546b

File tree

9 files changed

+43
-14
lines changed

9 files changed

+43
-14
lines changed

app/actions/buildpack_update.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def validation_error!(error, message)
2727
error!(%(Stack '#{message.stack}' does not exist)) if error.errors.on(:stack)&.include?(:buildpack_stack_does_not_exist)
2828
error!(%(Buildpack stack cannot be changed)) if error.errors.on(:stack)&.include?(:buildpack_cant_change_stacks)
2929
error!(%(Buildpack with name '#{error.model.name}' and an unassigned stack already exists)) if error.errors.on(:stack)&.include?(:unique)
30-
error!(%(Buildpack with name '#{error.model.name}' and stack '#{error.model.stack}' already exists)) if error.errors.on(%i[name stack])&.include?(:unique)
30+
31+
if error.errors.on(%i[name stack lifecycle])&.include?(:unique)
32+
error!(%(Buildpack with name '#{error.model.name}', stack '#{error.model.stack}' and lifecycle '#{error.model.lifecycle}' already exists))
33+
end
3134

3235
error!(error.message)
3336
end

app/jobs/runtime/buildpack_installer_factory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def plan(buildpack_name, manifest_fields)
3737

3838
planned_jobs = []
3939

40-
found_buildpacks = Buildpack.where(name: buildpack_name, lifecycle: manifest_fields[0][:lifecycle]).all
40+
found_buildpacks = Buildpack.where(name: buildpack_name, lifecycle: manifest_fields[0][:options][:lifecycle]).all
4141
ensure_no_attempt_to_upgrade_a_stackless_locked_buildpack(buildpack_name, found_buildpacks, manifest_fields)
4242

4343
manifest_fields.each do |buildpack_fields|

app/models/runtime/buildpack.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module VCAP::CloudController
44
class Buildpack < Sequel::Model
55
plugin :list, scope: :lifecycle
6+
plugin :after_initialize
67

78
export_attributes :name, :stack, :position, :enabled, :locked, :filename, :lifecycle
89
import_attributes :name, :stack, :position, :enabled, :locked, :filename, :lifecycle, :key
@@ -12,6 +13,10 @@ class Buildpack < Sequel::Model
1213
READY_STATE = 'READY'.freeze
1314
].map(&:freeze).freeze
1415

16+
def after_initialize
17+
self.lifecycle ||= Lifecycles::BUILDPACK
18+
end
19+
1520
one_to_many :labels, class: 'VCAP::CloudController::BuildpackLabelModel', key: :resource_guid, primary_key: :guid
1621
one_to_many :annotations, class: 'VCAP::CloudController::BuildpackAnnotationModel', key: :resource_guid, primary_key: :guid
1722

@@ -39,12 +44,13 @@ def self.at_last_position
3944
end
4045

4146
def validate
42-
validates_unique %i[name stack]
47+
validates_unique %i[name stack lifecycle]
4348
validates_format(/\A(\w|-)+\z/, :name, message: 'can only contain alphanumeric characters')
4449

4550
validate_stack_existence
4651
validate_stack_change
4752
validate_multiple_nil_stacks
53+
validate_lifecycle_change
4854
end
4955

5056
def locked?
@@ -87,6 +93,12 @@ def validate_stack_change
8793
errors.add(:stack, :buildpack_cant_change_stacks) if column_changes.key?(:stack)
8894
end
8995

96+
def validate_lifecycle_change
97+
return if initial_value(:lifecycle).nil?
98+
99+
errors.add(:lifecycle, :buildpack_cant_change_lifecycle) if column_changes.key?(:lifecycle)
100+
end
101+
90102
def validate_stack_existence
91103
return unless stack
92104

spec/unit/actions/app_apply_manifest_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ module VCAP::CloudController
119119
end
120120

121121
describe 'using cnb type' do
122+
let(:message) { AppManifestMessage.create_from_yml({ name: 'blah', buildpack: buildpack.name, lifecycle: 'cnb' }) }
122123
let(:app) { AppModel.make(:cnb) }
123124

124125
it 'calls AppUpdate with the correct arguments' do

spec/unit/actions/buildpack_update_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ module VCAP::CloudController
120120
end
121121
end
122122

123-
it 'raises a human-friendly error when name and stack conflict' do
123+
it 'raises a human-friendly error when name, stack and lifecycle conflict' do
124124
expect(buildpack1.stack).to eq buildpack2.stack
125125
message = BuildpackUpdateMessage.new(name: buildpack1.name)
126126

127127
expect do
128128
BuildpackUpdate.new.update(buildpack2, message)
129-
end.to raise_error(BuildpackUpdate::Error, "Buildpack with name '#{buildpack1.name}' and stack '#{buildpack1.stack}' already exists")
129+
end.to raise_error(BuildpackUpdate::Error, "Buildpack with name '#{buildpack1.name}', stack '#{buildpack1.stack}' and lifecycle '#{buildpack1.lifecycle}' already exists")
130130
end
131131

132132
it 're-raises when there is an unknown error' do

spec/unit/jobs/runtime/buildpack_installer_factory_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Jobs::Runtime
66
describe '#plan' do
77
let(:name) { 'the-buildpack' }
88
let(:file) { 'the-file' }
9-
let(:opts) { { enabled: true, locked: false, position: 1 } }
9+
let(:opts) { { enabled: true, locked: false, position: 1, lifecycle: 'buildpack' } }
1010
let(:factory) { BuildpackInstallerFactory.new }
1111
let(:jobs) { factory.plan(name, buildpack_fields) }
1212

spec/unit/lib/cloud_controller/diego/lifecycles/buildpack_lifecycle_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ module VCAP::CloudController
169169
it 'returns the expected value' do
170170
expect(buildpack_lifecycle.buildpack_infos).to eq(stubbed_data[:buildpack_infos])
171171

172-
expect(BuildpackLifecycleFetcher).to have_received(:fetch).with(%w[cool-buildpack rad-buildpack], Stack.default.name)
172+
expect(BuildpackLifecycleFetcher).to have_received(:fetch).with(%w[cool-buildpack rad-buildpack], Stack.default.name, 'buildpack')
173173
end
174174
end
175175

spec/unit/lib/cloud_controller/diego/lifecycles/cnb_lifecycle_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,18 @@ module VCAP::CloudController
171171
let(:stubbed_data) { { stack: app.lifecycle_data.stack, buildpack_infos: [instance_double(BuildpackInfo)] } }
172172
let(:request_data) do
173173
{
174-
buildpacks: %w[docker://cool-buildpack docker://rad-buildpack]
174+
buildpacks: %w[cool-buildpack docker://rad-buildpack]
175175
}
176176
end
177177

178+
before do
179+
allow(BuildpackLifecycleFetcher).to receive(:fetch).and_return(stubbed_data)
180+
end
181+
178182
it 'returns the expected value' do
179-
expect(cnb_lifecycle.buildpack_infos).to have(2).items
183+
expect(cnb_lifecycle.buildpack_infos).to eq(stubbed_data[:buildpack_infos])
180184

181-
expect(cnb_lifecycle.buildpack_infos[0].buildpack_url).to eq('docker://cool-buildpack')
182-
expect(cnb_lifecycle.buildpack_infos[1].buildpack_url).to eq('docker://rad-buildpack')
185+
expect(BuildpackLifecycleFetcher).to have_received(:fetch).with(%w[cool-buildpack docker://rad-buildpack], app.lifecycle_data.stack, 'cnb')
183186
end
184187
end
185188
end

spec/unit/models/runtime/buildpack_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def ordered_buildpacks
99
it { is_expected.to have_timestamp_columns }
1010

1111
describe 'Validations' do
12-
it { is_expected.to validate_uniqueness %i[name stack] }
12+
it { is_expected.to validate_uniqueness %i[name stack lifecycle] }
1313

1414
describe 'stack' do
1515
let(:stack) { Stack.make(name: 'happy') }
@@ -74,11 +74,21 @@ def ordered_buildpacks
7474
end
7575
end
7676
end
77+
78+
describe 'lifecycle' do
79+
it 'cannot be changed once it is set' do
80+
buildpack = Buildpack.create(name: 'test', lifecycle: 'cnb')
81+
buildpack.lifecycle = 'buildpack'
82+
83+
expect(buildpack).not_to be_valid
84+
expect(buildpack.errors.on(:lifecycle)).to include(:buildpack_cant_change_lifecycle)
85+
end
86+
end
7787
end
7888

7989
describe 'Serialization' do
80-
it { is_expected.to export_attributes :name, :stack, :position, :enabled, :locked, :filename }
81-
it { is_expected.to import_attributes :name, :stack, :position, :enabled, :locked, :filename, :key }
90+
it { is_expected.to export_attributes :name, :stack, :position, :enabled, :locked, :filename, :lifecycle }
91+
it { is_expected.to import_attributes :name, :stack, :position, :enabled, :locked, :filename, :lifecycle, :key }
8292

8393
it 'does not string mung(e)?' do
8494
expect(Buildpack.new(name: "my_custom_buildpack\r\n").to_json).to eq '"my_custom_buildpack\r\n"'

0 commit comments

Comments
 (0)