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_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_get_volume_by_name(self):
        self.resource.get_by.return_value = [{"name": "Test Volume", 'uri': '/uri'}]
        self.mock_ansible_module.params = PARAMS_GET_BY_NAME

        VolumeFactsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            ansible_facts=dict(storage_volumes=[{"name": "Test Volume", 'uri': '/uri'}]))
    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

        VolumeFactsModule()

        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_get_by_raises_error(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.volumes.get_by.side_effect = Exception(ERROR_MSG)

        mock_ov_client_from_json_file.return_value = mock_ov_instance

        mock_ansible_instance = create_ansible_mock(PARAMS_GET_BY_NAME)
        mock_ansible_module.return_value = mock_ansible_instance

        VolumeFactsModule().run()

        mock_ansible_instance.fail_json.assert_called_once()
    def test_should_get_all_volumes_and_appliance_information(self):
        self.resource.get_all.return_value = [{"name": "Test Volume"}]
        self.resource.get_extra_managed_storage_volume_paths.return_value = ['/path1', '/path2']
        self.resource.get_attachable_volumes.return_value = [{"name": "attachable Volume 1"}]

        self.mock_ansible_module.params = PARAMS_GET_ALL_WITH_OPTIONS

        VolumeFactsModule().run()

        self.mock_ansible_module.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_get_all_volumes(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_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

        VolumeFactsModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            ansible_facts=dict(storage_volumes=[{
                "name": "Test Volume"
            }]))