Skip to content

Commit

Permalink
feat: update tests and add code-cov
Browse files Browse the repository at this point in the history
  • Loading branch information
tushar5526 committed May 21, 2024
1 parent b81fcc6 commit 2e66534
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
Binary file removed .coverage
Binary file not shown.
18 changes: 18 additions & 0 deletions .github/workflows/code-cov.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ nginx-confs/*
*keys.txt

# Ignore the nginx conf for sarthi
sarthi.conf
sarthi.conf

.coverage
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
5 changes: 3 additions & 2 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
)


Expand Down Expand Up @@ -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}")

Expand Down
41 changes: 25 additions & 16 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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;
Expand All @@ -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):
Expand All @@ -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",
]


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand All @@ -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"}}',
Expand Down Expand Up @@ -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

0 comments on commit 2e66534

Please sign in to comment.