Skip to content

Commit 11686ae

Browse files
committed
Fix remaining test failure, add detailed manifest route options error messages
1 parent 2d147b8 commit 11686ae

File tree

5 files changed

+40
-57
lines changed

5 files changed

+40
-57
lines changed

app/actions/manifest_route_update.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,15 @@ def find_or_create_valid_route(app, manifest_route, user_audit_info)
9292
domain: existing_domain,
9393
manifest_triggered: true
9494
)
95+
elsif route.space.guid != app.space_guid
96+
raise InvalidRoute.new('Routes cannot be mapped to destinations in different spaces')
9597
elsif manifest_route[:options] && route[:options] != manifest_route[:options]
9698
# remove nil values from options
9799
manifest_route[:options] = manifest_route[:options].compact
98100
message = RouteUpdateMessage.new({
99101
'options' => manifest_route[:options]
100102
})
101103
route = RouteUpdate.new.update(route:, message:)
102-
103-
elsif route.space.guid != app.space_guid
104-
raise InvalidRoute.new('Routes cannot be mapped to destinations in different spaces')
105104
end
106105

107106
return route

app/messages/manifest_routes_update_message.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ def validate(record)
1313
return
1414
end
1515

16-
if contains_invalid_route_options?(record.routes)
17-
record.errors.add(:routes, message: 'contains invalid route options')
18-
return
19-
end
20-
21-
return unless contains_invalid_lb_algo?(record.routes)
22-
23-
record.errors.add(:routes, message: 'contains an invalid loadbalancing-algorithm option')
16+
contains_invalid_route_options?(record)
17+
contains_invalid_lb_algo?(record)
2418
nil
2519
end
2620

@@ -32,23 +26,34 @@ def contains_non_route_hash_values?(routes)
3226
routes.any? { |r| !(r.is_a?(Hash) && r[:route].present?) }
3327
end
3428

35-
def contains_invalid_route_options?(routes)
29+
def contains_invalid_route_options?(record)
30+
routes = record.routes
3631
routes.any? do |r|
3732
next unless r[:options]
3833

3934
return true unless r[:options].is_a?(Hash)
4035

4136
return false if r[:options].empty?
4237

