Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

import mock 

import SR 

import testlib 

import unittest 

import uuid 

import VDI 

import vhdutil 

import xs_errors 

import util 

import errno 

import cbtutil 

from bitarray import bitarray 

import base64 

import xmlrpclib 

 

 

class TestVDI(VDI.VDI): 

    def load(self, vdi_uuid): 

        self.vdi_type = vhdutil.VDI_TYPE_VHD 

        self._state_mock = mock.Mock() 

        self.path = "/mock/sr_path/" + str(vdi_uuid) 

        self.block_tracking_state = False 

 

    @property 

    def state_mock(self): 

        return self._state_mock 

 

    def _get_blocktracking_status(self, uuid=None): 

        return self.block_tracking_state 

 

    def _ensure_cbt_space(self): 

        super(TestVDI, self)._ensure_cbt_space() 

        self.state_mock._ensure_cbt_space() 

 

    def _get_cbt_logpath(self, uuid): 

        super(TestVDI, self)._get_cbt_logpath(uuid) 

        self.state_mock._get_cbt_logpath(uuid) 

        return "/mock/sr_path/{0}.log".format(uuid) 

 

    def _create_cbt_log(self): 

        logpath = super(TestVDI, self)._create_cbt_log() 

        self.state_mock._create_cbt_log() 

        self.block_tracking_state = True 

        return logpath 

 

    def _delete_cbt_log(self): 

        self.state_mock._delete_cbt_log() 

        self.block_tracking_state = False 

 

    def _rename(self, from_path, to_path): 

        self.state_mock._rename(from_path, to_path) 

 

    def _do_snapshot(self, sr_uuid, vdi_uuid, snapType, 

                     cloneOp=False, secondary=None, cbtlog=None): 

        self.state_mock._do_snapshot(sr_uuid, vdi_uuid, snapType, cloneOp, 

                                     secondary, cbtlog) 

 

    def _activate_cbt_log(self, logname): 

        self.state_mock._activate_cbt_log(logname) 

 

    def _deactivate_cbt_log(self, logname): 

        self.state_mock._deactivate_cbt_log(logname) 

 

