Hide keyboard shortcuts

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

"""Test certbot.reverter.""" 

import csv 

import logging 

import os 

import shutil 

import tempfile 

import unittest 

 

import mock 

import six 

 

from certbot import errors 

 

from certbot.tests import util as test_util 

 

 

class ReverterCheckpointLocalTest(test_util.ConfigTestCase): 

# pylint: disable=too-many-instance-attributes, too-many-public-methods 

"""Test the Reverter Class.""" 

def setUp(self): 

super(ReverterCheckpointLocalTest, self).setUp() 

from certbot.reverter import Reverter 

 

# Disable spurious errors... we are trying to test for them 

logging.disable(logging.CRITICAL) 

 

self.reverter = Reverter(self.config) 

 

tup = setup_test_files() 

self.config1, self.config2, self.dir1, self.dir2, self.sets = tup 

 

def tearDown(self): 

shutil.rmtree(self.config.work_dir) 

shutil.rmtree(self.dir1) 

shutil.rmtree(self.dir2) 

 

logging.disable(logging.NOTSET) 

 

@mock.patch("certbot.reverter.Reverter._read_and_append") 

def test_no_change(self, mock_read): 

mock_read.side_effect = OSError("cannot even") 

try: 

self.reverter.add_to_checkpoint(self.sets[0], "save1") 

except OSError: 

pass 

self.reverter.finalize_checkpoint("blah") 

path = os.listdir(self.reverter.config.backup_dir)[0] 

no_change = os.path.join(self.reverter.config.backup_dir, path, "CHANGES_SINCE") 

with open(no_change, "r") as f: 

x = f.read() 

self.assertTrue("No changes" in x) 

 

@test_util.broken_on_windows 

def test_basic_add_to_temp_checkpoint(self): 

# These shouldn't conflict even though they are both named config.txt 

self.reverter.add_to_temp_checkpoint(self.sets[0], "save1") 

self.reverter.add_to_temp_checkpoint(self.sets[1], "save2") 

 

self.assertTrue(os.path.isdir(self.config.temp_checkpoint_dir)) 

self.assertEqual(get_save_notes( 

self.config.temp_checkpoint_dir), "save1save2") 

self.assertFalse(os.path.isfile( 

os.path.join(self.config.temp_checkpoint_dir, "NEW_FILES"))) 

 

self.assertEqual( 

get_filepaths(self.config.temp_checkpoint_dir), 

"{0}\n{1}\n".format(self.config1, self.config2)) 

 

def test_add_to_checkpoint_copy_failure(self): 

with mock.patch("certbot.reverter.shutil.copy2") as mock_copy2: 

mock_copy2.side_effect = IOError("bad copy") 

self.assertRaises( 

errors.ReverterError, self.reverter.add_to_checkpoint, 

self.sets[0], "save1") 

 

def test_checkpoint_conflict(self): 

"""Make sure that checkpoint errors are thrown appropriately.""" 

config3 = os.path.join(self.dir1, "config3.txt") 

self.reverter.register_file_creation(True, config3) 

update_file(config3, "This is a new file!") 

 

self.reverter.add_to_checkpoint(self.sets[2], "save1") 

# This shouldn't throw an error 

self.reverter.add_to_temp_checkpoint(self.sets[0], "save2") 

# Raise error 

self.assertRaises(errors.ReverterError, self.reverter.add_to_checkpoint, 

self.sets[2], "save3") 

# Should not cause an error 

self.reverter.add_to_checkpoint(self.sets[1], "save4") 

 

# Check to make sure new files are also checked... 

self.assertRaises(errors.ReverterError, self.reverter.add_to_checkpoint, 

set([config3]), "invalid save") 

 

@test_util.broken_on_windows 

def test_multiple_saves_and_temp_revert(self): 

self.reverter.add_to_temp_checkpoint(self.sets[0], "save1") 

update_file(self.config1, "updated-directive") 

