From 2e66534dd7a1b9db1cc64edace295139c9a844c5 Mon Sep 17 00:00:00 2001 From: Tushar <30565750+tushar5526@users.noreply.github.com> Date: Wed, 22 May 2024 00:32:21 +0530 Subject: [PATCH] feat: update tests and add code-cov --- .coverage | Bin 53248 -> 0 bytes .github/workflows/code-cov.yml | 18 +++++++++++++++ .gitignore | 4 +++- Makefile | 9 ++++++++ server/utils.py | 5 ++-- tests/test_utils.py | 41 ++++++++++++++++++++------------- 6 files changed, 58 insertions(+), 19 deletions(-) delete mode 100644 .coverage create mode 100644 .github/workflows/code-cov.yml diff --git a/.coverage b/.coverage deleted file mode 100644 index 5896526a67fbd8ec46cb536d100a11f77fc485dc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 53248 zcmeI)%WoS+90%}OZ)(Sm-7ShLD~d|y0!h>)ZUj;(1!{{zGy&@1A^0fc}95<(#UfO0_A9EWn)YPVZfR*d4ZE%a+gXvps!l_P zzHabDNBd~1C4T=@$6-r{wv)jtskmvQrOcWqFTcFp2cn2k-?G=YZ$f+dy zwDfdSd)ldMKhDLJ-Iy*+UwglhE02yE*L74T<<;pn(o-7R(J9KwCM8sNy$0vL<|(JX zs{I+Mwn|ucDWB^>Tw2d=|c4E#%#cm0z8whvElM4Cl%d6UOxmQA1&#YRwM(t(AOFquJU$py}}O!R>~7NpBf~n<8DaQbmsHf^5VpxrnA)qs`Fc`h0U>St~@?&T+K(# zD5E>1>yN+%Z{=9j6{rTvRm+HK`DlKG0K;W$+q`?+)SdE!BY zBFahXls7CP#V1O4FZPBNI-wtr~H6=St~H@zSb(Ah~!pdQ}BFu-jU4kSH@z z!@8t=S@$#@q`7o@(tKDm;M%Ir^CMp+5A#d8%?Tq{K782N43o|nk26)DCiS*Tvz=^a zdK$^LgPr!WL<26p+Uag5Y}duXoADWVaEoiO7AvHolGi$gX}-WWtb2lPSReoa2tWV= z5P$##AOHafKmY;|*n0w|kuh?7{hzUZ5!UbYfCT~&fB*y_009U<00Izz00bZaf!!%k z%$SF)^eY}mEW;cxCtm_MI`{0{5&n{Y#=0%6+t%N^vw+Ao1Rwwb2tWV=5P$##AOHaf zKmY=%K+!y8r0)V`3g&n|c@uy?|2M5AVSQtLL{C^C009U<00Izz00bZa0SG_<0ucCb zf#aqrE?3_2>7NTKLFlh4&sS}=;;G7-=bqE`z_0kqqo3qg0`1cVKUTxQZu#`z1xGWc z2zL~j(x>b9N*eK)VTz3%MU2l%f3VTyH99AM{%=~Rg>}RFmY%Rc00Izz00bZa0SG_< z0uX=z1R$^jffvln#v@#b_gw#*&za%QB5%9?&m1#1@{e%VuIqpN|KAQO6+s{X0SG_< z0uX=z1Rwwb2tWV=yH~)ZZw-|A^MAv-FX)B^0uX=z1Rwwb2tWV=5P$##AOL|qC}5hU zeEj?Wo5K3T`og-o2MdWJK>z{}fB*y_009U<00Izz00efGz^FN7*$ zU*El3c`<(^Lpy7ke?Gtez4_&*8-Hf!-u>#c-_HMF?!P5Yp-->_#e@9sNtiP-~y9xxEg#ZK~009U<00Izz00bZa0SG{#pFo!0 zEHF$nddg&m=#7Br^M7%mUtsKm00bZa0SG_<0uX=z1Rwwb2tZ)32=M3sxc=WOwTW^- z00Izz00bZa0SG_<0uX=z1o{i`@BeZA-`@s%ApijgKmY;|fB*y_009U<00MhO;9nkl B$T9!` diff --git a/.github/workflows/code-cov.yml b/.github/workflows/code-cov.yml new file mode 100644 index 0000000..0185514 --- /dev/null +++ b/.github/workflows/code-cov.yml @@ -0,0 +1,18 @@ +name: Workflow for Codecov example-python +on: [push, pull_request] +jobs: + run: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: "3.10" + - name: Install dependencies + run: pip install -r requirements.txt + - name: Run tests and collect coverage + run: pytest --cov app + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 diff --git a/.gitignore b/.gitignore index 264d252..35a7b51 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,6 @@ nginx-confs/* *keys.txt # Ignore the nginx conf for sarthi -sarthi.conf \ No newline at end of file +sarthi.conf + +.coverage \ No newline at end of file diff --git a/Makefile b/Makefile index 56d1e22..a11cf45 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,11 @@ local-dev: sarthi: python app.py +.PHONY: test-lint +test-lint: + pre-commit run --all-files + python -m pytest -vvv tests + .PHONY: test test: python -m pytest -vvv tests @@ -23,3 +28,7 @@ reset: rm -f .env keys.txt parsed-key.txt ansi-keys.txt docker compose -f docker-compose-local.yml down -v rm -rf deployments nginx-confs + +.PHONY: code-cov +code-cov: + coverage run -m pytest tests diff --git a/server/utils.py b/server/utils.py index af35537..7861359 100644 --- a/server/utils.py +++ b/server/utils.py @@ -50,8 +50,8 @@ def get_project_hash(self): def __repr__(self): return ( - f"DeploymentConfig({self.project_name_raw=!r}, {self.branch_name_raw=!r}, {self.project_git_url=!r}, " - f"{self.compose_file_location=!r}, {self.rest_action=!r})" + f"DeploymentConfig({self.project_name_raw!r}, {self.branch_name_raw!r}, {self.project_git_url!r}, " + f"{self.compose_file_location!r}, {self.rest_action!r})" ) @@ -429,6 +429,7 @@ def cleanup_deployment_variables(self): f"Tried Removing Deployment variables from Vault {response.status_code}" ) response.raise_for_status() + return response except requests.HTTPError as e: logger.error(f"Error removing deployment secrets {e}") diff --git a/tests/test_utils.py b/tests/test_utils.py index 1db5505..6a82f5e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,8 +1,9 @@ import pathlib -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, call, patch import pytest import requests +from fastapi import HTTPException from server.utils import ComposeHelper @@ -127,7 +128,8 @@ def test_find_free_port_fails(nginx_helper, mocker): # Then with pytest.raises( - RuntimeError, match="Could not find a free port in the specified range" + HTTPException, + match="Could not find a free port in the specified range", ): # When nginx_helper.find_free_port() @@ -148,7 +150,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker): == """ server { listen 80; - server_name ~7a38dba0c2.localhost; + server_name ~5022fe75f1.localhost; location / { proxy_pass http://host.docker.internal:12345; @@ -160,7 +162,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker): } """ ) - mock_open.assert_called_with("/path/to/outer/conf/rojectname-7a38dba0c2.conf", "w") + mock_open.assert_called_with("/path/to/outer/conf/testprojec-5022fe75f1.conf", "w") def test_generate_project_proxy_conf_file(nginx_helper, mocker): @@ -175,10 +177,10 @@ def test_generate_project_proxy_conf_file(nginx_helper, mocker): proxy_conf_path, urls = nginx_helper.generate_project_proxy_conf_file(services) # Then - assert proxy_conf_path == "/path/to/deployment/project/rojectname-7a38dba0c2.conf" + assert proxy_conf_path == "/path/to/deployment/project/testprojec-5022fe75f1.conf" assert urls == [ - "http://rojectname-testbranchname-1000-7a38dba0c2.localhost", - "http://rojectname-testbranchname-2000-7a38dba0c2.localhost", + "http://testprojec-testbranchname-1000-5022fe75f1.localhost", + "http://testprojec-testbranchname-2000-5022fe75f1.localhost", ] @@ -220,7 +222,7 @@ def test_remove_outer_proxy(nginx_helper, mocker): nginx_helper.remove_outer_proxy() # Then - mock_remove.assert_called_with("/path/to/outer/conf/rojectname-7a38dba0c2.conf") + mock_remove.assert_called_with("/path/to/outer/conf/testprojec-5022fe75f1.conf") def test_remove_outer_proxy_when_file_is_deleted_already(nginx_helper, mocker): @@ -258,12 +260,20 @@ def test_create_env_placeholder_with_sample_env_file( secrets_helper_instance._create_env_placeholder() # Assertions + print(mock_requests.post.call_args_list) + assert mock_requests.post.call_args_list == [ + call( + url="http://vault:8200/v1/kv/data/project_name/default-dev-secrets", + headers={"X-Vault-Token": "hvs.randomToken"}, + data='{"data": {"key": "secret-value"}}', + ), + call( + url="http://vault:8200/v1/kv/data/project_name/branch_name", + headers={"X-Vault-Token": "hvs.randomToken"}, + data='{"data": {"key": "secret-value"}}', + ), + ] mock_dotenv_values.assert_called() - mock_requests.post.assert_called_once_with( - url="http://vault:8200/v1/kv/data/project_name/branch_name", - headers={"X-Vault-Token": "hvs.randomToken"}, - data='{"data": {"key": "secret-value"}}', - ) @patch("server.utils.os") @@ -280,7 +290,7 @@ def test_create_env_placeholder_with_sample_env_file_missing( # Assertions mock_os.path.exists.assert_called_with("/path/to/project/.env.sample") - mock_requests.post.assert_called_once_with( + mock_requests.post.assert_called_with( url="http://vault:8200/v1/kv/data/project_name/branch_name", headers={"X-Vault-Token": "hvs.randomToken"}, data='{"data": {"key": "secret-value"}}', @@ -370,11 +380,10 @@ def test_cleanup_deployment_variables_failure( mock_requests_delete.return_value = mock_response # Calling the method under test - result = secrets_helper_instance.cleanup_deployment_variables() + secrets_helper_instance.cleanup_deployment_variables() # Assertions mock_requests_delete.assert_called_once_with( url="http://vault:8200/v1/kv/metadata/project_name/branch_name", headers={"X-Vault-Token": "hvs.randomToken"}, ) - assert result.status_code == 500