def test_should_not_reconfigure_when_resource_not_found(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = None
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_CONFIGURE)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=LOGICAL_ENCLOSURE_REQUIRED)
    def test_should_not_update_from_group_when_resource_not_found(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = None
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_UPDATE_FROM_GROUP)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=LOGICAL_ENCLOSURE_REQUIRED)
    def test_should_update_script_when_resource_exists(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_UPDATE_SCRIPT)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_CONFIGURATION_SCRIPT_UPDATED,
            ansible_facts=dict(configuration_script='# script (updated)'))
    def test_should_do_nothing_when_resource_already_absent(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = None
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_ABSENT)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=LOGICAL_ENCLOSURE_ALREADY_ABSENT,
            ansible_facts=dict(logical_enclosure=None))
    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

        LogicalEnclosureModule()

        mock_ov_client_from_json_file.assert_called_once_with('config.json')
        mock_ov_client_from_env_vars.not_been_called()
    def test_should_delete_logical_enclosure_when_resource_exist(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ov_client.logical_enclosures.delete.return_value = True
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_ABSENT)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_DELETED,
            ansible_facts=dict(logical_enclosure=None))
    def test_should_not_update_when_existing_data_is_equals(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_NO_RENAME)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=LOGICAL_ENCLOSURE_ALREADY_UPDATED,
            ansible_facts=dict(
                logical_enclosure=DICT_DEFAULT_LOGICAL_ENCLOSURE))
    def test_should_generate_support_dump_when_resource_exist(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ov_client.logical_enclosures.generate_support_dump.return_value = '/rest/appliance/dumpedfile'
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_DUMP)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_DUMP_GENERATED,
            ansible_facts=dict(
                generated_dump_uri='/rest/appliance/dumpedfile'))
    def test_should_update_from_group_when_resource_exist(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ov_client.logical_enclosures.update_from_group.return_value = {
            'Updated from group'
        }
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_UPDATE_FROM_GROUP)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_UPDATED_FROM_GROUP,
            ansible_facts=dict(logical_enclosure={'Updated from group'}))
    def test_should_reconfigure_when_resource_exist(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ov_client.logical_enclosures.update_configuration.return_value = {
            'Configuration', 'Updated'
        }
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_CONFIGURE)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_RECONFIGURED,
            ansible_facts=dict(logical_enclosure={'Configuration', 'Updated'}))
    def test_should_update_firmware_when_resource_exists(self):
        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ov_client.logical_enclosures.patch.return_value = {
            'PATCH', 'EXECUTED'
        }
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_FIRMWARE_UPDATE)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_FIRMWARE_UPDATED,
            ansible_facts=dict(logical_enclosure={'PATCH', 'EXECUTED'}))
    def test_should_update_when_data_has_modified_attributes(self):
        data_merged = DICT_DEFAULT_LOGICAL_ENCLOSURE.copy()
        data_merged['newName'] = 'New Name'

        self.mock_ov_client.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        self.mock_ov_client.logical_enclosures.update.return_value = data_merged
        self.mock_ansible_module.params = yaml.load(
            YAML_LOGICAL_ENCLOSURE_RENAME)

        LogicalEnclosureModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_UPDATED,
            ansible_facts=dict(logical_enclosure=data_merged))
    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_update_script(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 = DICT_DEFAULT_LOGICAL_ENCLOSURE
        mock_ov_client_from_json_file.return_value = mock_ov_instance

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

        LogicalEnclosureModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_CONFIGURATION_SCRIPT_UPDATED,
            ansible_facts=dict(configuration_script='# script (updated)'))
    def test_should_not_update_when_data_is_equals(
            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 = DICT_DEFAULT_LOGICAL_ENCLOSURE
        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.exit_json.assert_called_once_with(
            changed=False,
            msg=LOGICAL_ENCLOSURE_ALREADY_UPDATED,
            ansible_facts=dict(
                logical_enclosure=DICT_DEFAULT_LOGICAL_ENCLOSURE))
    def test_support_dump(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 = DICT_DEFAULT_LOGICAL_ENCLOSURE
        mock_ov_instance.logical_enclosures.generate_support_dump.return_value = '/rest/appliance/dumpedfile'
        mock_ov_client_from_json_file.return_value = mock_ov_instance

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

        LogicalEnclosureModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_DUMP_GENERATED,
            ansible_facts=dict(
                generated_dump_uri='/rest/appliance/dumpedfile'))
    def test_update_firmware(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 = DICT_DEFAULT_LOGICAL_ENCLOSURE
        mock_ov_instance.logical_enclosures.patch.return_value = {
            'PATCH', 'EXECUTED'
        }
        mock_ov_client_from_json_file.return_value = mock_ov_instance

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

        LogicalEnclosureModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_FIRMWARE_UPDATED,
            ansible_facts=dict(logical_enclosure={'PATCH', 'EXECUTED'}))
    def test_update_from_group(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 = DICT_DEFAULT_LOGICAL_ENCLOSURE
        mock_ov_instance.logical_enclosures.update_from_group.return_value = {
            'Updated from group'
        }
        mock_ov_client_from_json_file.return_value = mock_ov_instance

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

        LogicalEnclosureModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_UPDATED_FROM_GROUP,
            ansible_facts=dict(logical_enclosure={'Updated from group'}))
    def test_reconfigure(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 = DICT_DEFAULT_LOGICAL_ENCLOSURE
        mock_ov_instance.logical_enclosures.update_configuration.return_value = {
            'Configuration', 'Updated'
        }
        mock_ov_client_from_json_file.return_value = mock_ov_instance

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

        LogicalEnclosureModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_RECONFIGURED,
            ansible_facts=dict(logical_enclosure={'Configuration', 'Updated'}))
    def test_update_when_data_has_modified_attributes(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        data_merged = DICT_DEFAULT_LOGICAL_ENCLOSURE.copy()
        data_merged['newName'] = 'New Name'

        # Mock OneView Client
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_enclosures.get_by_name.return_value = DICT_DEFAULT_LOGICAL_ENCLOSURE
        mock_ov_instance.logical_enclosures.update.return_value = data_merged
        mock_ov_client_from_json_file.return_value = mock_ov_instance

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

        LogicalEnclosureModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_ENCLOSURE_UPDATED,
            ansible_facts=dict(logical_enclosure=data_merged))