class TestCBT(unittest.TestCase): 

 

    def setUp(self): 

        self.sr = mock.MagicMock() 

        self.vdi_uuid = uuid.uuid4() 

        self.sr_uuid = uuid.uuid4() 

        self.xenapi = mock.MagicMock() 

        self.xenapi.VDI = mock.MagicMock() 

 

        self.sr.session = mock.MagicMock() 

 

        self.sr.session.xenapi = self.xenapi 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.VDI._cbt_op', autospec=True) 

    def test_configure_blocktracking_enable_success(self, context, mock_cbt, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

 

        self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

        self._check_tapdisk_paused_and_resumed(mock_bt_vdi, self.vdi_uuid) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.cbtutil', autospec=True) 

    def test_configure_blocktracking_enable_already_enabled(self, context, mock_cbt, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

 

        self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

        self._check_setting_not_changed() 

        self._check_tapdisk_not_modified(mock_bt_vdi) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('VDI.VDI._cbt_op', autospec=True) 

    @mock.patch('lock.Lock') 

    def test_configure_blocktracking_disable_when_enabled_without_parent( 

            self, context, mock_lock, mock_cbt, mock_logchecker, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

 

        self._set_initial_state(self.vdi, True) 

 

        self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, False) 

 

        self._check_tapdisk_paused_and_resumed(mock_bt_vdi, self.vdi_uuid) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock') 

    def test_configure_blocktracking_disable_when_enabled_with_parent( 

            self, context, mock_lock, mock_logcheck, mock_cbt, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

 

        self._set_initial_state(self.vdi, True) 

        (parent_uuid, child_uuid) = self._set_CBT_chain_state(mock_logcheck, 

                                                              mock_cbt, True) 

        logpath = self.vdi._get_cbt_logpath(self.vdi_uuid) 

        expected_parent_path = '/mock/sr_path/%s.log' % parent_uuid 

 

        self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, False) 

 

        logfile = self._check_setting_state(self.vdi, False) 

        self._check_tapdisk_paused_and_resumed(mock_bt_vdi, self.vdi_uuid) 

        parent_call = [mock.call(logpath)] 

        child_call = [mock.call(expected_parent_path, uuid.UUID(int=0))] 

        mock_cbt.get_cbt_parent.assert_has_calls(parent_call) 

        mock_cbt.set_cbt_child.assert_has_calls(child_call) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    def test_configure_blocktracking_disable_already_disabled(self, context, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

 

        self._set_initial_state(self.vdi, False) 

 

        self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, False) 

 

        self._check_setting_not_changed() 

        self._check_tapdisk_not_modified(mock_bt_vdi) 

 

    @testlib.with_context 

    def test_configure_blocktracking_enable_raw_vdi(self, context): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = VDI.VDI(self.sr, self.vdi_uuid) 

        self.vdi.path = "/mock/sr_path/" + str(self.vdi_uuid) 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

    @testlib.with_context 

    def test_configure_blocktracking_enable_snapshot(self, context): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self.xenapi.VDI.get_is_a_snapshot.return_value = True 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.util', autospec=True) 

    @mock.patch('VDI.cbtutil', autospec=True) 

    def test_configure_blocktracking_enable_pause_fail(self, context, mock_cbt, mock_util, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

 

        self._set_initial_state(self.vdi, False) 

        mock_bt_vdi.tap_pause.return_value = False 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.util', autospec=True) 

    def test_configure_blocktracking_disable_pause_fail(self, context, mock_util, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

 

        self._set_initial_state(self.vdi, True) 

        mock_bt_vdi.tap_pause.return_value = False 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, False) 

 

    @testlib.with_context 

    @mock.patch('VDI.util', autospec=True) 

    def test_configure_blocktracking_enable_metadata_no_space(self, context, mock_util): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self.vdi.state_mock._ensure_cbt_space.side_effect = [ xs_errors.XenError('SRNoSpace') ] 

 

        self._set_initial_state(self.vdi, False) 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.cbtutil', autospec=True) 

    def test_configure_blocktracking_enable_metadata_creation_fail(self, context, mock_cbt, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

        self.vdi.state_mock._get_cbt_logpath.side_effect = [ xs_errors.XenError('CBTActivateFailed') ] 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

        self._check_tapdisk_not_modified(mock_bt_vdi) 

        self._check_setting_state(self.vdi, False) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.cbtutil', autospec=True) 

    def test_configure_blocktracking_enable_metadata_initialisation_fail(self, context, mock_cbt, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

        mock_cbt.create_cbt_log.side_effect = Exception(errno.EIO) 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) 

 

        self._check_tapdisk_not_modified(mock_bt_vdi) 

        self._check_setting_state(self.vdi, False) 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI') 

    @mock.patch('lock.Lock') 

    def test_configure_blocktracking_disable_metadata_deletion_fail(self, context, mock_lock, mock_bt_vdi): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        self.vdi.state_mock._delete_cbt_log.side_effect = [ xs_errors.XenError('CBTDeactivateFailed') ] 

 

        with self.assertRaises(SR.SROSError): 

            self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, False) 

 

        #self._check_tapdisk_not_modified(mock_bt_vdi) 

        self._check_setting_state(self.vdi, True) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    def test_activate_no_tracking_success(self, context, mock_cbt): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

        vdi_options = self.vdi.activate(self.sr_uuid, self.vdi_uuid) 

 

        self.assertIsNone(vdi_options) 

        # python2-mock-1.0.1-9.el doesn't support these asserts 

        #mock_cbt.get_cbt_consistency.assert_not_called() 

        self.assertEqual(0, mock_cbt.get_cbt_consistency.call_count) 

        self.assertEqual(0, self.vdi.state_mock._activate_cbt_log.call_count) 

 

    @testlib.with_context 

    @mock.patch('VDI.VDI._cbt_op', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_activate_consistent_success(self, context, mock_lock, mock_cbt): 

        context.setup_error_codes() 

 

        expected_log_path = '/mock/sr_path/{0}.log'.format(self.vdi_uuid) 

        logname = '%s.cbtlog' % self.vdi_uuid 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        mock_cbt.return_value = [True, None] 

 

        log_path = self.vdi.activate(self.sr_uuid, self.vdi_uuid) 

 

        args1 = (expected_log_path) 

        args2 = (expected_log_path, False) 

 

        calls = [mock.call(self.vdi, self.vdi_uuid, 

                           cbtutil.get_cbt_consistency, args1), 

                 mock.call(self.vdi, self.vdi_uuid, 

                           cbtutil.set_cbt_consistency, *args2)] 

 

        mock_cbt.assert_has_calls(calls) 

        self.assertEquals({'cbtlog': expected_log_path}, log_path) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_activate_consistency_check_fail(self, context, mock_lock, mock_cbt): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        mock_cbt.get_cbt_consistency.side_effect = [ False ] 

 

        result = self.vdi.activate(self.sr_uuid, self.vdi_uuid) 

 

        # Check that one message is raised and return is None 

        self._check_setting_state(self.vdi, False) 

        self.xenapi.message.create.assert_called_once() 

        self.assertEquals(result, None) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    def test_deactivate_no_tracking_success(self, context, mock_cbt): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

 

        self.vdi.deactivate(self.sr_uuid, self.vdi_uuid) 

 

        # python2-mock-1.0.1-9.el doesn't support these asserts 

        #mock_cbt.set_cbt_consistency.assert_not_called() 

        self.assertEqual(0, mock_cbt.set_cbt_consistency.call_count) 

        self.assertEqual(0, self.vdi.state_mock._deactivate_cbt_log.call_count) 

 

    @testlib.with_context 

    @mock.patch('VDI.VDI._cbt_op', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_deactivate_success(self, context, mock_lock, mock_cbt): 

        context.setup_error_codes() 

 

        expected_log_path = '/mock/sr_path/{0}.log'.format(self.vdi_uuid) 

        logname = '%s.cbtlog' % self.vdi_uuid 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

 

        self.vdi.deactivate(self.sr_uuid, self.vdi_uuid) 

 

        args = (expected_log_path, True) 

        mock_cbt.assert_called_with(self.vdi, self.vdi_uuid, 

                                    cbtutil.set_cbt_consistency, *args) 

 

    @testlib.with_context 

    def test_snapshot_success_with_CBT_disable(self, context): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

 

        self.vdi.snapshot(self.sr_uuid, self.vdi_uuid) 

 

        self.vdi.state_mock._do_snapshot.assert_called_with(self.sr_uuid, 

                                                            self.vdi_uuid, 

                                                            mock.ANY, 

                                                            mock.ANY, 

                                                            mock.ANY, 

                                                            None) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock') 

    def test_snapshot_success_no_parent(self, context, mock_lock, 

                                        mock_logchecker, mock_cbt): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        (parent_uuid, child_uuid) = self._set_CBT_chain_state(mock_logchecker, 

                                                              mock_cbt, False) 

        snap_uuid = uuid.uuid4() 

 

        self.vdi._cbt_snapshot(snap_uuid, True) 

 

        self._check_CBT_chain_created(self.vdi, mock_cbt, self.vdi_uuid, 

                                      snap_uuid, parent_uuid, child_uuid) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock') 

    def test_snapshot_success_with_parent(self, context, mock_lock, 

                                          mock_logcheck, mock_cbt): 

        #context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        # Set initial state 

        self._set_initial_state(self.vdi, True) 

        (parent_uuid, child_uuid) = self._set_CBT_chain_state(mock_logcheck, 

                                                              mock_cbt, True) 

        snap_uuid = uuid.uuid4() 

 

        self.vdi._cbt_snapshot(snap_uuid, True) 

 

        self._check_CBT_chain_created(self.vdi, mock_cbt, self.vdi_uuid, 

                                      snap_uuid, parent_uuid, child_uuid) 

 

    @testlib.with_context 

    @mock.patch('VDI.VDI._cbt_op', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('VDI.VDI._ensure_cbt_space', autospec=True) 

    @mock.patch('util.SMlog', autospec=True) 

    def test_snapshot_out_of_space_failure(self, context, mock_smlog, 

                        mock_ensure_space, mock_logcheck, mock_cbt): 

        context.setup_error_codes() 

 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        # Set initial state 

        self._set_initial_state(self.vdi, True) 

        snap_uuid = uuid.uuid4() 

 

        # Write util.SMlog calls to smlog_out 

        self.fakesmlog = "" 

        def fakeSMlog(inArg): 

            self.fakesmlog = self.fakesmlog + inArg.strip() 

 

        mock_smlog.side_effect = fakeSMlog 

        mock_ensure_space.side_effect = xs_errors.XenError('SRNoSpace') 

 

        self.vdi._cbt_snapshot(snap_uuid, True) 

 

        self.assertEqual(1, self.vdi.state_mock._delete_cbt_log.call_count) 

        self.assertTrue("insufficient space" in self.fakesmlog) 

        self._check_setting_state(self.vdi, False) 

        self.xenapi.message.create.assert_called_once() 

 

 

    @testlib.with_context 

    @mock.patch('VDI.VDI._cbt_op', autospec=True) 

    def test_resize_cbt_enabled(self, context, mock_cbt): 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        # Set initial state 

        self._set_initial_state(self.vdi, True) 

        size = 2093050 

        logpath = self.vdi._get_cbt_logpath(self.vdi_uuid) 

 

        self.vdi.resize_cbt(self.sr_uuid, self.vdi_uuid, size) 

        args = (logpath, size) 

        mock_cbt.assert_called_with(self.vdi, self.vdi_uuid, 

                                    cbtutil.set_cbt_size, *args) 

        self._check_setting_state(self.vdi, True) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    def test_resize_cbt_disable(self, context, mock_cbt): 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        # Set initial state 

        self._set_initial_state(self.vdi, False) 

        size = 2093050 

 

        self.vdi.resize_cbt(self.sr_uuid, self.vdi_uuid, size) 

        self.assertEqual(0, mock_cbt.set_cbt_size.call_count) 

        self._check_setting_state(self.vdi, False) 

 

    @testlib.with_context 

    @mock.patch('VDI.VDI._cbt_op', autospec=True) 

    def test_resize_exception(self, context, mock_cbt): 

        # Create the test object 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        # Set initial state 

        self._set_initial_state(self.vdi, True) 

        size = 2093050 

        mock_cbt.side_effect = util.CommandException(errno.EINVAL) 

 

        self.vdi.resize_cbt(self.sr_uuid, self.vdi_uuid, size) 

        self._check_setting_state(self.vdi, False) 

        self.xenapi.message.create.assert_called_once() 

 

    @testlib.with_context 

    @mock.patch('lock.Lock') 

    def test_vdi_data_destroy_cbt_enabled(self, context, mock_lock): 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

 

        self.vdi.delete(self.sr_uuid, self.vdi_uuid, data_only=True) 

        self.assertEqual(0, self.vdi.state_mock._delete_cbt_log.call_count) 

 

    @testlib.with_context 

    @mock.patch('lock.Lock') 

    def test_vdi_data_destroy_cbt_disabled(self, context, mock_lock): 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

 

        self.vdi.delete(self.sr_uuid, self.vdi_uuid, data_only=True) 

        self.assertEqual(0, self.vdi.state_mock._delete_cbt_log.call_count) 

 

    @testlib.with_context 

    @mock.patch('lock.Lock') 

    def test_vdi_delete_cbt_disabled(self, context, mock_lock): 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

 

        self.vdi.delete(self.sr_uuid, self.vdi_uuid, data_only=False) 

        self.assertEqual(0, self.vdi.state_mock._delete_cbt_log.call_count) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists') 

    @mock.patch('lock.Lock') 

    def test_vdi_delete_cbt_enabled_no_child(self, context, mock_lock, 

                                             mock_logcheck, mock_cbt): 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        parent_uuid = "parentUUID" 

        child_uuid = uuid.UUID(int=0) 

        mock_cbt.get_cbt_parent.return_value = parent_uuid 

        mock_cbt.get_cbt_child.return_value = child_uuid 

        parent_logpath = self.vdi._get_cbt_logpath(parent_uuid) 

        mock_logcheck.side_effect = [True, False] 

 

        self.vdi.delete(self.sr_uuid, self.vdi_uuid, data_only=False) 

 

        mock_cbt.set_cbt_child.assert_called_with(parent_logpath, child_uuid) 

        self.assertEqual(0, mock_cbt.set_cbt_parent.call_count) 

        self.assertEqual(0, mock_cbt.coalesce_bitmap.call_count) 

        self.vdi.state_mock._delete_cbt_log.assert_called_with() 

 

    @testlib.with_context 

    @mock.patch('blktap2.VDI', autospec=True) 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists') 

    @mock.patch('lock.Lock') 

    def test_vdi_delete_cbt_enabled_with_child(self, context, mock_lock, 

                                               mock_logcheck, mock_cbt, 

                                               mock_bt): 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        parent_uuid = "parentUUID" 

        child_uuid = "childUUID" 

        mock_cbt.get_cbt_parent.return_value = parent_uuid 

        mock_cbt.get_cbt_child.return_value = child_uuid 

        logpath = self.vdi._get_cbt_logpath(self.vdi_uuid) 

        parent_log = self.vdi._get_cbt_logpath(parent_uuid) 

        child_log = self.vdi._get_cbt_logpath(child_uuid) 

        mock_logcheck.side_effect = [True, True] 

        #Mock child log in use 

        mock_cbt.get_cbt_consistency.return_value = False 

 

        self.vdi.delete(self.sr_uuid, self.vdi_uuid, data_only=False) 

 

        mock_cbt.set_cbt_child.assert_called_with(parent_log, child_uuid) 

        mock_cbt.set_cbt_parent.assert_called_with(child_log, parent_uuid) 

        self._check_tapdisk_paused_and_resumed(mock_bt, child_uuid) 

        mock_cbt.coalesce_bitmap.assert_called_with(logpath, child_log) 

        self.vdi.state_mock._delete_cbt_log.assert_called_with() 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists') 

    @mock.patch('lock.Lock') 

    def test_vdi_delete_bitmap_coalesce_exc(self, context, mock_lock, 

                                            mock_logcheck, mock_cbt): 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        parent_uuid = "parentUUID" 

        child_uuid = "childUUID" 

        mock_cbt.get_cbt_parent.return_value = parent_uuid 

        mock_cbt.get_cbt_child.return_value = child_uuid 

        parent_log = self.vdi._get_cbt_logpath(parent_uuid) 

        child_log = self.vdi._get_cbt_logpath(child_uuid) 

        mock_logcheck.side_effect = [True, True, True, True] 

        mock_cbt.coalesce_bitmap.side_effect = util.CommandException(errno.EIO) 

 

        self.vdi.delete(self.sr_uuid, self.vdi_uuid, data_only=False) 

 

        mock_cbt.set_cbt_child.assert_called_with(parent_log, self.vdi_uuid) 

        mock_cbt.set_cbt_parent.assert_called_with(child_log, self.vdi_uuid) 

        self.assertEqual(0, self.vdi.state_mock._delete_cbt_log.call_count) 

 

    @testlib.with_context 

    def test_list_changed_blocks_same_vdi(self, context): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self.xenapi.VDI.get_uuid.return_value = self.vdi_uuid 

 

        with self.assertRaises(SR.SROSError) as exc: 

            self.vdi.list_changed_blocks() 

        # Test CBTChangedBlocksError is raised 

        self.assertEquals(exc.exception.errno, 460) 

 

    @testlib.with_context 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    def test_list_changed_blocks_not_related(self, context, mock_log): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self.xenapi.VDI.get_uuid.return_value = "target_uuid" 

        # Terminate the chain before target_uuid is reached 

        mock_log.side_effect = [True, False] 

 

        with self.assertRaises(SR.SROSError) as exc: 

            self.vdi.list_changed_blocks() 

        # Test CBTChangedBlocksError is raised 

        self.assertEquals(exc.exception.errno, 460) 

 

    @testlib.with_context 

    def test_list_changed_blocks_cbt_disabled(self, context): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, False) 

        self.xenapi.VDI.get_uuid.return_value = "target_uuid" 

 

        with self.assertRaises(SR.SROSError) as exc: 

            self.vdi.list_changed_blocks() 

        # Test CBTChangedBlocksError is raised 

        self.assertEquals(exc.exception.errno, 460) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_list_changed_blocks_success(self, context, mock_lock, 

                                         mock_log, mock_cbt): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        # Set up scenario such that metadata chain looks like 

        # source_vdi->snap_vdi->target_vdi 

        snap_uuid = "snapUUID" 

        target_uuid = "targetUUID" 

        self.xenapi.VDI.get_uuid.return_value = target_uuid 

        mock_cbt.get_cbt_child.side_effect = [snap_uuid, target_uuid] 

        bitmap1 = bitarray(1024) 

        bitmap2 = bitarray(1024) 

        mock_cbt.get_cbt_bitmap.side_effect = [bitmap1.tobytes(), 

                                               bitmap2.tobytes()] 

        bitmap1.bytereverse() 

        bitmap2.bytereverse() 

        expected_string = base64.b64encode((bitmap1 | bitmap2).tobytes()) 

        expected_result = xmlrpclib.dumps((expected_string,), "", True) 

 

        result = self.vdi.list_changed_blocks() 

        # Assert that bitmap is only read for VDIs from source + 1 to target 

        self.assertEqual(2, mock_cbt.get_cbt_bitmap.call_count) 

        self.assertEquals(result, expected_result) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_list_changed_blocks_vdi_resized_success(self, context, 

                                                     mock_lock, mock_log, 

                                                     mock_cbt): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        # Set up scenario such that metadata chain looks like 

        # source_vdi->snap_vdi->target_vdi 

        snap_uuid = "snapUUID" 

        target_uuid = "targetUUID" 

        self.xenapi.VDI.get_uuid.return_value = target_uuid 

        mock_cbt.get_cbt_child.side_effect = [snap_uuid, target_uuid] 

        vdi_size1 = 5242880 # 5MB 

        vdi_size2 = 10485760 # 10MB 

        bitmap1 = bitarray(80) 

        bitmap2 = bitarray(160) 

        mock_cbt.get_cbt_size.side_effect = [vdi_size1, vdi_size2] 

        mock_cbt.get_cbt_bitmap.side_effect = [bitmap1.tobytes(), 

                                               bitmap2.tobytes()] 

        bitmap1.bytereverse() 

        # Pad bitmap1 with extra 0s 

        bitmap1 += 80 * bitarray('0') 

        bitmap2.bytereverse() 

        expected_string = base64.b64encode((bitmap1 | bitmap2).tobytes()) 

        expected_result = xmlrpclib.dumps((expected_string,), "", True) 

 

        result = self.vdi.list_changed_blocks() 

        self.assertEquals(result, expected_result) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_list_changed_blocks_vdi_shrunk(self, context, mock_lock, 

                                            mock_log, mock_cbt): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        target_uuid = "targetUUID" 

        self.xenapi.VDI.get_uuid.return_value = target_uuid 

        vdi_size1 = 10485760 # 10MB 

        vdi_size2 = 5242880 # 5MB 

        mock_cbt.get_cbt_size.side_effect = [vdi_size1, vdi_size2] 

        bitmap1 = bitarray(160) 

        bitmap2 = bitarray(80) 

        mock_cbt.get_cbt_bitmap.side_effect = [bitmap1.tobytes(), 

                                               bitmap2.tobytes()] 

 

        with self.assertRaises(SR.SROSError) as exc: 

            self.vdi.list_changed_blocks() 

        # Test CBTChangedBlocksError is raised 

        self.assertEquals(exc.exception.errno, 459) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_list_changed_blocks_smaller_bitmap(self, context, 

                                                mock_lock, 

                                                mock_log, mock_cbt): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        #target_uuid = "targetUUID" 

        #self.xenapi.VDI.get_uuid.return_value = target_uuid 

        bitmap1 = bitarray(160) 

        bitmap2 = bitarray(80) 

        mock_cbt.get_cbt_bitmap.side_effect = [bitmap1.tobytes(), 

                                               bitmap2.tobytes()] 

 

        with self.assertRaises(SR.SROSError) as exc: 

            self.vdi.list_changed_blocks() 

        # Test CBTChangedBlocksError is raised 

        self.assertEquals(exc.exception.errno, 459) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil', autospec=True) 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_list_changed_blocks_larger_bitmap(self, context, 

                                               mock_lock, 

                                               mock_log, mock_cbt): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        # Set up scenario such that metadata chain looks like 

        # source_vdi->snap_vdi->target_vdi 

        snap_uuid = "snapUUID" 

        target_uuid = "targetUUID" 

        self.xenapi.VDI.get_uuid.return_value = target_uuid 

        mock_cbt.get_cbt_child.side_effect = [snap_uuid, target_uuid] 

        vdi_size1 = 5242880 # 5MB 

        vdi_size2 = 5242880 # 5MB 

        bitmap1 = bitarray(80) 

        bitmap2 = bitarray(160) 

        mock_cbt.get_cbt_size.side_effect = [vdi_size1, vdi_size2] 

        mock_cbt.get_cbt_bitmap.side_effect = [bitmap1.tobytes(), 

                                               bitmap2.tobytes()] 

        bitmap1.bytereverse() 

        bitmap2.bytereverse() 

        # Trim bitmap to the expected size 

        bitmap2 = bitmap2[:80] 

        expected_string = base64.b64encode((bitmap1 | bitmap2).tobytes()) 

        expected_result = xmlrpclib.dumps((expected_string,), "", True) 

 

        result = self.vdi.list_changed_blocks() 

        self.assertEquals(result, expected_result) 

 

    @testlib.with_context 

    @mock.patch('VDI.cbtutil.get_cbt_size') 

    @mock.patch('VDI.cbtutil.get_cbt_child') 

    @mock.patch('VDI.cbtutil._call_cbt_util') 

    @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) 

    @mock.patch('lock.Lock', autospec=True) 

    def test_list_changed_blocks_strip_sensitive_bitmap(self, context, mock_lock, 

                                                        mock_log, mock_call, 

                                                        mock_child, mock_size): 

        context.setup_error_codes() 

        # Create the test object and initialise 

        self.vdi = TestVDI(self.sr, self.vdi_uuid) 

        self._set_initial_state(self.vdi, True) 

        # Set up scenario such that metadata chain looks like 

        # source_vdi->snap_vdi->target_vdi 

        snap_uuid = "snapUUID" 

        target_uuid = "targetUUID" 

        self.xenapi.VDI.get_uuid.return_value = target_uuid 

        mock_child.side_effect = [snap_uuid, target_uuid] 

        vdi_size1 = 8388608 # 8MB 

        vdi_size2 = 8388608 # 8MB 

        bitmap1 = bitarray(128) 

        bitmap2 = bitarray() 

        # strip sensitive byte string 

        data = b'\x0c\xbb3a\xf75Jy\x17\x04\x92;\x0b\x93\xf3E' 

        bitmap2.frombytes(data) 

        mock_size.side_effect = [vdi_size1, vdi_size2] 

        mock_call.side_effect = [bitmap1.tobytes(), data] 

        bitmap1.bytereverse() 

        bitmap2.bytereverse() 

        expected_string = base64.b64encode((bitmap1 | bitmap2).tobytes()) 

        expected_result = xmlrpclib.dumps((expected_string,), "", True) 

 

        result = self.vdi.list_changed_blocks() 

        self.assertEquals(result, expected_result) 

 

 

    def _set_initial_state(self, vdi, cbt_enabled): 

        self.xenapi.VDI.get_is_a_snapshot.return_value = False 

        vdi.block_tracking_state = cbt_enabled 

 

    def _set_CBT_chain_state(self, mock_logcheck, mock_cbt, parent_exists): 

        parent_uuid = None 

        if parent_exists: 

            parent_uuid = "parentUUID" 

            mock_cbt.get_cbt_parent.return_value = parent_uuid 

        mock_logcheck.return_value = parent_exists 

        child_uuid = "childUUID" 

        mock_cbt.get_cbt_child.return_value = child_uuid 

        return (parent_uuid, child_uuid) 

 

    def _check_setting_state(self, vdi, cbt_enabled): 

        self.assertEquals(vdi._get_blocktracking_status(), cbt_enabled) 

        if cbt_enabled: 

            return vdi._get_cbt_logpath(self.vdi_uuid) 

        else: 

            return None 

 

    def _check_setting_not_changed(self): 

        pass 

 

    def _check_tapdisk_refreshed(self, check_mock): 

        check_mock.tap_refresh.assert_called_with(self.sr.session, 

                                                  self.sr_uuid, self.vdi_uuid) 

        # python2-mock-1.0.1-9.el doesn't support these asserts 

        #check_mock.tap_pause.assert_not_called() 

        #check_mock.tap_unpause.assert_not_called() 

 

    def _check_tapdisk_paused_and_resumed(self, check_mock, vdi_uuid): 

        check_mock.tap_pause.assert_called_with(self.sr.session, 

                                                self.sr_uuid, vdi_uuid) 

        check_mock.tap_unpause.assert_called_with(self.sr.session, 

                                                  self.sr_uuid, vdi_uuid) 

 

    def _check_tapdisk_not_modified(self, mock): 

        # python2-mock-1.0.1-9.el doesn't support these asserts 

        #mock.tap_refresh.assert_not_called() 

        #mock.tap_pause.assert_not_called() 

        #mock.tap_unpause.assert_not_called() 

        pass 

 

    def _check_CBT_chain_created(self, vdi, mock_cbt, vdi_uuid, 

                                 snap_uuid, parent_uuid, child_uuid): 

        vdi_logpath = vdi._get_cbt_logpath(vdi_uuid) 

        snap_logpath = vdi._get_cbt_logpath(snap_uuid) 

 

        vdi.state_mock._rename.assert_called_with(vdi_logpath, snap_logpath) 

        vdi.state_mock._create_cbt_log.assert_called_with() 

        calls = [mock.call(snap_logpath, vdi_uuid), 

                 mock.call(vdi_logpath, child_uuid)] 

 

        if parent_uuid: 

            parent_logpath = vdi._get_cbt_logpath(parent_uuid) 

            calls.append(mock.call(parent_logpath, snap_uuid)) 

 

        mock_cbt.set_cbt_parent.assert_called_with(vdi_logpath, snap_uuid) 

        mock_cbt.set_cbt_child.assert_has_calls(calls, any_order=True)