import importlib.util,pathlib,tempfile,unittest,subprocess ROOT=pathlib.Path(__file__).resolve().parent spec=importlib.util.spec_from_file_location('retention',ROOT/'release-retention.py') r=importlib.util.module_from_spec(spec);spec.loader.exec_module(r) class RetentionTests(unittest.TestCase): def test_active_dirty_unpushed_and_symlink_protected(self): with tempfile.TemporaryDirectory() as d: root=pathlib.Path(d); old=root/('a'*40);old.mkdir();active=root/('b'*40);active.mkdir() self.assertFalse(r.eligible(old,root,[str(old/'backend')],clean=True,pushed=True)) self.assertFalse(r.eligible(old,root,[],clean=False,pushed=True)) self.assertFalse(r.eligible(old,root,[],clean=True,pushed=False)) self.assertTrue(r.eligible(old,root,[str(active)],clean=True,pushed=True)) link=root/('c'*40);link.symlink_to(old,target_is_directory=True) self.assertFalse(r.eligible(link,root,[],clean=True,pushed=True)) self.assertFalse(r.eligible(root,root,[],clean=True,pushed=True)) def test_inspect_real_git_dirty_and_unpushed(self): with tempfile.TemporaryDirectory() as d: p=pathlib.Path(d) def g(*a):return subprocess.check_output(['git','-C',d,*a],stderr=subprocess.DEVNULL,text=True) g('init');g('config','user.email','test@example.invalid');g('config','user.name','Test') (p/'source').write_text('preserve');g('add','.');g('commit','-m','fixture') self.assertTrue(r.inspect(p)['unpushed']) g('update-ref','refs/remotes/origin/develop','HEAD') self.assertEqual(r.inspect(p)['unpushed'],'') (p/'untracked').write_text('preserve') self.assertIn('untracked',r.inspect(p)['status']) def test_only_exact_generated_changes_are_disposable(self): with tempfile.TemporaryDirectory() as d: p=pathlib.Path(d) def g(*a):return subprocess.check_output(['git','-C',d,*a],stderr=subprocess.DEVNULL,text=True) g('init');g('config','user.email','test@example.invalid');g('config','user.name','Test') (p/'nginx.conf').write_text('proxy __BACKEND_HOST__ __APP_HOST__ __ADMIN_HOST__');(p/'.dockerignore').write_text('original\n');g('add','.');g('commit','-m','fixture') (p/'nginx.conf').write_text('proxy backend_stage stage.canina.ir stageadmin.canina.ir');(p/'.dockerignore').write_text('original\n\n.support-chat.env\n.revalidation.env\n.stage-*.json\n') (p/'.revalidation.env').write_text('private') self.assertEqual(r.disposable_state(p,'stage'),['.revalidation.env']) (p/'nginx.conf').write_text('uncommitted source') with self.assertRaises(RuntimeError):r.disposable_state(p,'stage') g('checkout','--','nginx.conf');(p/'unknown.env').write_text('unknown') with self.assertRaises(RuntimeError):r.disposable_state(p,'stage') def test_disk_pressure_thresholds(self): spec=importlib.util.spec_from_file_location('watch',ROOT/'disk-watch.py');w=importlib.util.module_from_spec(spec);spec.loader.exec_module(w) self.assertTrue(w.pressure(100*1024**3,9*1024**3)) self.assertTrue(w.pressure(100*1024**3,15*1024**3)) self.assertFalse(w.pressure(100*1024**3,30*1024**3)) if __name__=='__main__':unittest.main()