Skip to content

Commit

Permalink
adding some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandeivert committed Mar 5, 2020
1 parent 8abb1a8 commit 8ce8f35
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 7 deletions.
9 changes: 4 additions & 5 deletions streamalert_cli/test/event_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
class TestEventFile:
"""TestEventFile handles caching results of test events within a test file"""

def __init__(self, full_path, config):
def __init__(self, full_path):
self._full_path = full_path
self._config = config
self._results = []
self._results = [] # type: streamalert_cli.test.results.TestResult
self.error = None

def __bool__(self):
Expand Down Expand Up @@ -87,9 +86,9 @@ def load_file(self):
for event in data:
yield event

def process_file(self, verbose, with_rules):
def process_file(self, config, verbose, with_rules):
for idx, event in enumerate(self.load_file()):
test_result = TestResult(idx, event, verbose, with_rules)
self._results.append(test_result)
if test_result.prepare(self._config):
if test_result.prepare(config):
yield test_result
4 changes: 2 additions & 2 deletions streamalert_cli/test/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ def _process_directory(self, directory):
def _process_test_file(self, test_file_path):
"""Process an individual test file"""
# Iterate over the individual test events in the file
event_file = TestEventFile(test_file_path, self._config)
for event in event_file.process_file(self._verbose, self._testing_rules):
event_file = TestEventFile(test_file_path)
for event in event_file.process_file(self._config, self._verbose, self._testing_rules):
if not self._contains_filtered_rules(event):
continue

Expand Down
Empty file.
41 changes: 41 additions & 0 deletions tests/unit/streamalert_cli/test/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Copyright 2017-present Airbnb, Inc.
Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json

from nose.tools import nottest


@nottest
def basic_test_file_json():
return json.dumps(basic_test_file_data())


@nottest
def basic_test_file_data():
return [
{
'data': {
'key': 'value'
},
'description': 'Integration test event for unit testing',
'log': 'misc_log_type',
'service': 'unit-test-service',
'source': 'unit-test-source',
'trigger_rules': [
'misc_rule'
]
}
]
137 changes: 137 additions & 0 deletions tests/unit/streamalert_cli/test/test_event_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""
Copyright 2017-present Airbnb, Inc.
Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from mock import Mock

from nose.tools import assert_equal
from pyfakefs import fake_filesystem_unittest

from streamalert_cli.test.event_file import TestEventFile
from tests.unit.streamalert_cli.test.helpers import (
basic_test_file_data,
basic_test_file_json
)


class TestConfigLoading(fake_filesystem_unittest.TestCase):
"""Test config loading logic with a mocked filesystem."""

_DEFAULT_EVENT_PATH = 'rules/community/unit_test/file.json'

def setUp(self):
self.setUpPyfakefs()

# Create a single test event file
self.fs.create_file(
self._DEFAULT_EVENT_PATH,
contents=basic_test_file_json()
)

@staticmethod
def _mock_classified_result():
# log_schema_type is used to validate a classification result
return Mock(log_schema_type='misc_log_type')

def _fake_result(self, passed):
# pylint: disable=protected-access

# Importing here because for some reason nose hates it otherwise
from streamalert_cli.test.results import TestResult
result = TestResult(0, basic_test_file_data()[0])
result._classified_result = self._mock_classified_result() if passed else False
return result

def test_stringer(self):
"""StreamAlert CLI - TestEventFile Stringer"""
# pylint: disable=protected-access
event = TestEventFile(self._DEFAULT_EVENT_PATH)
event._results.append(self._fake_result(True))

expected_stringer = (
'\033[4m\nFile: rules/community/unit_test/file.json\n\033[0m'
'\nTest #01: \033[0;32;1mPass\033[0m'
)

assert_equal(str(event), expected_stringer)

def test_stringer_with_error(self):
"""StreamAlert CLI - TestEventFile Stringer, Error"""
event = TestEventFile(self._DEFAULT_EVENT_PATH)
event.error = 'Bad thing happened'

expected_stringer = (
'\033[4m\nFile: rules/community/unit_test/file.json\n\033[0m'
'\n\033[0;31;1mBad thing happened\033[0m'
)

assert_equal(str(event), expected_stringer)

def test_bool(self):
"""StreamAlert CLI - TestEventFile Bool"""
# pylint: disable=protected-access
event = TestEventFile(self._DEFAULT_EVENT_PATH)

assert_equal(bool(event), False)

event._results.append(self._fake_result(True))

assert_equal(bool(event), True)

def test_passed(self):
"""StreamAlert CLI - TestEventFile Passed"""
# pylint: disable=protected-access
event = TestEventFile(self._DEFAULT_EVENT_PATH)
event._results.append(self._fake_result(True))

assert_equal(event.passed, True)

def test_failed(self):
"""StreamAlert CLI - TestEventFile Failed"""
# pylint: disable=protected-access
event = TestEventFile(self._DEFAULT_EVENT_PATH)
event._results.append(self._fake_result(False))

assert_equal(event.failed, True)

def test_load_file_valid(self):
"""StreamAlert CLI - TestEventFile Load File, Valid"""
event = TestEventFile(self._DEFAULT_EVENT_PATH)
value = list(event.load_file())

assert_equal(len(value), 1)
assert_equal(event.error, None)

def test_load_file_invalid_json(self):
"""StreamAlert CLI - TestEventFile Load File, Invalid JSON"""
file_path = 'rules/community/unit_test/bad-file.json'
self.fs.create_file(file_path, contents='invalid json string')

event = TestEventFile(file_path)
list(event.load_file())

assert_equal(event.error, 'Test event file is not valid JSON')

def test_load_file_invalid_format(self):
"""StreamAlert CLI - TestEventFile Load File, Invalid Format"""
file_path = 'rules/community/unit_test/bad-file.json'
self.fs.create_file(file_path, contents='{"bad": "format"}')

event = TestEventFile(file_path)
list(event.load_file())

assert_equal(
event.error,
'Test event file is improperly formatted; events should be in a list'
)

0 comments on commit 8ce8f35

Please sign in to comment.