def test_should_configure_network(self):
        self.mock_connection.get.side_effect = [SERVERS, SERVERS]
        self.mock_connection.post.return_value = JOB_RESOURCE
        self.mock_server_service.get_server.return_value = DEFAULT_SERVER

        mock_ansible_instance = create_ansible_mock_yaml(YAML_NETWORK_CONFIGURED)
        self.mock_ansible_module.return_value = mock_ansible_instance

        ICspServerModule().run()

        network_config_state = yaml.load(YAML_NETWORK_CONFIGURED)

        network_config = {
            "serverData": [
                {"serverUri": DEFAULT_SERVER['uri'], "personalityData": network_config_state['server_personality_data'],
                 "skipReboot": True}],
            "failMode": None,
            "osbpUris": []
        }

        uri = '/rest/os-deployment-jobs/?writeOnly=true'

        self.mock_connection.post.assert_called_once_with(uri, network_config)

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=CUSTOM_ATTR_NETWORK_UPDATED,
            ansible_facts=dict(target_server=DEFAULT_SERVER)
        )
    def test_update_when_data_has_modified_attributes_but_bandwidth_is_equal(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        data_merged = DEFAULT_ENET_TEMPLATE.copy()
        data_merged['purpose'] = 'Management'

        mock_ov_instance = mock.Mock()
        mock_ov_instance.ethernet_networks.get_by.return_value = [
            DEFAULT_ENET_TEMPLATE
        ]
        mock_ov_instance.ethernet_networks.update.return_value = data_merged
        mock_ov_instance.connection_templates.get.return_value = {
            "bandwidth": DICT_PARAMS_WITH_CHANGES['bandwidth']
        }

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_PARAMS_WITH_CHANGES)
        mock_ansible_module.return_value = mock_ansible_instance

        EthernetNetworkModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=ETHERNET_NETWORK_UPDATED,
            ansible_facts=dict(ethernet_network=data_merged))
    def test_should_update_the_server_hardware_type(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()

        srv_hw_type = DICT_DEFAULT_SERVER_HARDWARE_TYPE.copy()
        srv_hw_type['uri'] = '/rest/id'

        mock_ov_instance.server_hardware_types.get_by.return_value = [
            srv_hw_type
        ]
        mock_ov_instance.server_hardware_types.update.return_value = {
            "name": "name"
        }

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_SERVER_HARDWARE_TYPE_CHANGE)
        mock_ansible_module.return_value = mock_ansible_instance

        ServerHardwareTypeModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=SERVER_HARDWARE_TYPE_UPDATED,
            ansible_facts=dict(server_hardware_type={"name": "name"}))
