Skip to content

Commit 9e9b978

Browse files
modulo11pbusko
authored andcommitted
Fix tests
Co-authored-by: Pavel Busko <[email protected]>
1 parent 89d47cd commit 9e9b978

File tree

7 files changed

+51
-25
lines changed

7 files changed

+51
-25
lines changed

app/controllers/runtime/buildpacks_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ def self.dependencies
77
define_attributes do
88
attribute :name, String
99
attribute :stack, String, default: nil
10+
attribute :lifecycle, String, default: Lifecycles::BUILDPACK, exclude_in: :update
1011
attribute :position, Integer, default: 0
1112
attribute :enabled, Message::Boolean, default: true
1213
attribute :locked, Message::Boolean, default: false
1314
end
1415

15-
query_parameters :name, :stack
16+
query_parameters :name, :stack, :lifecycle
1617

1718
def self.translate_validation_exception(e, attributes)
1819
buildpack_errors = e.errors.on(%i[name stack])

lib/cloud_controller/install_buildpacks.rb

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ def install(buildpacks)
1212
CloudController::DependencyLocator.instance.buildpack_blobstore.ensure_bucket_exists
1313
job_factory = VCAP::CloudController::Jobs::Runtime::BuildpackInstallerFactory.new
1414

15-
buildpack_install_jobs = []
16-
1715
factory_options = []
1816
buildpacks.each do |bpack|
1917
buildpack_opts = bpack.deep_symbolize_keys
@@ -41,19 +39,10 @@ def install(buildpacks)
4139
logger.error "File not found: #{buildpack_file}, for the buildpack_opts: #{bpack}"
4240
next
4341
end
44-
45-
detected_stack = VCAP::CloudController::Buildpacks::StackNameExtractor.extract_from_file(buildpack_file) if buildpack_opts[:lifecycle] == Lifecycles::BUILDPACK
46-
detected_stack = buildpack_opts[:stack] if buildpack_opts[:lifecycle] == Lifecycles::CNB
47-
48-
factory_options << { name: buildpack_name, file: buildpack_file, options: buildpack_opts, stack: detected_stack }
42+
factory_options << { name: buildpack_name, file: buildpack_file, options: buildpack_opts, stack: detected_stack(buildpack_file, buildpack_opts) }
4943
end
5044

51-
buildpacks_by_lifecycle = factory_options.group_by { |options| options[:options][:lifecycle] }
52-
buildpacks_by_lifecycle.each_value do |options|
53-
options.group_by { |opts| opts[:name] }.each do |name, buildpack_options|
54-
buildpack_install_jobs << job_factory.plan(name, buildpack_options)
55-
end
56-
end
45+
buildpack_install_jobs = generate_install_jobs(factory_options, job_factory)
5746

5847
buildpack_install_jobs.flatten!
5948
run_canary(buildpack_install_jobs)
@@ -66,6 +55,23 @@ def logger
6655

6756
private
6857

58+
def generate_install_jobs(factory_options, job_factory)
59+
buildpack_install_jobs = []
60+
buildpacks_by_lifecycle = factory_options.group_by { |options| options[:options][:lifecycle] }
61+
buildpacks_by_lifecycle.each_value do |options|
62+
options.group_by { |opts| opts[:name] }.each do |name, buildpack_options|
63+
buildpack_install_jobs << job_factory.plan(name, buildpack_options)
64+
end
65+
end
66+
buildpack_install_jobs
67+
end
68+
69+
def detected_stack(file, opts)
70+
return opts[:stack] if opts[:lifecycle] == Lifecycles::CNB
71+
72+
VCAP::CloudController::Buildpacks::StackNameExtractor.extract_from_file(file)
73+
end
74+
6975
def buildpack_zip(package, zipfile)
7076
return zipfile if zipfile
7177

spec/unit/controllers/runtime/buildpacks_controller_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def ordered_buildpacks
2525
stack: { type: 'string' },
2626
position: { type: 'integer', default: 0 },
2727
enabled: { type: 'bool', default: true },
28-
locked: { type: 'bool', default: false }
28+
locked: { type: 'bool', default: false },
29+
lifecycle: { type: 'string', default: 'buildpack' }
2930
})
3031
end
3132

spec/unit/controllers/v3/buildpacks_controller_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,10 @@
512512
let(:buildpack_bits_name) { 'buildpack.zip' }
513513

514514
before do
515-
allow(File).to receive(:stat).and_return(stat_double)
516-
# allow(VCAP::CloudController::BuildpackUpload).to receive(:new).and_return(uploader)
515+
allow(File).to receive_messages(
516+
stat: stat_double,
517+
read: "PK\x03\x04".force_encoding('binary')
518+
)
517519
end
518520

519521
describe 'permissions' do

