-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathroute_spec.rb
More file actions
1460 lines (1186 loc) · 48.3 KB
/
route_spec.rb
File metadata and controls
1460 lines (1186 loc) · 48.3 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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'spec_helper'
module VCAP::CloudController
RSpec.describe VCAP::CloudController::Route, type: :model do
it { is_expected.to have_timestamp_columns }
describe '#protocol' do
let(:routing_api_client) { double('routing_api_client', router_group:) }
let(:router_group) { double('router_group', type: 'tcp', guid: 'router-group-guid') }
before do
allow_any_instance_of(CloudController::DependencyLocator).to receive(:routing_api_client).and_return(routing_api_client)
end
context 'when the route belongs to a domain with the "http" protocol' do
let!(:domain) { SharedDomain.make }
it 'returns "http"' do
route = Route.new(domain:)
expect(route.protocol).to eq('http')
end
end
context 'when the route belongs to a domain with the "tcp" protocol' do
let!(:tcp_domain) { SharedDomain.make(router_group_guid: 'guid') }
it 'returns "tcp"' do
route = Route.new(domain: tcp_domain, port: 6000)
expect(route.protocol).to eq('tcp')
end
end
end
describe '#tcp?' do
let(:routing_api_client) { double('routing_api_client', router_group:) }
let(:router_group) { double('router_group', type: 'tcp', guid: 'router-group-guid') }
before do
allow_any_instance_of(RouteValidator).to receive(:validate)
allow_any_instance_of(CloudController::DependencyLocator).to receive(:routing_api_client).and_return(routing_api_client)
end
context 'when the route belongs to a shared domain' do
context 'and that domain is a TCP domain' do
let!(:tcp_domain) { SharedDomain.make(router_group_guid: 'guid') }
context 'and the route has a port and it aint kubes' do
let(:route) { Route.new(domain: tcp_domain, port: 6000) }
it 'returns true' do
# turn off kubernetes mode to surface actual tcp checking
TestConfig.override(kubernetes: nil)
expect(route.tcp?).to equal(true)
end
end
context 'and the route does not have a port' do
let(:route) { Route.new(domain: tcp_domain) }
it 'returns false' do
TestConfig.override(kubernetes: nil)
expect(route.tcp?).to equal(false)
end
end
context 'and that domain is not a TCP domain' do
let!(:domain) { SharedDomain.make }
context 'and the route has a port' do
let(:route) { Route.new(domain: domain, port: 6000) }
it 'returns false' do
TestConfig.override(kubernetes: nil)
expect(route.tcp?).to equal(false)
end
end
context 'and the route does not have a port' do
let(:route) { Route.new(domain: tcp_domain) }
it 'returns false' do
expect(route.tcp?).to equal(false)
end
end
end
end
end
context 'when the route belongs to a private domain' do
let(:space) { Space.make }
let!(:private_domain) { PrivateDomain.make(owning_organization: space.organization) }
context 'and the route has a port' do
let(:route) { Route.new(space: space, domain: private_domain, port: 6000) }
it 'returns false' do
expect(route.tcp?).to equal(false)
end
end
context 'and the route does not have a port' do
let(:route) { Route.new(space: space, domain: private_domain) }
it 'returns false' do
expect(route.tcp?).to equal(false)
end
end
end
end
describe 'Associations' do
it { is_expected.to have_associated :domain }
it { is_expected.to have_associated :space, associated_instance: ->(route) { Space.make(organization: route.domain.owning_organization) } }
it { is_expected.to have_associated :route_mappings, associated_instance: ->(route) { RouteMappingModel.make(app: AppModel.make(space: route.space), route: route) } }
describe 'apps association' do
let(:space) { Space.make }
let(:process) { ProcessModelFactory.make(space:) }
let(:route) { Route.make(space:) }
it 'associates apps through route mappings' do
RouteMappingModel.make(app: process.app, route: route, process_type: process.type)
expect(route.apps).to contain_exactly(process)
end
it 'does not associate non-web v2 apps' do
non_web_process = ProcessModelFactory.make(type: 'other', space: space)
RouteMappingModel.make(app: process.app, route: route, process_type: process.type)
RouteMappingModel.make(app: non_web_process.app, route: route, process_type: non_web_process.type)
expect(route.apps).to contain_exactly(process)
end
it 'returns a single app when an app is bound to multiple ports' do
RouteMappingModel.make(app: process.app, route: route, app_port: 8080)
RouteMappingModel.make(app: process.app, route: route, app_port: 9090)
expect(route.apps.length).to eq(1)
end
end
context 'when bound to a service instance' do
let(:route) { Route.make }
let(:service_instance) { ManagedServiceInstance.make(:routing, space: route.space) }
let!(:route_binding) { RouteBinding.make(route:, service_instance:) }
it 'has a service instance' do
expect(route.service_instance).to eq service_instance
end
end
context 'changing space' do
context 'when the route sharing flag is enabled' do
let!(:feature_flag) { VCAP::CloudController::FeatureFlag.make(name: 'route_sharing', enabled: true, error_message: nil) }
it 'succeeds with no mapped apps' do
route = Route.make(space: ProcessModelFactory.make.space, domain: SharedDomain.make)
expect { route.space = Space.make }.not_to raise_error
end
it 'succeeds when there are apps mapped to it' do
process = ProcessModelFactory.make
route = Route.make(space: process.space, domain: SharedDomain.make)
RouteMappingModel.make(app: process.app, route: route, process_type: process.type)
expect { route.space = Space.make }.not_to raise_error
end
end
context 'when the route sharing flag is disabled' do
let!(:feature_flag) { VCAP::CloudController::FeatureFlag.make(name: 'route_sharing', enabled: false, error_message: nil) }
it 'succeeds with no mapped apps' do
route = Route.make(space: ProcessModelFactory.make.space, domain: SharedDomain.make)
expect { route.space = Space.make }.not_to raise_error
end
it 'fails when changing the space when there are apps mapped to it' do
process = ProcessModelFactory.make
route = Route.make(space: process.space, domain: SharedDomain.make)
RouteMappingModel.make(app: process.app, route: route, process_type: process.type)
expect { route.space = Space.make }.to raise_error(CloudController::Errors::InvalidAppRelation)
end
end
context 'with domain' do
it 'succeeds if its a shared domain' do
route = Route.make(domain: SharedDomain.make)
expect { route.space = Space.make }.not_to raise_error
end
context 'private domain' do
let(:org) { Organization.make }
let(:domain) { PrivateDomain.make(owning_organization: org) }
let(:route) { Route.make(domain: domain, space: Space.make(organization: org)) }
it 'succeeds if in the same organization' do
expect { route.space = Space.make(organization: org) }.not_to raise_error
end
context 'with a different organization' do
it 'fails' do
expect { route.space = Space.make }.to raise_error(Route::InvalidOrganizationRelation, /Organization cannot use domain/)
end
it 'succeeds if the organization shares the domain' do
space = Space.make
domain.add_shared_organization(space.organization)
expect { route.space = space }.not_to raise_error
end
end
end
end
end
context 'creating context path routes across spaces' do
let(:org1) { Organization.make(name: 'org1') }
let(:space1a) { Space.make(name: 'space1a', organization: org1) }
let(:space1b) { Space.make(name: 'space1b', organization: org1) }
let(:domain) { PrivateDomain.make(name: 'tld.org', owning_organization: org1) }
let(:org2) { Organization.make(name: 'org2') }
let(:space2) { Space.make(name: 'space2', organization: org2) }
let(:disable_context_route_sharing) { false }
let(:host2) { 'host-' + SecureRandom.uuid }
before do
TestConfig.override(disable_private_domain_cross_space_context_path_route_sharing: disable_context_route_sharing)
end
context 'when the route matches host and domain' do
let!(:first_route) { Route.make(host: 'host', domain: domain, space: space1a) }
it 'cannot create the duplicate (no path) route in the same-org space' do
expect do
Route.make(host: 'host', domain: domain, space: space1b)
end.to raise_error(Sequel::ValidationFailed, /host and domain_id and path unique/)
end
context 'when private domain context path route sharing is disabled' do
let(:disable_context_route_sharing) { true }
it 'cannot create a pathful route in the same-org space' do
expect do
Route.make(host: 'host', domain: domain, space: space1b, path: '/apples/kumquats')
end.to raise_error(Sequel::ValidationFailed, /domain_id and host host_and_domain_taken_different_space/)
end
end
context 'when private domain context path route sharing is NOT disabled' do
it 'CAN create a pathful route in the same-org space' do
r = Route.make(host: 'host', domain: domain, space: space1b, path: '/apples/kumquats')
expect(r).to be_valid
end
end
end
context 'when the route does not match host and domain' do
it 'can create a no-path route in the same-org space' do
r = Route.make(host: 'host-' + SecureRandom.uuid, domain: domain, space: space1b)
expect(r).to be_valid
end
end
context 'the first route does have a path' do
let!(:first_route) { Route.make(host: 'host', domain: domain, space: space1a, path: '/my-path') }
context 'when private domain context path route sharing is NOT disabled' do
it 'succeeds' do
r = Route.make(host: 'host', domain: domain, space: space1b)
expect(r).to be_valid
end
end
context 'when private domain context path route sharing is disabled' do
let(:disable_context_route_sharing) { true }
it 'fails' do
expect do
Route.make(host: 'host', domain: domain, space: space1b)
end.to raise_error(Sequel::ValidationFailed, /domain_id and host host_and_domain_taken_different_space/)
end
end
end
context 'when sharing private domains to other orgs' do
let!(:first_route) { Route.make(host: 'host', domain: domain, space: space1a, path: '/mangos') }
before do
domain.add_shared_organization(org2)
end
context 'when private domain context path route sharing is NOT disabled' do
it 'succeeds' do
r = Route.make(host: 'host', domain: domain, space: space2, path: '/grapes')
expect(r).to be_valid
end
end
context 'when private domain context path route sharing is disabled' do
let(:disable_context_route_sharing) { true }
it 'fails' do
expect do
Route.make(host: 'host', domain: domain, space: space2, path: '/grapes')
end.to raise_error(Sequel::ValidationFailed, /domain_id and host host_and_domain_taken_different_space/)
end
end
end
end
context 'changing domain' do
context 'when shared' do
it "succeeds if it's the same domain" do
domain = SharedDomain.make
route = Route.make(domain:)
route.domain = route.domain = domain
expect { route.save }.not_to raise_error
end
it "fails if it's different" do
route = Route.make(domain: SharedDomain.make)
route.domain = SharedDomain.make
expect(route).not_to be_valid
end
end
context 'when private' do
let(:space) { Space.make }
let(:domain) { PrivateDomain.make(owning_organization: space.organization) }
it 'succeeds if it is the same domain' do
route = Route.make(space:, domain:)
route.domain = domain
expect { route.save }.not_to raise_error
end
it 'fails if its a different domain' do
route = Route.make(space:, domain:)
route.domain = PrivateDomain.make(owning_organization: space.organization)
expect(route).not_to be_valid
end
end
end
context 'deleting with route mappings' do
before do
TestConfig.override(
kubernetes: {}
)
end
it 'removes the associated route mappings' do
route = Route.make
app = AppModel.make(space: route.space)
mapping1 = RouteMappingModel.make(route: route, app: app, process_type: 'thing')
mapping2 = RouteMappingModel.make(route: route, app: app, process_type: 'other')
route.destroy
expect(mapping1).not_to exist
expect(mapping2).not_to exist
end
end
end
describe 'Validations' do
let!(:route) { Route.new }
it { is_expected.to validate_presence :domain }
it { is_expected.to validate_presence :space }
it { is_expected.to validate_presence :host }
it 'calls RouteValidator' do
validator = double
expect(RouteValidator).to receive(:new).and_return(validator)
expect(validator).to receive(:validate)
route.validate
end
context 'when routing api is disabled' do
before do
validator = double
allow(RouteValidator).to receive(:new).and_return(validator)
allow(validator).to receive(:validate).and_raise(RoutingApi::RoutingApiDisabled)
end
it 'adds routing_api_disabled to errors' do
route.validate
expect(route.errors.on(:routing_api)).to include :routing_api_disabled
end
end
context 'when routing api raises UaaUnavailable error' do
before do
validator = double
allow(RouteValidator).to receive(:new).and_return(validator)
allow(validator).to receive(:validate).and_raise(RoutingApi::UaaUnavailable)
end
it 'adds uaa_unavailable to errors' do
route.validate
expect(route.errors.on(:routing_api)).to include :uaa_unavailable
end
end
context 'when routing api raises RoutingApiUnavailable error' do
before do
validator = double
allow(RouteValidator).to receive(:new).and_return(validator)
allow(validator).to receive(:validate).and_raise(RoutingApi::RoutingApiUnavailable)
end
it 'adds routing_api_unavailable to errors' do
route.validate
expect(route.errors.on(:routing_api)).to include :routing_api_unavailable
end
end
context 'when the requested route is a system hostname with a system domain' do
let(:domain) { Domain.find(name: TestConfig.config[:system_domain]) }
let(:space) { Space.make(organization: domain.owning_organization) }
let(:host) { 'loggregator' }
let(:route) { Route.new(domain:, space:, host:) }
it 'is invalid' do
expect(route).not_to be_valid
expect(route.errors.on(:host)).to include :system_hostname_conflict
end
end
context 'when a route with the same hostname and domain already exists' do
let(:domain) { SharedDomain.make }
let(:space) { Space.make }
let(:host) { 'example' }
context 'with a context path' do
let(:path) { '/foo' }
before do
Route.make(domain:, space:, host:)
end
it 'is valid' do
route_obj = Route.new(domain:, host:, space:, path:)
expect(route_obj).to be_valid
end
context 'and a user attempts to create the route in another space' do
let(:another_space) { Space.make }
it 'is not valid' do
route_obj = Route.new(domain: domain, space: another_space, host: host, path: path)
expect(route_obj).not_to be_valid
expect(route_obj.errors.on(%i[domain_id host])).to include :host_and_domain_taken_different_space
end
end
context 'and the domain is a private domain' do
let(:domain) { PrivateDomain.make }
let(:space) { Space.make(organization: domain.owning_organization) }
context 'and a user attempts to create the route in another space' do
let(:another_space) { Space.make(organization: domain.owning_organization) }
it 'is valid' do
route_obj = Route.new(domain: domain, space: another_space, host: host, path: path)
expect(route_obj).to be_valid
end
end
end
end
context 'without a context path' do
before do
Route.make(domain: domain, space: space, host: host, path: '/bar')
end
context 'and a user attempts to create the route in another space' do
let(:another_space) { Space.make }
it 'is not valid' do
route_obj = Route.new(domain: domain, space: another_space, host: host)
expect(route_obj).not_to be_valid
expect(route_obj.errors.on(%i[domain_id host])).to include :host_and_domain_taken_different_space
end
end
end
end
context 'route ports' do
let(:route) { Route.make }
it 'validates that the port is greater than equal to 0' do
route.port = -1
expect(route).not_to be_valid
end
it 'validates that the port is less than 65536' do
route.port = 65_536
expect(route).not_to be_valid
end
it 'requires a host or port' do
route.host = nil
route.port = nil
expect(route).not_to be_valid
end
it 'defaults the port to nil' do
expect(route.port).to be_nil
end
context 'when port is specified' do
let(:domain) { SharedDomain.make(router_group_guid: 'tcp-router-group') }
let(:space_quota_definition) { SpaceQuotaDefinition.make }
let(:space) { Space.make(space_quota_definition: space_quota_definition, organization: space_quota_definition.organization) }
let(:routing_api_client) { double('routing_api_client', router_group:) }
let(:router_group) { double('router_group', type: 'tcp', guid: 'router-group-guid') }
before do
TestConfig.override(kubernetes: nil)
allow_any_instance_of(CloudController::DependencyLocator).to receive(:routing_api_client).and_return(routing_api_client)
validator = double
allow(RouteValidator).to receive(:new).and_return(validator)
allow(validator).to receive(:validate)
Route.make(space: space, domain: domain, host: '', port: 1)
end
it 'does not validate uniqueness of host' do
expect do
Route.make(space: space, port: 10, host: '', domain: domain)
end.not_to raise_error
end
it 'validates the uniqueness of the port' do
new_route = Route.new(space: space, port: 1, host: '', domain: domain)
expect(new_route).not_to be_valid
expect(new_route.errors.on(%i[host domain_id port])).to include :unique
end
end
end
context 'unescaped paths' do
it 'validates uniqueness' do
r = Route.make(path: '/a')
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id, path: r.path)
end.to raise_error(Sequel::ValidationFailed)
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id, path: '/b')
end.not_to raise_error
end
it 'does not allow two blank paths with same host and domain' do
r = Route.make
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id)
end.to raise_error(Sequel::ValidationFailed)
end
it 'is case-insensitive' do
r = Route.make(path: '/path')
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id, path: '/PATH')
end.to raise_error(Sequel::ValidationFailed)
end
end
context 'escaped paths' do
it 'validates uniqueness' do
path = '/a%20path'
r = Route.make(path:)
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id, path: path)
end.to raise_error(Sequel::ValidationFailed)
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id, path: '/b%20path')
end.not_to raise_error
end
it 'allows another route with same host and domain but no path' do
path = '/a%20path'
r = Route.make(path:)
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id)
end.not_to raise_error
end
it 'allows a route with same host and domain with a path' do
r = Route.make
expect do
Route.make(host: r.host, space_guid: r.space_guid, domain_id: r.domain_id, path: '/a/path')
end.not_to raise_error
end
end
context 'paths over 128 characters' do
it 'raises exceeds valid length error' do
path = '/path' * 100
expect { Route.make(path:) }.to raise_error(Sequel::ValidationFailed)
end
end
describe 'host' do
let(:space) { Space.make }
let(:domain) { PrivateDomain.make(owning_organization: space.organization) }
before do
route.space = space
route.domain = domain
end
it 'allows * to be the host name' do
route.host = '*'
expect(route).to be_valid
end
it 'does not allow * in the host name' do
route.host = 'a*'
expect(route).not_to be_valid
end
it 'does not allow . in the host name' do
route.host = 'a.b'
expect(route).not_to be_valid
end
it 'does not allow / in the host name' do
route.host = 'a/b'
expect(route).not_to be_valid
end
it 'does not allow a nil host' do
expect do
Route.make(space: space, domain: domain, host: nil)
end.to raise_error(Sequel::ValidationFailed)
end
it 'allows an empty host' do
Route.make(
space: space,
domain: domain,
host: ''
)
end
it 'does not allow a blank host' do
expect do
Route.make(
space: space,
domain: domain,
host: ' '
)
end.to raise_error(Sequel::ValidationFailed)
end
it 'does not allow a long host' do
expect do
Route.make(
space: space,
domain: domain,
host: 'f' * 63
)
end.not_to raise_error
expect do
Route.make(
space: space,
domain: domain,
host: 'f' * 64
)
end.to raise_error(Sequel::ValidationFailed)
end
it 'does not allow a host which, along with the domain, exceeds the maximum length' do
domain_200_chars = "#{'f' * 49}.#{'f' * 49}.#{'f' * 49}.#{'f' * 50}"
domain_253_chars = "#{'f' * 49}.#{'f' * 49}.#{'f' * 49}.#{'f' * 49}.#{'f' * 50}.ff"
domain = PrivateDomain.make(owning_organization: space.organization, name: domain_200_chars)
domain_that_cannot_have_a_host = PrivateDomain.make(owning_organization: space.organization, name: domain_253_chars)
valid_host = 'f' * 52
invalid_host = 'f' * 53
expect do
Route.make(
space: space,
domain: domain,
host: valid_host
)
end.not_to raise_error
expect do
Route.make(
space: space,
domain: domain_that_cannot_have_a_host,
host: ''
)
end.not_to raise_error
expect do
Route.make(
space: space,
domain: domain,
host: invalid_host
)
end.to raise_error(Sequel::ValidationFailed)
end
context 'shared domains' do
it 'does not allow route to match existing domain' do
SharedDomain.make name: 'bar.foo.com'
expect do
Route.make(
space: space,
domain: SharedDomain.make(name: 'foo.com'),
host: 'bar'
)
end.to raise_error(Sequel::ValidationFailed, /domain_conflict/)
end
context 'when the host is missing' do
it 'raises an informative error' do
domain = SharedDomain.make name: 'bar.foo.com'
expect do
Route.make(
space: space,
domain: domain,
host: nil
)
end.to raise_error(Sequel::ValidationFailed, /host is required for shared-domains/)
end
end
end
end
describe 'total allowed routes' do
let(:space) { Space.make }
let(:org_quota) { space.organization.quota_definition }
let(:space_quota) { nil }
before do
space.space_quota_definition = space_quota
end
let(:domain) { PrivateDomain.make(owning_organization: space.organization) }
subject(:route) { Route.new(space: space, domain: domain, host: 'bar') }
context 'for organization quotas' do
context 'on create' do
context 'when not exceeding total allowed routes' do
before do
org_quota.total_routes = 10
org_quota.save
end
it 'does not have an error on organization' do
subject.valid?
expect(subject.errors.on(:organization)).to be_nil
end
end
context 'when exceeding total allowed routes' do
before do
org_quota.total_routes = 0
org_quota.total_reserved_route_ports = 0
org_quota.save
end
it 'has the error on organization' do
subject.valid?
expect(subject.errors.on(:organization)).to include :total_routes_exceeded
end
end
end
context 'on update' do
it 'does not validate the total routes limit if already existing' do
subject.save
expect(subject).to be_valid
org_quota.total_routes = 0
org_quota.total_reserved_route_ports = 0
org_quota.save
expect(subject).to be_valid
end
end
end
context 'for space quotas' do
let(:space_quota) { SpaceQuotaDefinition.make(organization: subject.space.organization) }
let(:tcp_domain) { SharedDomain.make(router_group_guid: 'guid') }
context 'on create' do
context 'when not exceeding total allowed routes' do
before do
space_quota.total_routes = 10
space_quota.save
end
it 'does not have an error on the space' do
subject.valid?
expect(subject.errors.on(:space)).to be_nil
end
end
context 'when exceeding total allowed routes' do
before do
space_quota.total_routes = 0
space_quota.save
end
it 'has the error on the space' do
subject.valid?
expect(subject.errors.on(:space)).to include :total_routes_exceeded
end
end
context 'when creating another tcp route' do
subject(:another_route) { Route.new(space: space, domain: tcp_domain, host: '', port: 4444) }
let!(:mock_router_api_client) do
router_group = double('router_group', type: 'tcp', reservable_ports: [4444, 6000, 1234, 3455, 2222])
routing_api_client = double('routing_api_client', router_group: router_group, enabled?: true)
allow(CloudController::DependencyLocator).to receive(:instance).and_return(double(:api_client, routing_api_client:))
end
before do
space_quota.total_reserved_route_ports = 0
space_quota.save
end
it 'is invalid' do
expect(subject).not_to be_valid
expect(subject.errors.on(:space)).to include :total_reserved_route_ports_exceeded
end
end
end
context 'on update' do
it 'does not validate the total routes limit if already existing' do
subject.save
expect(subject).to be_valid
space_quota.total_routes = 0
space_quota.save
expect(subject).to be_valid
end
end
end
describe 'quota evaluation order' do
let(:space_quota) { SpaceQuotaDefinition.make(organization: subject.space.organization) }
before do
org_quota.total_routes = 0
org_quota.total_reserved_route_ports = 0
space_quota.total_routes = 10
org_quota.save
space_quota.save
end
it 'fails when the space quota is valid and the organization quota is exceeded' do
subject.valid?
expect(subject.errors.on(:space)).to be_nil
expect(subject.errors.on(:organization)).to include :total_routes_exceeded
end
end
end
describe 'total reserved route ports' do
let(:space_quota) { SpaceQuotaDefinition.make }
let(:space) { Space.make(space_quota_definition: space_quota, organization: space_quota.organization) }
let(:org_quota) { space.organization.quota_definition }
let(:http_domain) { SharedDomain.make }
let(:tcp_domain) { SharedDomain.make(router_group_guid: 'guid') }
let(:validator) { double }
let(:http_route) do
Route.new(space: space,
domain: http_domain,
host: 'bar')
end
subject(:tcp_route) do
Route.new(space: space,
domain: tcp_domain,
host: '',
port: 6000)
end
before do
router_group = double('router_group', type: 'tcp', reservable_ports: [4444, 6000])
routing_api_client = double('routing_api_client', router_group: router_group, enabled?: true)
allow_any_instance_of(CloudController::DependencyLocator).to receive(:routing_api_client).and_return(routing_api_client)
end
context 'on create' do
context 'when the space does not have a space quota' do
let(:space) { Space.make }
before do
TestConfig.override(kubernetes: nil)
end
it 'is valid' do
expect(subject).to be_valid
end
end
context 'when not exceeding total allowed routes' do
before do
TestConfig.override(kubernetes: nil)
org_quota.total_routes = 2
org_quota.total_reserved_route_ports = 1
org_quota.save
end
it 'is valid' do
expect(subject).to be_valid
end
context 'when creating another http route' do
subject(:another_route) { Route.new(space: space, domain: http_domain, host: 'foo') }
before do
http_route.save
end
it 'is valid' do
expect(subject).to be_valid
end
end
context 'when creating another tcp route' do
subject(:another_route) { Route.new(space: space, domain: tcp_domain, host: '', port: 4444) }
before do
TestConfig.override(kubernetes: nil)
end
context 'when exceeding total_reserved_route_ports in org quota' do
before do
tcp_route.save
end
it 'is invalid' do
expect(subject).not_to be_valid
expect(subject.errors.on(:organization)).to include :total_reserved_route_ports_exceeded
end
end
context 'when exceeding total_reserved_route_ports in space quota' do
before do
org_quota.total_routes = 10
org_quota.total_reserved_route_ports = 2
org_quota.save
space_quota.total_reserved_route_ports = 1
space_quota.save
tcp_route.save
end
it 'is invalid' do
expect(subject).not_to be_valid
expect(subject.errors.on(:space)).to include :total_reserved_route_ports_exceeded
end
end
end
end
context 'when creating a route would exceed total routes' do
before do
org_quota.total_routes = 1
org_quota.total_reserved_route_ports = 0
org_quota.save
end
it 'has the error on organization' do
expect(subject).not_to be_valid
expect(subject.errors.on(:organization)).to include :total_reserved_route_ports_exceeded
end