Exemple #4
0
    def test_should_not_update_when_data_is_equals(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        switch_type_replaced = DICT_DEFAULT_LOGICAL_SWITCH_GROUP.copy()
        del switch_type_replaced['switchMapTemplate'][
            'switchMapEntryTemplates'][0]['permittedSwitchTypeName']

        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switch_groups.get_by.return_value = [
            DICT_DEFAULT_LOGICAL_SWITCH_GROUP
        ]
        mock_ov_instance.switch_types.get_by.return_value = [{
            'uri':
            SWITCH_TYPE_URI
        }]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_LOGICAL_SWITCH_GROUP)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchGroupModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            msg=LOGICAL_SWITCH_GROUP_ALREADY_UPDATED,
            ansible_facts=dict(
                logical_switch_group=DICT_DEFAULT_LOGICAL_SWITCH_GROUP))
    def test_reset_successfully(self, mock_ansible_module,
                                mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.ethernet_networks.get_by.return_value = [
            DICT_PARAMS_WITH_CHANGES
        ]
        mock_ov_instance.connection_templates.update.return_value = {
            'result': 'success'
        }
        mock_ov_instance.connection_templates.get.return_value = {
            "bandwidth": DICT_PARAMS_WITH_CHANGES['bandwidth']
        }

        mock_ov_instance.connection_templates.get_default.return_value = {
            "bandwidth": {
                "max": 1
            }
        }

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_RESET_CONNECTION_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        EthernetNetworkModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=ETHERNET_NETWORK_CONNECTION_TEMPLATE_RESET,
            ansible_facts=dict(
                ethernet_network_connection_template={'result': 'success'}))
    def test_should_add_server(self):
        self.mock_connection.get.side_effect = [{'members': []}, SERVERS]
        self.mock_server_service.add_server.return_value = JOB_RESOURCE
        self.mock_icsp.jobs.return_value = ICSP_JOBS

        self.mock_icsp.common = mock.Mock()
        self.mock_icsp.common.monitor_execution.return_value = {}

        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_PRESENT)
        self.mock_ansible_module.return_value = mock_ansible_instance

        ICspServerModule().run()

        ilo_body = {'ipAddress': "16.124.135.239",
                    'username': "******",
                    'password': "******",
                    'port': 443}
        self.mock_server_service.add_server.assert_called_once_with(ilo_body)
        self.mock_icsp.common.monitor_execution.assert_called_once_with(JOB_RESOURCE, ICSP_JOBS)

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg="Server created: '/uri/239'",
            ansible_facts=dict(target_server=DEFAULT_SERVER)
        )
    def test_should_fail_when_try_configure_network_for_not_found_server(self):
        self.mock_connection.get.return_value = {'members': []}

        mock_ansible_instance = create_ansible_mock_yaml(YAML_NETWORK_CONFIGURED)
        self.mock_ansible_module.return_value = mock_ansible_instance

        ICspServerModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(changed=False, msg=SERVER_NOT_FOUND)
    def test_should_fail_with_args_joined_when_common_exception_raised_on_delete(self):
        self.mock_connection.get.return_value = SERVERS
        self.mock_server_service.delete_server.side_effect = Exception("Fake Message", "INVALID_RESOURCE")

        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_ABSENT)
        self.mock_ansible_module.return_value = mock_ansible_instance

        ICspServerModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(msg='Fake Message; INVALID_RESOURCE')
Exemple #9
0
    def test_should_not_add_server_when_already_present(self):
        self.mock_connection.get.return_value = SERVERS
        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_PRESENT)
        self.mock_ansible_module.return_value = mock_ansible_instance

        ICspServerModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            msg=SERVER_ALREADY_PRESENT,
            ansible_facts=dict(target_server=DEFAULT_SERVER))
Exemple #10
0
    def test_should_fail_when_get_profile_by_name_raises_exception(self, mock_ansible_module, mock_from_json):
        mock_ov_instance = mock.Mock()

        mock_ov_instance.server_profiles.get_by_name.side_effect = Exception(FAKE_MSG_ERROR)

        mock_from_json.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_EXTRA_REMOVED_BY_NAME)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StorageVolumeAttachmentModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(msg=FAKE_MSG_ERROR)
    def test_should_raise_exception_when_key_is_missing(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = [DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE]
        mock_ov_instance.storage_volume_templates.remove.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_VOLUME_TEMPLATE_MISSING_KEY)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StorageVolumeTemplateModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(msg=STORAGE_VOLUME_TEMPLATE_MANDATORY_FIELD_MISSING)
Exemple #12
0
    def test_should_fail_when_profile_name_not_found(self, mock_ansible_module, mock_from_json):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.server_profiles.get_by_name.return_value = None
        mock_ov_instance.storage_volume_attachments.remove_extra_presentations.return_value = MOCK_SERVER_PROFILE

        mock_from_json.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_EXTRA_REMOVED_BY_NAME)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeAttachmentModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(msg=PROFILE_NOT_FOUND)
    def test_should_fail_when_delete_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = [DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE]
        mock_ov_instance.storage_volume_templates.delete.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_VOLUME_TEMPLATE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StorageVolumeTemplateModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(msg=FAKE_MSG_ERROR)
    def test_should_fail_with_all_exe_attr_when_HPICspException_raised_on_delete(self):
        self.mock_connection.get.return_value = SERVERS
        exeption_value = {"message": "Fake Message", "details": "Details", "errorCode": "INVALID_RESOURCE"}
        self.mock_server_service.delete_server.side_effect = HPICspInvalidResource(exeption_value)

        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_ABSENT)
        self.mock_ansible_module.return_value = mock_ansible_instance

        ICspServerModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg='{"errorCode": "INVALID_RESOURCE", "message": "Fake Message", "details": "Details"}')