self.reverter.add_to_temp_checkpoint(self.sets[0], "save2-updated dir") 

update_file(self.config1, "new directive change that we won't keep") 

 

self.reverter.revert_temporary_config() 

self.assertEqual(read_in(self.config1), "directive-dir1") 

 

def test_multiple_registration_fail_and_revert(self): 

 

config3 = os.path.join(self.dir1, "config3.txt") 

update_file(config3, "Config3") 

config4 = os.path.join(self.dir2, "config4.txt") 

update_file(config4, "Config4") 

 

# Test multiple registrations and two registrations at once 

self.reverter.register_file_creation(True, self.config1) 

self.reverter.register_file_creation(True, self.config2) 

self.reverter.register_file_creation(True, config3, config4) 

 

# Simulate Certbot crash... recovery routine is run 

self.reverter.recovery_routine() 

 

self.assertFalse(os.path.isfile(self.config1)) 

self.assertFalse(os.path.isfile(self.config2)) 

self.assertFalse(os.path.isfile(config3)) 

self.assertFalse(os.path.isfile(config4)) 

 

@test_util.broken_on_windows 

def test_multiple_registration_same_file(self): 

self.reverter.register_file_creation(True, self.config1) 

self.reverter.register_file_creation(True, self.config1) 

self.reverter.register_file_creation(True, self.config1) 

self.reverter.register_file_creation(True, self.config1) 

 

files = get_new_files(self.config.temp_checkpoint_dir) 

 

self.assertEqual(len(files), 1) 

 

def test_register_file_creation_write_error(self): 

m_open = mock.mock_open() 

with mock.patch("certbot.reverter.open", m_open, create=True): 

m_open.side_effect = OSError("bad open") 

self.assertRaises( 

errors.ReverterError, self.reverter.register_file_creation, 

True, self.config1) 

 

def test_bad_registration(self): 

# Made this mistake and want to make sure it doesn't happen again... 

self.assertRaises( 

errors.ReverterError, self.reverter.register_file_creation, 

"filepath") 

 

@test_util.broken_on_windows 

def test_register_undo_command(self): 

coms = [ 

["a2dismod", "ssl"], 

["a2dismod", "rewrite"], 

["cleanslate"] 

] 

for com in coms: 

self.reverter.register_undo_command(True, com) 

 

act_coms = get_undo_commands(self.config.temp_checkpoint_dir) 

 

for a_com, com in six.moves.zip(act_coms, coms): 

self.assertEqual(a_com, com) 

 

def test_bad_register_undo_command(self): 

m_open = mock.mock_open() 

with mock.patch("certbot.reverter.open", m_open, create=True): 

m_open.side_effect = OSError("bad open") 

self.assertRaises( 

errors.ReverterError, self.reverter.register_undo_command, 

True, ["command"]) 

 

@test_util.broken_on_windows 

@mock.patch("certbot.util.run_script") 

def test_run_undo_commands(self, mock_run): 

mock_run.side_effect = ["", errors.SubprocessError] 

coms = [ 

["invalid_command"], 

["a2dismod", "ssl"], 

] 

for com in coms: 

self.reverter.register_undo_command(True, com) 

 

self.reverter.revert_temporary_config() 

 

self.assertEqual(mock_run.call_count, 2) 

 

def test_recovery_routine_in_progress_failure(self): 

self.reverter.add_to_checkpoint(self.sets[0], "perm save") 

 

# pylint: disable=protected-access 

self.reverter._recover_checkpoint = mock.MagicMock( 

side_effect=errors.ReverterError) 

self.assertRaises(errors.ReverterError, self.reverter.recovery_routine) 

 

def test_recover_checkpoint_revert_temp_failures(self): 

 

mock_recover = mock.MagicMock( 

side_effect=errors.ReverterError("e")) 

 

# pylint: disable=protected-access 

self.reverter._recover_checkpoint = mock_recover 

 

