def test_get_snapshot_snapshot_has_no_root(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        params = Mock()
        params.snapshot = "aa/bb/ee"

        template = Mock(vim.VirtualMachine)
        template.snapshot = Mock()
        template.snapshot.rootSnapshotList = None

        # Act
        self.assertRaises(ValueError, pv_service._get_snapshot, params, template)
Example #2
0
    def test_snapshot_mount_error_raises_Exception(
        self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen, mock_lvm_snap_remove
    ):
        mock_get_vol_fs_type.return_value = "xfs"
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_lvcreate_process, mock_mount_process = Mock(), Mock()

        mock_lvcreate_process.communicate.return_value = "", ""
        mock_lvcreate_process.returncode = 0

        mock_mount_process.communicate.return_value = "", "mount error"
        mock_mount_process.returncode = 1

        mock_popen.side_effect = [mock_lvcreate_process, mock_mount_process]

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"

        with self.assertRaises(Exception) as cm:
            lvm.lvm_snap(backup_opt)
        the_exception = cm.exception
        self.assertIn("lvm snapshot mounting error", the_exception.message)

        mock_lvm_snap_remove.assert_called_once_with(backup_opt)
Example #3
0
    def test_mysql_mode_locks_unlocks_tables(
        self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen
    ):
        mock_get_vol_fs_type.return_value = "xfs"
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_process = Mock()
        mock_process.communicate.return_value = "", ""
        mock_process.returncode = 0
        mock_popen.return_value = mock_process

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"
        backup_opt.mode = "mysql"
        backup_opt.mysql_db_inst = Mock()
        mock_cursor = Mock()
        backup_opt.mysql_db_inst.cursor.return_value = mock_cursor

        self.assertTrue(lvm.lvm_snap(backup_opt))

        first_call = call("FLUSH TABLES WITH READ LOCK")
        second_call = call("UNLOCK TABLES")
        self.assertEquals(first_call, mock_cursor.execute.call_args_list[0])
        self.assertEquals(second_call, mock_cursor.execute.call_args_list[1])
    def test_get_snapshot_snapshot(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        params = Mock()
        params.snapshot = "aa/bb/ee"

        template = Mock(vim.VirtualMachine)
        template.snapshot = Mock()

        aa = Mock(spec=[])
        bb1 = Mock(spec=[])
        bb2 = Mock(spec=[])
        cc = Mock(spec=[])
        dd = Mock(spec=[])
        ee = Mock(spec=[])
        aa.name = "aa"
        aa.createTime = 1

        bb1.name = "bb"
        bb1.createTime = 1

        bb2.name = "bb"
        bb2.createTime = 2

        cc.name = "cc"
        cc.createTime = 1

        dd.name = "dd"
        dd.createTime = 1

        ee.name = "ee"
        ee.createTime = 1

        ee.snapshot = Mock()

        aa.childSnapshotList = [dd, bb1, bb2]
        dd.childSnapshotList = [cc]
        bb2.childSnapshotList = [dd, cc, ee]

        template.snapshot.rootSnapshotList = [aa]

        # Act
        res = pv_service._get_snapshot(params, template)

        self.assertEqual(res, ee.snapshot)
    def test_cet_current_snapshot_returns_none_when_no_snapshot_exists(self):
        # Arrange
        vm = Mock()
        vm.snapshot = None

        # Act
        current_snapshot_name = SnapshotRetriever.get_current_snapshot_name(vm)

        # assert
        self.assertIsNone(current_snapshot_name)
    def test_empty_dict_when_vm_has_no_snapshots(self):
        # Arrange
        vm = Mock()
        vm.snapshot = None

        # Act
        all_snapshots = SnapshotRetriever.get_vm_snapshots(vm)

        # assert
        self.assertSequenceEqual(all_snapshots, {})
Example #7
0
    def test_with_auto_snap_param_path_mismatch_raises(
        self, mock_create_dir, mock_get_lvm_info, mock_validate_lvm_params
    ):
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}

        backup_opt = Mock()
        backup_opt.snapshot = False
        backup_opt.lvm_auto_snap = "/just/a/path"
        backup_opt.path_to_backup = "/different"
        backup_opt.lvm_dirmount = "/var/mountpoint"

        self.assertRaises(Exception, lvm.lvm_snap, backup_opt)
Example #8
0
    def test_with_snapshot_opt_simple_sets_correct_path_and_raises_on_perm(self, mock_create_dir, mock_get_lvm_info):
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "invalid_value"

        with self.assertRaises(Exception) as cm:
            lvm.lvm_snap(backup_opt)
        the_exception = cm.exception
        self.assertIn("Invalid value for option lvm-snap-perm", the_exception.message)
    def test_one_snapshot_when_one_snapshot_exists(self):
        # Arrange
        snapshot = Mock()
        snapshot.name = "snap1"
        snapshot.childSnapshotList = []

        vm = Mock()
        vm.snapshot = Mock()
        vm.snapshot.rootSnapshotList = [snapshot]

        # Act
        all_snapshots = SnapshotRetriever.get_vm_snapshots(vm)

        # assert
        self.assertSequenceEqual(all_snapshots.keys(), ["snap1"])
Example #10
0
    def test_ok(self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen, mock_validate_lvm_params):
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_process = Mock()
        mock_process.communicate.return_value = "", ""
        mock_process.returncode = 0
        mock_popen.return_value = mock_process

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"
        backup_opt.lvm_volgroup = ""
        backup_opt.lvm_srcvol = ""

        self.assertTrue(lvm.lvm_snap(backup_opt))
    def test_two_snapshots_when_root_snapshot_has_a_child(self):
        # Arrange
        child = Mock()
        child.name = "child"
        child.childSnapshotList = []

        root = Mock()
        root.name = "root"
        root.childSnapshotList = [child]

        vm = Mock()
        vm.snapshot = Mock()
        vm.snapshot.rootSnapshotList = [root]

        # Act
        all_snapshots = SnapshotRetriever.get_vm_snapshots(vm)

        # assert
        self.assertSequenceEqual(all_snapshots.keys(), ["root", "root/child"])
Example #12
0
    def test_snapshot_fails(
        self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen, mock_validate_lvm_params
    ):
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_process = Mock()
        mock_process.communicate.return_value = "", ""
        mock_process.returncode = 1
        mock_popen.return_value = mock_process

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"

        with self.assertRaises(Exception) as cm:
            lvm.lvm_snap(backup_opt)
        the_exception = cm.exception
        self.assertIn("lvm snapshot creation error", the_exception.message)
Example #13
0
 def test_no_lvm_configured_returns_false(self, mock_validate_lvm_params):
     backup_opt = Mock()
     backup_opt.lvm_auto_snap = ""
     mock_validate_lvm_params.return_value = False
     backup_opt.snapshot = False
     self.assertFalse(lvm.lvm_snap(backup_opt))