-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
test_servicemanagementservice.py
2724 lines (2303 loc) · 122 KB
/
test_servicemanagementservice.py
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
#-------------------------------------------------------------------------
# Copyright (c) Microsoft. All rights reserved.
#
# 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.
#--------------------------------------------------------------------------
import os
import time
import unittest
from datetime import datetime, timedelta
import azure.http.httpclient
from azure import (
_encode_base64,
)
from azure.servicemanagement import (
CaptureRoleAsVMImage,
CertificateSetting,
ConfigurationSet,
ConfigurationSetInputEndpoint,
KeyPair,
LinuxConfigurationSet,
Listener,
OSVirtualHardDisk,
PublicKey,
ResourceExtensionReference,
ResourceExtensionReferences,
ServiceManagementService,
VMImage,
WindowsConfigurationSet,
parse_response_for_async_op,
)
from azure.storage.blobservice import BlobService
from util import (
AzureTestCase,
create_service_management,
credentials,
getUniqueName,
set_service_options,
)
from time import sleep
# Enable these to view requests and responses
azure.http.httpclient.DEBUG_REQUESTS = False
azure.http.httpclient.DEBUG_RESPONSES = False
SERVICE_CERT_FORMAT = 'pfx'
SERVICE_CERT_PASSWORD = 'Python'
SERVICE_CERT_DATA = 'MIIJ7AIBAzCCCagGCSqGSIb3DQEHAaCCCZkEggmVMIIJkTCCBfoGCSqGSIb3DQEHAaCCBesEggXnMIIF4zCCBd8GCyqGSIb3DQEMCgECoIIE/jCCBPowHAYKKoZIhvcNAQwBAzAOBAhxOU59DvbmnAICB9AEggTYNM2UfOCtA1G0fhKNmu79z8/yUm5ybh5JamZqZ4Ra21wTc1khmVmWr0OAYhttaKtqtHfyFv7UY/cojg+fdOPCI+Fa8qQI7oXGEU7hS4O7VH3R/bDESctPB4TRdhjb88hLC+CdQc64PwjHFoaUHEQHFMsi7ujbi1u4Xg8YRqg4eKoG0AAraEQgy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DEwpQeXRob25UZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsjULNM53WPLkht1rbrDob/e4hZTHzj/hlLoBt2X3cNRc6dOPsMucxbMdchbCqAFa5RIaJvF5NDKqZuUSwq6bttD71twzy9bQ03EySOcRBad1VyqAZQ8DL8nUGSnXIUh+tpz4fDGM5f3Ly9NX8zfGqG3sT635rrFlUp3meJC+secCCwTLOOcIs3KQmuB+pMB5Y9rPhoxcekFfpq1pKtis6pmxnVbiL49kr6UUL6RQRDwik4t1jttatXLZqHETTmXl0Y0wS5AcJUXVAn5AL2kybULoThop2v01/E0NkPtFPAqLVs/kKBahniNn9uwUo+LS9FA8rWGu0FY4CZEYDfhb+QIDAQABo0owSDBGBgNVHQEEPzA9gBBS6knRHo54LppngxVCCzZVoRcwFTETMBEGA1UEAxMKUHl0aG9uVGVzdIIQ00IFaqV9VqVJxI+wZka0szAJBgUrDgMCHQUAA4IBAQAnZbP3YV+08wI4YTg6MOVA+j1njd0kVp35FLehripmaMNE6lgk3Vu1MGGl0JnvMr3fNFGFzRske/jVtFxlHE5H/CoUzmyMQ+W06eV/e995AduwTKsS0ZgYn0VoocSXWst/nyhpKOcbJgAOohOYxgsGI1JEqQgjyeqzcCIhw/vlWiA3V8bSiPnrC9vwhH0eB025hBd2VbEGDz2nWCYkwtuOLMTvkmLi/oFw3GOfgagZKk8k/ZPffMCafz+yR3vb1nqAjncrVcJLI8amUfpxhjZYexo8MbxBA432M6w8sjXN+uLCl7ByWZ4xs4vonWgkmjeObtU37SIzolHT4dxIgaP2'
SERVICE_CERT_THUMBPRINT = 'BEA4B74BD6B915E9DD6A01FB1B8C3C1740F517F2'
SERVICE_CERT_THUMBALGO = 'sha1'
DEPLOYMENT_ORIGINAL_CONFIG = '''<ServiceConfiguration serviceName="WindowsAzure1" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="2" osVersion="*" schemaVersion="2012-05.1.7">
<Role name="WorkerRole1">
<Instances count="2" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>'''
DEPLOYMENT_UPDATE_CONFIG = '''<ServiceConfiguration serviceName="WindowsAzure1" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="2" osVersion="*" schemaVersion="2012-05.1.7">
<Role name="WorkerRole1">
<Instances count="4" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>'''
CSPKG_PATH = 'data/WindowsAzure1.cspkg'
DATA_VHD_PATH = 'data/testhd'
# This blob must be created manually before running the unit tests,
# they must be present in the storage account listed in the credentials file.
LINUX_OS_VHD_URL = credentials.getLinuxOSVHD()
# The easiest way to create a Linux OS vhd is to use the Azure management
# portal to create a Linux VM, and have it store the VHD in the
# storage account listed in the credentials file. Then stop the VM,
# and use the following code to copy the VHD to another blob (if you
# try to use the VM's VHD directly without making a copy, you will get
# conflict errors).
# sourceblob = '/{0}/{1}/{2}'.format(credentials.getStorageServicesName(), 'vhdcontainername', 'vhdblobname')
# self.bc.copy_blob('vhdcontainername', 'targetvhdblobname', sourceblob)
#
# in the credentials file, set:
# "linuxosvhd" : "http://storageservicesname.blob.core.windows.net/vhdcontainername/targetvhdblobname",
#------------------------------------------------------------------------------
class ServiceManagementServiceTest(AzureTestCase):
def setUp(self):
self.sms = create_service_management(ServiceManagementService)
self.bc = BlobService(credentials.getStorageServicesName(),
credentials.getStorageServicesKey())
set_service_options(self.bc)
self.hosted_service_name = getUniqueName('utsvc')
self.container_name = getUniqueName('utctnr')
self.disk_name = getUniqueName('utdisk')
self.os_image_name = getUniqueName('utosimg')
self.data_disk_info = None
self.reserved_ip_address = None
def tearDown(self):
if self.data_disk_info is not None:
try:
disk = self.sms.get_data_disk(
self.data_disk_info[0], self.data_disk_info[1],
self.data_disk_info[2], self.data_disk_info[3])
try:
result = self.sms.delete_data_disk(
self.data_disk_info[0], self.data_disk_info[1],
self.data_disk_info[2], self.data_disk_info[3])
self._wait_for_async(result.request_id)
except:
pass
try:
self.sms.delete_disk(disk.disk_name)
except:
pass
except:
pass
disk_names = [self.disk_name]
try:
# Can't delete a hosted service if it has deployments, so delete
# those first
props = self.sms.get_hosted_service_properties(
self.hosted_service_name, True)
for deployment in props.deployments:
try:
for role in deployment.role_list:
role_props = self.sms.get_role(
self.hosted_service_name,
deployment.name,
role.role_name)
if role_props.os_virtual_hard_disk.disk_name \
not in disk_names:
disk_names.append(
role_props.os_virtual_hard_disk.disk_name)
except:
pass
try:
result = self.sms.delete_deployment(
self.hosted_service_name, deployment.name)
self._wait_for_async(result.request_id)
except:
pass
self.sms.delete_hosted_service(self.hosted_service_name)
except:
pass
try:
result = self.sms.delete_os_image(self.os_image_name)
self._wait_for_async(result.request_id)
except:
pass
for disk_name in disk_names:
try:
self.sms.delete_disk(disk_name)
except:
pass
try:
self.bc.delete_container(self.container_name)
except:
pass
if self.reserved_ip_address:
try:
result = self.sms.delete_reserved_ip_address(self.reserved_ip_address)
self._wait_for_async(result.request_id)
except:
pass
#--Helpers-----------------------------------------------------------------
def _wait_for_async(self, request_id):
self.sms.wait_for_operation_status(request_id, timeout=600)
def _wait_for_deployment(self, service_name, deployment_name,
status='Running'):
count = 0
props = self.sms.get_deployment_by_name(service_name, deployment_name)
while props.status != status:
count = count + 1
if count > 120:
self.assertTrue(
False, 'Timed out waiting for deployment status.')
time.sleep(5)
props = self.sms.get_deployment_by_name(
service_name, deployment_name)
def _wait_for_role(self, service_name, deployment_name, role_instance_name,
status='ReadyRole'):
count = 0
props = self.sms.get_deployment_by_name(service_name, deployment_name)
while self._get_role_instance_status(props, role_instance_name) != status:
count = count + 1
if count > 120:
self.assertTrue(
False, 'Timed out waiting for role instance status.')
time.sleep(5)
props = self.sms.get_deployment_by_name(
service_name, deployment_name)
def _wait_for_rollback_allowed(self, service_name, deployment_name):
count = 0
props = self.sms.get_deployment_by_name(service_name, deployment_name)
while props.rollback_allowed == False:
count = count + 1
if count > 120:
self.assertTrue(
False, 'Timed out waiting for rollback allowed.')
time.sleep(5)
props = self.sms.get_deployment_by_name(
service_name, deployment_name)
def _get_role_instance_status(self, deployment, role_instance_name):
for role_instance in deployment.role_instance_list:
if role_instance.instance_name == role_instance_name:
return role_instance.instance_status
return None
def _create_hosted_service(self, name, location=None, affinity_group=None):
if not location and not affinity_group:
location = 'West US'
result = self.sms.create_hosted_service(
name,
name + 'label',
name + 'description',
location,
affinity_group,
{'ext1': 'val1', 'ext2': 42})
self._wait_for_async(result.request_id)
def _hosted_service_exists(self, name):
try:
props = self.sms.get_hosted_service_properties(name)
return props is not None
except:
return False
def _create_service_certificate(self, service_name, data, format,
password):
result = self.sms.add_service_certificate(service_name, data,
format, password)
self._wait_for_async(result.request_id)
def _service_certificate_exists(self, service_name, thumbalgorithm,
thumbprint):
try:
props = self.sms.get_service_certificate(
service_name, thumbalgorithm, thumbprint)
return props is not None
except:
return False
def _deployment_exists(self, service_name, deployment_name):
try:
props = self.sms.get_deployment_by_name(
service_name, deployment_name)
return props is not None
except:
return False
def _make_blob_url(self, storage_account_name, container_name, blob_name):
return 'http://{0}.blob.core.windows.net/{1}/{2}'.format(
storage_account_name,
container_name,
blob_name
)
def _create_container_and_block_blob(self, container_name, blob_name,
blob_data):
self.bc.create_container(container_name, None, 'container', False)
resp = self.bc.put_blob(
container_name, blob_name, blob_data, 'BlockBlob')
self.assertIsNone(resp)
def _create_container_and_page_blob(self, container_name, blob_name,
content_length):
self.bc.create_container(container_name, None, 'container', False)
resp = self.bc.put_blob(container_name, blob_name, b'',
'PageBlob',
x_ms_blob_content_length=str(content_length))
self.assertIsNone(resp)
def _upload_file_to_block_blob(self, file_path, blob_name):
data = open(file_path, 'rb').read()
url = self._make_blob_url(credentials.getStorageServicesName(),
self.container_name, blob_name)
self._create_container_and_block_blob(
self.container_name, blob_name, data)
return url
def _upload_chunks(self, file_path, blob_name, chunk_size):
index = 0
with open(file_path, 'rb') as f:
while True:
data = f.read(chunk_size)
if data:
length = len(data)
self.bc.put_page(
self.container_name, blob_name, data,
'bytes=' + str(index) + '-' + str(index + length - 1),
'update')
index += length
else:
break
def _upload_file_to_page_blob(self, file_path, blob_name):
url = self._make_blob_url(credentials.getStorageServicesName(),
self.container_name, blob_name)
content_length = os.path.getsize(file_path)
self._create_container_and_page_blob(
self.container_name, blob_name, content_length)
self._upload_chunks(file_path, blob_name, 262144)
return url
def _upload_default_package_to_storage_blob(self, blob_name):
return self._upload_file_to_block_blob(CSPKG_PATH, blob_name)
def _upload_disk_to_storage_blob(self, blob_name):
return self._upload_file_to_page_blob(DATA_VHD_PATH, blob_name)
def _add_deployment(self, service_name, deployment_name,
deployment_slot='Production'):
configuration = _encode_base64(DEPLOYMENT_ORIGINAL_CONFIG)
package_url = self._upload_default_package_to_storage_blob(
deployment_name + 'Blob')
result = self.sms.create_deployment(
service_name, deployment_slot, deployment_name, package_url,
deployment_name + 'label', configuration, False, False,
{'dep1': 'val1', 'dep2': 'val2'})
self._wait_for_async(result.request_id)
def _create_hosted_service_with_deployment(self, service_name,
deployment_name):
self._create_hosted_service(service_name)
self._add_deployment(service_name, deployment_name)
def _role_exists(self, service_name, deployment_name, role_name):
try:
props = self.sms.get_role(service_name, deployment_name, role_name)
return props is not None
except:
return False
def _create_disk(self, disk_name, os, url):
result = self.sms.add_disk(None, disk_name, url, disk_name, os)
self.assertIsNone(result)
def _disk_exists(self, disk_name):
try:
disk = self.sms.get_disk(disk_name)
return disk is not None
except:
return False
def _create_os_image(self, name, blob_url, os):
result = self.sms.add_os_image(name + 'label', blob_url, name, os)
self._wait_for_async(result.request_id)
def _os_image_exists(self, image_name):
try:
image = self.sms.get_os_image(image_name)
return image is not None
except:
return False
def _blob_exists(self, container_name, blob_name):
try:
props = self.bc.get_blob_properties(container_name, blob_name)
return props is not None
except:
return False
def _data_disk_exists(self, service_name, deployment_name, role_name, lun):
try:
props = self.sms.get_data_disk(
service_name, deployment_name, role_name, lun)
return props is not None
except:
return False
def _add_data_disk_from_blob_url(self, service_name, deployment_name,
role_name, lun, label):
url = self._upload_disk_to_storage_blob('disk')
result = self.sms.add_data_disk(
service_name, deployment_name, role_name, lun, None, None, label,
None, None, url)
self._wait_for_async(result.request_id)
def _linux_image_name(self):
return self._image_from_publisher_name('Canonical')
def _windows_image_name(self):
return self._image_from_publisher_name('Microsoft Windows Server Group')
def _host_name_from_role_name(self, role_name):
return 'hn' + role_name[-13:]
def _image_from_publisher_name(self, publisher):
# return the first one listed, which should be the most stable
return [i.name for i in self.sms.list_os_images() \
if publisher in i.publisher_name][0]
def _windows_role(self, role_name, subnet_name=None, port='59913'):
host_name = self._host_name_from_role_name(role_name)
system = self._windows_config(host_name)
os_hd = self._os_hd(self._windows_image_name(),
self.container_name,
role_name + '.vhd')
network = self._network_config(subnet_name, port)
return (system, os_hd, network)
def _linux_role(self, role_name, subnet_name=None, port='59913'):
host_name = self._host_name_from_role_name(role_name)
system = self._linux_config(host_name)
os_hd = self._os_hd(self._linux_image_name(),
self.container_name,
role_name + '.vhd')
network = self._network_config(subnet_name, port)
return (system, os_hd, network)
def _windows_config(self, hostname):
system = WindowsConfigurationSet(
hostname, 'u7;9jbp!', False, False, 'Pacific Standard Time',
'azureuser')
system.domain_join = None
system.stored_certificate_settings.stored_certificate_settings.append(
CertificateSetting(SERVICE_CERT_THUMBPRINT, 'My', 'LocalMachine'))
listener = Listener('Https', SERVICE_CERT_THUMBPRINT)
system.win_rm.listeners.listeners.append(listener)
return system
def _linux_config(self, hostname):
pk = PublicKey(SERVICE_CERT_THUMBPRINT,
u'/home/unittest/.ssh/authorized_keys')
pair = KeyPair(SERVICE_CERT_THUMBPRINT, u'/home/unittest/.ssh/id_rsa')
system = LinuxConfigurationSet(hostname, 'unittest', 'u7;9jbp!', True)
system.ssh.public_keys.public_keys.append(pk)
system.ssh.key_pairs.key_pairs.append(pair)
system.disable_ssh_password_authentication = False
return system
def _network_config(self, subnet_name=None, port='59913'):
network = ConfigurationSet()
network.configuration_set_type = 'NetworkConfiguration'
network.input_endpoints.input_endpoints.append(
ConfigurationSetInputEndpoint('utendpoint', 'tcp', port, '3394'))
if subnet_name:
network.subnet_names.append(subnet_name)
return network
def _os_hd(self, image_name, target_container_name, target_blob_name):
media_link = self._make_blob_url(
credentials.getStorageServicesName(),
target_container_name, target_blob_name)
os_hd = OSVirtualHardDisk(image_name, media_link,
disk_label=target_blob_name)
return os_hd
def _create_vm_linux(self, service_name, deployment_name, role_name):
self._create_hosted_service(service_name)
self._create_service_certificate(
service_name,
SERVICE_CERT_DATA, SERVICE_CERT_FORMAT, SERVICE_CERT_PASSWORD)
system, os_hd, network = self._linux_role(role_name)
result = self.sms.create_virtual_machine_deployment(
service_name, deployment_name, 'production',
deployment_name + 'label', role_name, system, os_hd,
network, role_size='Small')
self._wait_for_async(result.request_id)
self._wait_for_deployment(service_name, deployment_name)
self._wait_for_role(service_name, deployment_name, role_name)
self._assert_role_instance_endpoint(
service_name, deployment_name, 'utendpoint', 'tcp', '59913', '3394')
def _create_vm_windows(self, service_name, deployment_name, role_name):
self._create_hosted_service(service_name)
self._create_service_certificate(
service_name,
SERVICE_CERT_DATA, SERVICE_CERT_FORMAT, SERVICE_CERT_PASSWORD)
system, os_hd, network = self._windows_role(role_name)
result = self.sms.create_virtual_machine_deployment(
service_name, deployment_name, 'production',
deployment_name + 'label', role_name, system, os_hd,
network, role_size='Small')
self._wait_for_async(result.request_id)
self._wait_for_deployment(service_name, deployment_name)
self._wait_for_role(service_name, deployment_name, role_name)
self._assert_role_instance_endpoint(
service_name, deployment_name, 'utendpoint', 'tcp', '59913', '3394')
def _assert_role_instance_endpoint(self, service_name, deployment_name,
endpoint_name, protocol,
public_port, local_port):
deployment = self.sms.get_deployment_by_name(
service_name, deployment_name)
self.assertEqual(len(deployment.role_instance_list), 1)
role_instance = deployment.role_instance_list[0]
self.assertEqual(len(role_instance.instance_endpoints), 1)
endpoint = role_instance.instance_endpoints[0]
self.assertEqual(endpoint.name, endpoint_name)
self.assertEqual(endpoint.protocol, protocol)
self.assertEqual(endpoint.public_port, public_port)
self.assertEqual(endpoint.local_port, local_port)
def _add_role_windows(self, service_name, deployment_name, role_name, port):
system, os_hd, network = self._windows_role(role_name, port=port)
result = self.sms.add_role(service_name, deployment_name, role_name,
system, os_hd, network)
self._wait_for_async(result.request_id)
self._wait_for_role(service_name, deployment_name, role_name)
def _create_reserved_ip_address(self):
self.reserved_ip_address = getUniqueName('ip')
result = self.sms.create_reserved_ip_address(
self.reserved_ip_address,
'mylabel',
'West US')
self._wait_for_async(result.request_id)
def _reserved_ip_address_exists(self, name):
try:
result = self.sms.get_reserved_ip_address(name)
return result is not None
except:
return False
def _create_vm_image(self, image_name):
media_link = self._copy_linux_os_vhd_to_container()
img = VMImage()
img.name = image_name
img.label = image_name + 'label'
img.description = image_name + 'description'
img.os_disk_configuration.os_state = 'Specialized'
img.os_disk_configuration.os = 'Linux'
img.os_disk_configuration.media_link = media_link
img.language = 'English'
img.show_in_gui = True
result = self.sms.create_vm_image(img)
self._wait_for_async(result.request_id)
def _copy_linux_os_vhd_to_container(self):
blob_name = 'imagecopy.vhd'
self.bc.create_container(self.container_name,
x_ms_blob_public_access='blob')
resp = self.bc.copy_blob(self.container_name, blob_name,
credentials.getLinuxOSVHD())
return self.bc.make_blob_url(self.container_name, blob_name)
#--Test cases for http passthroughs --------------------------------------
def test_perform_get(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
# Act
response = self.sms.perform_get(
'/{0}/services/hostedservices/{1}'.format(
credentials.getSubscriptionId(),
self.hosted_service_name
)
)
# Assert
self.assertEqual(response.status, 200)
self.assertEqual(response.message, 'OK')
self.assertGreater(len(response.body), 0)
self.assertNotEqual(
response.body.decode().find(self.hosted_service_name), -1)
def test_perform_post(self):
# Arrange
# Act
xml = '''<?xml version="1.0" encoding="utf-8"?>
<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure">
<ServiceName>{0}</ServiceName>
<Label>{1}</Label>
<Description>{2}</Description>
<Location>{3}</Location>
</CreateHostedService>'''
response = self.sms.perform_post(
'/{0}/services/hostedservices'.format(
credentials.getSubscriptionId()
),
xml.format(
self.hosted_service_name,
_encode_base64(self.hosted_service_name + 'label'),
'mydescription',
'West US'
)
)
async = parse_response_for_async_op(response)
self._wait_for_async(async.request_id)
# Assert
self.assertEqual(response.status, 201)
self.assertEqual(response.message, 'Created')
self.assertTrue(self._hosted_service_exists(self.hosted_service_name))
def test_perform_put(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
# Act
xml = '''<?xml version="1.0" encoding="utf-8"?>
<UpdateHostedService xmlns="http://schemas.microsoft.com/windowsazure">
<Label>{0}</Label>
</UpdateHostedService>'''
response = self.sms.perform_put(
'/{0}/services/hostedservices/{1}'.format(
credentials.getSubscriptionId(),
self.hosted_service_name
),
xml.format(
_encode_base64(self.hosted_service_name + 'new')
)
)
async = parse_response_for_async_op(response)
self._wait_for_async(async.request_id)
# Assert
self.assertEqual(response.status, 200)
self.assertEqual(response.message, 'OK')
props = self.sms.get_hosted_service_properties(self.hosted_service_name)
self.assertEqual(props.hosted_service_properties.label,
self.hosted_service_name + 'new')
def test_perform_delete(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
# Act
response = self.sms.perform_delete(
'/{0}/services/hostedservices/{1}'.format(
credentials.getSubscriptionId(),
self.hosted_service_name
)
)
async = parse_response_for_async_op(response)
self._wait_for_async(async.request_id)
# Assert
self.assertEqual(response.status, 200)
self.assertEqual(response.message, 'OK')
self.assertFalse(self._hosted_service_exists(self.hosted_service_name))
#--Test cases for subscriptions --------------------------------------
def test_list_role_sizes(self):
# Arrange
# Act
result = self.sms.list_role_sizes()
# Assert
self.assertIsNotNone(result)
self.assertTrue(len(result) > 0)
role_size = result[0]
self.assertTrue(len(role_size.name) > 0)
self.assertTrue(len(role_size.label) > 0)
self.assertTrue(role_size.cores > 0)
self.assertTrue(role_size.max_data_disk_count > 0)
self.assertTrue(role_size.memory_in_mb > 0)
self.assertTrue(role_size.virtual_machine_resource_disk_size_in_mb > 0)
self.assertTrue(role_size.web_worker_resource_disk_size_in_mb > 0)
self.assertIsInstance(role_size.supported_by_virtual_machines, bool)
self.assertIsInstance(role_size.supported_by_web_worker_roles, bool)
@unittest.skip('Can only be used with oauth')
def test_list_subscriptions(self):
# Arrange
# Act
result = self.sms.list_subscriptions()
# Assert
self.assertIsNotNone(result)
self.assertTrue(len(result) > 0)
#--Test cases for hosted services ------------------------------------
def test_list_hosted_services(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
# Act
result = self.sms.list_hosted_services()
# Assert
self.assertIsNotNone(result)
self.assertTrue(len(result) > 0)
service = None
for temp in result:
if temp.service_name == self.hosted_service_name:
service = temp
break
self.assertIsNotNone(service)
self.assertIsNotNone(service.service_name)
self.assertIsNotNone(service.url)
self.assertIsNotNone(service.hosted_service_properties)
self.assertIsNotNone(service.hosted_service_properties.affinity_group)
self.assertIsNotNone(service.hosted_service_properties.date_created)
self.assertIsNotNone(
service.hosted_service_properties.date_last_modified)
self.assertIsNotNone(service.hosted_service_properties.description)
self.assertIsNotNone(service.hosted_service_properties.label)
self.assertIsNotNone(service.hosted_service_properties.location)
self.assertIsNotNone(service.hosted_service_properties.status)
self.assertIsNotNone(
service.hosted_service_properties.extended_properties['ext1'])
self.assertIsNotNone(
service.hosted_service_properties.extended_properties['ext2'])
self.assertIsNone(service.deployments)
def test_get_hosted_service_properties(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
# Act
result = self.sms.get_hosted_service_properties(
self.hosted_service_name)
# Assert
self.assertIsNotNone(result)
self.assertIsNotNone(result.service_name)
self.assertIsNotNone(result.url)
self.assertIsNotNone(result.hosted_service_properties)
self.assertIsNotNone(result.hosted_service_properties.affinity_group)
self.assertIsNotNone(result.hosted_service_properties.date_created)
self.assertIsNotNone(
result.hosted_service_properties.date_last_modified)
self.assertIsNotNone(result.hosted_service_properties.description)
self.assertIsNotNone(result.hosted_service_properties.label)
self.assertIsNotNone(result.hosted_service_properties.location)
self.assertIsNotNone(result.hosted_service_properties.status)
self.assertIsNotNone(
result.hosted_service_properties.extended_properties['ext1'])
self.assertIsNotNone(
result.hosted_service_properties.extended_properties['ext2'])
self.assertIsNone(result.deployments)
def test_get_hosted_service_properties_with_embed_detail(self):
# Arrange
deployment_name = 'utdeployment'
self._create_hosted_service_with_deployment(
self.hosted_service_name, deployment_name)
# Act
result = self.sms.get_hosted_service_properties(
self.hosted_service_name, True)
# Assert
self.assertIsNotNone(result)
self.assertIsNotNone(result.service_name)
self.assertIsNotNone(result.url)
self.assertIsNotNone(result.hosted_service_properties)
self.assertIsNotNone(result.hosted_service_properties.affinity_group)
self.assertIsNotNone(result.hosted_service_properties.date_created)
self.assertIsNotNone(
result.hosted_service_properties.date_last_modified)
self.assertIsNotNone(result.hosted_service_properties.description)
self.assertIsNotNone(result.hosted_service_properties.label)
self.assertIsNotNone(result.hosted_service_properties.location)
self.assertIsNotNone(result.hosted_service_properties.status)
self.assertIsNotNone(
result.hosted_service_properties.extended_properties['ext1'])
self.assertIsNotNone(
result.hosted_service_properties.extended_properties['ext2'])
self.assertIsNotNone(result.deployments)
self.assertIsNotNone(result.deployments[0].configuration)
self.assertIsNotNone(result.deployments[0].created_time)
self.assertIsNotNone(result.deployments[0].deployment_slot)
self.assertIsNotNone(result.deployments[0].extended_properties['dep1'])
self.assertIsNotNone(result.deployments[0].extended_properties['dep2'])
self.assertIsNotNone(result.deployments[0].label)
self.assertIsNotNone(result.deployments[0].last_modified_time)
self.assertFalse(result.deployments[0].locked)
self.assertEqual(result.deployments[0].name, deployment_name)
self.assertIsNone(result.deployments[0].persistent_vm_downtime_info)
self.assertIsNotNone(result.deployments[0].private_id)
self.assertIsNotNone(result.deployments[0].role_list[0].os_version)
self.assertEqual(result.deployments[0]
.role_list[0].role_name, 'WorkerRole1')
self.assertFalse(result.deployments[0].rollback_allowed)
self.assertIsNotNone(result.deployments[0].sdk_version)
self.assertIsNotNone(result.deployments[0].status)
self.assertIsNotNone(result.deployments[0].upgrade_domain_count)
self.assertIsNone(result.deployments[0].upgrade_status)
self.assertIsNotNone(result.deployments[0].url)
self.assertIsNotNone(result.deployments[0].role_instance_list[0].fqdn)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].instance_error_code)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].instance_fault_domain)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].instance_name)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].instance_size)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].instance_state_details)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].instance_status)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].instance_upgrade_domain)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].ip_address)
self.assertIsNotNone(
result.deployments[0].role_instance_list[0].power_state)
self.assertEqual(
result.deployments[0].role_instance_list[0].role_name, 'WorkerRole1')
def test_create_hosted_service(self):
# Arrange
label = 'pythonlabel'
description = 'python hosted service description'
location = 'West US'
# Act
result = self.sms.create_hosted_service(
self.hosted_service_name, label, description, location, None,
{'ext1': 'val1', 'ext2': 'val2'})
self._wait_for_async(result.request_id)
# Assert
self.assertTrue(self._hosted_service_exists(self.hosted_service_name))
def test_update_hosted_service(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
label = 'ptvslabelupdate'
description = 'ptvs description update'
# Act
result = self.sms.update_hosted_service(
self.hosted_service_name, label, description,
{'ext1': 'val1update', 'ext2': 'val2update', 'ext3': 'brandnew'})
# Assert
self.assertIsNone(result)
props = self.sms.get_hosted_service_properties(
self.hosted_service_name)
self.assertEqual(props.hosted_service_properties.label, label)
self.assertEqual(
props.hosted_service_properties.description, description)
self.assertEqual(
props.hosted_service_properties.extended_properties['ext1'],
'val1update')
self.assertEqual(
props.hosted_service_properties.extended_properties['ext2'],
'val2update')
self.assertEqual(
props.hosted_service_properties.extended_properties['ext3'],
'brandnew')
def test_delete_hosted_service(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
# Act
result = self.sms.delete_hosted_service(self.hosted_service_name)
self._wait_for_async(result.request_id)
# Assert
self.assertFalse(self._hosted_service_exists(self.hosted_service_name))
def test_get_deployment_by_slot(self):
# Arrange
deployment_name = 'utdeployment'
self._create_hosted_service_with_deployment(
self.hosted_service_name, deployment_name)
# Act
result = self.sms.get_deployment_by_slot(
self.hosted_service_name, 'Production')
# Assert
self.assertIsNotNone(result)
self.assertEqual(result.name, deployment_name)
self.assertEqual(result.deployment_slot, 'Production')
self.assertIsNotNone(result.label)
self.assertIsNotNone(result.configuration)
def test_get_deployment_by_name(self):
# Arrange
deployment_name = 'utdeployment'
self._create_hosted_service_with_deployment(
self.hosted_service_name, deployment_name)
# Act
result = self.sms.get_deployment_by_name(
self.hosted_service_name, deployment_name)
# Assert
self.assertIsNotNone(result)
self.assertEqual(result.name, deployment_name)
self.assertEqual(result.deployment_slot, 'Production')
self.assertIsNotNone(result.label)
self.assertIsNotNone(result.configuration)
def test_create_deployment(self):
# Arrange
self._create_hosted_service(self.hosted_service_name)
configuration = _encode_base64(DEPLOYMENT_ORIGINAL_CONFIG)
package_url = self._upload_default_package_to_storage_blob(
'WindowsAzure1Blob')
# Act
result = self.sms.create_deployment(
self.hosted_service_name, 'production', 'WindowsAzure1',
package_url, 'deploylabel', configuration)
self._wait_for_async(result.request_id)
# Assert
self.assertTrue(
self._deployment_exists(self.hosted_service_name, 'WindowsAzure1'))
def test_delete_deployment(self):
# Arrange
deployment_name = 'utdeployment'
self._create_hosted_service_with_deployment(
self.hosted_service_name, deployment_name)
# Act
result = self.sms.delete_deployment(
self.hosted_service_name, deployment_name)
self._wait_for_async(result.request_id)
# Assert
self.assertFalse(
self._deployment_exists(self.hosted_service_name, deployment_name))
def test_delete_deployment_with_vhd(self):
# Arrange
service_name = self.hosted_service_name
deployment_name = self.hosted_service_name
role_name = self.hosted_service_name
self._create_vm_linux(service_name, deployment_name, role_name)
self.assertTrue(
self._blob_exists(self.container_name, role_name + '.vhd'))
# Act
result = self.sms.delete_deployment(
self.hosted_service_name, deployment_name, delete_vhd=True)
self._wait_for_async(result.request_id)
# Assert
# Blob is taking 30 minutes to delete currently after the async id comes back
timeToSleep = 30 * 60
count = 0
while self._blob_exists(self.container_name, role_name + '.vhd'):
if count >= timeToSleep:
self.assertFalse(True, "Blob exists")
else:
sleep(5)
count += 5
print("Time to run:", count)
self.assertFalse(
self._deployment_exists(self.hosted_service_name, deployment_name))