Skip to content

Commit a863736

Browse files
committed
Stack Management - Model update and Unit Test
Signed-off-by: Rashed Kamal <[email protected]>
1 parent d69cdab commit a863736

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

app/models/runtime/stack.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ class MissingDefaultStackError < StandardError
1010
class AppsStillPresentError < StandardError
1111
end
1212

13+
STACK_ACTIVE = 'ACTIVE'.freeze
14+
STACK_RESTRICTED = 'RESTRICTED'.freeze
15+
STACK_DEPRECATED = 'DEPRECATED'.freeze
16+
STACK_DISABLED = 'DISABLED'.freeze
17+
18+
VALID_STATES = [
19+
STACK_ACTIVE,
20+
STACK_RESTRICTED,
21+
STACK_DEPRECATED,
22+
STACK_DISABLED
23+
].freeze
24+
1325
# NOTE: that "apps" here returns processes for v2 meta-reasons
1426
many_to_many :apps,
1527
class: 'VCAP::CloudController::ProcessModel',
@@ -43,6 +55,7 @@ def around_save
4355
def validate
4456
validates_presence :name
4557
validates_unique :name
58+
validates_includes VALID_STATES, :state, allow_nil: true
4659
end
4760

4861
def before_destroy
@@ -98,5 +111,28 @@ def self.populate_from_hash(hash)
98111
create(hash.slice('name', 'description', 'build_rootfs_image', 'run_rootfs_image'))
99112
end
100113
end
114+
def active?
115+
state == STACK_ACTIVE
116+
end
117+
118+
def deprecated?
119+
state == STACK_DEPRECATED
120+
end
121+
122+
def restricted?
123+
state == STACK_RESTRICTED
124+
end
125+
126+
def disabled?
127+
state == STACK_DISABLED
128+
end
129+
130+
def can_stage_new_app?
131+
!restricted? && !disabled?
132+
end
133+
134+
def can_restage_apps?
135+
!disabled?
136+
end
101137
end
102138
end

spec/unit/models/runtime/stack_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,34 @@ module VCAP::CloudController
2626
it { is_expected.to validate_presence :name }
2727
it { is_expected.to validate_uniqueness :name }
2828
it { is_expected.to strip_whitespace :name }
29+
30+
describe 'state validation' do
31+
it 'accepts valid states' do
32+
stack = Stack.make
33+
Stack::VALID_STATES.each do |valid_state|
34+
stack.state = valid_state
35+
expect(stack).to be_valid
36+
end
37+
end
38+
39+
it 'rejects invalid states' do
40+
stack = Stack.make
41+
stack.state = 'INVALID'
42+
expect(stack).not_to be_valid
43+
expect(stack.errors[:state]).to include(:includes)
44+
end
45+
46+
it 'allows nil state' do
47+
stack = Stack.make
48+
stack.state = nil
49+
expect(stack).to be_valid
50+
end
51+
end
2952
end
3053

3154
describe 'Serialization' do
3255
it { is_expected.to export_attributes :name, :description, :build_rootfs_image, :run_rootfs_image }
33-
it { is_expected.to import_attributes :name, :description, :build_rootfs_image, :run_rootfs_image }
56+
it { is_expected.to import_attributes :name, :description, :build_rootfs_image, :run_rootfs_image}
3457
end
3558

3659
describe '.configure' do

0 commit comments

Comments
 (0)