Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bhnedo committed Feb 11, 2017
1 parent bf5a392 commit dc98011
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 73 deletions.
68 changes: 4 additions & 64 deletions tests/unit/filament.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
from unittest import mock
from unittest.mock import Mock, patch

import pytest
from apscheduler.schedulers.background import BackgroundScheduler

import fibratus.filament as fil
import fibratus.filament as flmt
from fibratus.errors import FilamentError
from fibratus.filament import Filament
from fibratus.output.amqp import AmqpOutput
Expand All @@ -38,7 +36,7 @@ def ansi_term_mock():
def filament(ansi_term_mock):
fil.FILAMENTS_DIR = os.path.join(os.path.dirname(__file__), '..', 'fixtures\\filaments')
f = Filament()
flmt._ansi_term = ansi_term_mock
f._ansi_term = ansi_term_mock
f.scheduler = Mock(spec_set=BackgroundScheduler)
return f

Expand All @@ -51,6 +49,7 @@ def test_load_filament(self, filament):
assert filament._filament_module
assert isinstance(filament._filament_module, type(test_filament))
assert filament._filament_module.__doc__
assert 'test_filament' in filament.name

f = filament.filament_module
assert hasattr(f, 'set_filter')
Expand Down Expand Up @@ -153,11 +152,11 @@ def test_limit_non_integer(self, filament):
with pytest.raises(FilamentError):
f.limit('20')

def test_setup_adapters(self, filament):
def test_do_output_accessors(self, filament):
with patch('os.listdir', return_value=['test_filament.py']):
filament.load_filament('test_filament')
outputs = {'amqp': Mock(spec_set=AmqpOutput)}
filament.setup_adapters(outputs)
filament.do_output_accessors(outputs)
assert getattr(filament.filament_module, 'amqp')

def test_set_columns_not_list(self, filament):
Expand All @@ -172,63 +171,4 @@ def test_render_tabular(self, filament, ansi_term_mock):
filament.filament_module.columns(['IP', 'Port'])
filament.render_tabular()
ansi_term_mock.setup_console.assert_called_once()
ansi_term_mock.cls.assert_called_once()
ansi_term_mock.write_output.assert_called_once()

def test_run(self, filament):
import tempfile
filament.load_filament('test_filament_interval')
filament.keventq = Mock(spec_set=Queue)
filament.keventq.get.return_value = dict()
filament.logger = os.path.join(tempfile.gettempdir(), 'fibratus.log')
with mock.patch.object(filament, '_poll') as filament_mock_poll:
filament_mock_poll.return_value = AlmostAlwaysTrue(5)
filament.run()
filament.scheduler.add_job.assert_called_with(filament._filament_module.on_interval,
'interval',
seconds=1,
max_instances=4,
misfire_grace_time=60)


class Queue(object):
def __init__(self, maxsize=-1):
self._maxsize = maxsize

def qsize(self):
return 0

def empty(self):
return False

def full(self):
return False

def put(self, obj, block=True, timeout=None):
pass

def put_nowait(self, obj):
pass

def get(self, block=True, timeout=None):
pass


# Credits to http://stackoverflow.com/questions/27637653/how-to-test-an-infinite-while-loop-with-pytest
class AlmostAlwaysTrue(object):
def __init__(self, total_iterations=1):
self.total_iterations = total_iterations
self.current_iteration = 0

def __nonzero__(self):
if self.current_iteration < self.total_iterations:
self.current_iteration += 1
return bool(1)
return bool(0)

# Python >= 3
def __bool__(self):
if self.current_iteration < self.total_iterations:
self.current_iteration += 1
return bool(1)
return bool(0)
37 changes: 30 additions & 7 deletions tests/unit/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from fibratus.fs import FsIO, FileOps
from fibratus.handle import HandleInfo, HandleType
from fibratus.kevent import KEvent
from fibratus.kevent_types import CREATE_FILE, DELETE_FILE, WRITE_FILE, RENAME_FILE
from fibratus.kevent_types import CREATE_FILE, DELETE_FILE, WRITE_FILE, RENAME_FILE, SET_FILE_INFORMATION
from fibratus.thread import ThreadRegistry


Expand All @@ -49,26 +49,30 @@ def test_init_fsio(self, fsio):