self.reverter.add_to_temp_checkpoint(self.sets[0], "config1 save") 

 

self.assertRaises( 

errors.ReverterError, self.reverter.revert_temporary_config) 

 

def test_recover_checkpoint_rollback_failure(self): 

mock_recover = mock.MagicMock( 

side_effect=errors.ReverterError("e")) 

# pylint: disable=protected-access 

self.reverter._recover_checkpoint = mock_recover 

 

self.reverter.add_to_checkpoint(self.sets[0], "config1 save") 

self.reverter.finalize_checkpoint("Title") 

 

self.assertRaises( 

errors.ReverterError, self.reverter.rollback_checkpoints, 1) 

 

def test_recover_checkpoint_copy_failure(self): 

self.reverter.add_to_temp_checkpoint(self.sets[0], "save1") 

 

with mock.patch("certbot.reverter.shutil.copy2") as mock_copy2: 

mock_copy2.side_effect = OSError("bad copy") 

self.assertRaises( 

errors.ReverterError, self.reverter.revert_temporary_config) 

 

def test_recover_checkpoint_rm_failure(self): 

self.reverter.add_to_temp_checkpoint(self.sets[0], "temp save") 

 

with mock.patch("certbot.reverter.shutil.rmtree") as mock_rmtree: 

mock_rmtree.side_effect = OSError("Cannot remove tree") 

self.assertRaises( 

errors.ReverterError, self.reverter.revert_temporary_config) 

 

@test_util.broken_on_windows 

@mock.patch("certbot.reverter.logger.warning") 

def test_recover_checkpoint_missing_new_files(self, mock_warn): 

self.reverter.register_file_creation( 

True, os.path.join(self.dir1, "missing_file.txt")) 

self.reverter.revert_temporary_config() 

self.assertEqual(mock_warn.call_count, 1) 

 

@mock.patch("certbot.reverter.os.remove") 

def test_recover_checkpoint_remove_failure(self, mock_remove): 

self.reverter.register_file_creation(True, self.config1) 

mock_remove.side_effect = OSError("Can't remove") 

self.assertRaises( 

errors.ReverterError, self.reverter.revert_temporary_config) 

 

@test_util.broken_on_windows 

def test_recovery_routine_temp_and_perm(self): 

# Register a new perm checkpoint file 

config3 = os.path.join(self.dir1, "config3.txt") 

self.reverter.register_file_creation(False, config3) 

update_file(config3, "This is a new perm file!") 

 

# Add changes to perm checkpoint 

self.reverter.add_to_checkpoint(self.sets[0], "perm save1") 

update_file(self.config1, "updated perm config1") 

self.reverter.add_to_checkpoint(self.sets[1], "perm save2") 

update_file(self.config2, "updated perm config2") 

 

# Add changes to a temporary checkpoint 

self.reverter.add_to_temp_checkpoint(self.sets[0], "temp save1") 

update_file(self.config1, "second update now temp config1") 

 

# Register a new temp checkpoint file 

config4 = os.path.join(self.dir2, "config4.txt") 

self.reverter.register_file_creation(True, config4) 

update_file(config4, "New temporary file!") 

 

# Now erase everything 

self.reverter.recovery_routine() 

 

# Now Run tests 

# These were new files.. they should be removed 

self.assertFalse(os.path.isfile(config3)) 

self.assertFalse(os.path.isfile(config4)) 

 

# Check to make sure everything got rolled back appropriately 

self.assertEqual(read_in(self.config1), "directive-dir1") 

self.assertEqual(read_in(self.config2), "directive-dir2") 

 

 

class TestFullCheckpointsReverter(test_util.ConfigTestCase): 

# pylint: disable=too-many-instance-attributes 

"""Tests functions having to deal with full checkpoints.""" 

def setUp(self): 

super(TestFullCheckpointsReverter, self).setUp() 

from certbot.reverter import Reverter 

# Disable spurious errors... 

