def test_should_get_by_storage_volume_attachment_uri( self, mock_ansible_module, mock_from_json): mock_ov_instance = mock.Mock() mock_ov_instance.storage_volume_attachments.get.return_value = { "name": "Storage Volume Attachment Name" } mock_ov_instance.storage_volume_attachments.URI = "/rest/storage-volume-attachments" mock_from_json.return_value = mock_ov_instance params_get_by_uri = dict( config='config.json', storageVolumeAttachmentUri= "/rest/storage-volume-attachments/ED247E27-011B-44EB-9E1B-5891011") mock_ansible_instance = create_ansible_mock(params_get_by_uri) mock_ansible_module.return_value = mock_ansible_instance StorageVolumeAttachmentFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(storage_volume_attachments=( [{ "name": "Storage Volume Attachment Name" }])))
def test_should_update_with_current_switches_and_group_when_not_provided( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.logical_switches.get_by.side_effect = [[ LOGICAL_SWITCH_FROM_ONEVIEW ], []] mock_ov_instance.logical_switches.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_UPDATE) mock_ansible_module.return_value = mock_ansible_instance LogicalSwitchModule().run() data_for_update = { 'logicalSwitch': { 'name': 'Test Logical Switch - Renamed', 'uri': '/rest/logical-switches/f0d7ad37-2053-46ac-bb11-4ebdd079bb66', 'logicalSwitchGroupUri': '/rest/logical-switch-groups/af370d9a-f2f4-4beb-a1f1-670930d6741d', 'switchCredentialConfiguration': [{ 'logicalSwitchManagementHost': '172.16.1.1' }, { 'logicalSwitchManagementHost': '172.16.1.2' }], }, 'logicalSwitchCredentials': [] } mock_ov_instance.logical_switches.update.assert_called_once_with( data_for_update)
def test_should_turn_off_hardware_before_delete(self, mock_ansible_module, mock_ov_from_file): sh_uri = '/rest/server-hardware/37333036-3831-76jh-4831-303658389766' profile_data = deepcopy(BASIC_PROFILE) profile_data['serverHardwareUri'] = sh_uri mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = profile_data mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, ServerProfileModule().run()) mock_ov_instance.server_hardware.update_power_state.assert_called_once_with( { 'powerControl': 'PressAndHold', 'powerState': 'Off' }, sh_uri) mock_ov_instance.server_profiles.delete.assert_called_once_with( profile_data) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_DELETED)
def test_should_gather_facts_about_interconnect_subport_statistics(self, mock_ansible_module, mock_ov_from_file): fake_statistics = dict(name='test') mock_ov_instance = mock.Mock() mock_ov_instance.interconnects.get_by.return_value = MOCK_INTERCONNECTS mock_ov_instance.interconnects.get_subport_statistics.return_value = fake_statistics mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_SUBPORT_STATISTICS) mock_ansible_module.return_value = mock_ansible_instance InterconnectFactsModule().run() mock_ov_instance.interconnects.get_subport_statistics.assert_called_once_with( INTERCONNECT_URI, PORT_NAME, SUBPORT_NUMBER ) mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict( interconnects=MOCK_INTERCONNECTS, interconnect_subport_statistics=fake_statistics ) )
def define_mocks(mock_ov_client_from_json_file, mock_ansible_module, params): mock_ov_instance = mock.Mock() mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(params) mock_ansible_module.return_value = mock_ansible_instance return mock_ov_instance, mock_ansible_instance
def test_should_get_by_server_name_and_volume_name(self, mock_ansible_module, mock_from_json): mock_ov_instance = mock.Mock() mock_ov_instance.storage_volume_attachments.get.return_value = RETURN_GET_BY_PROFILE_AND_VOLUME mock_ov_instance.storage_volume_attachments.URI = "/rest/storage-volume-attachments" mock_from_json.return_value = mock_ov_instance mock_ov_instance.volumes.get_by.return_value = [{ "uri": "/rest/storage-volumes/12345-AAA-BBBB-CCCC-121212AA" }] mock_ansible_instance = create_ansible_mock(PARAMS_GET_ONE_VOLUME_NAME) mock_ansible_module.return_value = mock_ansible_instance StorageVolumeAttachmentFactsModule().run() mock_ov_instance.volumes.get_by.assert_called_once_with( 'name', 'VolumeTest') mock_ov_instance.storage_volume_attachments.get.assert_called_once_with( URI) mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(storage_volume_attachments=([ATTACHMENT])))
def test_should_try_create_with_automatically_selected_hardware_25_times_when_not_exists( self, mock_module, mock_ov_from_file, mock_sleep): mock_server_hardware = { 'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' } present = deepcopy(PARAMS_FOR_PRESENT) mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.side_effect = TASK_ERROR mock_ov_instance.server_profiles.get_available_targets.return_value = AVAILABLE_TARGETS mock_ov_instance.server_hardware.get_by.return_value = [ mock_server_hardware ] mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance ServerProfileModule().run() times_get_targets_called = mock_ov_instance.server_profiles.get_available_targets.call_count self.assertEqual(25, times_get_targets_called) times_create_called = mock_ov_instance.server_profiles.create.call_count self.assertEqual(25, times_create_called) mock_ansible_instance.fail_json.assert_called_once_with( msg=ERROR_ALLOCATE_SERVER_HARDWARE)
def test_should_power_off_before_update_when_data_changed( self, mock_module, mock_ov_from_file, mock_resource_compare): mock_profile_data = deepcopy(BASIC_PROFILE) mock_profile_data['serverHardwareUri'] = SHT_URI params_for_present = deepcopy(PARAMS_FOR_PRESENT) mock_resource_compare.return_value = False mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = mock_profile_data mock_ov_instance.server_profiles.update.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(params_for_present) mock_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance) ServerProfileModule().run() power_set_calls = [ mock.call(dict(powerState='Off', powerControl='PressAndHold'), SHT_URI), mock.call(dict(powerState='On', powerControl='MomentaryPress'), SHT_URI) ] mock_ov_instance.server_hardware.update_power_state.assert_has_calls( power_set_calls) mock_ov_instance.server_profiles.update.assert_called_once_with( mock_profile_data, SERVER_PROFILE_URI) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_UPDATED, ansible_facts=mock_facts)
def test_should_fail_when_exception_is_not_related_with_server_hardware( self, mock_module, mock_ov_from_file, mock_sleep): mock_server_hardware = {'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E'} present = deepcopy(PARAMS_FOR_PRESENT) mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.side_effect = Exception(FAKE_MSG_ERROR) mock_ov_instance.server_profiles.get_available_targets.return_value = AVAILABLE_TARGETS mock_ov_instance.server_hardware.get_by.return_value = [mock_server_hardware] mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance ServerProfileModule().run() times_get_targets_called = mock_ov_instance.server_profiles.get_available_targets.call_count self.assertEqual(1, times_get_targets_called) times_create_called = mock_ov_instance.server_profiles.create.call_count self.assertEqual(1, times_create_called) mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR )
def test_should_create_missing_ethernet_networks(self, mock_ansible_module, mock_ov_client_from_json_file): enet_get_range_return = [ {'name': 'TestNetwork_1', 'vlanId': 1}, {'name': 'TestNetwork_2', 'vlanId': 2}, ] enet_bulk_create_return = [ {'name': 'TestNetwork_5', 'vlanId': 5}, {'name': 'TestNetwork_9', 'vlanId': 9}, {'name': 'TestNetwork_10', 'vlanId': 10}, ] mock_ov_instance = mock.Mock() mock_ov_instance.ethernet_networks.get_range = mock.MagicMock( side_effect=[enet_get_range_return, DEFAULT_BULK_ENET_TEMPLATE]) mock_ov_instance.ethernet_networks.create_bulk.return_value = enet_bulk_create_return mock_ov_instance.ethernet_networks.dissociate_values_or_ranges.return_value = [1, 2, 5, 9, 10] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_BULK_CREATED) mock_ansible_module.return_value = mock_ansible_instance EthernetNetworkModule().run() mock_ov_instance.ethernet_networks.create_bulk.assert_called_once_with( dict(namePrefix="TestNetwork", vlanIdRange="5,9,10")) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=MISSING_ETHERNET_NETWORKS_CREATED, ansible_facts=dict(ethernet_network_bulk=DEFAULT_BULK_ENET_TEMPLATE))
def test_should_try_create_with_informed_hardware_2_times_when_not_exists( self, mock_module, mock_ov_from_file, mock_sleep): profile_data = deepcopy(BASIC_PROFILE) profile_data['serverHardwareUri'] = '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' mock_server_hardware = {'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E'} present = deepcopy(PARAMS_FOR_PRESENT) present['data']['server_hardware'] = "ServerHardwareName" mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.side_effect = [TASK_ERROR, CREATED_BASIC_PROFILE] mock_ov_instance.server_hardware.get_by.return_value = [mock_server_hardware] mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance, created=True) ServerProfileModule().run() times_get_targets_called = mock_ov_instance.server_profiles.get_available_targets.call_count self.assertEqual(0, times_get_targets_called) create_calls = [mock.call(profile_data), mock.call(profile_data)] mock_ov_instance.server_profiles.create.assert_has_calls(create_calls) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_CREATED, ansible_facts=mock_facts )
def test_should_try_create_with_automatically_selected_hardware_25_times_when_not_exists( self, mock_module, mock_ov_from_file, mock_sleep): mock_server_hardware = {'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E'} present = deepcopy(PARAMS_FOR_PRESENT) mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.side_effect = TASK_ERROR mock_ov_instance.server_profiles.get_available_targets.return_value = AVAILABLE_TARGETS mock_ov_instance.server_hardware.get_by.return_value = [mock_server_hardware] mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance ServerProfileModule().run() times_get_targets_called = mock_ov_instance.server_profiles.get_available_targets.call_count self.assertEqual(25, times_get_targets_called) times_create_called = mock_ov_instance.server_profiles.create.call_count self.assertEqual(25, times_create_called) mock_ansible_instance.fail_json.assert_called_once_with( msg=ERROR_ALLOCATE_SERVER_HARDWARE )
def test_should_create_with_informed_hardware_and_template_when_not_exists( self, mock_module, mock_ov_from_file, mock_sleep): profile_data = deepcopy(BASIC_PROFILE) profile_data['serverHardwareUri'] = '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' mock_server_hardware = {'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E'} template = deepcopy(BASIC_TEMPLATE) profile_from_template = deepcopy(BASIC_PROFILE) present = deepcopy(PARAMS_FOR_PRESENT) present['data']['server_hardware'] = "ServerHardwareName" present['data']['server_template'] = "TemplateA200" mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_hardware.get_by.return_value = [mock_server_hardware] mock_ov_instance.server_profile_templates.get_by_name.return_value = template mock_ov_instance.server_profile_templates.get_new_profile.return_value = profile_from_template mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance, created=True) ServerProfileModule().run() mock_ov_instance.server_profiles.create.assert_called_once_with(profile_data) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_CREATED, ansible_facts=mock_facts )
def test_should_create_from_template_uri_with_automatically_selected_hardware_when_not_exists( self, mock_ansible_module, mock_ov_from_file, mock_sleep): template = deepcopy(BASIC_TEMPLATE) profile_from_template = deepcopy(BASIC_PROFILE) profile_data = deepcopy(BASIC_PROFILE) profile_data['serverHardwareUri'] = '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' param_for_present = deepcopy(PARAMS_FOR_PRESENT) param_for_present['data']['serverProfileTemplateUri'] \ = '/rest/server-profile-templates/31393736-3831-4753-567h-30335837524E' mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_profiles.get_available_targets.return_value = AVAILABLE_TARGETS mock_ov_instance.server_profile_templates.get.return_value = template mock_ov_instance.server_profile_templates.get_new_profile.return_value = profile_from_template mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(param_for_present) mock_ansible_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance, created=True) ServerProfileModule().run() mock_ov_instance.server_profiles.create.assert_called_once_with(profile_data) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_CREATED, ansible_facts=mock_facts )
def test_should_power_off_when_is_offline_update(self, mock_ansible_module, mock_ov_from_file): mock_server = deepcopy(CREATED_BASIC_PROFILE) mock_server['templateCompliance'] = 'NonCompliant' params_compliant = deepcopy(PARAMS_FOR_COMPLIANT) mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = mock_server mock_ov_instance.server_profiles.patch.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance # shoud power off server to update mock_facts = gather_facts(mock_ov_instance, online_update=False) mock_ansible_instance = create_ansible_mock(params_compliant) mock_ansible_module.return_value = mock_ansible_instance ServerProfileModule().run() mock_ov_instance.server_profiles.patch.assert_called_once_with( CREATED_BASIC_PROFILE['uri'], 'replace', '/templateCompliance', 'Compliant') power_set_calls = [ mock.call(dict(powerState='Off', powerControl='PressAndHold'), mock_server['serverHardwareUri']), mock.call(dict(powerState='On', powerControl='MomentaryPress'), mock_server['serverHardwareUri'])] mock_ov_instance.server_hardware.update_power_state.assert_has_calls(power_set_calls) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=REMEDIATED_COMPLIANCE, ansible_facts=mock_facts)
def test_should_get_all_and_unmanaged_volumes_without_params( self, mock_ansible_module, mock_ov_client_from_json_file ): mock_ov_instance = mock.Mock() attachments = [ATTACHMENT, ATTACHMENT] mock_ov_instance.storage_volume_attachments.get_all.return_value = attachments mock_ov_instance.storage_volume_attachments.get_extra_unmanaged_storage_volumes.return_value = { "subresource": "value" } mock_ov_client_from_json_file.return_value = mock_ov_instance params = dict(config="config.json", options=["extraUnmanagedStorageVolumes"]) mock_ansible_instance = create_ansible_mock(params) mock_ansible_module.return_value = mock_ansible_instance StorageVolumeAttachmentFactsModule().run() mock_ov_instance.storage_volume_attachments.get_extra_unmanaged_storage_volumes.assert_called_once_with() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict( storage_volume_attachments=attachments, extra_unmanaged_storage_volumes={"subresource": "value"} ), )
def test_should_create_without_hardware_when_there_are_any_available( self, mock_ansible_module, mock_ov_from_file, mock_sleep): available_targets = deepcopy(AVAILABLE_TARGETS) available_targets['targets'] = [] mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_profiles.get_available_targets.return_value = available_targets mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(deepcopy(PARAMS_FOR_PRESENT)) mock_ansible_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance, created=True) ServerProfileModule().run() mock_ov_instance.server_profiles.create.assert_called_once_with(deepcopy(BASIC_PROFILE)) power_set_calls = mock_ov_instance.server_hardware.update_power_state.call_count self.assertEqual(0, power_set_calls) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_CREATED, ansible_facts=mock_facts )
def test_should_get_one_with_options(self, mock_ansible_module, mock_from_json): mock_ov_instance = mock.Mock() mock_ov_instance.storage_volume_attachments.URI = "/rest/storage-volume-attachments" mock_ov_instance.storage_volume_attachments.get.return_value = RETURN_GET_BY_PROFILE_AND_VOLUME mock_from_json.return_value = mock_ov_instance params_get_all_options = dict( config="config.json", serverProfileName="ProfileTest", storageVolumeUri="/rest/storage-volumes/12345-AAA-BBBB-CCCC-121212AA", options=["extraUnmanagedStorageVolumes", "paths"], ) mock_ansible_instance = create_ansible_mock(params_get_all_options) mock_ansible_module.return_value = mock_ansible_instance mock_ov_instance.storage_volume_attachments.get_extra_unmanaged_storage_volumes.return_value = { "subresource": "value" } mock_ov_instance.storage_volume_attachments.get_paths.return_value = {"subresource": "value"} StorageVolumeAttachmentFactsModule().run() mock_ov_instance.storage_volume_attachments.get.assert_called_once_with(URI) mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict( storage_volume_attachments=([ATTACHMENT]), storage_volume_attachment_paths={"subresource": "value"}, extra_unmanaged_storage_volumes={"subresource": "value"}, ), )
def test_should_update_when_data_changed(self, mock_module, mock_ov_from_file, mock_resource_compare): profile_data = deepcopy(BASIC_PROFILE) params_for_present = deepcopy(PARAMS_FOR_PRESENT) mock_resource_compare.return_value = False mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = profile_data mock_ov_instance.server_profiles.update.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(params_for_present) mock_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance) ServerProfileModule().run() mock_ov_instance.server_profiles.update.assert_called_once_with(profile_data, SERVER_PROFILE_URI) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_UPDATED, ansible_facts=mock_facts )
def test_should_create_from_template_uri_with_automatically_selected_hardware_when_not_exists( self, mock_ansible_module, mock_ov_from_file, mock_sleep): template = deepcopy(BASIC_TEMPLATE) profile_from_template = deepcopy(BASIC_PROFILE) profile_data = deepcopy(BASIC_PROFILE) profile_data[ 'serverHardwareUri'] = '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' param_for_present = deepcopy(PARAMS_FOR_PRESENT) param_for_present['data']['serverProfileTemplateUri'] \ = '/rest/server-profile-templates/31393736-3831-4753-567h-30335837524E' mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_profiles.get_available_targets.return_value = AVAILABLE_TARGETS mock_ov_instance.server_profile_templates.get.return_value = template mock_ov_instance.server_profile_templates.get_new_profile.return_value = profile_from_template mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(param_for_present) mock_ansible_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance, created=True) ServerProfileModule().run() mock_ov_instance.server_profiles.create.assert_called_once_with( profile_data) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_CREATED, ansible_facts=mock_facts)
def test_should_power_off_before_update_when_data_changed(self, mock_module, mock_ov_from_file, mock_resource_compare): mock_profile_data = deepcopy(BASIC_PROFILE) mock_profile_data['serverHardwareUri'] = SHT_URI params_for_present = deepcopy(PARAMS_FOR_PRESENT) mock_resource_compare.return_value = False mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = mock_profile_data mock_ov_instance.server_profiles.update.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(params_for_present) mock_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance) ServerProfileModule().run() power_set_calls = [ mock.call(dict(powerState='Off', powerControl='PressAndHold'), SHT_URI), mock.call(dict(powerState='On', powerControl='MomentaryPress'), SHT_URI)] mock_ov_instance.server_hardware.update_power_state.assert_has_calls(power_set_calls) mock_ov_instance.server_profiles.update.assert_called_once_with(mock_profile_data, SERVER_PROFILE_URI) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_UPDATED, ansible_facts=mock_facts )
def test_should_create_without_hardware_when_there_are_any_available( self, mock_ansible_module, mock_ov_from_file, mock_sleep): available_targets = deepcopy(AVAILABLE_TARGETS) available_targets['targets'] = [] mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_profiles.get_available_targets.return_value = available_targets mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock( deepcopy(PARAMS_FOR_PRESENT)) mock_ansible_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance, created=True) ServerProfileModule().run() mock_ov_instance.server_profiles.create.assert_called_once_with( deepcopy(BASIC_PROFILE)) power_set_calls = mock_ov_instance.server_hardware.update_power_state.call_count self.assertEqual(0, power_set_calls) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_CREATED, ansible_facts=mock_facts)
def test_fail_when_informed_template_not_exist(self, mock_module, mock_ov_from_file, mock_resource_compare): mock_server_hardware = {'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E'} profile_data = deepcopy(CREATED_BASIC_PROFILE) present = deepcopy(PARAMS_FOR_PRESENT) present['data']['server_hardware'] = "ServerHardwareName" present['data']['server_template'] = "TemplateA200" mock_resource_compare.return_value = False mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = profile_data mock_ov_instance.server_hardware.get_by.return_value = [mock_server_hardware] mock_ov_instance.server_profile_templates.get_by_name.return_value = None mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance ServerProfileModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg="Informed Server Profile Template 'TemplateA200' not found")
def test_fail_when_informed_hardware_not_exist(self, mock_module, mock_ov_from_file, mock_resource_compare): profile_data = deepcopy(CREATED_BASIC_PROFILE) present = deepcopy(PARAMS_FOR_PRESENT) present['data']['server_hardware'] = "ServerHardwareName" present['data']['server_template'] = "TemplateA200" mock_resource_compare.return_value = False mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = profile_data mock_ov_instance.server_hardware.get_by.return_value = None mock_ov_instance.server_profile_templates.get_by_name.return_value = BASIC_TEMPLATE mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance ServerProfileModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg="Informed Server Hardware 'ServerHardwareName' not found")
def test_should_turn_off_hardware_before_delete( self, mock_ansible_module, mock_ov_from_file): sh_uri = '/rest/server-hardware/37333036-3831-76jh-4831-303658389766' profile_data = deepcopy(BASIC_PROFILE) profile_data['serverHardwareUri'] = sh_uri mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = profile_data mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, ServerProfileModule().run()) mock_ov_instance.server_hardware.update_power_state.assert_called_once_with( {'powerControl': 'PressAndHold', 'powerState': 'Off'}, sh_uri) mock_ov_instance.server_profiles.delete.assert_called_once_with(profile_data) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_DELETED )
def test_should_get_all_and_unmanaged_volumes_without_params( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() attachments = [ATTACHMENT, ATTACHMENT] mock_ov_instance.storage_volume_attachments.get_all.return_value = attachments mock_ov_instance.storage_volume_attachments.get_extra_unmanaged_storage_volumes.return_value = { 'subresource': 'value' } mock_ov_client_from_json_file.return_value = mock_ov_instance params = dict(config='config.json', options=['extraUnmanagedStorageVolumes']) mock_ansible_instance = create_ansible_mock(params) mock_ansible_module.return_value = mock_ansible_instance StorageVolumeAttachmentFactsModule().run() mock_ov_instance.storage_volume_attachments.get_extra_unmanaged_storage_volumes.assert_called_once_with( ) mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict( storage_volume_attachments=attachments, extra_unmanaged_storage_volumes={'subresource': 'value'}))
def test_should_get_all_volumes_and_appliance_information( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.volumes.get_all.return_value = [{ "name": "Test Volume" }] mock_ov_instance.volumes.get_extra_managed_storage_volume_paths.return_value = [ '/path1', '/path2' ] mock_ov_instance.volumes.get_attachable_volumes.return_value = [{ "name": "attachable Volume 1" }] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock( PARAMS_GET_ALL_WITH_OPTIONS) mock_ansible_module.return_value = mock_ansible_instance VolumeFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(storage_volumes=[{ "name": "Test Volume" }], attachable_volumes=[{ "name": "attachable Volume 1" }], extra_managed_volume_paths=['/path1', '/path2']))
def test_should_update_server_when_task_include_custom_attributes(self): self.mock_get_build_plan.return_value = DEFAULT_BUILD_PLAN self.mock_get_server_by_serial.return_value = DEFAULT_SERVER self.mock_server_service.get_server.side_effect = [ DEFAULT_SERVER, DEFAULT_SERVER_UPDATED ] self.mock_server_service.update_server.return_value = DEFAULT_SERVER_UPDATED task_with_custom_attr = deepcopy(TASK_OS_DEPLOYMENT) custom_attr = [{ "SSH_CERT": "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" }] task_with_custom_attr['custom_attributes'] = custom_attr mock_ansible_instance = create_ansible_mock(task_with_custom_attr) self.mock_ansible_module.return_value = mock_ansible_instance hpe_icsp_os_deployment.main() personality_data = deepcopy(DEFAULT_SERVER) personality_data['customAttributes'] = [{ 'values': [{ 'scope': 'server', 'value': "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" }], 'key': 'SSH_CERT' }] self.mock_server_service.update_server.assert_called_once_with( personality_data)
def test_should_get_volume_by_name_with_snapshots_by_name( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.volumes.get_by.return_value = [{ "name": "Test Volume", 'uri': '/uri' }] mock_ov_instance.volumes.get_snapshot_by.return_value = [{ "filename": "snapshot_name" }] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock( PARAMS_GET_SNAPSHOT_BY_NAME) mock_ansible_module.return_value = mock_ansible_instance VolumeFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(storage_volumes=[{ "name": "Test Volume", 'uri': '/uri' }], snapshots=[{ "filename": "snapshot_name" }]))
def test_should_get_interconnect_statistics_by_interconnect_name(self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.interconnects.get_by.return_value = MOCK_INTERCONNECTS fake_statistics = dict() mock_ov_instance.interconnects.get_statistics.return_value = fake_statistics mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS) mock_ansible_module.return_value = mock_ansible_instance InterconnectFactsModule().run() mock_ov_instance.interconnects.get_statistics.assert_called_once_with(INTERCONNECT_URI) mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict( interconnects=MOCK_INTERCONNECTS, interconnect_statistics=fake_statistics, ) )
def test_should_get_server_hardware_by_name_with_options(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [{"name": "Server Hardware Name", "uri": "resuri"}] mock_ov_instance.server_hardware.get_bios.return_value = {'subresource': 'value'} mock_ov_instance.server_hardware.get_environmental_configuration.return_value = {'subresource': 'value'} mock_ov_instance.server_hardware.get_java_remote_console_url.return_value = {'subresource': 'value'} mock_ov_instance.server_hardware.get_ilo_sso_url.return_value = {'subresource': 'value'} mock_ov_instance.server_hardware.get_remote_console_url.return_value = {'subresource': 'value'} mock_ov_instance.server_hardware.get_utilization.return_value = {'subresource': 'value'} mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_WITH_OPTIONS) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts={'server_hardwares': [{'name': 'Server Hardware Name', 'uri': 'resuri'}], 'server_hardware_remote_console_url': {'subresource': 'value'}, 'server_hardware_utilization': {'subresource': 'value'}, 'server_hardware_ilo_sso_url': {'subresource': 'value'}, 'server_hardware_bios': {'subresource': 'value'}, 'server_hardware_java_remote_console_url': {'subresource': 'value'}, 'server_hardware_env_config': {'subresource': 'value'}} )
def test_should_get_datacenter_visual_content( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.datacenters.get_by.return_value = [{ "name": "Data Center Name", "uri": "/rest/datacenter/id" }] mock_ov_instance.datacenters.get_visual_content.return_value = { "name": "Visual Content" } mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_GET_CONNECTED) mock_ansible_module.return_value = mock_ansible_instance DatacenterFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts={ 'datacenter_visual_content': { 'name': 'Visual Content' }, 'datacenters': [{ 'name': 'Data Center Name', 'uri': '/rest/datacenter/id' }] })
def test_should_fail_when_create_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.logical_switches.get_by.return_value = [] mock_ov_instance.logical_switch_groups.get_by.return_value = [{'uri': '/rest/logical-switch-groups/aa-bb-cc'}] mock_ov_instance.logical_switches.create.side_effect = Exception(FAKE_MSG_ERROR) mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, LogicalSwitchModule().run()) mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR ) @mock.patch.object(OneViewClient, 'from_json_file') @mock.patch('oneview_logical_switch.AnsibleModule') def test_should_fail_when_group_not_found(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.logical_switches.get_by.return_value = [] mock_ov_instance.logical_switch_groups.get_by.return_value = [] mock_ov_instance.logical_switches.create.return_value = LOGICAL_SWITCH_FROM_ONEVIEW mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, LogicalSwitchModule().run()) mock_ansible_instance.fail_json.assert_called_once_with( msg=LOGICAL_SWITCH_GROUP_NOT_FOUND )
def test_should_get_one_and_path_uri(self, mock_ansible_module, mock_from_json): mock_ov_instance = mock.Mock() mock_ov_instance.storage_volume_attachments.get_paths.return_value = {"subresource": "value"} mock_ov_instance.storage_volume_attachments.get.return_value = RETURN_GET_BY_PROFILE_AND_VOLUME mock_ov_instance.storage_volume_attachments.URI = "/rest/storage-volume-attachments" mock_from_json.return_value = mock_ov_instance params_get_path = dict( config="config.json", serverProfileName="ProfileTest", storageVolumeUri="/rest/storage-volumes/12345-AAA-BBBB-CCCC-121212AA", options=[{"paths": {"pathUri": "/test/uri/AAA-NNN-878787H"}}], ) mock_ansible_instance = create_ansible_mock(params_get_path) mock_ansible_module.return_value = mock_ansible_instance StorageVolumeAttachmentFactsModule().run() mock_ov_instance.storage_volume_attachments.get.assert_called_once_with(URI) mock_ov_instance.storage_volume_attachments.get_paths.assert_called_once_with( "/rest/storage-volume-attachments/ED247E27", "/test/uri/AAA-NNN-878787H" ) mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict( storage_volume_attachments=[ATTACHMENT], storage_volume_attachment_paths=[{"subresource": "value"}] ), )
def test_should_update_with_current_switches_and_group_when_not_provided(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.logical_switches.get_by.side_effect = [[LOGICAL_SWITCH_FROM_ONEVIEW], []] mock_ov_instance.logical_switches.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_UPDATE) mock_ansible_module.return_value = mock_ansible_instance LogicalSwitchModule().run() data_for_update = { 'logicalSwitch': { 'name': 'Test Logical Switch - Renamed', 'uri': '/rest/logical-switches/f0d7ad37-2053-46ac-bb11-4ebdd079bb66', 'logicalSwitchGroupUri': '/rest/logical-switch-groups/af370d9a-f2f4-4beb-a1f1-670930d6741d', 'switchCredentialConfiguration': [{'logicalSwitchManagementHost': '172.16.1.1'}, {'logicalSwitchManagementHost': '172.16.1.2'}], }, 'logicalSwitchCredentials': [] } mock_ov_instance.logical_switches.update.assert_called_once_with(data_for_update)
def test_should_get_rack_device_topology(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() rack = [{"name": "Rack Name", "uri": "/rest/uri/123"}] mock_ov_instance.racks.get_by.return_value = rack mock_ov_instance.racks.get_device_topology.return_value = { "name": "Rack Name" } mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_GET_TOPOLOGY) mock_ansible_module.return_value = mock_ansible_instance RackFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts={ 'rack_device_topology': { 'name': 'Rack Name' }, 'racks': rack })
def test_should_update_with_given_switches_and_group_when_provided(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.logical_switches.get_by.side_effect = [[LOGICAL_SWITCH_FROM_ONEVIEW], []] mock_ov_instance.logical_switches.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW mock_ov_instance.logical_switch_groups.get_by.return_value = [{'uri': '/rest/logical-switch-groups/aa-bb-cc'}] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_UPDATE_WITH_SWITCHES_AND_GROUPS) mock_ansible_module.return_value = mock_ansible_instance LogicalSwitchModule().run() data_for_update = { 'logicalSwitch': { 'name': 'Test Logical Switch', 'uri': LOGICAL_SWITCH_FROM_ONEVIEW['uri'], 'logicalSwitchGroupUri': '/rest/logical-switch-groups/aa-bb-cc', 'switchCredentialConfiguration': [{'logicalSwitchManagementHost': '172.16.1.3'}, {'logicalSwitchManagementHost': '172.16.1.4'}], }, 'logicalSwitchCredentials': [] } mock_ov_instance.logical_switches.update.assert_called_once_with(data_for_update)
def test_should_get_all_network_sets(self, mock_ansible_module, mock_ov_client_from_json_file): network_sets = [{ "name": "Network Set 1", "networkUris": ['/rest/ethernet-networks/aaa-bbb-ccc'] }, { "name": "Network Set 2", "networkUris": [ '/rest/ethernet-networks/ddd-eee-fff', '/rest/ethernet-networks/ggg-hhh-fff' ] }] mock_ov_instance = mock.Mock() mock_ov_instance.network_sets.get_all.return_value = network_sets mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_GET_ALL) mock_ansible_module.return_value = mock_ansible_instance NetworkSetFactsModule().run() mock_ov_instance.network_sets.get_all.assert_called_once_with() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(network_sets=network_sets))
def test_update_when_data_has_modified_attributes( self, mock_ansible_module, mock_ov_client_from_json_file): data_merged = dict(name=NETWORK_SET['name'] + " - Renamed", networkUris=[ '/rest/ethernet-networks/aaa-bbb-ccc', '/rest/ethernet-networks/ddd-eee-fff' ]) mock_ov_instance = mock.Mock() mock_ov_instance.network_sets.get_by.side_effect = [NETWORK_SET], [] mock_ov_instance.network_sets.update.return_value = data_merged mock_ov_instance.ethernet_networks.get_by.return_value = [{ 'uri': '/rest/ethernet-networks/ddd-eee-fff' }] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_WITH_CHANGES) mock_ansible_module.return_value = mock_ansible_instance NetworkSetModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=NETWORK_SET_UPDATED, ansible_facts=dict(network_set=data_merged))
def test_should_create_with_informed_hardware_when_not_exists( self, mock_module, mock_ov_from_file, mock_sleep): profile_data = deepcopy(BASIC_PROFILE) profile_data[ 'serverHardwareUri'] = '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' mock_server_hardware = { 'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' } present = deepcopy(PARAMS_FOR_PRESENT) present['data']['server_hardware'] = "ServerHardwareName" mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_hardware.get_by.return_value = [ mock_server_hardware ] mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance, created=True) ServerProfileModule().run() mock_ov_instance.server_profiles.create.assert_called_once_with( profile_data) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_CREATED, ansible_facts=mock_facts)
def test_should_get_connectable_storage_volume_templates( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_volume_templates.get_all.return_value = { "name": "Storage System Name" } mock_ov_instance.storage_volume_templates.get_connectable_volume_templates.return_value = { "name": "Storage System Name" } mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_GET_CONNECTED) mock_ansible_module.return_value = mock_ansible_instance StorageVolumeTemplateFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts={ 'connectable_volume_templates': { 'name': 'Storage System Name' }, 'storage_volume_templates': { 'name': 'Storage System Name' } })
def test_should_fail_when_exception_is_not_related_with_server_hardware( self, mock_module, mock_ov_from_file, mock_sleep): mock_server_hardware = { 'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' } present = deepcopy(PARAMS_FOR_PRESENT) mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = None mock_ov_instance.server_profiles.create.side_effect = Exception( FAKE_MSG_ERROR) mock_ov_instance.server_profiles.get_available_targets.return_value = AVAILABLE_TARGETS mock_ov_instance.server_hardware.get_by.return_value = [ mock_server_hardware ] mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance ServerProfileModule().run() times_get_targets_called = mock_ov_instance.server_profiles.get_available_targets.call_count self.assertEqual(1, times_get_targets_called) times_create_called = mock_ov_instance.server_profiles.create.call_count self.assertEqual(1, times_create_called) mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR)
def test_should_update_when_data_changed(self, mock_module, mock_ov_from_file, mock_resource_compare): profile_data = deepcopy(BASIC_PROFILE) params_for_present = deepcopy(PARAMS_FOR_PRESENT) mock_resource_compare.return_value = False mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = profile_data mock_ov_instance.server_profiles.update.return_value = CREATED_BASIC_PROFILE mock_ov_instance.server_hardware.update_power_state.return_value = {} mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(params_for_present) mock_module.return_value = mock_ansible_instance mock_facts = gather_facts(mock_ov_instance) ServerProfileModule().run() mock_ov_instance.server_profiles.update.assert_called_once_with( profile_data, SERVER_PROFILE_URI) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_PROFILE_UPDATED, ansible_facts=mock_facts)
def test_should_get_storage_system_pools_by_ip_hostname( self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_ip_hostname.return_value = { "ip_hostname": "10.0.0.0", "uri": "uri" } mock_ov_instance.storage_systems.get_storage_pools.return_value = { "name": "Storage Pool" } mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock( PARAMS_GET_POOL_BY_IP_HOSTNAME) mock_ansible_module.return_value = mock_ansible_instance StorageSystemFactsModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(storage_system_pools=({ "name": "Storage Pool" }), storage_systems={ "ip_hostname": "10.0.0.0", "uri": "uri" }))
def test_fail_when_informed_template_not_exist(self, mock_module, mock_ov_from_file, mock_resource_compare): mock_server_hardware = { 'uri': '/rest/server-hardware/31393736-3831-4753-567h-30335837524E' } profile_data = deepcopy(CREATED_BASIC_PROFILE) present = deepcopy(PARAMS_FOR_PRESENT) present['data']['server_hardware'] = "ServerHardwareName" present['data']['server_template'] = "TemplateA200" mock_resource_compare.return_value = False mock_ov_instance = mock.Mock() mock_ov_instance.server_profiles.get_by_name.return_value = profile_data mock_ov_instance.server_hardware.get_by.return_value = [ mock_server_hardware ] mock_ov_instance.server_profile_templates.get_by_name.return_value = None mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(present) mock_module.return_value = mock_ansible_instance ServerProfileModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg="Informed Server Profile Template 'TemplateA200' not found")
def test_should_get_one_and_paths(self, mock_ansible_module, mock_from_json): mock_ov_instance = mock.Mock() mock_ov_instance.storage_volume_attachments.get_paths.return_value = [{ 'subresource': 'value' }] mock_ov_instance.storage_volume_attachments.get.return_value = RETURN_GET_BY_PROFILE_AND_VOLUME mock_ov_instance.storage_volume_attachments.URI = "/rest/storage-volume-attachments" mock_from_json.return_value = mock_ov_instance params_get_paths = dict( config='config.json', serverProfileName="ProfileTest", storageVolumeUri= "/rest/storage-volumes/12345-AAA-BBBB-CCCC-121212AA", options=['paths']) mock_ansible_instance = create_ansible_mock(params_get_paths) mock_ansible_module.return_value = mock_ansible_instance StorageVolumeAttachmentFactsModule().run() mock_ov_instance.storage_volume_attachments.get.assert_called_once_with( URI) mock_ov_instance.storage_volume_attachments.get_paths.assert_called_once_with( "/rest/storage-volume-attachments/ED247E27") mock_ansible_instance.exit_json.assert_called_once_with( changed=False, ansible_facts=dict(storage_volume_attachments=[ATTACHMENT], storage_volume_attachment_paths=[{ 'subresource': 'value' }]))
def define_mocks_for_get_by_name(mock_ov_from_file, mock_ansible_module, PARAMS): mock_ov_instance = mock.Mock() mock_ov_instance.logical_interconnects.get_by_name.return_value = LOGICAL_INTERCONNECT mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS) mock_ansible_module.return_value = mock_ansible_instance return mock_ov_instance, mock_ansible_instance
def test_should_fail_when_oneview_client_raises_an_exception(self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_profile_templates.get_all.side_effect = Exception(ERROR_MSG) mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_GET_ALL) mock_ansible_module.return_value = mock_ansible_instance ServerProfileTemplateFactsModule().run() mock_ansible_instance.fail_json.assert_called_once_with(msg=ERROR_MSG)
def test_should_fail_when_get_all_raises_an_exception(self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.logical_interconnects.get_all.side_effect = Exception(ERROR_MSG) mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_GET_ALL) mock_ansible_module.return_value = mock_ansible_instance LogicalInterconnectFactsModule().run() mock_ansible_instance.fail_json.assert_called_once_with(msg=ERROR_MSG)
def test_should_load_config_from_file(self, mock_ansible_module, mock_ov_client_from_env_vars, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock({'config': 'config.json'}) mock_ansible_module.return_value = mock_ansible_instance SanManagerFactsModule() mock_ov_client_from_json_file.assert_called_once_with('config.json') mock_ov_client_from_env_vars.not_been_called()
def test_should_load_config_from_file(self, mock_ansible_module, mock_ov_client_from_env_vars, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock({'config': 'config.json'}) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule() mock_ov_client_from_json_file.assert_called_once_with('config.json') mock_ov_client_from_env_vars.not_been_called()
def test_should_fail_when_oneview_client_raises_exception(self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_profile_templates.get_by_name.side_effect = Exception(FAKE_MSG_ERROR) mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance ServerProfileTemplateModule().run() mock_ansible_instance.fail_json.assert_called_once_with(msg=FAKE_MSG_ERROR)
def test_should_fail_when_managed_san_not_found(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.managed_sans.get_by_name.return_value = None mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_TO_CREATE_ISSUES_REPORT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, ManagedSanModule().run()) mock_ansible_instance.fail_json.assert_called_once_with(msg=MANAGED_SAN_NOT_FOUND)
def test_should_load_config_from_file(self, mock_ansible_module, mock_ov_client_from_env_vars, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_GET_ALL) mock_ansible_module.return_value = mock_ansible_instance FcNetworkFactsModule() mock_ov_client_from_json_file.assert_called_once_with('config.json') mock_ov_client_from_env_vars.not_been_called()
def test_should_do_nothing_when_fc_network_not_exist(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.fc_networks.get_by.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT) mock_ansible_module.return_value = mock_ansible_instance FcNetworkModule().run() mock_ansible_instance.exit_json.assert_called_once_with(changed=False, msg=FC_NETWORK_ALREADY_ABSENT)