From 351fd34c60fe099ca02b4c99766e259667a6f570 Mon Sep 17 00:00:00 2001 From: Pavel Dat Date: Mon, 11 Sep 2023 23:08:38 +0300 Subject: [PATCH] Add module tests and add new stage in pipeline --- .github/workflows/deploy-job.yml | 11 ++++++----- tests/test_exec_shell_command.py | 21 ++++++++++++++++++++ tests/test_get_hostname.py | 18 +++++++++++++++++ tests/test_http_headers_grabber.py | 31 ++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 tests/test_exec_shell_command.py create mode 100644 tests/test_get_hostname.py create mode 100644 tests/test_http_headers_grabber.py diff --git a/.github/workflows/deploy-job.yml b/.github/workflows/deploy-job.yml index c291c18..0882600 100644 --- a/.github/workflows/deploy-job.yml +++ b/.github/workflows/deploy-job.yml @@ -12,15 +12,12 @@ jobs: build: runs-on: ubuntu-latest needs: info - strategy: - matrix: - python-version: [ "3.10" ] steps: - uses: actions/checkout@v3 - - name: Set up Python ${{ matrix.python-version }} + - name: Set up Python 3.10 uses: actions/setup-python@v3 with: - python-version: ${{ matrix.python-version }} + python-version: "3.10" - name: Install dependencies run: | python -m pip install --upgrade pip @@ -45,12 +42,16 @@ jobs: python -m pip install --upgrade pip sudo apt install pycodestyle pylint pip install -r py-requirements.txt + pip install pytest - name: Analysing the code with pycodestyle run: | pycodestyle src/**/*.py - name: Analysing the code with pylint run: | pylint src/**/*.py + - name: Run Module tests + run: | + python3 -m pytest -sr tests/test_c*.py deploy: runs-on: ubuntu-latest needs: test diff --git a/tests/test_exec_shell_command.py b/tests/test_exec_shell_command.py new file mode 100644 index 0000000..b5a7a17 --- /dev/null +++ b/tests/test_exec_shell_command.py @@ -0,0 +1,21 @@ +import os +import sys +import pytest + +sys.path.insert( + 0, + 'src' +) + +from exec_shell_command.exec_shell_command import exec_shell_command + +@pytest.fixture +def exec_pwd(): + return exec_shell_command('pwd').removesuffix('\n') + +def test_exec_correct_command(exec_pwd): + assert exec_pwd == os.getcwd() + +def test_exec_incorrect_command(): + with pytest.raises(ValueError): + exec_shell_command('pweed') diff --git a/tests/test_get_hostname.py b/tests/test_get_hostname.py new file mode 100644 index 0000000..16a52bf --- /dev/null +++ b/tests/test_get_hostname.py @@ -0,0 +1,18 @@ +import sys +import socket +import pytest + +sys.path.insert( + 0, + 'src' +) + +from ip.ip import GetHostname + +@pytest.fixture +def get_hostname(): + return GetHostname.get_hostname_ip() + +def test_get_hostname(get_hostname): + hostname, _ = get_hostname + assert hostname == socket.gethostname() diff --git a/tests/test_http_headers_grabber.py b/tests/test_http_headers_grabber.py new file mode 100644 index 0000000..6958825 --- /dev/null +++ b/tests/test_http_headers_grabber.py @@ -0,0 +1,31 @@ +import sys +import pytest + +sys.path.insert( + 0, + 'src' +) + +from http_headers_grabber.http_headers_grabber import HttpHeadersGrabber + +@pytest.fixture +def http_headers_grabber_available(): + return HttpHeadersGrabber.http_headers_grabber('https://google.com') + +@pytest.fixture +def http_headers_grabber_witout_preffix(): + return HttpHeadersGrabber.http_headers_grabber('google.com') + +def test_http_headers_grabber_available(http_headers_grabber_available): + assert dict(http_headers_grabber_available)['X-Frame-Options'] == 'SAMEORIGIN' + assert dict(http_headers_grabber_available)['Content-Encoding'] == 'gzip' + +def test_http_headers_grabber_witout_preffix(http_headers_grabber_witout_preffix): + assert dict(http_headers_grabber_witout_preffix)['X-Frame-Options'] == 'SAMEORIGIN' + assert dict(http_headers_grabber_witout_preffix)['Content-Encoding'] == 'gzip' + +def test_http_headers_grabber_invalid_url_type(): + try: + return HttpHeadersGrabber.http_headers_grabber(123) + except BaseException as ex: + assert str(ex) == 'Target must be a string not . Got target: 123'