logging.disable(logging.CRITICAL) 

 

self.reverter = Reverter(self.config) 

 

tup = setup_test_files() 

self.config1, self.config2, self.dir1, self.dir2, self.sets = tup 

 

def tearDown(self): 

shutil.rmtree(self.config.work_dir) 

shutil.rmtree(self.dir1) 

shutil.rmtree(self.dir2) 

 

logging.disable(logging.NOTSET) 

 

def test_rollback_improper_inputs(self): 

self.assertRaises( 

errors.ReverterError, self.reverter.rollback_checkpoints, "-1") 

self.assertRaises( 

errors.ReverterError, self.reverter.rollback_checkpoints, -1000) 

self.assertRaises( 

errors.ReverterError, self.reverter.rollback_checkpoints, "one") 

 

@test_util.broken_on_windows 

def test_rollback_finalize_checkpoint_valid_inputs(self): 

 

config3 = self._setup_three_checkpoints() 

 

# Check resulting backup directory 

self.assertEqual(len(os.listdir(self.config.backup_dir)), 3) 

# Check rollbacks 

# First rollback 

self.reverter.rollback_checkpoints(1) 

self.assertEqual(read_in(self.config1), "update config1") 

self.assertEqual(read_in(self.config2), "update config2") 

# config3 was not included in checkpoint 

self.assertEqual(read_in(config3), "Final form config3") 

 

# Second rollback 

self.reverter.rollback_checkpoints(1) 

self.assertEqual(read_in(self.config1), "update config1") 

self.assertEqual(read_in(self.config2), "directive-dir2") 

self.assertFalse(os.path.isfile(config3)) 

 

# One dir left... check title 

all_dirs = os.listdir(self.config.backup_dir) 

self.assertEqual(len(all_dirs), 1) 

self.assertTrue( 

"First Checkpoint" in get_save_notes( 

os.path.join(self.config.backup_dir, all_dirs[0]))) 

# Final rollback 

self.reverter.rollback_checkpoints(1) 

self.assertEqual(read_in(self.config1), "directive-dir1") 

 

def test_finalize_checkpoint_no_in_progress(self): 

# No need to warn for this... just make sure there are no errors. 

self.reverter.finalize_checkpoint("No checkpoint...") 

 

@mock.patch("certbot.reverter.shutil.move") 

def test_finalize_checkpoint_cannot_title(self, mock_move): 

self.reverter.add_to_checkpoint(self.sets[0], "perm save") 

mock_move.side_effect = OSError("cannot move") 

 

self.assertRaises( 

errors.ReverterError, self.reverter.finalize_checkpoint, "Title") 

 

@mock.patch("certbot.reverter.compat.os_rename") 

def test_finalize_checkpoint_no_rename_directory(self, mock_rename): 

 

self.reverter.add_to_checkpoint(self.sets[0], "perm save") 

mock_rename.side_effect = OSError 

 

self.assertRaises( 

errors.ReverterError, self.reverter.finalize_checkpoint, "Title") 

 

@test_util.broken_on_windows 

@mock.patch("certbot.reverter.logger") 

def test_rollback_too_many(self, mock_logger): 

# Test no exist warning... 

self.reverter.rollback_checkpoints(1) 

self.assertEqual(mock_logger.warning.call_count, 1) 

 

# Test Generic warning 

self._setup_three_checkpoints() 

mock_logger.warning.call_count = 0 

self.reverter.rollback_checkpoints(4) 

self.assertEqual(mock_logger.warning.call_count, 1) 

 

@test_util.broken_on_windows 

def test_multi_rollback(self): 

config3 = self._setup_three_checkpoints() 

self.reverter.rollback_checkpoints(3) 

 

self.assertEqual(read_in(self.config1), "directive-dir1") 

self.assertEqual(read_in(self.config2), "directive-dir2") 

self.assertFalse(os.path.isfile(config3)) 

 

@test_util.patch_get_utility() 

