<commit_before>from unittest import TestCase

from mock import Mock, patch

from cloudshell.cp.vcenter.commands.retrieve_snapshots import RetrieveSnapshotsCommand


class TestRetrieveSnapshotCommand(TestCase):
    @patch('cloudshell.cp.vcenter.commands.restore_snapshot.SnapshotRetriever.get_vm_snapshots')
    def test_restore_snapshot_should_success_on_existing_snapshot(self, mock_get_vm_snapshots):
        vm = Mock()

        pyvmomi_service = Mock()
        pyvmomi_service.find_by_uuid = Mock(return_value=vm)

        snapshot_restore_command = RetrieveSnapshotsCommand(pyvmomi_service=pyvmomi_service)
        si = Mock()

        mock_get_vm_snapshots.return_value = {'snap1': Mock()}

        # Act
        snapshots = snapshot_restore_command.get_snapshots(si=si, logger=Mock(), vm_uuid='machine1')

        # Assert
        self.assertSequenceEqual(snapshots, ['snap1'])<commit_msg>add unit tests for RetrieveSnapshotsCommand<commit_after>from unittest import TestCase

from mock import Mock, patch
from pyVmomi import vim

from cloudshell.cp.vcenter.commands.retrieve_snapshots import RetrieveSnapshotsCommand


class TestRetrieveSnapshotCommand(TestCase):
    @patch('cloudshell.cp.vcenter.commands.restore_snapshot.SnapshotRetriever.get_vm_snapshots')
    def test_restore_snapshot_should_success_on_existing_snapshot(self, mock_get_vm_snapshots):
        vm = Mock()

        pyvmomi_service = Mock()
        pyvmomi_service.find_by_uuid = Mock(return_value=vm)

        snapshot_restore_command = RetrieveSnapshotsCommand(pyvmomi_service=pyvmomi_service)
        si = Mock()

        mock_get_vm_snapshots.return_value = {'snap1': Mock()}

        # Act
        snapshots = snapshot_restore_command.get_snapshots(si=si, logger=Mock(), vm_uuid='machine1')

        # Assert
        self.assertSequenceEqual(snapshots, ['snap1'])

    def test_exception_raised_when_get_snapshots_fails_with_no_permission_exception(self):
        vm = Mock()
        vm.CreateSnapshot = Mock(side_effect=vim.fault.NoPermission())
        pyvmomi_service = Mock()
        pyvmomi_service.find_by_uuid = Mock(return_value=vm)

        retrieve_snapshot_command = RetrieveSnapshotsCommand(pyvmomi_service)
        si = Mock()
        logger = Mock()

        # Act + Assert
        self.assertRaises(Exception, retrieve_snapshot_command.get_snapshots, si, logger, 'machine1')
        logger.error.assert_called()

    def test_exception_raised_when_save_snapshot_fails_with_general_exception(self):
        vm = Mock()
        vm.CreateSnapshot = Mock(side_effect=Exception())
        pyvmomi_service = Mock()
        pyvmomi_service.find_by_uuid = Mock(return_value=vm)

        retrieve_snapshot_command = RetrieveSnapshotsCommand(pyvmomi_service)
        si = Mock()
        logger = Mock()

        # Act + Assert
        self.assertRaises(Exception, retrieve_snapshot_command.get_snapshots, si, logger, 'machine1')
        logger.error.assert_called()
{'fraction_non_alphanumeric': 0.0833051134439553, 'fraction_numerical': 0.0027091093802912294, 'mean_word_length': 4.0843373493975905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2014355', 'n_tokens_mistral': 893, 'n_tokens_neox': 889, 'n_words': 135}