diff --git a/.github/workflows/pythontest.yml b/.github/workflows/pythontest.yml new file mode 100644 index 000000000..4ea6d6ed0 --- /dev/null +++ b/.github/workflows/pythontest.yml @@ -0,0 +1,37 @@ +name: python-test + +on: + push: + branches: [ $default-branch ] + pull_request: + branches: [ $default-branch ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest \ No newline at end of file diff --git a/.gitignore b/.gitignore index 19447927b..86b97cfea 100644 --- a/.gitignore +++ b/.gitignore @@ -56,4 +56,8 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk -# End of https://www.gitignore.io/api/macos,windows \ No newline at end of file +# End of https://www.gitignore.io/api/macos,windows + +# Pytest output +__pycache__ +.coverage diff --git a/salt/sensoroni/files/analyzers/pytest.ini b/salt/sensoroni/files/analyzers/pytest.ini new file mode 100644 index 000000000..cc6ad652e --- /dev/null +++ b/salt/sensoroni/files/analyzers/pytest.ini @@ -0,0 +1,8 @@ +[pytest] +python_files = *_test.py +python_classes = Test +python_functions = test_* + +[report] +exclude_lines = + if __name__ == .__main__.: \ No newline at end of file diff --git a/salt/sensoroni/files/analyzers/virustotal.py b/salt/sensoroni/files/analyzers/virustotal.py deleted file mode 100644 index 622d6f72d..000000000 --- a/salt/sensoroni/files/analyzers/virustotal.py +++ /dev/null @@ -1,5 +0,0 @@ -def main(): - print '{"foo":"bar","summary":"something here"}' - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/salt/sensoroni/files/analyzers/whois/whois.py b/salt/sensoroni/files/analyzers/whois/whois.py index 0ecb925bc..c35e2f75b 100644 --- a/salt/sensoroni/files/analyzers/whois/whois.py +++ b/salt/sensoroni/files/analyzers/whois/whois.py @@ -1,5 +1,5 @@ def main(): - print '{"result":{ "requestId": "something-generated-by-whois", "someother_field": "more data" }, "summary": "botsrv.btc-goblin.ru"}' + print('{"result":{ "requestId": "something-generated-by-whois", "someother_field": "more data" }, "summary": "botsrv.btc-goblin.ru"}') if __name__ == "__main__": main() \ No newline at end of file diff --git a/salt/sensoroni/files/analyzers/whois/whois_test.py b/salt/sensoroni/files/analyzers/whois/whois_test.py new file mode 100644 index 000000000..aa27c5b95 --- /dev/null +++ b/salt/sensoroni/files/analyzers/whois/whois_test.py @@ -0,0 +1,12 @@ +from io import StringIO +from unittest.mock import patch +from whois import whois +import unittest + +class TestWhoisMethods(unittest.TestCase): + + def test_main(self): + with patch('sys.stdout', new = StringIO()) as mock_stdout: + whois.main() + expected = '{"result":{ "requestId": "something-generated-by-whois", "someother_field": "more data" }, "summary": "botsrv.btc-goblin.ru"}\n' + self.assertEqual(mock_stdout.getvalue(), expected)