Skip to content

Commit 93ad9c6

Browse files
authored
Use raw sql for stack check (#4106)
* Use raw sql for stack check * Move config file class out of stack * Rename file * Rename config references * Use raw sql for stack check * Rubocop changes * Remove not needed checks * Use empty check * Use empty check
1 parent 86a7e70 commit 93ad9c6

File tree

6 files changed

+49
-74
lines changed

6 files changed

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

27-
raise "rake task 'stack_check' failed, stack '#{deprecated_stack}' not supported" if deprecated_stack_in_db
27+
raise "rake task 'stack_check' failed, stack '#{deprecated_stack}' not supported" unless no_deprecated_stack_in_db
2828
end
2929
end
3030
end

lib/tasks/stack_check.rake

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ namespace :stacks do
44
logger = Steno.logger('cc.stack')
55
db = VCAP::CloudController::DB.connect(RakeConfig.config.get(:db), logger)
66
next unless db.table_exists?(:stacks)
7-
next unless db.table_exists?(:buildpack_lifecycle_data)
87

9-
RakeConfig.config.load_db_encryption_key
10-
require 'models/runtime/buildpack_lifecycle_data_model'
11-
require 'models/runtime/stack'
8+
require 'models/helpers/stack_config_file'
129
require 'cloud_controller/check_stacks'
13-
VCAP::CloudController::CheckStacks.new(RakeConfig.config).validate_stacks
10+
VCAP::CloudController::CheckStacks.new(RakeConfig.config, db).validate_stacks
1411
end
1512
end

spec/tasks/stack_check_spec.rb

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,7 @@
5555
end
5656

5757
it 'validates stacks' do
58-
expect_any_instance_of(VCAP::CloudController::CheckStacks).to receive(:validate_stacks).and_call_original
59-
Rake::Task['stacks:stack_check'].execute
60-
end
61-
end
62-
end
63-
64-
context 'buildpack_lifecycle_data' do
65-
context 'when buildpack_lifecycle_data table doesnt exist' do
66-
before do
67-
allow(db_double).to receive(:table_exists?).with(:buildpack_lifecycle_data).and_return false
68-
allow(VCAP::CloudController::DB).to receive(:connect).and_return(db_double)
69-
end
70-
71-
it 'does nothing' do
72-
expect_any_instance_of(VCAP::CloudController::CheckStacks).not_to receive(:validate_stacks)
73-
Rake::Task['stacks:stack_check'].execute
74-
end
75-
end
76-
77-
context 'when buildpack_lifecycle_data table does exist' do
78-
before do
79-
allow(double).to receive(:table_exists?).with(:buildpack_lifecycle_data).and_return true
80-
allow(VCAP::CloudController::DB).to receive(:connect).and_return(db_double)
81-
end
82-
83-
it 'validates stacks' do
84-
expect_any_instance_of(VCAP::CloudController::CheckStacks).to receive(:validate_stacks).and_call_original
58+
expect_any_instance_of(VCAP::CloudController::CheckStacks).to receive(:validate_stacks)
8559
Rake::Task['stacks:stack_check'].execute
8660
end
8761
end

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)