diff --git a/tests/integrational/test_cleanup.py b/tests/integrational/test_cleanup.py index 955994db..724c16d4 100644 --- a/tests/integrational/test_cleanup.py +++ b/tests/integrational/test_cleanup.py @@ -16,6 +16,7 @@ def setUpClass(cls): from flask import Flask cls.app = Flask(__name__) + cls.app.sessions = None from core import db cls.app.database = db.Database(config.DATABASE) @@ -24,24 +25,34 @@ def setUp(self): self.ctx = self.app.app_context() self.ctx.push() - def tearDown(self): - self.ctx.pop() - - def test_file_deletion(self): with patch( 'core.utils.init.home_dir', Mock(return_value=config.BASE_DIR) ), patch( 'core.logger.setup_logging', Mock(return_value=Mock()) + ), patch( + 'flask.current_app.sessions', Mock() + ), patch( + 'core.network.Network', Mock() + ), patch( + 'core.connection.Virsh', Mock() ): from vmmaster import cleanup + self.cleanup = cleanup + from core.sessions import Session + self.session = Session() + self.session.status = 'unknown' - session = Session() - session.status = 'unknown' - session.name = '__test_file_deletion' - session.save() + def tearDown(self): + self.ctx.pop() - session_dir = os.path.join(config.SCREENSHOTS_DIR, str(session.id)) + def test_file_deletion(self): + self.session.name = '__test_file_deletion' + self.session.save() + + session_dir = os.path.join( + config.SCREENSHOTS_DIR, str(self.session.id) + ) system_utils.run_command( ["mkdir", config.SCREENSHOTS_DIR], silent=True) @@ -52,30 +63,20 @@ def test_file_deletion(self): ["touch", os.path.join(session_dir, "file_for_deletion")], silent=True) - cleanup.delete_session_data([session]) + self.cleanup.delete_session_data([self.session]) self.assertEqual(os.path.isdir(session_dir), 0) system_utils.run_command( ["rm", "-rf", config.SCREENSHOTS_DIR], silent=True) def test_outdated_sessions(self): - with patch( - 'core.utils.init.home_dir', Mock(return_value=config.BASE_DIR) - ), patch( - 'core.logger.setup_logging', Mock(return_value=Mock()) - ): - from vmmaster import cleanup - from core.sessions import Session - - session = Session() - session.status = 'unknown' - session.closed = True - session.name = '__test_outdated_sessions' - session.created = \ + self.session.closed = True + self.session.name = '__test_outdated_sessions' + self.session.created = \ datetime.now() - timedelta(days=config.SCREENSHOTS_DAYS, seconds=1) - session.save() + self.session.save() - outdated_sessions = cleanup.old_sessions() + outdated_sessions = self.cleanup.old_sessions() outdated_ids = [s.id for s in outdated_sessions] - self.assertIn(session.id, outdated_ids) - cleanup.delete_session_data([session]) + self.assertIn(self.session.id, outdated_ids) + self.cleanup.delete_session_data([self.session])