43-
return r[:options].keys.all? { |key| RouteOptionsMessage::VALID_MANIFEST_ROUTE_OPTIONS.exclude?(key) }
38+
r[:options].each_key do |key|
39+
RouteOptionsMessage::VALID_MANIFEST_ROUTE_OPTIONS.exclude?(key) &&
40+
record.errors.add(:base,
41+
message: "Route '#{r[:route]}' contains invalid route option '#{key}'. \
42+
Valid keys: '#{RouteOptionsMessage::VALID_MANIFEST_ROUTE_OPTIONS.join(', ')}'")
43+
end
4444
end
4545
end
4646

47-
def contains_invalid_lb_algo?(routes)
48-
routes.any? do |r|
47+
def contains_invalid_lb_algo?(record)
48+
routes = record.routes
49+
routes.each do |r|
4950
next unless r[:options] && r[:options][:'loadbalancing-algorithm']
5051

51-
return true if r[:options][:'loadbalancing-algorithm'] && RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.exclude?(r[:options][:'loadbalancing-algorithm'])
52+
lb_algo = r[:options][:'loadbalancing-algorithm']
53+
RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.exclude?(lb_algo) &&
54+
record.errors.add(:base,
55+
message: "Route '#{r[:route]}' contains invalid load-balancing algorithm '#{lb_algo}'. \
56+
Valid algorithms: '#{RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.join(', ')}'")
5257
end
5358
end
5459
end

spec/request/space_manifests_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,8 @@
646646
post "/v3/spaces/#{space.guid}/actions/apply_manifest", yml_manifest, yml_headers(user_header)
647647

648648
expect(last_response).to have_status_code(422)
649-
expect(last_response).to have_error_message('Routes contains invalid route options')
649+
expect(last_response).to have_error_message("For application '#{app1_model.name}': \
650+
Route 'https://#{route.host}.#{route.domain.name}' contains invalid route option 'doesnt-exist'. Valid keys: 'loadbalancing-algorithm'")
650651
end
651652
end
652653

@@ -811,7 +812,8 @@
811812
post "/v3/spaces/#{space.guid}/actions/apply_manifest", yml_manifest, yml_headers(user_header)
812813

813814
expect(last_response).to have_status_code(422)
814-
expect(last_response).to have_error_message('Routes contains an invalid loadbalancing-algorithm option')
815+
expect(last_response).to have_error_message("For application '#{app1_model.name}': \
816+
Route 'https://#{route.host}.#{route.domain.name}' contains invalid load-balancing algorithm 'unsupported-lb-algorithm'. Valid algorithms: 'round-robin, least-connections'")
815817
end
816818
end
817819

spec/unit/lib/cloud_controller/app_manifest/manifest_route_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,6 @@ module VCAP::CloudController
117117
end
118118
end
119119

120-
context 'when options is set to null' do
121-
let(:route) { 'http://example.com' }
122-
123-
it 'is invalid' do
124-
options = nil
125-
manifest_route = ManifestRoute.parse(route, options)
126-
expect(manifest_route.valid?).to be(false)
127-
end
128-
end
129-
130120
context 'when there is an invalid loadbalancing-algorithm' do
131121
let(:route) { 'http://example.com' }
132122
let(:options) { { 'loadbalancing-algorithm': 'invalid' } }

spec/unit/messages/manifest_routes_update_message_spec.rb

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,6 @@ module VCAP::CloudController
225225
end
226226
end
227227

228-
context 'when a route contains route-options: null' do
229-
let(:body) do
230-
{ 'routes' =>
231-
[
232-
{ 'route' => 'existing.example.com',
233-
'options' => nil }
234-
] }
235-
end
236-
237-
it 'returns false' do
238-
msg = ManifestRoutesUpdateMessage.new(body)
239-
240-
expect(msg.valid?).to be(false)
241-
end
242-
end
243-
244228
context 'when a route contains invalid route options' do
245229
let(:body) do
246230
{ 'routes' =>
@@ -254,42 +238,45 @@ module VCAP::CloudController
254238
msg = ManifestRoutesUpdateMessage.new(body)
255239

256240
expect(msg.valid?).to be(false)
241+
expect(msg.errors.full_messages).to include("Route 'existing.example.com' contains invalid route option 'invalid'. Valid keys: 'loadbalancing-algorithm'")
257242
end
258243
end
259244

260-
context 'when a route contains a route option key: null' do
245+
context 'when a route contains a valid loadbalancing-algorithm' do
261246
let(:body) do
262247
{ 'routes' =>
263248
[
264249
{ 'route' => 'existing.example.com',
265250
'options' => {
266-
'loadbalancing-algorithm' => nil
251+
'loadbalancing-algorithm' => 'round-robin'
267252
} }
268253
] }
269254
end
270255

271-
it 'returns false' do
256+
it 'returns true' do
272257
msg = ManifestRoutesUpdateMessage.new(body)
273258

274-
expect(msg.valid?).to be(false)
259+
expect(msg.valid?).to be(true)
275260
end
276261
end
277262

278-
context 'when a route contains a valid loadbalancing-algorithm' do
263+
context 'when a route contains an invalid loadbalancing-algorithm' do
279264
let(:body) do
280265
{ 'routes' =>
281-
[
282-
{ 'route' => 'existing.example.com',
283-
'options' => {
284-
'loadbalancing-algorithm' => 'round-robin'
285-
} }
286-
] }
266+
[
267+
{ 'route' => 'existing.example.com',
268+
'options' => {
269+
'loadbalancing-algorithm' => 'sushi'
270+
} }
271+
] }
287272
end
288273

289-
it 'returns true' do
274+
it 'returns false' do
290275
msg = ManifestRoutesUpdateMessage.new(body)
291276

292-
expect(msg.valid?).to be(true)
277+
expect(msg.valid?).to be(false)
278+
expect(msg.errors.full_messages).to include("Route 'existing.example.com' contains invalid load-balancing algorithm 'sushi'. \
279+
Valid algorithms: 'round-robin, least-connections'")
293280
end
294281
end
295282
end

0 commit comments

Comments
 (0)