-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathroute_create_spec.rb
More file actions
819 lines (726 loc) · 35.8 KB
/
route_create_spec.rb
File metadata and controls
819 lines (726 loc) · 35.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
require 'spec_helper'
require 'actions/route_create'
require 'messages/route_create_message'
module VCAP::CloudController
RSpec.describe RouteCreate do
let(:user_audit_info) { UserAuditInfo.new(user_email: '[email protected]', user_guid: 'gooid') }
subject { RouteCreate.new(user_audit_info) }
before do
TestConfig.override(kubernetes: {})
end
describe '#create' do
let(:space) { VCAP::CloudController::Space.make(organization: org) }
let(:org) { VCAP::CloudController::Organization.make }
let(:domain) { VCAP::CloudController::PrivateDomain.make(owning_organization: org) }
context 'when successful' do
let(:message) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
end
it 'creates a route' do
expect do
subject.create(message:, space:, domain:)
end.to change(Route, :count).by(1)
route = Route.last
expect(route.space.guid).to eq space.guid
expect(route.domain.guid).to eq domain.guid
end
it 'creates an audit event' do
expect_any_instance_of(Repositories::RouteEventRepository).
to receive(:record_route_create).with(instance_of(Route),
user_audit_info,
message.audit_hash,
manifest_triggered: false)
subject.create(message:, space:, domain:)
end
end
context 'when given metadata' do
let(:message_with_label) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
},
metadata: {
labels: { 'la' => 'bel' }
}
})
end
let(:message_with_annotation) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
},
metadata: {
annotations: { 'anno' => 'tation' }
}
})
end
it 'creates a route and associated labels' do
expect do
subject.create(message: message_with_label, space: space, domain: domain)
end.to change(RouteLabelModel, :count).by(1)
route = Route.last
expect(route.labels.length).to eq(1)
expect(route.labels[0].key_name).to eq('la')
expect(route.labels[0].value).to eq('bel')
end
it 'creates a route and associated annotations' do
expect do
subject.create(message: message_with_annotation, space: space, domain: domain)
end.to change(RouteAnnotationModel, :count).by(1)
route = Route.last
expect(route.annotations.length).to eq(1)
expect(route.annotations[0].key_name).to eq('anno')
expect(route.annotations[0].value).to eq('tation')
end
end
context 'when given route options' do
context 'when creating a route with loadbalancing=hash' do
context 'with hash_header but without hash_balance' do
let(:message_with_hash_options) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
},
options: {
loadbalancing: 'hash',
hash_header: 'X-User-ID'
}
})
end
it 'creates a route with hash loadbalancing and hash_header options' do
expect do
subject.create(message: message_with_hash_options, space: space, domain: domain)
end.to change(Route, :count).by(1)
route = Route.last
expect(route.options).to include({ 'loadbalancing' => 'hash', 'hash_header' => 'X-User-ID' })
end
end
context 'with both hash_header and hash_balance' do
let(:message_with_hash_options) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
},
options: {
loadbalancing: 'hash',
hash_header: 'X-Session-ID',
hash_balance: '2'
}
})
end
it 'creates a route with hash loadbalancing, hash_header, and hash_balance options' do
expect do
subject.create(message: message_with_hash_options, space: space, domain: domain)
end.to change(Route, :count).by(1)
route = Route.last
expect(route.options).to include({ 'loadbalancing' => 'hash', 'hash_header' => 'X-Session-ID', 'hash_balance' => '2.0' })
end
end
context 'without hash_header (required)' do
let(:message_without_hash_header) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
},
options: {
loadbalancing: 'hash'
}
})
end
it 'raises an error indicating hash_header is required' do
expect do
subject.create(message: message_without_hash_header, space: space, domain: domain)
end.to raise_error(RouteCreate::Error, 'Hash header must be present when loadbalancing is set to hash.')
end
end
context 'when options size exceeds the configured limit' do
before do
TestConfig.override(max_route_options_size: 50)
# Enable hash_based_routing feature to allow hash_header in options
VCAP::CloudController::FeatureFlag.make(name: 'hash_based_routing', enabled: true, error_message: nil)
end
let(:message_with_large_options) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
},
options: {
loadbalancing: 'hash',
hash_header: 'X-Custom-Header-Name'
}
})
end
it 'raises an error indicating options size exceeded' do
expect do
subject.create(message: message_with_large_options, space: space, domain: domain)
end.to raise_error(RouteCreate::Error, /Route options size exceeded: options must be smaller than 50 bytes/)
end
end
end
context 'when creating a route with other loadbalancing options' do
let(:message_with_round_robin) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
},
options: {
loadbalancing: 'round-robin'
}
})
end
it 'creates a route with the specified loadbalancing option' do
expect do
subject.create(message: message_with_round_robin, space: space, domain: domain)
end.to change(Route, :count).by(1)
route = Route.last
expect(route.options).to include({ 'loadbalancing' => 'round-robin' })
end
end
end
context 'when the domain has an owning org that is different from the space\'s parent org' do
let(:other_org) { VCAP::CloudController::Organization.make }
let(:inaccessible_domain) { VCAP::CloudController::PrivateDomain.make(owning_organization: other_org) }
let(:message) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: inaccessible_domain.guid }
}
}
})
end
it 'raises an error with a helpful message' do
expect do
subject.create(message: message, space: space, domain: inaccessible_domain)
end.to raise_error(RouteCreate::Error, "Invalid domain. Domain '#{inaccessible_domain.name}' is not available in organization '#{space.organization.name}'.")
end
end
context 'when the domain already has a route' do
let!(:existing_route) { VCAP::CloudController::Route.make(host: '', space: space, domain: domain) }
let(:message) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
end
it 'raises an error with a helpful message' do
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Route already exists for domain '#{domain.name}'.")
end
end
context 'when a port is provided' do
let(:message) do
RouteCreateMessage.new({
host: 'wow',
port: 1234,
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
end
it 'raises an error with a helpful message' do
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Routes with protocol 'http' do not support ports.")
end
end
context 'when the space quota for routes is maxed out' do
let!(:space_quota_definition) { SpaceQuotaDefinition.make(total_routes: 0, organization: org) }
let!(:space_with_quota) do
Space.make(space_quota_definition: space_quota_definition,
organization: org)
end
let(:message) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space_with_quota.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
end
it 'raises an error with a helpful message' do
expect do
subject.create(message: message, space: space_with_quota, domain: domain)
end.to raise_error(RouteCreate::Error, "Routes quota exceeded for space '#{space_with_quota.name}'.")
end
end
context 'when the org quota for routes is maxed out' do
let!(:org_quota_definition) { QuotaDefinition.make(total_routes: 0, total_reserved_route_ports: 0) }
let!(:org_with_quota) { Organization.make(quota_definition: org_quota_definition) }
let!(:space_in_org_with_quota) do
Space.make(organization: org_with_quota)
end
let(:domain_in_org_with_quota) { Domain.make(owning_organization: org_with_quota) }
let(:message) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space_in_org_with_quota.guid }
},
domain: {
data: { guid: domain_in_org_with_quota.guid }
}
}
})
end
it 'raises an error with a helpful message' do
expect do
subject.create(message: message, space: space_in_org_with_quota, domain: domain_in_org_with_quota)
end.to raise_error(RouteCreate::Error, "Routes quota exceeded for organization '#{org_with_quota.name}'.")
end
end
context 'when the FQDN is too long' do
let(:domain_with_long_name) { Domain.make(owning_organization: org, name: "#{'a' * 60}.#{'b' * 60}.#{'c' * 60}.#{'d' * 60}.com") }
let(:message) do
RouteCreateMessage.new({
host: 'h' * 60,
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain_with_long_name.guid }
}
}
})
end
it 'raises an error with a helpful message' do
expect do
subject.create(message: message, space: space, domain: domain_with_long_name)
end.to raise_error(RouteCreate::Error, 'Host combined with domain name must be no more than 253 characters.')
end
end
context 'when the domain is unscoped' do
let(:shared_domain) { SharedDomain.make }
it 'requires host not to be empty' do
message = RouteCreateMessage.new({
host: '',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: shared_domain.guid }
}
}
})
expect do
subject.create(message: message, space: space, domain: shared_domain)
end.to raise_error(RouteCreate::Error, 'Missing host. Routes in shared domains must have a host defined.')
end
end
context 'when a path is invalid' do
it 'raises an error with a helpful message' do
message = RouteCreateMessage.new({
host: '',
path: '/\/\invalid-path',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, 'Path is invalid.')
end
end
context 'when a path is a single /' do
it 'raises an error with a helpful message' do
message = RouteCreateMessage.new({
host: '',
path: '/',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Path cannot be a single '/'.")
end
end
context 'when a path is missing a beginning slash' do
it 'raises an error with a helpful message' do
message = RouteCreateMessage.new({
host: '',
path: 'whereistheslash',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Path is missing the beginning '/'.")
end
end
context 'when a path is too long' do
it 'raises an error with a helpful message' do
message = RouteCreateMessage.new({
host: '',
path: '/pathtoolong' * 5000,
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, 'Path exceeds 128 characters.')
end
end
context 'when a path contains a ?' do
it 'raises an error with a helpful message' do
message = RouteCreateMessage.new({
host: '',
path: '/hmm?',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Path cannot contain '?'.")
end
end
context 'when a route already exists' do
it 'prevents conflict with hostless route on a matching domain' do
Route.make(domain: domain, host: '', space: space)
message = RouteCreateMessage.new({
host: '',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Route already exists for domain '#{domain.name}'.")
end
it 'prevents conflict with matching route on host' do
Route.make(domain: domain, host: 'a-host', space: space)
message = RouteCreateMessage.new({
host: 'a-host',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Route already exists with host 'a-host' for domain '#{domain.name}'.")
end
it 'prevents conflict with matching route on path' do
Route.make(domain: domain, host: 'a-host', path: '/a-path', space: space)
message = RouteCreateMessage.new({
host: 'a-host',
path: '/a-path',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Route already exists with host 'a-host' and path '/a-path' for domain '#{domain.name}'.")
end
end
context 'when the domain is internal' do
let(:internal_domain) { SharedDomain.make(internal: true) }
it 'requires host not to be a wildcard' do
message = RouteCreateMessage.new({
host: '*',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: internal_domain.guid }
}
}
})
expect do
subject.create(message: message, space: space, domain: internal_domain)
end.to raise_error(RouteCreate::Error, 'Wildcard hosts are not supported for internal domains.')
end
it 'disallows paths' do
message = RouteCreateMessage.new({
host: 'a',
path: '/path',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: internal_domain.guid }
}
}
})
expect do
subject.create(message: message, space: space, domain: internal_domain)
end.to raise_error(RouteCreate::Error, 'Paths are not supported for internal domains.')
end
context 'when the Kubernetes API is not configured' do
it 'does not raise an error' do
message = RouteCreateMessage.new({
host: 'a',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: internal_domain.guid }
}
}
})
expect do
subject.create(message: message, space: space, domain: internal_domain)
end.not_to raise_error
end
end
end
context 'when using a reserved system hostname' do
let(:system_domain) { SharedDomain.make }
before do
VCAP::CloudController::Config.config.set(:system_domain, system_domain.name)
VCAP::CloudController::Config.config.set(:system_hostnames, ['host'])
end
it 'prevents conflict with the system domain' do
message = RouteCreateMessage.new({
host: 'host',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: system_domain.guid }
}
}
})
expect do
subject.create(message: message, space: space, domain: system_domain)
end.to raise_error(RouteCreate::Error, 'Route conflicts with a reserved system route.')
end
end
describe 'ports' do
context 'when the domain supports ports (tcp)' do
let(:domain) { SharedDomain.make(router_group_guid: 'some-router-group') }
let(:message) do
RouteCreateMessage.new({
port: 1234,
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
end
let(:routing_api_client) { instance_double(RoutingApi::Client) }
let(:router_group) { instance_double(RoutingApi::RouterGroup) }
before do
allow(CloudController::DependencyLocator).to receive_message_chain(:instance, :routing_api_client).
and_return(routing_api_client)
allow(routing_api_client).to receive_messages(router_group: router_group, enabled?: true)
allow(router_group).to receive_messages(type: 'tcp', reservable_ports: [1234])
end
context 'when the port is available' do
it 'creates a route with the port' do
expect do
subject.create(message:, space:, domain:)
end.to change(Route, :count).by(1)
route = Route.last
expect(route.port).to eq(1234)
end
end
context 'when a route with the same domain and port exist' do
let!(:duplicate_route) { Route.make(domain: domain, host: '', port: 1234, space: space) }
it 'errors to prevent creating a duplicate route' do
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Route already exists with port '1234' for domain '#{domain.name}'.")
end
end
context 'when the port is not reservable for the router group' do
before do
allow(router_group).to receive(:reservable_ports).and_return([])
end
it 'errors and respects the reserved port' do
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Port '1234' is not available. Try a different port or use a different domain.")
end
end
context 'when the space quota limit on reserved ports has been maxed out' do
let!(:space_quota_definition) { SpaceQuotaDefinition.make(total_reserved_route_ports: 0, organization: org) }
let!(:space) do
Space.make(space_quota_definition: space_quota_definition, organization: org)
end
it 'raises an error with a helpful message' do
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Reserved route ports quota exceeded for space '#{space.name}'.")
end
end
context 'when the org quota limit on reserved ports has been maxed out' do
let!(:org_quota_definition) { QuotaDefinition.make(total_reserved_route_ports: 0) }
let!(:org_with_quota) { Organization.make(quota_definition: org_quota_definition) }
let!(:space) { Space.make(organization: org_with_quota) }
let(:domain) { Domain.make(owning_organization: org_with_quota) }
it 'raises an error with a helpful message' do
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, "Reserved route ports quota exceeded for organization '#{org_with_quota.name}'.")
end
end
context 'no port is provided' do
let(:message) do
RouteCreateMessage.new({
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
end
let(:router_group) { double('router_group1', type: 'tcp', guid: 'router_group_guid', reservable_ports: [1024]) }
let(:routing_api_client) { instance_double(VCAP::CloudController::RoutingApi::Client) }
before do
allow_any_instance_of(CloudController::DependencyLocator).to receive(:routing_api_client).and_return(routing_api_client)
allow(routing_api_client).to receive_messages(enabled?: true, router_group: router_group)
end
it 'randomly assigns an available port' do
expect do
subject.create(message:, space:, domain:)
end.to change(Route, :count).by(1)
route = Route.last
expect(route.port).to eq(1234)
end
end
context 'when path is provided' do
let(:message) do
RouteCreateMessage.new({
port: 1234,
path: '/monkeys',
relationships: {
space: {
data: { guid: space.guid }
},
domain: {
data: { guid: domain.guid }
}
}
})
end
it 'errors with a helpful error message' do
expect do
subject.create(message:, space:, domain:)
end.to raise_error(RouteCreate::Error, 'Paths are not supported for TCP routes.')
end
end
end
end
end
end
end