Exemple #15
0
    def test_should_not_try_delete_server_when_it_is_already_absent(self):
        self.mock_connection.get.return_value = {'members': []}
        self.mock_server_service.delete_server.return_value = {}
        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_ABSENT)
        self.mock_ansible_module.return_value = mock_ansible_instance

        ICspServerModule().run()

        self.mock_server_service.delete_server.assert_not_called()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False, msg=SERVER_ALREADY_ABSENT)
    def test_raise_error_when_name_not_exists(self, mock_ansible_module, mock_ov_client_from_json_file):
        # Mock OneView Client
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_enclosures.get_by_name.return_value = None
        mock_ov_client_from_json_file.return_value = mock_ov_instance

        # Mock Ansible Module
        mock_ansible_instance = create_ansible_mock_yaml(YAML_LOGICAL_ENCLOSURE_NO_RENAME)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalEnclosureModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(msg=LOGICAL_ENCLOSURE_REQUIRED)
    def test_expect_exception_not_caught_when_configure_network_raise_exception(self):
        self.mock_connection.get.return_value = SERVERS
        self.mock_connection.post.side_effect = Exception("message")

        mock_ansible_instance = create_ansible_mock_yaml(YAML_NETWORK_CONFIGURED)
        self.mock_ansible_module.return_value = mock_ansible_instance

        try:
            ICspServerModule().run()
        except Exception as e:
            self.assertEqual("message", e.args[0])
        else:
            self.fail("Expected Exception was not raised")
    def test_should_remove_storage_volume_template(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = [DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_VOLUME_TEMPLATE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeTemplateModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={}, changed=True, msg=STORAGE_VOLUME_TEMPLATE_DELETED
        )
    def test_expect_exception_not_caught_when_create_server_raise_exception(self):
        self.mock_connection.get.side_effect = [{'members': []}, SERVERS]
        self.mock_server_service.add_server.side_effect = Exception("message")

        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_PRESENT)
        self.mock_ansible_module.return_value = mock_ansible_instance

        try:
            ICspServerModule().run()
        except Exception as e:
            self.assertEqual("message", e.args[0])
        else:
            self.fail("Expected Exception was not raised")
    def test_should_raise_exception_when_key_is_missing(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.connection_templates.get_by.return_value = [DICT_DEFAULT_CONNECTION_TEMPLATE]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_CONNECTION_TEMPLATE_MISSING_KEY)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, ConnectionTemplateModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=CONNECTION_TEMPLATE_MANDATORY_FIELD_MISSING
        )
    def test_should_fail_when_key_is_missing(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_pools.get_by.return_value = [DICT_DEFAULT_STORAGE_POOL]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_POOL_MISSING_KEY)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StoragePoolModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=STORAGE_POOL_MANDATORY_FIELD_MISSING
        )
    def test_should_fail_when_remove_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_pools.get_by.return_value = [DICT_DEFAULT_STORAGE_POOL]
        mock_ov_instance.storage_pools.remove.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_POOL_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StoragePoolModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_fail_when_delete_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switch_groups.get_by.return_value = [DICT_DEFAULT_LOGICAL_SWITCH_GROUP]
        mock_ov_instance.logical_switch_groups.delete.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_LOGICAL_SWITCH_GROUP_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, LogicalSwitchGroupModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_fail_when_reset_not_existing_ethernet_network(self, mock_ansible_module,
                                                                  mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.ethernet_networks.get_by.return_value = [None]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_RESET_CONNECTION_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        EthernetNetworkModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=ETHERNET_NETWORK_NOT_FOUND
        )
