Skip to content

Commit ec15ddc

Browse files
pbuskomodulo11
authored andcommitted
Add missing tests
Co-authored-by: Johannes Dillmann <[email protected]>
1 parent d815656 commit ec15ddc

File tree

14 files changed

+140
-26
lines changed

14 files changed

+140
-26
lines changed

app/fetchers/buildpack_lifecycle_fetcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module VCAP::CloudController
44
class BuildpackLifecycleFetcher
55
class << self
6-
def fetch(buildpack_names, stack_name, lifecycle = VCAP::CloudController::Lifecycles::BUILDPACK)
6+
def fetch(buildpack_names, stack_name, lifecycle=VCAP::CloudController::Lifecycles::BUILDPACK)
77
{
88
stack: Stack.find(name: stack_name),
99
buildpack_infos: ordered_buildpacks(buildpack_names, stack_name, lifecycle)

app/messages/buildpack_upload_message.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'messages/base_message'
22

33
module VCAP::CloudController
4-
GZIP_MIME = Regexp.new("\x1F\x8B\x08".force_encoding("binary"))
5-
ZIP_MIME = Regexp.new("PK\x03\x04".force_encoding("binary"))
4+
GZIP_MIME = Regexp.new("\x1F\x8B\x08".force_encoding('binary'))
5+
ZIP_MIME = Regexp.new("PK\x03\x04".force_encoding('binary'))
66

77
class BuildpackUploadMessage < BaseMessage
88
class MissingFilePathError < StandardError; end
@@ -49,12 +49,11 @@ def is_archive
4949
return unless bits_name
5050
return unless bits_path
5151

52-
case IO.read(bits_path, 4)
53-
when /^#{VCAP::CloudController::GZIP_MIME}/, /^#{VCAP::CloudController::ZIP_MIME}/
54-
else
55-
errors.add(:base, "#{bits_name} is not a zip or gzip archive")
56-
end
52+
mime_bits = File.read(bits_path, 4)
5753

54+
return if mime_bits =~ /^#{VCAP::CloudController::GZIP_MIME}/ || mime_bits =~ /^#{VCAP::CloudController::ZIP_MIME}/
55+
56+
errors.add(:base, "#{bits_name} is not a zip or gzip archive")
5857
end
5958

6059
def missing_file_path

app/models/runtime/buildpack.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module VCAP::CloudController
44
class Buildpack < Sequel::Model
55
plugin :list, scope: :lifecycle
66

7-
export_attributes :name, :stack, :position, :enabled, :locked, :filename
8-
import_attributes :name, :stack, :position, :enabled, :locked, :filename, :key
7+
export_attributes :name, :stack, :position, :enabled, :locked, :filename, :lifecycle
8+
import_attributes :name, :stack, :position, :enabled, :locked, :filename, :lifecycle, :key
99

1010
PACKAGE_STATES = [
1111
CREATED_STATE = 'AWAITING_UPLOAD'.freeze,
@@ -24,7 +24,7 @@ def self.user_visibility_filter(_user)
2424

2525
def self.list_admin_buildpacks(stack_name=nil, lifecycle=VCAP::CloudController::Lifecycles::BUILDPACK)
2626
scoped = exclude(key: nil).exclude(key: '')
27-
scoped = scoped.filter(:lifecycle => lifecycle)
27+
scoped = scoped.filter(lifecycle:)
2828
if stack_name.present?
2929
scoped = scoped.filter(Sequel.or([
3030
[:stack, stack_name],

app/models/runtime/cnb_lifecycle_data_model.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def credentials=(creds)
9696
private
9797

9898
def attributes_from_buildpack_name(buildpack_name)
99-
if UriUtils.is_buildpack_uri?(buildpack_name)
99+
if UriUtils.is_cnb_buildpack_uri?(buildpack_name)
100100
{ buildpack_url: buildpack_name, admin_buildpack_name: nil }
101101
else
102102
{ buildpack_url: nil, admin_buildpack_name: buildpack_name }
@@ -107,7 +107,7 @@ def attributes_from_buildpack_key(key)
107107
admin_buildpack = Buildpack.find(key:)
108108
if admin_buildpack
109109
{ buildpack_url: nil, admin_buildpack_name: admin_buildpack.name }
110-
elsif UriUtils.is_buildpack_uri?(key)
110+
elsif UriUtils.is_cnb_buildpack_uri?(key)
111111
{ buildpack_url: key, admin_buildpack_name: nil }
112112
else
113113
{} # Will fail a validity check downstream

lib/cloud_controller/diego/lifecycles/buildpack_info.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class BuildpackInfo
77
def initialize(buildpack_name_or_url, buildpack_record)
88
@buildpack = buildpack_name_or_url
99
@buildpack_record = buildpack_record
10-
@buildpack_url = buildpack_name_or_url if UriUtils.is_buildpack_uri?(buildpack_name_or_url)
10+
@buildpack_url = buildpack_name_or_url if UriUtils.is_buildpack_uri?(buildpack_name_or_url) || UriUtils.is_cnb_buildpack_uri?(buildpack_name_or_url)
1111
end
1212

1313
def buildpack_exists_in_db?

spec/support/test_tgz.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'zlib'
2+
require 'rubygems/package'
3+
4+
module TestTgz
5+
def self.create(tgz_name, file_count, file_size=1024, &)
6+
File.open(tgz_name, 'wb') do |file|
7+
Zlib::GzipWriter.wrap(file) do |gzip|
8+
Gem::Package::TarWriter.new(gzip) do |tar|
9+
file_count.times do |i|
10+
tar.add_file_simple("ziptest_#{i}", 0o644, file_size) do |f|
11+
f.write('A' * file_size)
12+
end
13+
end
14+
end
15+
end
16+
end
17+
end
18+
end

spec/unit/actions/buildpack_create_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module VCAP::CloudController
1919
name: 'the-name',
2020
stack: 'the-stack',
2121
enabled: false,
22-
locked: true
22+
locked: true,
23+
lifecycle: Lifecycles::BUILDPACK
2324
)
2425
buildpack = BuildpackCreate.new.create(message)
2526

@@ -28,6 +29,7 @@ module VCAP::CloudController
2829
expect(buildpack.position).to eq(1)
2930
expect(buildpack.enabled).to be(false)
3031
expect(buildpack.locked).to be(true)
32+
expect(buildpack.lifecycle).to eq(Lifecycles::BUILDPACK)
3133
end
3234
end
3335

@@ -114,6 +116,19 @@ module VCAP::CloudController
114116
end
115117
end
116118

119+
context 'when lifecycle is provided' do
120+
it 'creates a buildpack with locked set to true' do
121+
message = BuildpackCreateMessage.new(
122+
name: 'the-name',
123+
stack: 'the-stack',
124+
lifecycle: Lifecycles::CNB
125+
)
126+
buildpack = BuildpackCreate.new.create(message)
127+
128+
expect(buildpack.lifecycle).to eq(Lifecycles::CNB)
129+
end
130+
end
131+
117132
context 'when a model validation fails' do
118133
it 'raises an error' do
119134
errors = Sequel::Model::Errors.new

spec/unit/lib/cloud_controller/diego/buildpack_entry_generator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
module VCAP::CloudController
55
module Diego
66
RSpec.describe BuildpackEntryGenerator do
7-
subject(:buildpack_entry_generator) { BuildpackEntryGenerator.new(blobstore_url_generator) }
7+
subject(:buildpack_entry_generator) { BuildpackEntryGenerator.new(blobstore_url_generator, Lifecycles::BUILDPACK) }
88

99
let(:admin_buildpack_download_url) { 'http://admin-buildpack.example.com' }
1010
let(:app_package_download_url) { 'http://app-package.example.com' }

spec/unit/lib/cloud_controller/diego/cnb/staging_action_builder_spec.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ module CNB
131131

132132
let(:buildpacks) do
133133
[
134-
{ url: 'gcr.io/paketo-buildpacks/node-start', skip_detect: false },
135-
{ url: 'gcr.io/paketo-buildpacks/node-engine', skip_detect: false }
134+
{ name: 'custom', url: 'gcr.io/paketo-buildpacks/node-start', skip_detect: false },
135+
{ name: 'custom', url: 'gcr.io/paketo-buildpacks/node-engine', skip_detect: false }
136136
]
137137
end
138138

@@ -240,6 +240,52 @@ module CNB
240240
end
241241
end
242242
end
243+
244+
context('when system-buildpaks are used') do
245+
let(:buildpacks) do
246+
[
247+
{ name: 'node-cnb', key: 'node-key', skip_detect: false },
248+
{ name: 'java-cnb', key: 'java-key', skip_detect: false }
249+
]
250+
end
251+
252+
let(:run_staging_action) do
253+
::Diego::Bbs::Models::RunAction.new(
254+
path: '/tmp/lifecycle/builder',
255+
user: 'vcap',
256+
args: ['--cache-dir', '/tmp/cache', '--cache-output', '/tmp/cache-output.tgz', '--system-buildpack', 'node-key', '--system-buildpack',
257+
'java-key', '--pass-env-var', 'FOO', '--pass-env-var', 'BAR'],
258+
env: bbs_env
259+
)
260+
end
261+
262+
it 'returns the buildpack staging action without download actions' do
263+
result = builder.action
264+
265+
serial_action = result.serial_action
266+
actions = serial_action.actions
267+
268+
expect(actions[1].run_action).to eq(run_staging_action)
269+
expect(builder.cached_dependencies).to include(::Diego::Bbs::Models::CachedDependency.new(
270+
name: 'node-cnb',
271+
from: '',
272+
to: '/tmp/buildpacks/54548f35489d6234',
273+
cache_key: 'node-cnb',
274+
log_source: '',
275+
checksum_algorithm: '',
276+
checksum_value: ''
277+
))
278+
expect(builder.cached_dependencies).to include(::Diego::Bbs::Models::CachedDependency.new(
279+
name: 'java-cnb',
280+
from: '',
281+
to: '/tmp/buildpacks/c56b3bfdba7aa4dd',
282+
cache_key: 'java-cnb',
283+
log_source: '',
284+
checksum_algorithm: '',
285+
checksum_value: ''
286+
))
287+
end
288+
end
243289
end
244290

245291
describe '#cached_dependencies' do

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module VCAP::CloudController
4343
let(:lifecycle_request_data) { { buildpacks: ['docker://nodejs', 'http://buildpack.com', 'http://other.com'] } }
4444

4545
before do
46-
Buildpack.make(name: 'custom-bp')
46+
Buildpack.make(name: 'custom-bp', lifecycle: 'cnb')
4747
end
4848

4949
it 'uses all of the buildpacks' do
@@ -96,8 +96,8 @@ module VCAP::CloudController
9696
end
9797

9898
describe '#validation' do
99-
context 'with no buildpacks' do
100-
let(:lifecycle_request_data) { {} }
99+
context 'with unknown admin buildpack' do
100+
let(:lifecycle_request_data) { { buildpacks: %w[foo] } }
101101

102102
it 'invalid' do
103103
expect(lifecycle.valid?).to be(false)
@@ -113,7 +113,12 @@ module VCAP::CloudController
113113
end
114114

115115
context 'with buildpacks' do
116-
let(:lifecycle_request_data) { { buildpacks: %w[foo bar] } }
116+
before do
117+
Buildpack.make(name: 'foo', lifecycle: 'cnb')
118+
Buildpack.make(name: 'bar', lifecycle: 'cnb')
119+
end
120+
121+
let(:lifecycle_request_data) { { buildpacks: ['foo', 'bar', 'docker://nodejs:latest'] } }
117122

118123
it 'valid' do
119124
expect(lifecycle.valid?).to be(true)

0 commit comments

Comments
 (0)