Skip to content

Commit b6cd622

Browse files
committed
Use raw sql for stack check
1 parent f3834d2 commit b6cd622

File tree

6 files changed

+52
-46
lines changed

6 files changed

+52
-46
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
module VCAP::CloudController
3+
class StackConfigFile
4+
def initialize(file_path)
5+
@hash = YAMLConfig.safe_load_file(file_path).tap do |h|
6+
Schema.validate(h)
7+
end
8+
end
9+
10+
def stacks
11+
@hash['stacks']
12+
end
13+
14+
def deprecated_stacks
15+
@hash['deprecated_stacks']
16+
end
17+
18+
def default
19+
@hash['default']
20+
end
21+
22+
Schema = Membrane::SchemaParser.parse do
23+
{
24+
'default' => String,
25+
'stacks' => [{
26+
'name' => String,
27+
'description' => String,
28+
optional('build_rootfs_image') => String,
29+
optional('run_rootfs_image') => String
30+
}],
31+
optional('deprecated_stacks') => [
32+
String
33+
]
34+
}
35+
end
36+
end
37+
end

app/models/runtime/stack.rb

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'models/helpers/process_types'
2+
require 'models/helpers/stack_config_file'
23

34
module VCAP::CloudController
45
class Stack < Sequel::Model
@@ -65,7 +66,7 @@ def run_rootfs_image
6566
end
6667

6768
def self.configure(file_path)
68-
@config_file = (ConfigFile.new(file_path) if file_path)
69+
@config_file = (StackConfigFile.new(file_path) if file_path)
6970
end
7071

7172
def self.populate
@@ -97,40 +98,5 @@ def self.populate_from_hash(hash)
9798
create(hash.slice('name', 'description', 'build_rootfs_image', 'run_rootfs_image'))
9899
end
99100
end
100-
101-
class ConfigFile
102-
def initialize(file_path)
103-
@hash = YAMLConfig.safe_load_file(file_path).tap do |h|
104-
Schema.validate(h)
105-
end
106-
end
107-
108-
def stacks
109-
@hash['stacks']
110-
end
111-
112-
def deprecated_stacks
113-
@hash['deprecated_stacks']
114-
end
115-
116-
def default
117-
@hash['default']
118-
end
119-
120-
Schema = Membrane::SchemaParser.parse do
121-
{
122-
'default' => String,
123-
'stacks' => [{
124-
'name' => String,
125-
'description' => String,
126-
optional('build_rootfs_image') => String,
127-
optional('run_rootfs_image') => String
128-
}],
129-
optional('deprecated_stacks') => [
130-
String
131-
]
132-
}
133-
end
134-
end
135101
end
136102
end

lib/cloud_controller/check_stacks.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ module VCAP::CloudController
22
class CheckStacks
33
attr_reader :config
44

5-
def initialize(config)
5+
def initialize(config, db)
66
@config = config
7-
@stack_config = VCAP::CloudController::Stack::ConfigFile.new(config.get(:stacks_file))
7+
@stack_config = VCAP::CloudController::StackConfigFile.new(config.get(:stacks_file))
8+
@db = db
89
end
910

1011
def validate_stacks
@@ -19,11 +20,12 @@ def validate_stacks
1920
def validate_stack(deprecated_stack)
2021
configured_stacks = @stack_config.stacks
2122
deprecated_stack_in_config = (configured_stacks.find { |stack| stack['name'] == deprecated_stack }).present?
22-
2323
return if deprecated_stack_in_config
2424

25-
deprecated_stack_in_db = VCAP::CloudController::Stack.first(name: deprecated_stack).present?
26-
25+
deprecated_stack_in_db = false
26+
@db.fetch('SELECT * FROM stacks WHERE name LIKE ? ', deprecated_stack) do |_row|
27+
deprecated_stack_in_db = true
28+
end
2729
raise "rake task 'stack_check' failed, stack '#{deprecated_stack}' not supported" if deprecated_stack_in_db
2830
end
2931
end

lib/tasks/stack_check.rake

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ namespace :stacks do
77
next unless db.table_exists?(:buildpack_lifecycle_data)
88

99
RakeConfig.config.load_db_encryption_key
10-
require 'models/runtime/buildpack_lifecycle_data_model'
11-
require 'models/runtime/stack'
10+
require 'models/helpers/stack_config_file'
1211
require 'cloud_controller/check_stacks'
13-
VCAP::CloudController::CheckStacks.new(RakeConfig.config).validate_stacks
12+
VCAP::CloudController::CheckStacks.new(RakeConfig.config, db).validate_stacks
1413
end
15-
end
14+
end

spec/tasks/stack_check_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
before do
5353
allow(db_double).to receive(:table_exists?).with(:stacks).and_return true
5454
allow(VCAP::CloudController::DB).to receive(:connect).and_return(db_double)
55+
allow(db_double).to receive(:fetch).with('SELECT * FROM stacks WHERE name LIKE ? ', 'cflinuxfs3').and_return('1')
5556
end
5657

5758
it 'validates stacks' do
@@ -78,6 +79,7 @@
7879
before do
7980
allow(double).to receive(:table_exists?).with(:buildpack_lifecycle_data).and_return true
8081
allow(VCAP::CloudController::DB).to receive(:connect).and_return(db_double)
82+
allow(db_double).to receive(:fetch).with('SELECT * FROM stacks WHERE name LIKE ? ', 'cflinuxfs3').and_return('1')
8183
end
8284

8385
it 'validates stacks' do

spec/unit/lib/cloud_controller/check_stacks_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module VCAP::CloudController
2828
TestConfig.override(stacks_file: file.path)
2929
end
3030

31-
let(:stack_checker) { CheckStacks.new(TestConfig.config_instance) }
31+
let(:stack_checker) { CheckStacks.new(TestConfig.config_instance, DbConfig.new.connection) }
3232

3333
describe 'the deprecated stacks is nil' do
3434
let(:stack_file_contents) do

0 commit comments

Comments
 (0)