Exemple #25
0
    def test_should_fail_when_the_server_hardware_was_not_found(self, mock_ansible_module,
                                                                mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.server_hardware.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_ILO_FIRMWARE)
        mock_ansible_module.return_value = mock_ansible_instance

        ServerHardwareModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=SERVER_HARDWARE_NOT_FOUND
        )
    def test_should_fail_when_the_power_device_was_not_found(self, mock_ansible_module,
                                                             mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_POWER_DEVICE_UID_STATE)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=POWER_DEVICE_NOT_FOUND
        )
    def test_should_do_nothing_when_enclosure_group_not_exist(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.enclosure_groups.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_ENCLOSURE_GROUP_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        EnclosureGroupModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            msg=ENCLOSURE_GROUP_ALREADY_ABSENT
        )
Exemple #28
0
    def test_should_raise_exception_when_connection_template_was_not_found(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.connection_templates.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_CONNECTION_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        ConnectionTemplateModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=CONNECTION_TEMPLATE_NOT_FOUND)
    def test_should_fail_when_the_power_device_was_not_found(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_POWER_DEVICE_UID_STATE)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=POWER_DEVICE_NOT_FOUND)
    def test_should_remove_power_device(self, mock_ansible_module,
                                        mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = [{'name': 'name'}]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_POWER_DEVICE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={}, changed=True, msg=POWER_DEVICE_DELETED)
    def test_should_fail_when_delete_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.datacenters.get_by.return_value = [DICT_DEFAULT_DATACENTER]
        mock_ov_instance.datacenters.remove.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_DATACENTER_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, DatacenterModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_remove_enclosure_group(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.enclosure_groups.get_by.return_value = [DICT_DEFAULT_ENCLOSURE_GROUP]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_ENCLOSURE_GROUP_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        EnclosureGroupModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=ENCLOSURE_GROUP_DELETED
        )
    def test_should_fail_when_server_hardware_type_was_not_found(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.server_hardware_types.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_SERVER_HARDWARE_TYPE)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, ServerHardwareTypeModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=SERVER_HARDWARE_TYPE_NOT_FOUND)
    def test_should_add_new_ipdu(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.add_ipdu.return_value = {"name": "name"}
        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_IPDU)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=POWER_DEVICE_IPDU_ADDED,
            ansible_facts=dict(power_device={"name": "name"})
        )
    def test_should_add_new_ipdu(self, mock_ansible_module,
                                 mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.add_ipdu.return_value = {"name": "name"}
        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_IPDU)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=POWER_DEVICE_IPDU_ADDED,
            ansible_facts=dict(power_device={"name": "name"}))
    def test_should_raise_exception_when_connection_template_was_not_found(self, mock_ansible_module,
                                                                           mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.connection_templates.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_CONNECTION_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        ConnectionTemplateModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=CONNECTION_TEMPLATE_NOT_FOUND
        )
    def test_should_do_nothing_when_power_device_not_exist(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_POWER_DEVICE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={}, changed=False, msg=POWER_DEVICE_ALREADY_ABSENT)
    def test_should_fail_when_update_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.connection_templates.get_by.return_value = [DICT_DEFAULT_CONNECTION_TEMPLATE]
        mock_ov_instance.connection_templates.update.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_CONNECTION_TEMPLATE_CHANGE)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, ConnectionTemplateModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
Exemple #39
0
    def test_should_do_nothing_when_enclosure_group_not_exist(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.enclosure_groups.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_ENCLOSURE_GROUP_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        EnclosureGroupModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False, msg=ENCLOSURE_GROUP_ALREADY_ABSENT)
    def test_should_fail_when_remove_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = [{'name': 'name'}]
        mock_ov_instance.power_devices.remove.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_POWER_DEVICE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, PowerDeviceModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_fail_when_update_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.ethernet_networks.get_by.return_value = [DEFAULT_ENET_TEMPLATE]
        mock_ov_instance.ethernet_networks.update.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_PARAMS_WITH_CHANGES)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, EthernetNetworkModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_fail_when_update_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.enclosure_groups.get_by.return_value = [DICT_DEFAULT_ENCLOSURE_GROUP]
        mock_ov_instance.enclosure_groups.update.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_ENCLOSURE_GROUP_CHANGES)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, EnclosureGroupModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_fail_when_reset_not_existing_ethernet_network(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.ethernet_networks.get_by.return_value = [None]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_RESET_CONNECTION_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        EthernetNetworkModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=ETHERNET_NETWORK_NOT_FOUND)
