Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Show progress as int #269

Merged
merged 6 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
also ospd in editable mode. This means after running poetry install ospd will
directly be importable in the virtual python environment. [#252](https://github.com/greenbone/ospd/pull/252)
- Progress bar calculation does not take in account the dead hosts. [#266](https://github.com/greenbone/ospd/pull/266)
- Show progress as integer for get_scans. [#269](https://github.com/greenbone/ospd/pull/269)

### Fixed
- Fix stop scan. Wait for the scan process to be stopped before delete it from the process table. [#204](https://github.com/greenbone/ospd/pull/204)
Expand Down
16 changes: 11 additions & 5 deletions ospd/ospd.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,20 +602,26 @@ def sort_host_finished(
)

def set_scan_progress_batch(
self, scan_id: str, host_progress: Dict[str, int]
self, scan_id: str, host_progress: Dict[str, Union[float, int]]
):
self.scan_collection.set_host_progress(scan_id, host_progress)

scan_progress = self.calculate_progress(scan_id)
self.scan_collection.set_progress(scan_id, scan_progress)

def set_scan_host_progress(
self, scan_id: str, host: str = None, progress: int = None,
self,
scan_id: str,
host: str = None,
progress: Union[float, int] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should be a float still allowed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from the wrapper. It depends on the wrapper implementation. Currently ospd-openvas sends a float value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix that?

) -> None:
""" Sets host's progress which is part of target.
Each time a host progress is updated, the scan progress
is updated too.
"""
if host is None or progress is None:
return

host_progress = {host: progress}
self.set_scan_progress_batch(scan_id, host_progress)

Expand Down Expand Up @@ -713,7 +719,7 @@ def _get_scan_progress_xml(self, scan_id: str):
current_progress[
'current_hosts'
] = self.scan_collection.get_current_target_progress(scan_id)
current_progress['overall'] = self.scan_collection.get_progress(scan_id)
current_progress['overall'] = self.get_scan_progress(scan_id)
current_progress['count_alive'] = self.scan_collection.get_count_alive(
scan_id
)
Expand Down Expand Up @@ -1266,9 +1272,9 @@ def check_scan_process(self, scan_id: str) -> None:
elif progress == PROGRESS_FINISHED:
scan_process.join(0)

def get_scan_progress(self, scan_id: str):
def get_scan_progress(self, scan_id: str) -> int:
""" Gives a scan's current progress value. """
return self.scan_collection.get_progress(scan_id)
return int(self.scan_collection.get_progress(scan_id))

def get_scan_host(self, scan_id: str) -> str:
""" Gives a scan's target. """
Expand Down
8 changes: 7 additions & 1 deletion ospd/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def set_host_progress(
self, scan_id: str, host_progress_batch: Dict[str, int]
) -> None:
""" Sets scan_id scan's progress. """

host_progresses = self.scans_table[scan_id].get('target_progress')
host_progresses.update(host_progress_batch)

Expand Down Expand Up @@ -274,7 +275,12 @@ def get_count_alive(self, scan_id: str) -> int:
def get_current_target_progress(self, scan_id: str) -> Dict[str, int]:
""" Get a scan's current dead host count. """

return self.scans_table[scan_id]['target_progress']
hosts = self.scans_table[scan_id]['target_progress']
if hosts:
for host, progress in hosts.items():
hosts[host] = int(progress)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we ensure that progress is already an int in the scans table?


return hosts

def simplify_exclude_host_count(self, scan_id: str) -> int:
""" Remove from exclude_hosts the received hosts in the finished_hosts
Expand Down
30 changes: 30 additions & 0 deletions tests/test_scan_and_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,36 @@ def test_sort_host_finished(self):
rounded_progress = int(daemon.calculate_progress(scan_id))
self.assertEqual(rounded_progress, 66)

def test_calculate_progress_without_current_hosts(self):
daemon = DummyWrapper([])

fs = FakeStream()
daemon.handle_command(
'<start_scan parallel="2">'
'<scanner_params />'
'<targets><target>'
'<hosts>localhost1, localhost2, localhost3, localhost4</hosts>'
'<ports>22</ports>'
'</target></targets>'
'</start_scan>',
fs,
)
response = fs.get_response()

scan_id = response.findtext('id')
daemon.set_scan_host_progress(scan_id)
daemon.set_scan_host_progress(scan_id, 'localhost3', -1)
daemon.set_scan_host_progress(scan_id, 'localhost4', 100)

daemon.sort_host_finished(scan_id, ['localhost3', 'localhost4'])

float_progress = daemon.calculate_progress(scan_id)
self.assertEqual(int(float_progress), 33)

daemon.scan_collection.set_progress(scan_id, float_progress)
progress = daemon.get_scan_progress(scan_id)
self.assertEqual(progress, 33)

def test_get_scan_progress_xml(self):
daemon = DummyWrapper([])

Expand Down