def test_view_config_changes(self, mock_output): 

"""This is not strict as this is subject to change.""" 

self._setup_three_checkpoints() 

 

# Make sure it doesn't throw any errors 

self.reverter.view_config_changes() 

 

# Make sure notification is output 

self.assertEqual(mock_output().notification.call_count, 1) 

 

@mock.patch("certbot.reverter.logger") 

def test_view_config_changes_no_backups(self, mock_logger): 

self.reverter.view_config_changes() 

self.assertTrue(mock_logger.info.call_count > 0) 

 

def test_view_config_changes_bad_backups_dir(self): 

# There shouldn't be any "in progress directories when this is called 

# It must just be clean checkpoints 

os.makedirs(os.path.join(self.config.backup_dir, "in_progress")) 

 

self.assertRaises( 

errors.ReverterError, self.reverter.view_config_changes) 

 

def test_view_config_changes_for_logging(self): 

self._setup_three_checkpoints() 

 

config_changes = self.reverter.view_config_changes(for_logging=True) 

 

self.assertTrue("First Checkpoint" in config_changes) 

self.assertTrue("Second Checkpoint" in config_changes) 

self.assertTrue("Third Checkpoint" in config_changes) 

 

def _setup_three_checkpoints(self): 

"""Generate some finalized checkpoints.""" 

# Checkpoint1 - config1 

self.reverter.add_to_checkpoint(self.sets[0], "first save") 

self.reverter.finalize_checkpoint("First Checkpoint") 

 

update_file(self.config1, "update config1") 

 

# Checkpoint2 - new file config3, update config2 

config3 = os.path.join(self.dir1, "config3.txt") 

self.reverter.register_file_creation(False, config3) 

update_file(config3, "directive-config3") 

self.reverter.add_to_checkpoint(self.sets[1], "second save") 

self.reverter.finalize_checkpoint("Second Checkpoint") 

 

update_file(self.config2, "update config2") 

update_file(config3, "update config3") 

 

# Checkpoint3 - update config1, config2 

self.reverter.add_to_checkpoint(self.sets[2], "third save") 

self.reverter.finalize_checkpoint("Third Checkpoint - Save both") 

 

update_file(self.config1, "Final form config1") 

update_file(self.config2, "Final form config2") 

update_file(config3, "Final form config3") 

 

return config3 

 

 

def setup_test_files(): 

"""Setup sample configuration files.""" 

dir1 = tempfile.mkdtemp("dir1") 

dir2 = tempfile.mkdtemp("dir2") 

config1 = os.path.join(dir1, "config.txt") 

config2 = os.path.join(dir2, "config.txt") 

with open(config1, "w") as file_fd: 

file_fd.write("directive-dir1") 

with open(config2, "w") as file_fd: 

file_fd.write("directive-dir2") 

 

sets = [set([config1]), 

set([config2]), 

set([config1, config2])] 

 

return config1, config2, dir1, dir2, sets 

 

 

def get_save_notes(dire): 

"""Read save notes""" 

return read_in(os.path.join(dire, "CHANGES_SINCE")) 

 

 

def get_filepaths(dire): 

"""Get Filepaths""" 

return read_in(os.path.join(dire, "FILEPATHS")) 

 

 

def get_new_files(dire): 

"""Get new files.""" 

return read_in(os.path.join(dire, "NEW_FILES")).splitlines() 

 

 

def get_undo_commands(dire): 

"""Get new files.""" 

with open(os.path.join(dire, "COMMANDS")) as csvfile: 

return list(csv.reader(csvfile)) 

 

 

def read_in(path): 

"""Read in a file, return the str""" 

with open(path, "r") as file_fd: 

return file_fd.read() 

 

 

def update_file(filename, string): 

"""Update a file with a new value.""" 

with open(filename, "w") as file_fd: 

file_fd.write(string) 

 

 

if __name__ == "__main__": 

unittest.main() # pragma: no cover