test_keep_local_archive.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """Tests pour l'option 'conserver l'archive en local' (job.keep_local_archive)."""
  2. import pytest
  3. def _make_job(app, keep_local_archive, with_destination):
  4. from db import db, Job, JobDestination, Destination
  5. dest = None
  6. if with_destination:
  7. dest = Destination(name="d1", host="h", port=22, user="u",
  8. remote_path="/backups", key_name="k", enabled=True)
  9. db.session.add(dest)
  10. db.session.flush()
  11. job = Job(name="j1", type="custom_dir",
  12. config_json='{"source_path": "/opt/app", "excludes": [], "restore": {}}',
  13. cron_expr="", retention_mode="count", retention_value=5,
  14. enabled=True, keep_local_archive=keep_local_archive)
  15. db.session.add(job)
  16. db.session.flush()
  17. if dest:
  18. db.session.add(JobDestination(job_id=job.id, dest_type="ssh", dest_id=dest.id))
  19. db.session.commit()
  20. return job.id
  21. @pytest.fixture(autouse=True)
  22. def _stub_side_effects(monkeypatch):
  23. """Neutralise tout ce qui touche au disque/réseau réel."""
  24. import jobs.custom_dir
  25. import jobs.utils
  26. import retention
  27. import notifications
  28. monkeypatch.setattr(jobs.custom_dir, "backup_custom_dir",
  29. lambda job, instance, backup_dir: ("archive_20260101", "log", False))
  30. monkeypatch.setattr(jobs.utils, "sudo_getsize", lambda path: 123)
  31. monkeypatch.setattr(retention, "apply_retention", lambda job, name, backup_dir: ([], []))
  32. monkeypatch.setattr(retention, "apply_ssh_retention", lambda job, dest, data_dir: [])
  33. monkeypatch.setattr(notifications, "send_job_notification", lambda run, job: None)
  34. class TestKeepLocalArchive:
  35. def test_conserve_si_aucune_destination(self, app, monkeypatch):
  36. import jobs.utils
  37. called = []
  38. monkeypatch.setattr(jobs.utils, "sudo_rm_archive", lambda *a, **k: called.append(a))
  39. with app.app_context():
  40. from jobs.ynh_backup import execute_job
  41. from db import db, Run
  42. job_id = _make_job(app, keep_local_archive=False, with_destination=False)
  43. execute_job(job_id)
  44. run = Run.query.filter_by(job_id=job_id).first()
  45. assert not called
  46. assert "aucune destination configurée" in run.log_text
  47. def test_supprime_si_transfert_reussi(self, app, monkeypatch):
  48. import jobs.utils
  49. import jobs.transfer
  50. called = []
  51. monkeypatch.setattr(jobs.transfer, "transfer_archive", lambda *a, **k: "ok")
  52. monkeypatch.setattr(jobs.utils, "sudo_rm_archive", lambda *a, **k: called.append(a))
  53. with app.app_context():
  54. from jobs.ynh_backup import execute_job
  55. from db import db, Run
  56. job_id = _make_job(app, keep_local_archive=False, with_destination=True)
  57. execute_job(job_id)
  58. run = Run.query.filter_by(job_id=job_id).first()
  59. assert called == [("archive_20260101", app.config["YUNOHOST_BACKUP_DIR"])]
  60. assert "Archive locale supprimée" in run.log_text
  61. def test_conserve_si_transfert_echoue(self, app, monkeypatch):
  62. import jobs.utils
  63. import jobs.transfer
  64. called = []
  65. monkeypatch.setattr(jobs.transfer, "transfer_archive",
  66. lambda *a, **k: (_ for _ in ()).throw(RuntimeError("boom")))
  67. monkeypatch.setattr(jobs.utils, "sudo_rm_archive", lambda *a, **k: called.append(a))
  68. with app.app_context():
  69. from jobs.ynh_backup import execute_job
  70. from db import db, Run
  71. job_id = _make_job(app, keep_local_archive=False, with_destination=True)
  72. execute_job(job_id)
  73. run = Run.query.filter_by(job_id=job_id).first()
  74. assert not called
  75. assert "au moins un transfert a échoué" in run.log_text
  76. def test_conserve_par_defaut(self, app, monkeypatch):
  77. import jobs.utils
  78. import jobs.transfer
  79. called = []
  80. monkeypatch.setattr(jobs.transfer, "transfer_archive", lambda *a, **k: "ok")
  81. monkeypatch.setattr(jobs.utils, "sudo_rm_archive", lambda *a, **k: called.append(a))
  82. with app.app_context():
  83. from jobs.ynh_backup import execute_job
  84. job_id = _make_job(app, keep_local_archive=True, with_destination=True)
  85. execute_job(job_id)
  86. assert not called