@pytest.mark.parametrize('expected_op, kfsio',
[(FileOps.SUPERSEDE, dd({"file_object": 18446738026482168384, "ttid": 1484,
"process_id": 859,
"create_options": 1223456,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"irp_ptr": 18446738026471032392, "share_access": 1, "file_attributes": 0})),
(FileOps.OPEN, dd({"file_object": 18446738026482168384, "ttid": 1484,
(FileOps.OPEN, dd({"file_object": 18446738026482168384, "ttid": 1484, "process_id": 859,
"create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"irp_ptr": 18446738026471032392, "share_access": 2, "file_attributes": 0})),
(FileOps.CREATE, dd({"file_object": 18446738026482168384, "ttid": 1484,
(FileOps.CREATE, dd({"file_object": 18446738026482168384, "ttid": 1484, "process_id": 859,
"create_options": 33554532,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"irp_ptr": 18446738026471032392, "share_access": 4, "file_attributes": 0})),
(FileOps.OPEN_IF, dd({"file_object": 18446738026482168384, "ttid": 1484,
"process_id": 859,
"create_options": 58651617,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"irp_ptr": 18446738026471032392, "share_access": 3, "file_attributes": 0})),
(FileOps.OVERWRITE, dd({"file_object": 18446738026482168384, "ttid": 1484,
"process_id": 859,
"create_options": 78874400,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"irp_ptr": 18446738026471032392, "share_access": 5, "file_attributes": 0})),
(FileOps.OVERWRITE_IF, dd({"file_object": 18446738026482168384, "ttid": 1484,
"process_id": 859,
"create_options": 83886112,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"irp_ptr": 18446738026471032392, "share_access": 6, "file_attributes": 0}))])
Expand All @@ -79,52 +83,71 @@ def test_create_file_operation(self, expected_op, kfsio, fsio, kevent):
kparams = kevent.params
assert kparams.file == kfsio.open_path
assert kparams.tid == kfsio.ttid
assert kparams.pid == kfsio.process_id
assert kparams.operation == expected_op.name

@pytest.mark.parametrize('expected_share_mask, kfsio',
[('r--', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": 1, "file_attributes": 0})),
('-w-', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": 2, "file_attributes": 0})),
('--d', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": 4, "file_attributes": 0})),
('rw-', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": 3, "file_attributes": 0})),
('r-d', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": 5, "file_attributes": 0})),
('-wd', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": 6, "file_attributes": 0})),
('rwd', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": 7, "file_attributes": 0})),
('---', dd({"file_object": 18446738026482168384, "ttid": 1484, "create_options": 18874368,
"open_path": "\\Device\\HarddiskVolume2\\Windows\\system32\\kernel32.dll",
"process_id": 859,
"irp_ptr": 18446738026471032392, "share_access": -1, "file_attributes": 0}))])
def test_create_file_share_mask(self, expected_share_mask, kfsio, fsio, kevent):
fsio.parse_fsio(CREATE_FILE, kfsio)
assert kevent.params.share_mask == expected_share_mask

def test_delete_file(self, fsio, kevent):
kfsio = dd({"file_object": 18446738026474426144, "ttid": 1956, "irp_ptr": 18446738026471032392})
kfsio = dd({"file_object": 18446738026474426144, "ttid": 1956, "process_id": 859, "irp_ptr": 18446738026471032392})
fsio.parse_fsio(DELETE_FILE, kfsio)
assert kevent.params.tid == kfsio.ttid
assert kevent.params.file == '\\Device\\HarddiskVolume2\\fibratus.log'

def test_write_file(self, fsio, kevent):
kfsio = dd({"file_object": 18446738026474426144, "io_flags": 0, "io_size": 8296, "offset": 75279, "ttid": 1956})
kfsio = dd({"file_object": 18446738026474426144, "process_id": 859, "io_flags": 0, "io_size": 8296,
"offset": 75279, "ttid": 1956})
fsio.parse_fsio(WRITE_FILE, kfsio)
assert kevent.params.tid == kfsio.ttid
assert kevent.params.file == NA
assert kevent.params.io_size == kfsio.io_size / 1024

def test_rename_file(self, fsio, kevent):
kfsio = dd({"file_object": 18446738023471035392, "ttid": 1956, "irp_ptr": 18446738026471032392})
kfsio = dd({"file_object": 18446738023471035392, "ttid": 1956, "process_id": 859, "irp_ptr": 18446738026471032392})
fsio.parse_fsio(RENAME_FILE, kfsio)
assert kevent.params.tid == kfsio.ttid
assert kevent.params.file == '\\Device\\HarddiskVolume2\\Windows\\system32\\rpcss.dll'
assert kevent.params.file == '\\Device\\HarddiskVolume2\\Windows\\system32\\rpcss.dll'

def test_set_file_information(self, fsio, kevent):
kfsio = dd(
{"file_object": 18446738023471035392, "ttid": 1956, "info_class": 20, "process_id": 859,
"irp_ptr": 18446738026471032392})
fsio.parse_fsio(SET_FILE_INFORMATION, kfsio)
assert kevent.params.tid == kfsio.ttid
assert kevent.params.info_class == 20
assert kevent.params.file == '\\Device\\HarddiskVolume2\\Windows\\system32\\rpcss.dll'
6 changes: 4 additions & 2 deletions tests/unit/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def handle_repo_mock():

@pytest.fixture(scope='module')
def image_meta_registry_mock():
return Mock(spec_set=ImageMetaRegistry)
imeta_meta_registry_mock = Mock(spec_set=ImageMetaRegistry)
imeta_meta_registry_mock.get_image_meta.return_value = None
return imeta_meta_registry_mock


@pytest.fixture(scope='module')
Expand Down Expand Up @@ -68,7 +70,7 @@ def thread_registry(handle_repo_mock, image_meta_registry_mock):
return thread_registry


class TestThreadRegistry():
class TestThreadRegistry:

def test_init_thread_registry(self, thread_registry):

Expand Down

0 comments on commit dc98011

Please sign in to comment.