@@ -197,7 +197,8 @@ def test_enable_disable(self):
197197
198198class SerialconsoleAdminCommandsTest (LiveScenarioTest ):
199199
200- def check_result (self , resource_group_name , vm_vmss_name , vmss_instanceid = None , message = "" ):
200+ def check_result (self , resource_group_name , vm_vmss_name , vmss_instanceid = None , message = "" , hasManagedStorageAccount = False ):
201+ print ("Checking serial console output for message: " , message )
201202 ARM_ENDPOINT = "https://management.azure.com"
202203 RP_PROVIDER = "Microsoft.SerialConsole"
203204 subscription_id = self .get_subscription_id ()
@@ -215,21 +216,66 @@ def check_result(self, resource_group_name, vm_vmss_name, vmss_instanceid=None,
215216 headers = {'authorization' : "Bearer " + access_token ,
216217 'accept' : application_json_format ,
217218 'content-type' : application_json_format }
218- result = requests .post (connection_url , headers = headers )
219- json_results = json .loads (result .text )
220- self .assertTrue (result .status_code ==
221- 200 and "connectionString" in json_results )
219+
220+ postRetryCounter = 1
221+
222+ while True :
223+ result = requests .post (connection_url , headers = headers )
224+ json_results = json .loads (result .text )
225+
226+ if result .status_code == 200 and "connectionString" in json_results :
227+ break
228+ else :
229+ print ("Failed to get connection string from serial console connect endpoint." )
230+ print ("Status code: " , result .status_code )
231+ print ("Response text: " , result .text )
232+
233+ if postRetryCounter > 3 :
234+ self .fail ("Failed to get connection string from serial console connect endpoint after retrying multiple times... Failing test. See status and response of retries in logs." )
235+ else :
236+ postRetryCounter += 1
237+ time .sleep (10 )
238+
222239 websocket_url = json_results ["connectionString" ]
223240
224241 ws = websocket .WebSocket ()
225242 ws .connect (websocket_url + "?authorization=" + access_token , timeout = 30 )
243+ print ("WebSocket connected for verifying message." )
244+ print ("Sleeping 60 seconds to allow 'Connecting...' message to appear." )
245+ time .sleep (60 )
246+
247+ if not hasManagedStorageAccount :
248+ print ("Sending access token to start custom storage account setup..." )
249+ ws .send (access_token )
250+ print ("Finished sending access token" )
251+
226252 buffer = ""
253+ iter = 0
254+ print ("Starting to read from WebSocket to find message..." )
227255 while True :
228- try :
229- buffer += ws .recv ()
230- except (websocket .WebSocketTimeoutException , websocket .WebSocketConnectionClosedException ):
256+ iter += 1
257+ print (f"Current timestamp: { time .strftime ('%X' )} , current iteration: { iter } /5" )
258+
259+ while True :
260+ try :
261+ buffer += ws .recv ()
262+ except (websocket .WebSocketTimeoutException , websocket .WebSocketConnectionClosedException ):
263+ break
264+
265+ if message in buffer :
266+ print ("Found message in buffer! Finished verification." )
231267 break
232-
268+
269+ print (f"Message not found yet in buffer, current buffer: { buffer } " )
270+
271+ if iter >= 10 :
272+ print ("Max retries reached, exiting read loop." )
273+ break
274+ else :
275+ print ("Sleeping 10 seconds before retrying..." )
276+ time .sleep (10 )
277+
278+ ws .close ()
233279 assert message in buffer
234280
235281 @ResourceGroupPreparer (name_prefix = 'cli_test_serialconsole' , location = 'westus2' )
@@ -243,27 +289,13 @@ def test_send_sysrq_VMSS(self, resource_group, storage_account):
243289 'urn' : 'Ubuntu2204' ,
244290 'loc' : 'westus2'
245291 })
246- self .cmd (
247- 'az vmss create -g {rg} -n {name} --image {urn} --instance-count 2 -l {loc} --orchestration-mode uniform' )
248- self .cmd ('az vmss update --name {name} --resource-group {rg} --set virtualMachineProfile.diagnosticsProfile="{{\\ "bootDiagnostics\\ ": {{\\ "Enabled\\ " : \\ "True\\ ",\\ "StorageUri\\ ":\\ "https://{sa}.blob.core.windows.net/\\ "}}}}"' )
249- result = self .cmd (
250- 'vmss list-instances --resource-group {rg} --name {name} --query "[].instanceId"' ).get_output_in_json ()
251- self .kwargs .update ({'id' : result [1 ]})
252- self .cmd (
253- 'az vmss update-instances -g {rg} -n {name} --instance-ids {id}' )
254- time .sleep (60 )
255- for i in range (5 ):
256- try :
257- self .cmd ('vmss get-instance-view --resource-group {rg} --name {name} --instance-id {id}' , checks = [
258- self .check ('statuses[0].code' ,
259- 'ProvisioningState/succeeded' ),
260- self .check ('statuses[1].code' , 'PowerState/running' ),
261- ])
262- break
263- except JMESPathCheckAssertionError :
264- time .sleep (30 )
292+ result = self .createVMSS ()
293+ print ("Sending SysRq..." )
294+ stopwatch_start = time .time ()
265295 self .cmd (
266296 'serial-console send sysrq -g {rg} -n {name} --instance-id {id} --input h' )
297+ stopwatch_end = time .time ()
298+ print (f"SysRq sent in { stopwatch_end - stopwatch_start } seconds." )
267299 self .check_result (resource_group , name ,
268300 vmss_instanceid = result [1 ], message = "sysrq: HELP" )
269301
@@ -278,27 +310,13 @@ def test_send_nmi_VMSS(self, resource_group, storage_account):
278310 'urn' : 'Ubuntu2204' ,
279311 'loc' : 'westus2'
280312 })
281- self .cmd (
282- 'az vmss create -g {rg} -n {name} --image {urn} --instance-count 2 -l {loc} --orchestration-mode uniform' )
283- self .cmd ('az vmss update --name {name} --resource-group {rg} --set virtualMachineProfile.diagnosticsProfile="{{\\ "bootDiagnostics\\ ": {{\\ "Enabled\\ " : \\ "True\\ ",\\ "StorageUri\\ ":\\ "https://{sa}.blob.core.windows.net/\\ "}}}}"' )
284- result = self .cmd (
285- 'vmss list-instances --resource-group {rg} --name {name} --query "[].instanceId"' ).get_output_in_json ()
286- self .kwargs .update ({'id' : result [1 ]})
287- self .cmd (
288- 'az vmss update-instances -g {rg} -n {name} --instance-ids {id}' )
289- time .sleep (60 )
290- for i in range (5 ):
291- try :
292- self .cmd ('vmss get-instance-view --resource-group {rg} --name {name} --instance-id {id}' , checks = [
293- self .check ('statuses[0].code' ,
294- 'ProvisioningState/succeeded' ),
295- self .check ('statuses[1].code' , 'PowerState/running' ),
296- ])
297- break
298- except JMESPathCheckAssertionError :
299- time .sleep (30 )
313+ result = self .createVMSS ()
314+ print ("Sending NMI..." )
315+ stopwatch_start = time .time ()
300316 self .cmd (
301317 'serial-console send nmi -g {rg} -n {name} --instance-id {id}' )
318+ stopwatch_end = time .time ()
319+ print (f"NMI sent in { stopwatch_end - stopwatch_start } seconds." )
302320 self .check_result (resource_group , name ,
303321 vmss_instanceid = result [1 ], message = "NMI received" )
304322
@@ -313,26 +331,14 @@ def test_send_reset_VMSS(self, resource_group, storage_account):
313331 'urn' : 'Ubuntu2204' ,
314332 'loc' : 'westus2'
315333 })
316- self .cmd (
317- 'az vmss create -g {rg} -n {name} --image {urn} --instance-count 2 -l {loc} --orchestration-mode uniform' )
318- self .cmd ('az vmss update --name {name} --resource-group {rg} --set virtualMachineProfile.diagnosticsProfile="{{\\ "bootDiagnostics\\ ": {{\\ "Enabled\\ " : \\ "True\\ ",\\ "StorageUri\\ ":\\ "https://{sa}.blob.core.windows.net/\\ "}}}}"' )
319- result = self .cmd (
320- 'vmss list-instances --resource-group {rg} --name {name} --query "[].instanceId"' ).get_output_in_json ()
321- self .kwargs .update ({'id' : result [1 ]})
322- self .cmd (
323- 'az vmss update-instances -g {rg} -n {name} --instance-ids {id}' )
324- time .sleep (60 )
325- for i in range (5 ):
326- try :
327- self .cmd ('vmss get-instance-view --resource-group {rg} --name {name} --instance-id {id}' , checks = [
328- self .check ('statuses[0].code' ,
329- 'ProvisioningState/succeeded' ),
330- self .check ('statuses[1].code' , 'PowerState/running' ),
331- ])
332- break
333- except JMESPathCheckAssertionError :
334- time .sleep (30 )
334+ result = self .createVMSS ()
335+ print ("Sending Reset..." )
336+ stopwatch_start = time .time ()
335337 self .cmd ('serial-console send reset -g {rg} -n {name} --instance-id {id}' )
338+ stopwatch_end = time .time ()
339+ print (f"Reset sent in { stopwatch_end - stopwatch_start } seconds." )
340+ self .check_result (resource_group , name ,
341+ vmss_instanceid = result [1 ], message = "Record successful boot" )
336342
337343 @ResourceGroupPreparer (name_prefix = 'cli_test_serialconsole' , location = 'westus2' )
338344 @StorageAccountPreparer (name_prefix = 'cli' , location = "westus2" )
@@ -345,21 +351,12 @@ def test_send_nmi_VM(self, resource_group, storage_account):
345351 'urn' : 'Ubuntu2204' ,
346352 'loc' : 'westus2'
347353 })
348- self .cmd (
349- 'az vm create -g {rg} -n {name} --image {urn} --boot-diagnostics-storage {sa} -l {loc} --generate-ssh-keys' )
350- time .sleep (60 )
351- for i in range (5 ):
352- try :
353- self .cmd ('vm get-instance-view --resource-group {rg} --name {name}' , checks = [
354- self .check (
355- 'instanceView.statuses[0].code' , 'ProvisioningState/succeeded' ),
356- self .check (
357- 'instanceView.statuses[1].code' , 'PowerState/running' ),
358- ])
359- break
360- except JMESPathCheckAssertionError :
361- time .sleep (30 )
354+ self .createVM ()
355+ print ("Sending NMI..." )
356+ stopwatch_start = time .time ()
362357 self .cmd ('serial-console send nmi -g {rg} -n {name}' )
358+ stopwatch_end = time .time ()
359+ print (f"NMI sent in { stopwatch_end - stopwatch_start } seconds." )
363360 self .check_result (resource_group , name , message = "NMI received" )
364361
365362 @ResourceGroupPreparer (name_prefix = 'cli_test_serialconsole' , location = 'westus2' )
@@ -373,21 +370,12 @@ def test_send_sysrq_VM(self, resource_group, storage_account):
373370 'urn' : 'Ubuntu2204' ,
374371 'loc' : 'westus2'
375372 })
376- self .cmd (
377- 'az vm create -g {rg} -n {name} --image {urn} --boot-diagnostics-storage {sa} -l {loc} --generate-ssh-keys' )
378- time .sleep (60 )
379- for i in range (5 ):
380- try :
381- self .cmd ('vm get-instance-view --resource-group {rg} --name {name}' , checks = [
382- self .check (
383- 'instanceView.statuses[0].code' , 'ProvisioningState/succeeded' ),
384- self .check (
385- 'instanceView.statuses[1].code' , 'PowerState/running' ),
386- ])
387- break
388- except JMESPathCheckAssertionError :
389- time .sleep (30 )
373+ self .createVM ()
374+ print ("Sending Sysrq..." )
375+ stopwatch_start = time .time ()
390376 self .cmd ('serial-console send sysrq -g {rg} -n {name} --input h' )
377+ stopwatch_end = time .time ()
378+ print (f"Sysrq sent in { stopwatch_end - stopwatch_start } seconds." )
391379 self .check_result (resource_group , name , message = "sysrq: HELP" )
392380
393381 @ResourceGroupPreparer (name_prefix = 'cli_test_serialconsole' , location = 'westus2' )
@@ -401,6 +389,38 @@ def test_send_reset_VM(self, resource_group, storage_account):
401389 'urn' : 'Ubuntu2204' ,
402390 'loc' : 'westus2'
403391 })
392+ self .createVM ()
393+ print ("Sending Reset..." )
394+ stopwatch_start = time .time ()
395+ self .cmd ('serial-console send reset -g {rg} -n {name}' )
396+ stopwatch_end = time .time ()
397+ print (f"Reset sent in { stopwatch_end - stopwatch_start } seconds." )
398+ self .check_result (resource_group , name , message = "Record successful boot" )
399+
400+ def createVMSS (self ):
401+ self .cmd (
402+ 'az vmss create -g {rg} -n {name} --image {urn} --instance-count 2 -l {loc} --orchestration-mode uniform' )
403+ self .cmd (
404+ 'az vmss update --name {name} --resource-group {rg} --set virtualMachineProfile.diagnosticsProfile="{{\\ "bootDiagnostics\\ ": {{\\ "Enabled\\ " : \\ "True\\ ",\\ "StorageUri\\ ":\\ "https://{sa}.blob.core.windows.net/\\ "}}}}"' )
405+ result = self .cmd (
406+ 'vmss list-instances --resource-group {rg} --name {name} --query "[].instanceId"' ).get_output_in_json ()
407+ self .kwargs .update ({'id' : result [1 ]})
408+ self .cmd (
409+ 'az vmss update-instances -g {rg} -n {name} --instance-ids {id}' )
410+ time .sleep (60 )
411+ for i in range (5 ):
412+ try :
413+ self .cmd ('vmss get-instance-view --resource-group {rg} --name {name} --instance-id {id}' , checks = [
414+ self .check ('statuses[0].code' ,
415+ 'ProvisioningState/succeeded' ),
416+ self .check ('statuses[1].code' , 'PowerState/running' ),
417+ ])
418+ break
419+ except JMESPathCheckAssertionError :
420+ time .sleep (30 )
421+ return result
422+
423+ def createVM (self ):
404424 self .cmd (
405425 'az vm create -g {rg} -n {name} --image {urn} --boot-diagnostics-storage {sa} -l {loc} --generate-ssh-keys' )
406426 time .sleep (60 )
@@ -415,4 +435,3 @@ def test_send_reset_VM(self, resource_group, storage_account):
415435 break
416436 except JMESPathCheckAssertionError :
417437 time .sleep (30 )
418- self .cmd ('serial-console send reset -g {rg} -n {name}' )
0 commit comments