This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 516
/
application_container_test.rb
executable file
·1316 lines (1076 loc) · 50.6 KB
/
application_container_test.rb
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
#!/usr/bin/env oo-ruby
#--
# Copyright 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#++
#
# Test the OpenShift application_container model
#
require_relative '../test_helper'
require 'fileutils'
require 'yaml'
module OpenShift
;
end
class ApplicationContainerTest < OpenShift::NodeTestCase
def setup
@ports_begin = 35531
@ports_per_user = 5
@uid_begin = 500
@config.stubs(:get).with("PORT_BEGIN").returns(@ports_begin.to_s)
@config.stubs(:get).with("PORTS_PER_USER").returns(@ports_per_user.to_s)
@config.stubs(:get).with("UID_BEGIN").returns(@uid_begin.to_s)
@config.stubs(:get).with("GEAR_BASE_DIR").returns("/tmp")
script_dir = File.expand_path(File.dirname(__FILE__))
cart_base_path = File.join(script_dir, '..', '..', '..', 'cartridges')
raise "Couldn't find cart base path at #{cart_base_path}" unless File.exists?(cart_base_path)
@config.stubs(:get).with("CARTRIDGE_BASE_PATH").returns(cart_base_path)
#@config.stubs(:get).with("CONTAINERIZATION_PLUGIN").returns('openshift-origin-container-selinux')
# Set up the container
@gear_uuid = '5504'
@user_uid = '5504'
@app_name = 'ApplicatioContainerTestCase'
@gear_name = @app_name
@namespace = 'jwh201204301647'
@gear_ip = '127.0.0.1'
Etc.stubs(:getpwnam).returns(
OpenStruct.new(
uid: @user_uid.to_i,
gid: @user_uid.to_i,
gecos: "OpenShift guest",
dir: "/var/lib/openshift/#{@gear_uuid}"
)
)
@container = OpenShift::Runtime::ApplicationContainer.new(@gear_uuid, @gear_uuid, @user_uid,
@app_name, @gear_uuid, @namespace, nil, nil, nil)
@mock_manifest = %q{#
Name: mock
Cartridge-Short-Name: MOCK
Cartridge-Version: 1.0
Cartridge-Vendor: unit_test
Display-Name: Mock
Description: "A mock cartridge for development use only."
Version: 0.1
License: "None"
Vendor: Red Hat
Categories:
- service
Provides:
- mock
Scaling:
Min: 1
Max: -1
Group-Overrides:
- components:
- mock
Endpoints:
- Private-IP-Name: EXAMPLE_IP1
Private-Port-Name: EXAMPLE_PORT1
Private-Port: 8080
Public-Port-Name: EXAMPLE_PUBLIC_PORT1
Options: { primary: true }
Mappings:
- Frontend: "/front1a"
Backend: "/back1a"
Options: { websocket: true, tohttps: true }
- Frontend: "/front1b"
Backend: "/back1b"
Options: { noproxy: true }
- Private-IP-Name: EXAMPLE_IP1
Private-Port-Name: EXAMPLE_PORT2
Private-Port: 8081
Public-Port-Name: EXAMPLE_PUBLIC_PORT2
Mappings:
- Frontend: "/front2"
Backend: "/back2"
Options: { file: true }
- Private-IP-Name: EXAMPLE_IP1
Private-Port-Name: EXAMPLE_PORT3
Private-Port: 8082
Public-Port-Name: EXAMPLE_PUBLIC_PORT3
Mappings:
- Frontend: "/front3"
Backend: "/back3"
- Private-IP-Name: EXAMPLE_IP2
Private-Port-Name: EXAMPLE_PORT4
Private-Port: 9090
Public-Port-Name: EXAMPLE_PUBLIC_PORT4
Mappings:
- Frontend: "/front4"
Backend: "/back4"
- Private-IP-Name: EXAMPLE_IP2
Private-Port-Name: EXAMPLE_PORT5
Private-Port: 9091
}
manifest = "/tmp/manifest-#{Process.pid}"
IO.write(manifest, @mock_manifest, 0)
@mock_cartridge = OpenShift::Runtime::Manifest.new(manifest, nil, :file)
@container.cartridge_model.stubs(:get_cartridge).with("mock").returns(@mock_cartridge)
end
def teardown
FileUtils.rm_rf @container.container_dir
end
def test_public_endpoints_create
OpenShift::Runtime::Utils::Environ.stubs(:for_gear).returns({
"OPENSHIFT_MOCK_EXAMPLE_IP1" => "127.0.0.1",
"OPENSHIFT_MOCK_EXAMPLE_IP2" => "127.0.0.2"
})
proxy = mock('OpenShift::Runtime::FrontendProxyServer')
OpenShift::Runtime::FrontendProxyServer.stubs(:new).returns(proxy)
proxy.expects(:add).with(@user_uid.to_i, "127.0.0.1", 8080).returns(@ports_begin)
proxy.expects(:add).with(@user_uid.to_i, "127.0.0.1", 8081).returns(@ports_begin+1)
proxy.expects(:add).with(@user_uid.to_i, "127.0.0.1", 8082).returns(@ports_begin+2)
proxy.expects(:add).with(@user_uid.to_i, "127.0.0.2", 9090).returns(@ports_begin+3)
@container.expects(:add_env_var).with('OPENSHIFT_MOCK_EXAMPLE_PUBLIC_PORT1', @ports_begin)
@container.expects(:add_env_var).with('OPENSHIFT_MOCK_EXAMPLE_PUBLIC_PORT2', @ports_begin+1)
@container.expects(:add_env_var).with('OPENSHIFT_MOCK_EXAMPLE_PUBLIC_PORT3', @ports_begin+2)
@container.expects(:add_env_var).with('OPENSHIFT_MOCK_EXAMPLE_PUBLIC_PORT4', @ports_begin+3)
@container.create_public_endpoints(@mock_cartridge.name)
end
def test_public_endpoints_delete
OpenShift::Runtime::Utils::Environ.stubs(:for_gear).returns({
"OPENSHIFT_MOCK_EXAMPLE_IP1" => "127.0.0.1",
"OPENSHIFT_MOCK_EXAMPLE_IP2" => "127.0.0.2"
})
proxy = mock('OpenShift::Runtime::FrontendProxyServer')
OpenShift::Runtime::FrontendProxyServer.stubs(:new).returns(proxy)
OpenShift::Runtime::V2CartridgeModel.any_instance.expects(:list_proxy_mappings).returns([
{public_port_name: "Endpoint_1", proxy_port: @ports_begin},
{public_port_name: "Endpoint_2", proxy_port: @ports_begin+1},
{public_port_name: "Endpoint_3", proxy_port: @ports_begin+2},
{public_port_name: "Endpoint_4", proxy_port: @ports_begin+3}])
#proxy.expects(:find_mapped_proxy_port).with(@user_uid, "127.0.0.1", 8080).returns(@ports_begin)
#proxy.expects(:find_mapped_proxy_port).with(@user_uid, "127.0.0.1", 8081).returns(@ports_begin+1)
#proxy.expects(:find_mapped_proxy_port).with(@user_uid, "127.0.0.1", 8082).returns(@ports_begin+2)
#proxy.expects(:find_mapped_proxy_port).with(@user_uid, "127.0.0.2", 9090).returns(@ports_begin+3)
delete_all_args = [@ports_begin, @ports_begin+1, @ports_begin+2, @ports_begin+3]
proxy.expects(:delete_all).with(delete_all_args, true).returns(nil)
@container.expects(:remove_env_var).returns(nil).times(4)
@container.delete_public_endpoints(@mock_cartridge.name)
end
def test_tidy_success
OpenShift::Runtime::Utils::Environ.stubs(:for_gear).returns(
{'OPENSHIFT_HOMEDIR' => '/foo', 'OPENSHIFT_APP_NAME' => 'app_name'})
@container.stubs(:stop_gear)
@container.stubs(:gear_level_tidy_tmp).with('/foo/.tmp')
@container.cartridge_model.expects(:tidy)
@container.stubs(:gear_level_tidy_git).with('/foo/git/app_name.git')
@container.stubs(:start_gear)
@container.stubs(:cartridge_model).returns(mock())
@container.tidy
end
def test_tidy_stop_gear_fails
OpenShift::Runtime::Utils::Environ.stubs(:for_gear).returns(
{'OPENSHIFT_HOMEDIR' => '/foo', 'OPENSHIFT_APP_NAME' => 'app_name'})
@container.stubs(:stop_gear).raises(Exception.new)
@container.stubs(:gear_level_tidy_tmp).with('/foo/.tmp')
@container.cartridge_model.expects(:tidy).never
@container.stubs(:gear_level_tidy_git).with('/foo/git/app_name.git')
@container.stubs(:start_gear).never
assert_raise Exception do
@container.tidy
end
end
def test_tidy_gear_level_tidy_fails
OpenShift::Runtime::Utils::Environ.stubs(:for_gear).returns(
{'OPENSHIFT_HOMEDIR' => '/foo', 'OPENSHIFT_APP_NAME' => 'app_name'})
@container.expects(:stop_gear)
@container.expects(:gear_level_tidy_tmp).with('/foo/.tmp').raises(Exception.new)
@container.expects(:start_gear)
@container.tidy
end
def test_force_stop
FileUtils.mkpath("/tmp/#@user_uid/app-root/runtime")
OpenShift::Runtime::Containerization::Plugin.stubs(:kill_procs).with(@user_uid).returns(nil)
@container.state.expects(:value=).with(OpenShift::Runtime::State::STOPPED)
@container.cartridge_model.expects(:create_stop_lock)
@container.force_stop
end
def test_connector_execute
cart_name = 'mock-0.1'
pub_cart_name = 'mock-plugin-0.1'
connector_type = 'ENV:NET_TCP'
connector = 'set-db-connection-info'
args = 'foo'
@container.cartridge_model.expects(:connector_execute).with(cart_name, pub_cart_name, connector_type, connector, args)
@container.connector_execute(cart_name, pub_cart_name, connector_type, connector, args)
end
# Tests a variety of UID/host ID to IP address conversions.
#
# TODO: Is there a way to do this algorithmically?
def test_get_ip_addr_success
scenarios = [
[501, 1, "127.0.250.129"],
[501, 10, "127.0.250.138"],
[501, 20, "127.0.250.148"],
[501, 100, "127.0.250.228"],
[501, 127, "127.0.250.255"],
[540, 1, "127.1.14.1"],
[560, 7, "127.1.24.7"],
# 2x 2^16 +- 1
[131071, 1, "127.128.0.1"],
[131071, 10, "127.128.0.10"],
[131071, 127, "127.128.0.127"],
[131072, 1, "127.0.0.129"],
[131072, 10, "127.0.0.138"],
[131072, 127, "127.0.0.255"],
[131073, 1, "127.0.1.1"],
[131073, 10, "127.0.1.10"],
[131073, 127, "127.0.1.127"],
# 10x 2^16 +- 1
[655359, 1, "127.128.0.1"],
[655359, 10, "127.128.0.10"],
[655359, 127, "127.128.0.127"],
[655360, 1, "127.0.0.129"],
[655360, 10, "127.0.0.138"],
[655360, 127, "127.0.0.255"],
[655361, 1, "127.0.1.1"],
[655361, 10, "127.0.1.10"],
[655361, 127, "127.0.1.127"],
]
scenarios.each do |s|
Etc.stubs(:getpwnam).returns(
OpenStruct.new(
uid: s[0].to_i,
gid: s[0].to_i,
gecos: "OpenShift guest",
dir: "/var/lib/openshift/gear_uuid"
)
)
container = OpenShift::Runtime::ApplicationContainer.new("gear_uuid", "gear_uuid", s[0],
"app_name", "gear_uuid", "namespace", nil, nil, nil)
assert_equal s[2], container.get_ip_addr(s[1])
end
end
def test_user_var_add()
target = ".env/user_vars"
source = "/var/lib/openshift/#{@gear_uuid}/#{target}"
filename = "#{source}/UNIT_TEST"
gears = ['unit_test.example.com']
@container.expects(:user_var_push).with(gears, true)
@container.user_var_add({'UNIT_TEST' => 'true'}, gears)
assert_path_exist filename
end
def test_user_var_remove()
path = "/var/lib/openshift/#{@gear_uuid}/.env/user_vars/UNIT_TEST"
gears = ['unit_test.example.com']
@container.expects(:user_var_push).with(gears, true)
@container.expects(:user_var_push).with(gears)
@container.user_var_add({'UNIT_TEST' => 'true'}, gears)
assert_path_exist path
@container.user_var_remove(['UNIT_TEST'], gears)
refute_path_exist path
end
def test_user_var_list()
@container.user_var_remove(['UNIT_TEST', 'FUNC_TEST'])
assert_equal({}, @container.user_var_list())
@container.user_var_add({'UNIT_TEST' => 'true'})
actual = @container.user_var_list()
assert_equal({'UNIT_TEST' => 'true'}, actual)
@container.user_var_add({'FUNC_TEST' => 'false'})
actual = @container.user_var_list(['FUNC_TEST'])
assert_equal({'FUNC_TEST' => 'false'}, actual)
end
def test_bad_user_var()
env = {'OPENSHIFT_TEST_IDENT' => 'x:x:x:x',
'OPENSHIFT_NAMESPACE' => 'namespace',
'OPENSHIFT_PRIMARY_CARTRIDGE_DIR' => 'mine',
'PATH' => '/usr/bin',
'IFS' => '/',
'USER' => 'none',
'SHELL' => 'tcsh',
'HOSTNAME' => 'remotehost',
'LOGNAME' => '/tmp/log'}
env.each do |key, value|
rc, msg = @container.user_var_add({key => value})
assert_equal 255, rc, "#{key} should have failed."
assert_equal "CLIENT_ERROR: #{key} cannot be overridden", msg
end
rc, msg = @container.user_var_add({'TOO_BIG' => '*' * 513})
assert_equal 255, rc
assert_equal "CLIENT_ERROR: 'TOO_BIG' value exceeds maximum size of 512b\n", msg
end
# Tests that no_overcommit logic works as intended
def test_no_overcommit
scenarios = [
[5507, true, 0.0],
[5508, true, 100.0],
[5509, false, 0.0],
[5510, false, 100.0],
]
scenarios.each do |s|
if s[1]
@config.stubs(:get_bool).with('no_overcommit_active', false).returns(true)
else
@config.stubs(:get_bool).with('no_overcommit_active', false).returns(false)
end
OpenShift::Runtime::Node.stubs(:node_utilization).returns({'gears_active_usage_pct' => s[2]})
OpenShift::Runtime::Node.stubs(:resource_limits).returns(@config)
containerization_plugin_mock = mock('OpenShift::Runtime::Containerization::Plugin')
containerization_plugin_mock.stubs(:create).returns(nil)
OpenShift::Runtime::Containerization::Plugin.stubs(:new).returns(containerization_plugin_mock)
Etc.stubs(:getpwnam).returns(
OpenStruct.new(
uid: s[0],
gid: s[0],
gecos: "OpenShift guest",
container_dir: "/var/lib/openshift/#{s[0].to_s}"
)
)
container = OpenShift::Runtime::ApplicationContainer.new(s[0].to_s, s[0].to_s, s[0],
@app_name, s[0].to_s, @namespace, nil, nil, nil)
container.stubs(:generate_ssh_key).returns('generate_ssh_key stub')
if s[1] and (s[2] == 100.0)
assert_raise OpenShift::Runtime::GearCreationException do
container.create
end
else
container.create
end
end
end
def test_standalone_proxy_repo
cartridge_model = mock()
cartridge_model.expects(:standalone_web_proxy?).returns(true)
@container.expects(:cartridge_model).returns(cartridge_model)
uuid1 = '1'
entry1 = {
uuid: "#{uuid1}",
namespace: 'bar',
dns: "#{uuid1}-bar.example.com",
proxy_hostname: 'node1.example.com',
proxy_port: '35571',
platform: 'windows'
}
web_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(entry1)
entries = {
:web => {
uuid1 => web_entry1,
}
}
gear_registry = mock()
gear_registry.expects(:entries).times(2).returns(entries)
@container.expects(:gear_registry).times(2).returns(gear_registry)
gear_env = {'FOO' => 'bar'}
::OpenShift::Runtime::Utils::Environ::expects(:for_gear).with(@container.container_dir).returns(gear_env)
ssh_url = "#{entry1[:uuid]}@#{entry1[:proxy_hostname]}"
location = PathUtils.join(@container.container_dir, 'mock', 'template')
@container.expects(:run_in_container_context).with("mkdir -p #{location} ; rsync -rOvS --exclude '.git' --delete --rsh=/usr/bin/oo-ssh #{ssh_url}:git/template/ #{location}",
env: gear_env,
chdir: @container.container_dir,
expected_exitstatus: 0)
repo = OpenShift::Runtime::ApplicationRepository.new(@container)
repo.destroy
repo.populate_from_cartridge('mock')
end
def test_remote_rsync_options
uuid1 = '1'
uuid2 = '2'
entry1 = {
uuid: "#{uuid1}",
namespace: 'bar',
dns: "#{uuid1}-bar.example.com",
proxy_hostname: 'node1.example.com',
proxy_port: '35571',
platform: 'linux'
}
entry2 = {
uuid: "#{uuid2}",
namespace: 'bar',
dns: "#{uuid2}-bar.example.com",
proxy_hostname: 'node1.example.com',
proxy_port: '35571',
platform: 'windows'
}
web_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(entry1)
web_entry2 = ::OpenShift::Runtime::GearRegistry::Entry.new(entry2)
entries = {
:web => {
uuid1 => web_entry1,
uuid2 => web_entry2,
}
}
gear_registry = mock()
gear_registry.expects(:entries).times(2).returns(entries)
@container.expects(:gear_registry).times(2).returns(gear_registry)
rsync_options = @container.send(:remote_rsync_options, uuid1)
assert_equal '-avzS', rsync_options
rsync_options = @container.send(:remote_rsync_options, uuid2)
assert_equal '-rltgoDOv', rsync_options
end
def test_deconfigure
@container.cartridge_model.expects(:deconfigure).with('mock-0.1')
@container.deconfigure('mock-0.1')
end
def test_unsubscribe
@container.cartridge_model.expects(:unsubscribe).with('mock-0.1', 'pub-cart')
@container.unsubscribe('mock-0.1', 'pub-cart')
end
def test_update_cluster_no_web_proxy
@container.cartridge_model.expects(:web_proxy).returns(nil)
::OpenShift::Runtime::Utils::Environ::expects(:for_gear).never
::OpenShift::Runtime::GearRegistry.expects(:new).never
@config.expects(:get).never
::OpenShift::Runtime::GearRegistry::Entry.expects(:new).never
@container.expects(:run_in_container_context).never
@container.expects(:current_deployment_datetime).never
@container.expects(:deployment_metadata_for).never
@container.expects(:activate).never
@container.cartridge_model.expects(:do_control).never
@container.update_cluster("", "", false, false)
end
def test_update_cluster_rollback
web_proxy = mock()
@container.cartridge_model.expects(:web_proxy).returns(web_proxy)
gear_env = {'OPENSHIFT_APP_DNS' => 'foo-bar.example.com', 'OPENSHIFT_GEAR_DNS' => 'foo-bar.example.com'}
::OpenShift::Runtime::Utils::Environ::expects(:for_gear).with(@container.container_dir).returns(gear_env)
gear_registry = mock()
@container.expects(:gear_registry).returns(gear_registry)
gear_registry.expects(:restore_from_backup)
args = "args"
@container.expects(:generate_update_cluster_control_args).with(nil).returns(args)
@container.cartridge_model.expects(:do_control).with('update-cluster', web_proxy, args: args)
@container.update_cluster("", "", true, false)
end
def test_update_cluster_add_gears
web_proxy = mock()
web_proxy.expects(:name).at_least_once.returns('foo')
primary_cartridge = mock()
primary_cartridge.expects(:name).at_least_once.returns('bar')
@container.cartridge_model.expects(:primary_cartridge).at_least_once.returns(primary_cartridge)
@container.cartridge_model.expects(:web_proxy).at_least_once.returns(web_proxy)
gear_env = {'OPENSHIFT_APP_DNS' => 'foo-bar.example.com', 'OPENSHIFT_GEAR_DNS' => '123-bar.example.com'}
::OpenShift::Runtime::Utils::Environ::expects(:for_gear).with(@container.container_dir).returns(gear_env)
gear_registry = mock()
@container.expects(:gear_registry).returns(gear_registry).at_least_once
@container.expects(:run_in_container_context).with("rsync -aAXS --rsh=/usr/bin/oo-ssh /var/lib/openshift/#{@gear_uuid}/.openshift_ssh/id_rsa{,.pub} #{@gear_uuid.to_i + 2}@node1.example.com:.openshift_ssh/",
env: gear_env,
chdir: @container.container_dir,
expected_exitstatus: 0)
uuid1 = @container.uuid
gear1 = {
uuid: uuid1,
namespace: 'bar',
dns: 'foo-bar.example.com',
proxy_hostname: 'node1.example.com',
proxy_port: '35561',
platform: 'linux'
}
web_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear1)
proxy_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear1.merge(proxy_port: 0))
uuid2 = (@container.uuid.to_i + 1).to_s
gear2 = {
uuid: uuid2,
namespace: 'bar',
dns: "#{uuid2}-bar.example.com",
proxy_hostname: 'node1.example.com',
proxy_port: '35566',
platform: 'linux'
}
web_entry2 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear2)
proxy_entry2 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear2.merge(proxy_port: 0))
uuid3 = (@container.uuid.to_i + 2).to_s
gear3 = {
uuid: uuid3,
namespace: 'bar',
dns: "#{uuid3}-bar.example.com",
proxy_hostname: 'node1.example.com',
proxy_port: '35571',
platform: 'linux'
}
web_entry3 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear3)
proxy_entry3 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear3.merge(proxy_port: 0))
old_entries = {
:web => {
uuid1 => web_entry1
},
:proxy => {
uuid1 => proxy_entry1
}
}
updated_entries = {
:web => {
uuid1 => web_entry1,
uuid2 => web_entry2,
uuid3 => web_entry3
},
:proxy => {
uuid1 => proxy_entry1,
uuid3 => proxy_entry3
}
}
gear_registry.expects(:backup)
gear_registry.expects(:entries).times(3).returns(old_entries, updated_entries)
gear_registry.expects(:clear)
@config.expects(:get).with('CLOUD_DOMAIN').returns('example.com')
web_uuids = [uuid1, uuid2, uuid3]
dns_entries = %W(foo #{uuid2} #{uuid3})
ports = %w(35561 35566 35571)
web_uuids.each_with_index do |uuid, index|
gear_registry.expects(:add).with(type: :web,
uuid: uuid,
namespace: "bar",
dns: "#{dns_entries[index]}-bar.example.com",
proxy_hostname: "node1.example.com",
proxy_port: ports[index],
platform: 'linux')
end
proxy_uuids = [uuid1, uuid3]
proxy_uuids.each_with_index do |uuid, index|
gear_registry.expects(:add).with(type: :proxy,
uuid: uuid,
namespace: "bar",
dns: "#{dns_entries[index]}-bar.example.com",
proxy_hostname: "node1.example.com",
proxy_port: 0,
platform: 'linux')
end
gear_registry.expects(:save)
[web_entry2, web_entry3].each do |new_entry|
@container.expects(:run_in_container_context).with("rsync -avzS --delete --rsh=/usr/bin/oo-ssh app-deployments/ #{new_entry.uuid}@#{new_entry.proxy_hostname}:app-deployments/",
env: gear_env,
chdir: @container.container_dir,
expected_exitstatus: 0)
end
current_deployment_datetime = '2013-08-16_13-36-36.880'
@container.expects(:current_deployment_datetime).returns(current_deployment_datetime)
deployment_id = 'abcd1234'
metadata = mock()
@container.expects(:deployment_metadata_for).with(current_deployment_datetime).returns(metadata)
metadata.expects(:id).returns(deployment_id)
@container.expects(:activate).with(gears: [uuid2, uuid3],
deployment_id: deployment_id,
post_install: true,
rotate: false)
.returns(status: 'success')
@container.expects(:run_in_container_context).with("rsync -avzS --delete --exclude hooks --rsh=/usr/bin/oo-ssh git/#{@app_name}.git/ #{proxy_entry3.uuid}@#{proxy_entry3.proxy_hostname}:git/#{@app_name}.git/",
env: gear_env,
chdir: @container.container_dir,
expected_exitstatus: 0)
do_control_args = web_uuids.each_with_index.map do |uuid, index|
"#{dns_entries[index]}-bar.example.com|node1.example.com:#{ports[index]}"
end.join(' ')
@container.cartridge_model.expects(:do_control).with('update-cluster', web_proxy, args: do_control_args)
proxy_arg = proxy_uuids.each_with_index.map do |uuid, index|
"#{uuid},#{dns_entries[index]},bar,node1.example.com,linux"
end.join(' ')
cluster_arg = web_uuids.each_with_index.map do |uuid, index|
"#{uuid},#{dns_entries[index]},bar,node1.example.com,#{ports[index]},linux"
end.join(' ')
@container.update_cluster(proxy_arg, cluster_arg, false, true)
end
def test_update_cluster_standalone_proxy
web_proxy = mock()
web_proxy.expects(:name).at_least_once.returns('foo')
@container.cartridge_model.expects(:primary_cartridge).at_least_once.returns(web_proxy)
@container.cartridge_model.expects(:web_proxy).at_least_once.returns(web_proxy)
gear_env = {'OPENSHIFT_APP_DNS' => 'foo-bar.example.com', 'OPENSHIFT_GEAR_DNS' => '123-bar.example.com'}
::OpenShift::Runtime::Utils::Environ::expects(:for_gear).twice.with(@container.container_dir).returns(gear_env)
gear_registry = mock()
@container.expects(:gear_registry).returns(gear_registry).at_least_once
uuid1 = @container.uuid
gear1 = {
uuid: uuid1,
namespace: 'bar',
dns: 'foo-bar.example.com',
proxy_hostname: 'node1.example.com',
proxy_port: '35561',
platform: 'windows'
}
location = PathUtils.join(@container.container_dir, 'foo', 'template')
ssh_url = "#{uuid1}@#{gear1[:proxy_hostname]}"
@container.expects(:run_in_container_context).with("mkdir -p #{location} ; rsync -rOvS --exclude '.git' --delete --rsh=/usr/bin/oo-ssh #{ssh_url}:git/template/ #{location}",
env: gear_env,
chdir: @container.container_dir,
expected_exitstatus: 0)
@container.expects(:setup_deployment)
@container.expects(:latest_deployment_datetime).returns('abc')
web_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear1)
proxy_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear1.merge(proxy_port: 0, platform: 'linux'))
entries = {
:web => {
uuid1 => web_entry1
},
:proxy => {
uuid1 => proxy_entry1
}
}
gear_registry.expects(:backup)
gear_registry.expects(:entries).times(4).returns(entries)
gear_registry.expects(:clear)
@config.expects(:get).with('CLOUD_DOMAIN').returns('example.com')
web_uuid = uuid1
dns_entry = 'foo'
port = '35561'
gear_registry.expects(:add).with(type: :web,
uuid: web_uuid,
namespace: "bar",
dns: "#{dns_entry}-bar.example.com",
proxy_hostname: "node1.example.com",
proxy_port: port,
platform: 'windows')
proxy_uuid = uuid1
gear_registry.expects(:add).with(type: :proxy,
uuid: proxy_uuid,
namespace: "bar",
dns: "#{dns_entry}-bar.example.com",
proxy_hostname: "node1.example.com",
proxy_port: 0,
platform: 'linux')
gear_registry.expects(:save)
do_control_args ="#{dns_entry}-bar.example.com|node1.example.com:#{port}"
@container.cartridge_model.expects(:do_control).with('update-cluster', web_proxy, args: do_control_args)
proxy_arg = "#{proxy_uuid},#{dns_entry},bar,node1.example.com,linux"
cluster_arg = "#{proxy_uuid},#{dns_entry},bar,node1.example.com,#{port},windows"
@container.update_cluster(proxy_arg, cluster_arg, false, true)
end
def test_update_cluster_add_multiple_gears_first_time
web_proxy = mock()
web_proxy.expects(:name).at_least_once.returns('foo')
primary_cartridge = mock()
primary_cartridge.expects(:name).at_least_once.returns('bar')
@container.cartridge_model.expects(:primary_cartridge).at_least_once.returns(primary_cartridge)
@container.cartridge_model.expects(:web_proxy).at_least_once.returns(web_proxy)
gear_env = {'OPENSHIFT_APP_DNS' => 'foo-bar.example.com', 'OPENSHIFT_GEAR_DNS' => '123-bar.example.com'}
::OpenShift::Runtime::Utils::Environ::expects(:for_gear).with(@container.container_dir).returns(gear_env)
gear_registry = mock()
@container.expects(:gear_registry).returns(gear_registry).at_least_once
uuid1 = @container.uuid
gear1 = {
uuid: uuid1,
namespace: 'bar',
dns: 'foo-bar.example.com',
proxy_hostname: 'node1.example.com',
proxy_port: '35561',
platform: 'linux'
}
web_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear1)
proxy_entry1 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear1.merge(proxy_port: 0))
uuid2 = (@container.uuid.to_i + 1).to_s
gear2 = {
uuid: uuid2,
namespace: 'bar',
dns: "#{uuid2}-bar.example.com",
proxy_hostname: 'node1.example.com',
proxy_port: '35566',
platform: 'linux'
}
web_entry2 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear2)
uuid3 = (@container.uuid.to_i + 2).to_s
gear3 = {
uuid: uuid3,
namespace: 'bar',
dns: "#{uuid3}-bar.example.com",
proxy_hostname: 'node1.example.com',
proxy_port: '35571',
platform: 'linux'
}
web_entry3 = ::OpenShift::Runtime::GearRegistry::Entry.new(gear3)
old_entries = {
}
updated_entries = {
:web => {
uuid1 => web_entry1,
uuid2 => web_entry2,
uuid3 => web_entry3
},
:proxy => {
uuid1 => proxy_entry1,
}
}
gear_registry.expects(:backup)
gear_registry.expects(:entries).times(2).returns(old_entries, updated_entries)
gear_registry.expects(:clear)
@config.expects(:get).with('CLOUD_DOMAIN').returns('example.com')
web_uuids = [uuid1, uuid2, uuid3]
dns_entries = %W(foo #{uuid2} #{uuid3})
ports = %w(35561 35566 35571)
web_uuids.each_with_index do |uuid, index|
gear_registry.expects(:add).with(type: :web,
uuid: uuid,
namespace: "bar",
dns: "#{dns_entries[index]}-bar.example.com",
proxy_hostname: "node1.example.com",
proxy_port: ports[index],
platform: 'linux')
end
proxy_uuids = [uuid1]
proxy_uuids.each_with_index do |uuid, index|
gear_registry.expects(:add).with(type: :proxy,
uuid: uuid,
namespace: "bar",
dns: "#{dns_entries[index]}-bar.example.com",
proxy_hostname: "node1.example.com",
proxy_port: 0,
platform: 'linux')
end
gear_registry.expects(:save)
[web_entry2, web_entry3].each do |new_entry|
@container.expects(:run_in_container_context).with("rsync -avzS --delete --rsh=/usr/bin/oo-ssh app-deployments/ #{new_entry.uuid}@#{new_entry.proxy_hostname}:app-deployments/",
env: gear_env,
chdir: @container.container_dir,
expected_exitstatus: 0)
end
current_deployment_datetime = '2013-08-16_13-36-36.880'
@container.expects(:current_deployment_datetime).returns(current_deployment_datetime)
deployment_id = 'abcd1234'
metadata = mock()
@container.expects(:deployment_metadata_for).with(current_deployment_datetime).returns(metadata)
metadata.expects(:id).returns(deployment_id)
@container.expects(:activate).with(gears: [uuid2, uuid3],
deployment_id: deployment_id,
post_install: true,
rotate: false)
.returns(status: 'success')
do_control_args = web_uuids.each_with_index.map do |uuid, index|
"#{dns_entries[index]}-bar.example.com|node1.example.com:#{ports[index]}"
end.join(' ')
@container.cartridge_model.expects(:do_control).with('update-cluster', web_proxy, args: do_control_args)
proxy_arg = proxy_uuids.each_with_index.map do |uuid, index|
"#{uuid},#{dns_entries[index]},bar,node1.example.com,linux"
end.join(' ')
cluster_arg = web_uuids.each_with_index.map do |uuid, index|
"#{uuid},#{dns_entries[index]},bar,node1.example.com,#{ports[index]},linux"
end.join(' ')
@container.update_cluster(proxy_arg, cluster_arg, false, true)
end
def test_with_gear_rotation_no_proxy_no_all
count = 0
yielded_target_gear = nil
yielded_local_gear_env = nil
gear_env = {a: 1}
::OpenShift::Runtime::Utils::Environ.expects(:for_gear).with(@container.container_dir).returns(gear_env)
@container.cartridge_model.expects(:web_proxy).returns(nil)
@container.expects(:calculate_batch_size).with(1, 0.2).returns(1)
Parallel.expects(:map).with([@container.uuid], :in_threads => 1).yields(@container.uuid)
with_gear_rotation_options = {b: 2}
@container.expects(:rotate_and_yield).with(@container.uuid, gear_env, with_gear_rotation_options).yields(@container.uuid, gear_env, with_gear_rotation_options)
@container.with_gear_rotation(with_gear_rotation_options) do |target_gear, local_gear_env|
count += 1
yielded_target_gear = target_gear
yielded_local_gear_env = local_gear_env
end
assert_equal 1, count
assert_equal @container.uuid, yielded_target_gear
assert_equal gear_env, yielded_local_gear_env
end
def test_with_gear_rotation_with_proxy_no_all
count = 0
yielded_target_gear = nil
yielded_local_gear_env = nil
gear_env = {a: 1}
::OpenShift::Runtime::Utils::Environ.expects(:for_gear).with(@container.container_dir).returns(gear_env)
proxy_cart = mock()
@container.cartridge_model.expects(:web_proxy).returns(proxy_cart)
gear_registry = mock()
@container.expects(:gear_registry).returns(gear_registry)
entry = mock()
entries = {:web => {@container.uuid => entry, 'a' => 'b'}}
gear_registry.expects(:entries).returns(entries)
@container.expects(:calculate_batch_size).with(1, 0.2).returns(1)
Parallel.expects(:map).with([entry], :in_threads => 1).yields(entry)
with_gear_rotation_options = {b: 2}
@container.expects(:rotate_and_yield).with(entry, gear_env, with_gear_rotation_options).yields(entry, gear_env, with_gear_rotation_options)
@container.with_gear_rotation(with_gear_rotation_options) do |target_gear, local_gear_env|
count += 1
yielded_target_gear = target_gear
yielded_local_gear_env = local_gear_env
end
assert_equal 1, count
assert_equal entry, yielded_target_gear
assert_equal gear_env, yielded_local_gear_env
end
def test_with_gear_rotation_proxy_and_all
count = 0
yielded_target_gears = []
yielded_local_gear_envs = []
gear_env = {a: 1}
::OpenShift::Runtime::Utils::Environ.expects(:for_gear).with(@container.container_dir).returns(gear_env)
proxy_cart = mock()
@container.cartridge_model.expects(:web_proxy).returns(proxy_cart)
gear_registry = mock()
@container.expects(:gear_registry).returns(gear_registry)
entry1 = mock()
entry2 = mock()
uuid2 = '5505'
entries = {:web => {@container.uuid => entry1, uuid2 => entry2}}
gear_registry.expects(:entries).returns(entries)