Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add main unit tests for compose helper util #12

Merged
merged 6 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
tushar5526 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run pytest on PR

on:
pull_request:
push:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.11 # Choose your Python version

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
tushar5526 marked this conversation as resolved.
Show resolved Hide resolved

- name: Run pytest
run: python -m pytest -vvv
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ venv.bak/

deployments/*
nginx-confs/*
*.txt
*keys.txt
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==7.4.4
pytest-mock==3.12.0
12 changes: 4 additions & 8 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ class DeploymentConfig:
compose_file_location: str = "docker-compose.yml"
rest_action: str = "POST"

def __post_init__(self):
# Check if all members are specified
missing_members = [
field.name for field in fields(self) if not hasattr(self, field.name)
]
if missing_members:
raise ValueError(f"Missing members: {', '.join(missing_members)}")

def get_project_hash(self):
return get_random_stub(f"{self.project_name}:{self.branch_name}")

Expand All @@ -53,6 +45,7 @@ class ComposeHelper:

def __init__(self, compose_file_location: str, load_compose_file=True):
self._compose_file_location = compose_file_location
self._compose = None
if load_compose_file:
self._compose = load_yaml_file(self._compose_file_location)
tushar5526 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -110,6 +103,9 @@ def _generate_processed_compose_file(
"services"
]["nginx"]

self._write_compose_file()

def _write_compose_file(self):
with open(self._compose_file_location, "w") as yaml_file:
tushar5526 marked this conversation as resolved.
Show resolved Hide resolved
# Dump the data to the YAML file
yaml.dump(self._compose, yaml_file, default_flow_style=False)
Expand Down
72 changes: 72 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest

from server.utils import ComposeHelper


@pytest.fixture
def compose_helper(mocker):
test_compose_file = """
# Test Docker compose to be used in tests

version: '3'

services:
webapp:
image: nginx:alpine
ports:
- "8080:80"

database:
image: postgres:latest
environment:
POSTGRES_DB: testdb
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpassword

redis:
image: redis:alpine
ports:
- "6379:6379"

messaging:
image: rabbitmq:management
ports:
- "5672:5672"
- "15672:15672"
environment:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest

api:
image: node:14
working_dir: /app
volumes:
- ./api:/app
command: npm start
ports:
- "3000:3000"
environment:
NODE_ENV: development

python_app:
image: python:3.8
volumes:
- ./python_app:/app
command: python app.py
environment:
PYTHON_ENV: testing

mongo_db:
image: mongo:latest
ports:
- "27017:27017"
environment:
MONGO_INITDB_DATABASE: testdb
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpassword

"""
mocker.patch("builtins.open", mocker.mock_open(read_data=test_compose_file))
compose_helper = ComposeHelper("test-docker-compose.yml")
mocker.patch.object(compose_helper, "_write_compose_file")
return compose_helper
76 changes: 76 additions & 0 deletions tests/test_utils.py
tushar5526 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pathlib

from server.utils import ComposeHelper, DeploymentConfig


def test_verify_project_hash_format():
# Given
config = DeploymentConfig(
project_name="test-project-name",
branch_name="test-branch-name",
project_git_url="https://github.com/tushar5526/test-project-name.git",
)
# When
project_hash = config.get_project_hash()

# Then
assert type(project_hash) == str
assert len(project_hash) == 16


def test_dont_load_compose_file_in_compose_helper():
compose_helper = ComposeHelper("random-compose.yml", load_compose_file=False)
assert getattr(compose_helper, "_compose") is None


def test_start_services(compose_helper, mocker):
# Given
mocked_run = mocker.patch("subprocess.run")

conf_file_path = "conf-file-path/some-nginx.conf"
deployment_namespace = "deployment-namespace"

# When
compose_helper.start_services(5000, conf_file_path, deployment_namespace)

# Then
# Processed compose file should be updated with following rules
# - No ports
# - No container_name
# - Restart should be present in each service
for service in compose_helper._compose["services"]:
if (
"ports" in compose_helper._compose["services"][service]
and service != f"nginx_{deployment_namespace}"
):
assert False, "Ports mapping present in processed compose file"
if "container_name" in compose_helper._compose["services"][service]:
assert False, "Container Name present in processed compose file"
if "restart" not in compose_helper._compose["services"][service]:
assert False, "Restart clause missing in processed compose file"
assert compose_helper._compose["services"][service]["restart"] == "always"
tushar5526 marked this conversation as resolved.
Show resolved Hide resolved

mocked_run.assert_called_once_with(
["docker-compose", "up", "-d", "--build"], check=True, cwd=pathlib.Path(".")
)


def test_remove_services(compose_helper, mocker):
mocked_run = mocker.patch("subprocess.run")
compose_helper.remove_services()
mocked_run.assert_called_once_with(
["docker-compose", "down", "-v"], check=True, cwd=pathlib.Path(".")
)


def test_get_service_ports_config(compose_helper):
service_config = {
"webapp": [("8080", "80")],
"database": [],
"redis": [("6379", "6379")],
"messaging": [("5672", "5672"), ("15672", "15672")],
"api": [("3000", "3000")],
"python_app": [],
"mongo_db": [("27017", "27017")],
}
assert compose_helper.get_service_ports_config() == service_config
Loading