spec/unit/jobs/deserialization_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ module Jobs
178178
- :lifecycle
179179
extra_keys: []
180180
lifecycle:
181+
:type: buildpack
181182
:data:
182183
:buildpacks:
183184
- ruby

spec/unit/lib/cloud_controller/install_buildpacks_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ module VCAP::CloudController
4646
let(:buildpack2_file) { 'abuildpack2.zip' }
4747

4848
let(:buildpack1a_fields) do
49-
{ name: 'buildpack1', file: buildpack1a_file, stack: 'cflinuxfs11', options: {} }
49+
{ name: 'buildpack1', file: buildpack1a_file, stack: 'cflinuxfs11', options: { lifecycle: Lifecycles::BUILDPACK } }
5050
end
5151
let(:buildpack1b_fields) do
52-
{ name: 'buildpack1', file: buildpack1b_file, stack: 'cflinuxfs12', options: {} }
52+
{ name: 'buildpack1', file: buildpack1b_file, stack: 'cflinuxfs12', options: { lifecycle: Lifecycles::BUILDPACK } }
5353
end
5454
let(:buildpack2_fields) do
55-
{ name: 'buildpack2', file: buildpack2_file, stack: nil, options: {} }
55+
{ name: 'buildpack2', file: buildpack2_file, stack: nil, options: { lifecycle: Lifecycles::BUILDPACK } }
5656
end
5757

5858
before do
@@ -153,7 +153,7 @@ module VCAP::CloudController
153153
# call install
154154
# verify that job_factory.plan was called with the right file
155155
expect(File).to receive(:file?).with('another.zip').and_return(true)
156-
expect(job_factory).to receive(:plan).with('buildpack1', [{ name: 'buildpack1', file: 'another.zip', stack: nil, options: {} }])
156+
expect(job_factory).to receive(:plan).with('buildpack1', [{ name: 'buildpack1', file: 'another.zip', stack: nil, options: { lifecycle: Lifecycles::BUILDPACK } }])
157157

158158
installer.install(TestConfig.config_instance.get(:install_buildpacks))
159159
end
@@ -167,7 +167,7 @@ module VCAP::CloudController
167167
it 'succeeds when no package is specified' do
168168
TestConfig.config[:install_buildpacks][0].delete('package')
169169
expect(File).to receive(:file?).with('another.zip').and_return(true)
170-
expect(job_factory).to receive(:plan).with('buildpack1', [{ name: 'buildpack1', file: 'another.zip', stack: nil, options: {} }])
170+
expect(job_factory).to receive(:plan).with('buildpack1', [{ name: 'buildpack1', file: 'another.zip', stack: nil, options: { lifecycle: Lifecycles::BUILDPACK } }])
171171

172172
installer.install(TestConfig.config_instance.get(:install_buildpacks))
173173
end
@@ -225,6 +225,7 @@ module VCAP::CloudController
225225
stack: nil,
226226
options: {
227227
enabled: true,
228+
lifecycle: Lifecycles::BUILDPACK,
228229
locked: false,
229230
position: 5
230231
} }])

spec/unit/messages/buildpack_upload_message_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ module VCAP::CloudController
99
let(:stat_double) { instance_double(File::Stat, size: 2) }
1010

1111
before do
12-
allow(File).to receive(:stat).and_return(stat_double)
12+
allow(File).to receive_messages(
13+
stat: stat_double,
14+
read: "PK\x03\x04".force_encoding('binary')
15+
)
1316
end
1417

1518
context 'when the <ngnix_upload_module_dummy> param is set' do
@@ -93,12 +96,23 @@ module VCAP::CloudController
9396
end
9497

9598
context 'when the file is not a zip' do
96-
let(:opts) { { bits_path: '/tmp/bar', bits_name: 'buildpack.tgz' } }
99+
let(:opts) { { bits_path: '/tmp/bar', bits_name: 'buildpack.abcd' } }
97100

98101
it 'is not valid' do
102+
allow(File).to receive(:read).and_return("PX\x03\x04".force_encoding('binary'))
99103
upload_message = BuildpackUploadMessage.new(opts)
100104
expect(upload_message).not_to be_valid
101-
expect(upload_message.errors.full_messages[0]).to include('buildpack.tgz is not a zip')
105+
expect(upload_message.errors.full_messages[0]).to include('buildpack.abcd is not a zip or gzip')
106+
end
107+
end
108+
109+
context 'when the file is a tgz' do
110+
let(:opts) { { bits_path: '/tmp/bar', bits_name: 'buildpack.tgz' } }
111+
112+
it 'is not valid' do
113+
allow(File).to receive(:read).and_return("\x1F\x8B\x08".force_encoding('binary'))
114+
upload_message = BuildpackUploadMessage.new(opts)
115+
expect(upload_message).to be_valid
102116
end
103117
end
104118

0 commit comments

Comments
 (0)