Exemple #44
0
    def test_should_fail_when_remove_raises_exception(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': 'name'}]
        mock_ov_instance.server_hardware.remove.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, ServerHardwareModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_do_nothing_when_storage_pool_not_exist(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_pools.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_STORAGE_POOL_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StoragePoolModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={}, changed=False, msg=STORAGE_POOL_ALREADY_ABSENT)
Exemple #46
0
    def test_should_fail_when_credentials_attribute_is_missing(self, mock_ansible_module,
                                                               mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_systems.get_by_name.return_value = []
        mock_ov_client_from_json_file.return_value = mock_ov_instance

        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_BY_NAME)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageSystemModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=STORAGE_SYSTEM_CREDENTIALS_MANDATORY
        )
Exemple #47
0
    def test_should_not_delete_when_oneview_exception(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 = [DICT_DEFAULT_STORAGE_SYSTEM]
        mock_ov_instance.storage_systems.remove.side_effect = Exception(FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StorageSystemModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR
        )
    def test_should_fail_when_switch_type_was_not_found(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switch_groups.get_by.return_value = []
        mock_ov_instance.logical_switch_groups.create.side_effect = Exception(FAKE_MSG_ERROR)
        mock_ov_instance.switch_types.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_LOGICAL_SWITCH_GROUP)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, LogicalSwitchGroupModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=SWITCH_TYPE_NOT_FOUND
        )
    def test_should_not_update_when_data_is_equals(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = [DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_VOLUME_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeTemplateModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            msg=STORAGE_VOLUME_TEMPLATE_ALREADY_UPDATED,
            ansible_facts=dict(storage_volume_template=DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE),
        )
    def test_should_fail_when_add_raises_exception(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_pools.get_by.return_value = []
        mock_ov_instance.storage_pools.add.side_effect = Exception(
            FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_POOL)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StoragePoolModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR)
    def test_should_do_nothing_when_storage_system_not_exist(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 = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageSystemModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=False,
            msg=STORAGE_SYSTEM_ALREADY_ABSENT
        )
Exemple #52
0
    def test_should_remove_storage_system(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 = DICT_DEFAULT_STORAGE_SYSTEM

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageSystemModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=True,
            msg=STORAGE_SYSTEM_DELETED
        )
    def test_should_do_nothing_when_storage_volume_template_not_exist(
        self, mock_ansible_module, mock_ov_client_from_json_file
    ):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_VOLUME_TEMPLATE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeTemplateModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={}, changed=False, msg=STORAGE_VOLUME_TEMPLATE_ALREADY_ABSENT
        )
    def test_should_not_update_when_it_already_present(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = [DICT_DEFAULT_POWER_DEVICE]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_POWER_DEVICE)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            msg=POWER_DEVICE_ALREADY_PRESENT,
            ansible_facts=dict(power_device=DICT_DEFAULT_POWER_DEVICE)
        )
    def test_should_remove_power_device(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = [{'name': 'name'}]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_POWER_DEVICE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=True,
            msg=POWER_DEVICE_DELETED
        )
    def test_should_do_nothing_when_power_device_not_exist(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.power_devices.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_POWER_DEVICE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        PowerDeviceModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=False,
            msg=POWER_DEVICE_ALREADY_ABSENT
        )
    def test_should_remove_storage_pool(self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_pools.get_by.return_value = [DICT_DEFAULT_STORAGE_POOL]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_POOL_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StoragePoolModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=True,
            msg=STORAGE_POOL_DELETED
        )