async def test_fixup(coresys: CoreSys, tmp_path): """Test fixup.""" store_execute_reset = FixupStoreExecuteReset(coresys) test_repo = Path(tmp_path, "test_repo") assert store_execute_reset.auto coresys.resolution.suggestions = Suggestion(SuggestionType.EXECUTE_RESET, ContextType.STORE, reference="test") coresys.resolution.issues = Issue(IssueType.CORRUPT_REPOSITORY, ContextType.STORE, reference="test") test_repo.mkdir() assert test_repo.exists() mock_repositorie = AsyncMock() mock_repositorie.git.path = test_repo coresys.store.repositories["test"] = mock_repositorie with patch("shutil.disk_usage", return_value=(42, 42, 2 * (1024.0**3))): await store_execute_reset() assert not test_repo.exists() assert mock_repositorie.load.called assert len(coresys.resolution.suggestions) == 0 assert len(coresys.resolution.issues) == 0
async def test_fixup(coresys: CoreSys): """Test fixup.""" store_execute_remove = FixupStoreExecuteRemove(coresys) assert store_execute_remove.auto coresys.resolution.suggestions = Suggestion( SuggestionType.EXECUTE_REMOVE, ContextType.STORE, reference="test" ) coresys.resolution.issues = Issue( IssueType.CORRUPT_REPOSITORY, ContextType.STORE, reference="test" ) mock_repositorie = AsyncMock() mock_repositorie.slug = "test" coresys.store.repositories["test"] = mock_repositorie await store_execute_remove() assert mock_repositorie.remove.called assert coresys.config.save_data.called assert len(coresys.resolution.suggestions) == 0 assert len(coresys.resolution.issues) == 0 assert "test" not in coresys.store.repositories
async def test_check_autofix(coresys: CoreSys): """Test check for setup.""" coresys.core.state = CoreState.RUNNING coresys.resolution.fixup._create_full_backup.process_fixup = AsyncMock() with patch( "supervisor.resolution.fixups.create_full_backup.FixupCreateFullBackup.auto", return_value=True, ): await coresys.resolution.fixup.run_autofix() coresys.resolution.fixup._create_full_backup.process_fixup.assert_not_called( ) coresys.resolution.suggestions = Suggestion( SuggestionType.CREATE_FULL_BACKUP, ContextType.SYSTEM) with patch( "supervisor.resolution.fixups.create_full_backup.FixupCreateFullBackup.auto", return_value=True, ): await coresys.resolution.fixup.run_autofix() coresys.resolution.fixup._create_full_backup.process_fixup.assert_called_once( ) assert len(coresys.resolution.suggestions) == 0
async def test_api_resolution_dismiss_suggestion(coresys: CoreSys, api_client): """Test resolution manager suggestion apply api.""" coresys.resolution.suggestions = clear_backup = Suggestion( SuggestionType.CLEAR_FULL_BACKUP, ContextType.SYSTEM) assert SuggestionType.CLEAR_FULL_BACKUP == coresys.resolution.suggestions[ -1].type await api_client.delete(f"/resolution/suggestion/{clear_backup.uuid}") assert clear_backup not in coresys.resolution.suggestions
async def test_resolution_dismiss_suggestion(coresys: CoreSys): """Test resolution manager suggestion apply api.""" coresys.resolution.suggestions = clear_snapshot = Suggestion( SuggestionType.CLEAR_FULL_SNAPSHOT, ContextType.SYSTEM) assert SuggestionType.CLEAR_FULL_SNAPSHOT == coresys.resolution.suggestions[ -1].type await coresys.resolution.dismiss_suggestion(clear_snapshot) assert clear_snapshot not in coresys.resolution.suggestions
async def test_resolution_dismiss_suggestion(coresys: CoreSys): """Test resolution manager suggestion apply api.""" coresys.resolution.suggestions = clear_backup = Suggestion( SuggestionType.CLEAR_FULL_BACKUP, ContextType.SYSTEM) assert SuggestionType.CLEAR_FULL_BACKUP == coresys.resolution.suggestions[ -1].type coresys.resolution.dismiss_suggestion(clear_backup) assert clear_backup not in coresys.resolution.suggestions with pytest.raises(ResolutionError): coresys.resolution.dismiss_suggestion(clear_backup)
async def test_api_resolution_base(coresys: CoreSys, api_client): """Test resolution manager api.""" coresys.resolution.unsupported = UnsupportedReason.OS coresys.resolution.suggestions = Suggestion( SuggestionType.CLEAR_FULL_BACKUP, ContextType.SYSTEM) coresys.resolution.create_issue(IssueType.FREE_SPACE, ContextType.SYSTEM) resp = await api_client.get("/resolution/info") result = await resp.json() assert UnsupportedReason.OS in result["data"][ATTR_UNSUPPORTED] assert (SuggestionType.CLEAR_FULL_BACKUP == result["data"] [ATTR_SUGGESTIONS][-1]["type"]) assert IssueType.FREE_SPACE == result["data"][ATTR_ISSUES][-1]["type"]
async def test_api_resolution_apply_suggestion(coresys: CoreSys, api_client): """Test resolution manager suggestion apply api.""" coresys.resolution.suggestions = clear_backup = Suggestion( SuggestionType.CLEAR_FULL_BACKUP, ContextType.SYSTEM) coresys.resolution.suggestions = create_backup = Suggestion( SuggestionType.CREATE_FULL_BACKUP, ContextType.SYSTEM) mock_backups = AsyncMock() mock_health = AsyncMock() coresys.backups.do_backup_full = mock_backups coresys.resolution.healthcheck = mock_health await api_client.post(f"/resolution/suggestion/{clear_backup.uuid}") await api_client.post(f"/resolution/suggestion/{create_backup.uuid}") assert clear_backup not in coresys.resolution.suggestions assert create_backup not in coresys.resolution.suggestions assert mock_backups.called assert mock_health.called with pytest.raises(ResolutionError): await coresys.resolution.apply_suggestion(clear_backup)
async def test_resolution_apply_suggestion(coresys: CoreSys): """Test resolution manager suggestion apply api.""" coresys.resolution.suggestions = clear_snapshot = Suggestion( SuggestionType.CLEAR_FULL_SNAPSHOT, ContextType.SYSTEM) coresys.resolution.suggestions = create_snapshot = Suggestion( SuggestionType.CREATE_FULL_SNAPSHOT, ContextType.SYSTEM) mock_snapshots = AsyncMock() mock_health = AsyncMock() coresys.snapshots.do_snapshot_full = mock_snapshots coresys.resolution.healthcheck = mock_health await coresys.resolution.apply_suggestion(clear_snapshot) await coresys.resolution.apply_suggestion(create_snapshot) assert mock_snapshots.called assert mock_health.called assert clear_snapshot not in coresys.resolution.suggestions assert create_snapshot not in coresys.resolution.suggestions with pytest.raises(ResolutionError): await coresys.resolution.apply_suggestion(clear_snapshot)
async def test_fixup(coresys: CoreSys): """Test fixup.""" create_full_backup = FixupSystemCreateFullBackup(coresys) assert not create_full_backup.auto coresys.resolution.suggestions = Suggestion( SuggestionType.CREATE_FULL_BACKUP, ContextType.SYSTEM) mock_backups = AsyncMock() coresys.backups.do_backup_full = mock_backups await create_full_backup() mock_backups.assert_called() assert len(coresys.resolution.suggestions) == 0
async def test_fixup(coresys: CoreSys): """Test fixup.""" create_full_snapshot = FixupCreateFullSnapshot(coresys) assert not create_full_snapshot.auto coresys.resolution.suggestions = Suggestion( SuggestionType.CREATE_FULL_SNAPSHOT, ContextType.SYSTEM) mock_snapshots = AsyncMock() coresys.snapshots.do_snapshot_full = mock_snapshots await create_full_snapshot() mock_snapshots.assert_called() assert len(coresys.resolution.suggestions) == 0
async def test_fixup(coresys: CoreSys): """Test fixup.""" system_execute_integrity = FixupSystemExecuteIntegrity(coresys) assert system_execute_integrity.auto coresys.resolution.suggestions = Suggestion( SuggestionType.EXECUTE_INTEGRITY, ContextType.SYSTEM) coresys.resolution.issues = Issue(IssueType.TRUST, ContextType.SYSTEM) coresys.security.integrity_check = AsyncMock(return_value=IntegrityResult( ContentTrustResult.PASS, ContentTrustResult.PASS, {"audio": ContentTrustResult.PASS}, )) await system_execute_integrity() assert coresys.security.integrity_check.called assert len(coresys.resolution.suggestions) == 0 assert len(coresys.resolution.issues) == 0
async def test_fixup_error(coresys: CoreSys): """Test fixup.""" system_execute_integrity = FixupSystemExecuteIntegrity(coresys) assert system_execute_integrity.auto coresys.resolution.suggestions = Suggestion( SuggestionType.EXECUTE_INTEGRITY, ContextType.SYSTEM) coresys.resolution.issues = Issue(IssueType.TRUST, ContextType.SYSTEM) coresys.security.integrity_check = AsyncMock(return_value=IntegrityResult( ContentTrustResult.FAILED, ContentTrustResult.PASS, {"audio": ContentTrustResult.PASS}, )) with time_machine.travel(utcnow() + timedelta(hours=24)): await system_execute_integrity() assert coresys.security.integrity_check.called assert len(coresys.resolution.suggestions) == 1 assert len(coresys.resolution.issues) == 1
async def test_fixup(coresys: CoreSys): """Test fixup.""" store_execute_reload = FixupStoreExecuteReload(coresys) assert store_execute_reload.auto coresys.resolution.suggestions = Suggestion(SuggestionType.EXECUTE_RELOAD, ContextType.STORE, reference="test") coresys.resolution.issues = Issue(IssueType.FATAL_ERROR, ContextType.STORE, reference="test") mock_repositorie = AsyncMock() coresys.store.repositories["test"] = mock_repositorie await store_execute_reload() assert mock_repositorie.load.called assert mock_repositorie.update.called assert len(coresys.resolution.suggestions) == 0 assert len(coresys.resolution.issues) == 0
async def test_fixup(coresys: CoreSys, tmp_path): """Test fixup.""" clear_full_backup = FixupClearFullBackup(coresys) assert not clear_full_backup.auto coresys.resolution.suggestions = Suggestion( SuggestionType.CLEAR_FULL_BACKUP, ContextType.SYSTEM) for slug in ["sn1", "sn2", "sn3", "sn4", "sn5"]: temp_tar = Path(tmp_path, f"{slug}.tar") with SecureTarFile(temp_tar, "w"): pass backup = Backup(coresys, temp_tar) backup._data = { # pylint: disable=protected-access ATTR_SLUG: slug, ATTR_DATE: utcnow().isoformat(), ATTR_TYPE: BackupType.PARTIAL if "1" in slug or "5" in slug else BackupType.FULL, } coresys.backups._backups[backup.slug] = backup newest_full_backup = coresys.backups._backups["sn4"] assert newest_full_backup in coresys.backups.list_backups assert (len([ x for x in coresys.backups.list_backups if x.sys_type == BackupType.FULL ]) == 3) await clear_full_backup() assert newest_full_backup in coresys.backups.list_backups assert (len([ x for x in coresys.backups.list_backups if x.sys_type == BackupType.FULL ]) == 1) assert len(coresys.resolution.suggestions) == 0
async def test_fixup(coresys: CoreSys, tmp_path): """Test fixup.""" clear_full_snapshot = FixupClearFullSnapshot(coresys) assert not clear_full_snapshot.auto coresys.resolution.suggestions = Suggestion( SuggestionType.CLEAR_FULL_SNAPSHOT, ContextType.SYSTEM) for slug in ["sn1", "sn2", "sn3", "sn4", "sn5"]: temp_tar = Path(tmp_path, f"{slug}.tar") with SecureTarFile(temp_tar, "w"): pass snapshot = Snapshot(coresys, temp_tar) snapshot._data = { # pylint: disable=protected-access ATTR_SLUG: slug, ATTR_DATE: utcnow().isoformat(), ATTR_TYPE: SNAPSHOT_PARTIAL if "1" in slug or "5" in slug else SNAPSHOT_FULL, } coresys.snapshots.snapshots_obj[snapshot.slug] = snapshot newest_full_snapshot = coresys.snapshots.snapshots_obj["sn4"] assert newest_full_snapshot in coresys.snapshots.list_snapshots assert (len([ x for x in coresys.snapshots.list_snapshots if x.sys_type == SNAPSHOT_FULL ]) == 3) await clear_full_snapshot() assert newest_full_snapshot in coresys.snapshots.list_snapshots assert (len([ x for x in coresys.snapshots.list_snapshots if x.sys_type == SNAPSHOT_FULL ]) == 1) assert len(coresys.resolution.suggestions) == 0
async def test_fixup(coresys: CoreSys, repository: Repository): """Test fixup.""" store_execute_remove = FixupStoreExecuteRemove(coresys) assert store_execute_remove.auto is False coresys.resolution.suggestions = Suggestion(SuggestionType.EXECUTE_REMOVE, ContextType.STORE, reference=repository.slug) coresys.resolution.issues = Issue(IssueType.CORRUPT_REPOSITORY, ContextType.STORE, reference=repository.slug) with patch.object(type(repository), "remove") as remove_repo: await store_execute_remove() assert remove_repo.called assert coresys.store.save_data.called assert len(coresys.resolution.suggestions) == 0 assert len(coresys.resolution.issues) == 0 assert repository.slug not in coresys.store.repositories