Skip to content

Commit

Permalink
fix unittests + bug in cal to _wait_for_ip
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeleta committed Jun 1, 2017
1 parent 9b7bc34 commit b97ab17
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
21 changes: 11 additions & 10 deletions sandbox_scripts/environment/setup/setup_VM.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ def __init__(self):
log_group=self.reservation_id,
log_category='Setup')

# ---------------------------
# ---------------------------
def execute(self):
self.sandbox = SandboxBase(self.reservation_id, self.logger)
#TODO: don't use networking save and restore to figure if it's a snapshot setup
self.is_snapshot = False
save_n_restore_mgr = SaveRestoreManager(self.sandbox)
if save_n_restore_mgr.get_storage_manager():
if save_n_restore_mgr.is_snapshot():
self.is_snapshot = True
# ---------------------------
# ---------------------------
def execute(self):

self.sandbox.report_info('Beginning VMs power on')
self._run_async_power_on_refresh_ip()

Expand Down Expand Up @@ -71,13 +72,13 @@ def _power_on_refresh_ip(self, lock, message_status, resource):
:return:
"""

deployed_app_name = resource.name
#deployed_app_name = resource.name
run_result = rsc_run_result_struct(resource.name)
power_on = "true"
wait_for_ip = "true"

if resource.model.lower() == "vcenter static vm":
self.logger.debug("Resource {0} is a static app".format(deployed_app_name))
self.logger.debug("Resource {0} is a static app".format(resource.name))
wait_for_ip = "false"
elif not self.is_snapshot:
return True, ""
Expand All @@ -87,19 +88,19 @@ def _power_on_refresh_ip(self, lock, message_status, resource):
except Exception as exc:
self.sandbox.report_error("Error powering on deployed app '{0}' in reservation '{1}'. Error: {2}"
.format(deployed_app_name, self.reservation_id, str(exc)),raise_error=False)
run_result.message = str("Error powering on deployed app '{0}'").format(deployed_app_name)
run_result.message = str("Error powering on deployed app '{0}'").format(resource.name)
run_result.run_result = False
return run_result
try:
if wait_for_ip.lower() == "true":
self._wait_for_ip(deployed_app_name, lock, message_status)
self._wait_for_ip(resource, lock, message_status)
else:
self.sandbox.report_info("Wait For IP is off for deployed app '{0}' in reservation '{1}'"
.format(deployed_app_name, self.reservation_id))
.format(resource.name, self.reservation_id))
except Exception as exc:
self.sandbox.report_error("Error refreshing IP on deployed app '{0}' in reservation '{1}'. Error: {2}"
.format(deployed_app_name, self.reservation_id, str(exc)),raise_error=False)
run_result.message = str("Error refreshing IP deployed app '{0}'. Error: {1}").format(deployed_app_name, exc.message)
.format(resource.name, self.reservation_id, str(exc)),raise_error=False)
run_result.message = str("Error refreshing IP deployed app '{0}'. Error: {1}").format(resource.name, exc.message)
run_result.run_result = False


Expand Down
34 changes: 19 additions & 15 deletions sandbox_scripts/environment/setup/tests/test_setup_VM.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ def resource_details_mock_side_effect(name):

self.setup_script.execute()

report_info_calls = [call('Beginning VMs power on')]
report_info_calls = [call('Beginning VMs power on'),call(message='No VMs to power on ', write_to_output_window=True)]
mock_sandboxbase.return_value.report_info.assert_has_calls(report_info_calls)

logger_debug_calls = [call("Skipping resource 'r1' - not an app, not powering on"),
call("Skipping resource 'r2' - not an app, not powering on")]
self.setup_script.logger.debug.assert_has_calls(logger_debug_calls, any_order=True)

@patch('cloudshell.helpers.scripts.cloudshell_scripts_helpers.get_api_session')
@patch('sandbox_scripts.environment.setup.setup_VM.SandboxBase')
Expand All @@ -108,6 +105,11 @@ def resource_details_mock_side_effect(name):

mock_sandboxbase.return_value.api_session.GetResourceDetails.side_effect = resource_details_mock_side_effect

resourcebase1 = Mock()
resourcebase1.name = 'r2'
resourcebase1.model = 'vcenter static vm'
mock_sandboxbase.return_value.get_root_vm_resources.return_value = [resourcebase1]

mock_save.return_value.is_snapshot.return_value = False

self.setup_script.execute()
Expand All @@ -116,18 +118,16 @@ def resource_details_mock_side_effect(name):
call('Apps are powering on... '),
call(log_message="Executing 'Power On' on deployed app 'r2' in reservation "
"'5487c6ce-d0b3-43e9-8ee7-e27af8406905'",
message="Executing 'Power On' on deployed app 'r2' ", write_to_output_window=True)]
message="Executing 'Power On' on deployed app 'r2' ", write_to_output_window=True),
call("Wait For IP is off for deployed app 'r2' in reservation "
"'5487c6ce-d0b3-43e9-8ee7-e27af8406905'")]
mock_sandboxbase.return_value.report_info.assert_has_calls(report_info_calls)

logger_debug_calls = [call("Resource r2 is a static app")]
self.setup_script.logger.debug.assert_has_calls(logger_debug_calls)

logger_info_calls = [call("Wait For IP is off for deployed app 'r2' in reservation "
"'5487c6ce-d0b3-43e9-8ee7-e27af8406905'")]
self.setup_script.logger.info.assert_has_calls(logger_info_calls)

api_calls = [call('5487c6ce-d0b3-43e9-8ee7-e27af8406905', 'r2', 'PowerOn', 'power')]
mock_sandboxbase.return_value.api_session.ExecuteResourceConnectedCommand.assert_has_calls(api_calls)
api_calls = [call.execute_connected_command(u'5487c6ce-d0b3-43e9-8ee7-e27af8406905', 'PowerOn', 'power')]
resourcebase1.assert_has_calls(api_calls)


@patch('cloudshell.helpers.scripts.cloudshell_scripts_helpers.get_api_session')
Expand All @@ -154,6 +154,11 @@ def resource_details_mock_side_effect(name):

mock_sandboxbase.return_value.api_session.GetResourceDetails.side_effect = resource_details_mock_side_effect

resourcebase1 = Mock()
resourcebase1.name = 'r2'
resourcebase1.model = 'deployed app'
mock_sandboxbase.return_value.get_root_vm_resources.return_value = [resourcebase1]

mock_save.return_value.is_snapshot.return_value = True

self.setup_script.execute()
Expand All @@ -167,10 +172,9 @@ def resource_details_mock_side_effect(name):
write_to_output_window=True)]
mock_sandboxbase.return_value.report_info.assert_has_calls(report_info_calls)

api_calls = [call('5487c6ce-d0b3-43e9-8ee7-e27af8406905', 'r2', 'PowerOn', 'power'),
call('5487c6ce-d0b3-43e9-8ee7-e27af8406905', 'r2', 'remote_refresh_ip', 'remote_connectivity')]
mock_sandboxbase.return_value.api_session.ExecuteResourceConnectedCommand.assert_has_calls(api_calls)

api_calls = [call.execute_connected_command(u'5487c6ce-d0b3-43e9-8ee7-e27af8406905', 'PowerOn', 'power'),
call.execute_connected_command(u'5487c6ce-d0b3-43e9-8ee7-e27af8406905', 'remote_refresh_ip', 'remote_connectivity')]
resourcebase1.assert_has_calls(api_calls)

if __name__ == '__main__':
unittest.main()

0 comments on commit b97ab17

Please sign in to comment.