Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Piddubny committed Nov 14, 2016
1 parent df0e2a9 commit 81c034b
Showing 1 changed file with 42 additions and 63 deletions.
105 changes: 42 additions & 63 deletions package/tests/test_cp/test_azure/test_azure_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def setUp(self, delete_azure_vm_operation, refresh_ip_operation, power_azure_vm_

@mock.patch("cloudshell.cp.azure.azure_shell.ValidatorsFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.CloudShellSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientsManager")
@mock.patch("cloudshell.cp.azure.azure_shell.LoggingSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.ErrorHandlingContext")
def test_deploy_azure_vm(self, error_handling_class, logging_context_class, azure_clients_factory_context_class,
def test_deploy_azure_vm(self, error_handling_class, logging_context_class, azure_clients_manager_class,
cloudshell_session_context_class, validators_factory_context_class):
"""Check that method uses ErrorHandlingContext and deploy_azure_vm_operation.deploy method"""
# mock Cloudshell Session
Expand All @@ -48,11 +48,8 @@ def test_deploy_azure_vm(self, error_handling_class, logging_context_class, azur
error_handling = mock.MagicMock()
error_handling_class.return_value = error_handling
# mock Azure clients
azure_client = mock.MagicMock()
azure_client_factory = mock.MagicMock()
azure_client_factory.get_client.return_value = azure_client
azure_clients_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=azure_client_factory))
azure_clients_factory_context_class.return_value = azure_clients_factory_context
azure_clients_manager = mock.MagicMock()
azure_clients_manager_class.return_value = azure_clients_manager
# mock ValidatorsFactoryContext
validator_factory = mock.MagicMock()
validator_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=validator_factory))
Expand All @@ -78,30 +75,27 @@ def test_deploy_azure_vm(self, error_handling_class, logging_context_class, azur
azure_vm_deployment_model=azure_vm_deployment_model,
cloud_provider_model=cloud_provider_model,
reservation=reservation,
network_client=azure_client,
compute_client=azure_client,
storage_client=azure_client,
network_client=azure_clients_manager.network_client,
compute_client=azure_clients_manager.compute_client,
storage_client=azure_clients_manager.storage_client,
validator_factory=validator_factory)

@mock.patch("cloudshell.cp.azure.azure_shell.jsonpickle")
@mock.patch("cloudshell.cp.azure.azure_shell.DeployDataHolder")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientsManager")
@mock.patch("cloudshell.cp.azure.azure_shell.LoggingSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.ErrorHandlingContext")
def test_prepare_connectivity(self, error_handling_class, logging_context_class,
azure_clients_factory_context_class, deploy_data_holder_class, jsonpickle):
azure_clients_manager_class, deploy_data_holder_class, jsonpickle):
"""Check that method uses ErrorHandlingContext and prepare_connectivity_operation"""
# mock LoggingSessionContext and ErrorHandlingContext
logging_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=self.logger))
logging_context_class.return_value = logging_context
error_handling = mock.MagicMock()
error_handling_class.return_value = error_handling
# mock Azure clients
azure_client = mock.MagicMock()
azure_client_factory = mock.MagicMock()
azure_client_factory.get_client.return_value = azure_client
azure_clients_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=azure_client_factory))
azure_clients_factory_context_class.return_value = azure_clients_factory_context
azure_clients_manager = mock.MagicMock()
azure_clients_manager_class.return_value = azure_clients_manager
# mock Resource Group name
reservation = mock.MagicMock()
self.azure_shell.model_parser.convert_to_reservation_model.return_value = reservation
Expand All @@ -124,32 +118,29 @@ def test_prepare_connectivity(self, error_handling_class, logging_context_class,
self.azure_shell.prepare_connectivity_operation.prepare_connectivity.assert_called_once_with(
reservation=reservation,
cloud_provider_model=cloud_provider_model,
storage_client=azure_client,
resource_client=azure_client,
network_client=azure_client,
storage_client=azure_clients_manager.storage_client,
resource_client=azure_clients_manager.resource_client,
network_client=azure_clients_manager.network_client,
logger=self.logger,
request=deploy_data_holder.driverRequest)

self.azure_shell.command_result_parser.set_command_result.assert_called_once_with(
{'driverResponse': {'actionResults': prepare_connectivity_result}})

@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientsManager")
@mock.patch("cloudshell.cp.azure.azure_shell.LoggingSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.ErrorHandlingContext")
def test_cleanup_connectivity(self, error_handling_class, logging_context_class,
azure_clients_factory_context_class):
azure_clients_manager_class):
"""Check that method uses ErrorHandlingContext and delete_azure_vm_operation"""
# mock LoggingSessionContext and ErrorHandlingContext
logging_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=self.logger))
logging_context_class.return_value = logging_context
error_handling = mock.MagicMock()
error_handling_class.return_value = error_handling
# mock Azure clients
azure_client = mock.MagicMock()
azure_client_factory = mock.MagicMock()
azure_client_factory.get_client.return_value = azure_client
azure_clients_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=azure_client_factory))
azure_clients_factory_context_class.return_value = azure_clients_factory_context
azure_clients_manager = mock.MagicMock()
azure_clients_manager_class.return_value = azure_clients_manager

