Skip to content

Commit 1d6ff88

Browse files
c0d1ngm0nk3ynicolasbender
authored andcommitted
WIP: Fix scoped buildpack order
Co-authored-by: Nicolas Bender <[email protected]>
1 parent 9fd28cc commit 1d6ff88

File tree

4 files changed

+42
-46
lines changed

4 files changed

+42
-46
lines changed

app/models/runtime/buildpack.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
module VCAP::CloudController
22
class Buildpack < Sequel::Model
3-
plugin :list
4-
plugin :single_table_inheritance, :lifecycle,
5-
model_map: {
6-
VCAP::CloudController::Lifecycles::BUILDPACK => "VCAP::CloudController::ClassicBuildpack",
7-
VCAP::CloudController::Lifecycles::CNB => "VCAP::CloudController::CNBBuildpack"
8-
}
3+
plugin :list, scope: :lifecycle
94

105
export_attributes :name, :stack, :position, :enabled, :locked, :filename
116
import_attributes :name, :stack, :position, :enabled, :locked, :filename, :key
@@ -25,6 +20,22 @@ def self.user_visibility_filter(_user)
2520
full_dataset_filter
2621
end
2722

23+
def self.list_admin_buildpacks(stack_name=nil, lifecycle='buildpack')
24+
scoped = exclude(key: nil).exclude(key: '')
25+
scoped = scoped.filter(:lifecycle => lifecycle)
26+
if stack_name.present?
27+
scoped = scoped.filter(Sequel.or([
28+
[:stack, stack_name],
29+
[:stack, nil]
30+
]))
31+
end
32+
scoped.order(:position).all
33+
end
34+
35+
def self.at_last_position
36+
where(position: max(:position)).first
37+
end
38+
2839
def validate
2940
validates_unique %i[name stack]
3041
validates_format(/\A(\w|-)+\z/, :name, message: 'can only contain alphanumeric characters')

app/models/runtime/classic_buildpack.rb

Lines changed: 0 additions & 20 deletions
This file was deleted.

app/models/runtime/cnb_buildpack.rb

Lines changed: 0 additions & 20 deletions
This file was deleted.

spec/unit/models/runtime/buildpack_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def ordered_buildpacks
9191
let(:buildpack_file_1) { Tempfile.new('admin buildpack 1') }
9292
let(:buildpack_file_2) { Tempfile.new('admin buildpack 2') }
9393
let(:buildpack_file_3) { Tempfile.new('admin buildpack 3') }
94+
let(:buildpack_file_cnb) { Tempfile.new('admin buildpack cnb') }
9495

9596
let(:buildpack_blobstore) { CloudController::DependencyLocator.instance.buildpack_blobstore }
9697

@@ -103,13 +104,17 @@ def ordered_buildpacks
103104
Timecop.return
104105
end
105106

107+
subject(:cnb_buildpacks) { Buildpack.list_admin_buildpacks(nil, 'cnb') }
106108
subject(:all_buildpacks) { Buildpack.list_admin_buildpacks }
107109

108110
context 'with prioritized buildpacks' do
109111
before do
110112
buildpack_blobstore.cp_to_blobstore(buildpack_file_1.path, 'a key')
111113
Buildpack.make(key: 'a key', position: 2)
112114

115+
buildpack_blobstore.cp_to_blobstore(buildpack_file_cnb.path, 'cnb key')
116+
@cnb_buildpack = Buildpack.make(key: 'cnb key', position: 1, lifecycle: 'cnb')
117+
113118
buildpack_blobstore.cp_to_blobstore(buildpack_file_2.path, 'b key')
114119
Buildpack.make(key: 'b key', position: 1)
115120

@@ -131,13 +136,24 @@ def ordered_buildpacks
131136
expect(all_buildpacks).to have(2).items
132137
end
133138

139+
it 'returns the list of cnb buildpacks' do
140+
expect(cnb_buildpacks.collect(&:key)).to eq(['cnb key'])
141+
end
142+
134143
it 'randomly orders any buildpacks with the same position (for now we did not want to make clever logic of shifting stuff around: up to the user to get it all correct)' do
135144
@another_buildpack.position = 1
136145
@another_buildpack.save
137146

138147
expect(all_buildpacks[2].key).to eq('a key')
139148
end
140149

150+
it 'does not reorder cnb buildpacks when classical buildpacks change position)' do
151+
@cnb_buildpack.position = 1
152+
@cnb_buildpack.save
153+
154+
expect(cnb_buildpacks[0].key).to eq('cnb key')
155+
end
156+
141157
context 'and there are buildpacks with null keys' do
142158
let!(:null_buildpack) { Buildpack.create(name: 'nil_key_custom_buildpack', stack: Stack.make.name, position: 0) }
143159

@@ -175,6 +191,7 @@ def ordered_buildpacks
175191
end
176192

177193
context 'with a stack' do
194+
subject(:cnb_buildpacks) { Buildpack.list_admin_buildpacks('stack2', 'cnb') }
178195
subject(:all_buildpacks) { Buildpack.list_admin_buildpacks('stack1') }
179196
let!(:stack1) { Stack.make(name: 'stack1') }
180197
let!(:stack2) { Stack.make(name: 'stack2') }
@@ -186,6 +203,9 @@ def ordered_buildpacks
186203
buildpack_blobstore.cp_to_blobstore(buildpack_file_2.path, 'b key')
187204
Buildpack.make(key: 'b key', position: 1, stack: 'stack2')
188205

206+
buildpack_blobstore.cp_to_blobstore(buildpack_file_cnb.path, 'cnb key')
207+
Buildpack.make(key: 'cnb key', position: 1, stack: 'stack2', lifecycle: 'cnb')
208+
189209
buildpack_blobstore.cp_to_blobstore(buildpack_file_3.path, 'c key')
190210
@another_buildpack = Buildpack.make(key: 'c key', position: 3, stack: nil)
191211
end
@@ -195,6 +215,11 @@ def ordered_buildpacks
195215
it 'returns the list in position order, including buildpacks with null stack or matching stacks' do
196216
expect(all_buildpacks.collect(&:key)).to eq(['a key', 'c key'])
197217
end
218+
219+
it 'returns the list of cnb buildpacks' do
220+
expect(cnb_buildpacks.collect(&:key)).to eq(['cnb key'])
221+
end
222+
198223
end
199224
end
200225

0 commit comments

Comments
 (0)