command_context = mock.MagicMock(reservation=mock.MagicMock(reservation_id=self.group_name))
cloud_provider_model = mock.MagicMock()
Expand All @@ -163,30 +154,27 @@ def test_cleanup_connectivity(self, error_handling_class, logging_context_class,
error_handling_class.assert_called_once_with(self.logger)

self.azure_shell.delete_azure_vm_operation.delete_resource_group.assert_called_once_with(
resource_client=azure_client,
resource_client=azure_clients_manager.resource_client,
group_name=self.group_name)

self.azure_shell.delete_azure_vm_operation.delete_sandbox_subnet.assert_called_once_with(
network_client=azure_client,
network_client=azure_clients_manager.network_client,
cloud_provider_model=cloud_provider_model,
resource_group_name=self.group_name)

@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientsManager")
@mock.patch("cloudshell.cp.azure.azure_shell.LoggingSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.ErrorHandlingContext")
def test_delete_azure_vm(self, error_handling_class, logging_context_class, azure_clients_factory_context_class):
def test_delete_azure_vm(self, error_handling_class, logging_context_class, azure_clients_manager_class):
"""Check that method uses ErrorHandlingContext and delete_azure_vm_operation.delete method"""
# mock LoggingSessionContext and ErrorHandlingContext
logging_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=self.logger))
logging_context_class.return_value = logging_context
error_handling = mock.MagicMock()
error_handling_class.return_value = error_handling
# mock Azure clients
azure_client = mock.MagicMock()
azure_client_factory = mock.MagicMock()
azure_client_factory.get_client.return_value = azure_client
azure_clients_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=azure_client_factory))
azure_clients_factory_context_class.return_value = azure_clients_factory_context
azure_clients_manager = mock.MagicMock()
azure_clients_manager_class.return_value = azure_clients_manager
# mock VM name
data_holder = mock.MagicMock()
vm_custom_param = mock.MagicMock()
Expand All @@ -208,17 +196,17 @@ def test_delete_azure_vm(self, error_handling_class, logging_context_class, azur
error_handling_class.assert_called_once_with(self.logger)

self.azure_shell.delete_azure_vm_operation.delete.assert_called_once_with(
compute_client=azure_client,
network_client=azure_client,
compute_client=azure_clients_manager.compute_client,
network_client=azure_clients_manager.network_client,
group_name=self.group_name,
vm_name=self.vm_name,
logger=self.logger)

@mock.patch("cloudshell.cp.azure.azure_shell.CloudShellSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientsManager")
@mock.patch("cloudshell.cp.azure.azure_shell.LoggingSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.ErrorHandlingContext")
def test_power_on_vm(self, error_handling_class, logging_context_class, azure_clients_factory_context_class,
def test_power_on_vm(self, error_handling_class, logging_context_class, azure_clients_manager_class,
cloudshell_session_context_class):
"""Check that method uses ErrorHandlingContext and power_vm_operation.power_on method"""
# mock Cloudshell Session
Expand All @@ -231,11 +219,8 @@ def test_power_on_vm(self, error_handling_class, logging_context_class, azure_cl
error_handling = mock.MagicMock()
error_handling_class.return_value = error_handling
# mock Azure clients
azure_client = mock.MagicMock()
azure_client_factory = mock.MagicMock()
azure_client_factory.get_client.return_value = azure_client
azure_clients_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=azure_client_factory))
azure_clients_factory_context_class.return_value = azure_clients_factory_context
azure_clients_manager = mock.MagicMock()
azure_clients_manager_class.return_value = azure_clients_manager
# mock VM name
data_holder = mock.MagicMock()
data_holder.name = self.vm_name
Expand All @@ -254,18 +239,18 @@ def test_power_on_vm(self, error_handling_class, logging_context_class, azure_cl
error_handling_class.assert_called_once_with(self.logger)

self.azure_shell.power_vm_operation.power_on.assert_called_once_with(
compute_client=azure_client,
compute_client=azure_clients_manager.compute_client,
resource_group_name=self.group_name,
vm_name=self.vm_name)

cloudshell_session.SetResourceLiveStatus.assert_called_once_with(
command_context.remote_endpoints[0].fullname, "Online", "Active")

@mock.patch("cloudshell.cp.azure.azure_shell.CloudShellSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientsManager")
@mock.patch("cloudshell.cp.azure.azure_shell.LoggingSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.ErrorHandlingContext")
def test_power_off_vm(self, error_handling_class, logging_context_class, azure_clients_factory_context_class,
def test_power_off_vm(self, error_handling_class, logging_context_class, azure_clients_manager_class,
cloudshell_session_context_class):
"""Check that method uses ErrorHandlingContext and power_vm_operation.power_off method"""
# mock Cloudshell Session
Expand All @@ -278,11 +263,8 @@ def test_power_off_vm(self, error_handling_class, logging_context_class, azure_c
error_handling = mock.MagicMock()
error_handling_class.return_value = error_handling
# mock Azure clients
azure_client = mock.MagicMock()
azure_client_factory = mock.MagicMock()
azure_client_factory.get_client.return_value = azure_client
azure_clients_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=azure_client_factory))
azure_clients_factory_context_class.return_value = azure_clients_factory_context
azure_clients_manager = mock.MagicMock()
azure_clients_manager_class.return_value = azure_clients_manager
# mock VM name
data_holder = mock.MagicMock()
data_holder.name = self.vm_name
Expand All @@ -301,18 +283,18 @@ def test_power_off_vm(self, error_handling_class, logging_context_class, azure_c
error_handling_class.assert_called_once_with(self.logger)

self.azure_shell.power_vm_operation.power_off.assert_called_once_with(
compute_client=azure_client,
compute_client=azure_clients_manager.compute_client,
resource_group_name=self.group_name,
vm_name=self.vm_name)

cloudshell_session.SetResourceLiveStatus.assert_called_once_with(
command_context.remote_endpoints[0].fullname, "Offline", "Powered Off")

@mock.patch("cloudshell.cp.azure.azure_shell.CloudShellSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientFactoryContext")
@mock.patch("cloudshell.cp.azure.azure_shell.AzureClientsManager")
@mock.patch("cloudshell.cp.azure.azure_shell.LoggingSessionContext")
@mock.patch("cloudshell.cp.azure.azure_shell.ErrorHandlingContext")
def test_refresh_ip(self, error_handling_class, logging_context_class, azure_clients_factory_context_class,
def test_refresh_ip(self, error_handling_class, logging_context_class, azure_clients_manager_class,
cloudshell_session_context_class):
"""Check that method uses ErrorHandlingContext and refresh_ip_operation.refresh_ip method"""
# mock Cloudshell Session
Expand All @@ -325,11 +307,8 @@ def test_refresh_ip(self, error_handling_class, logging_context_class, azure_cli
error_handling = mock.MagicMock()
error_handling_class.return_value = error_handling
# mock Azure clients
azure_client = mock.MagicMock()
azure_client_factory = mock.MagicMock()
azure_client_factory.get_client.return_value = azure_client
azure_clients_factory_context = mock.MagicMock(__enter__=mock.MagicMock(return_value=azure_client_factory))
azure_clients_factory_context_class.return_value = azure_clients_factory_context
azure_clients_manager = mock.MagicMock()
azure_clients_manager_class.return_value = azure_clients_manager
# mock VM name
data_holder = mock.MagicMock()
data_holder.name = self.vm_name
Expand All @@ -356,8 +335,8 @@ def test_refresh_ip(self, error_handling_class, logging_context_class, azure_cli

self.azure_shell.refresh_ip_operation.refresh_ip.assert_called_once_with(
cloudshell_session=cloudshell_session,
compute_client=azure_client,
network_client=azure_client,
compute_client=azure_clients_manager.compute_client,
network_client=azure_clients_manager.network_client,
resource_group_name=self.group_name,
vm_name=self.vm_name,
private_ip_on_resource=private_ip,
Expand Down

0 comments on commit 81c034b

Please sign in to comment.