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/unittest #438

Merged
merged 51 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
3f6a524
feat(Dependencies): Update dependency ansible to v7 [SECURITY]
renovate[bot] Nov 22, 2023
1ffb8ba
Merge branch 'dev' into deps/pypi-ansible-vulnerability
Nov 28, 2023
f01070e
started tests
dweinholz Dec 19, 2023
5c96f29
more tests
dweinholz Dec 19, 2023
11cfbf5
more tests
dweinholz Dec 19, 2023
6374c61
more tests
dweinholz Dec 19, 2023
37fe96d
added coverage action
dweinholz Dec 19, 2023
56aa907
Update coverage.yml
dweinholz Dec 19, 2023
7c95888
updated action
dweinholz Dec 19, 2023
50938fc
install coverage
dweinholz Dec 19, 2023
52af75c
updated coverage path
dweinholz Dec 19, 2023
7b820c0
add .coveragerc
dweinholz Dec 19, 2023
e9c12cd
Merge branch 'dev' into feat/unittest
dweinholz Dec 19, 2023
2db1346
testing coverage report
dweinholz Dec 19, 2023
d211dbf
Merge branch 'feat/unittest' of github.com:deNBI/simplevm-client into…
dweinholz Dec 19, 2023
217a54a
fixed imports
dweinholz Dec 19, 2023
5c8e892
updated imports
dweinholz Dec 20, 2023
fa994bf
fixed coverage setup
dweinholz Dec 20, 2023
ae269e1
createlogfile if not exist
dweinholz Dec 20, 2023
de5058a
Branch was auto-updated.
github-actions[bot] Dec 20, 2023
33efaac
only check specific files
dweinholz Dec 20, 2023
317b699
Merge branch 'feat/unittest' of github.com:deNBI/simplevm-client into…
dweinholz Dec 20, 2023
2d80a69
added tests for thrift_converter
dweinholz Dec 20, 2023
0e13af8
removed .coveragerc
dweinholz Dec 20, 2023
16140ad
100% thrift_converter
dweinholz Dec 20, 2023
3271211
feat(Tests):Template tests
dweinholz Dec 21, 2023
19cc25d
finished template tests
dweinholz Dec 21, 2023
9d4e1b2
Branch was auto-updated.
github-actions[bot] Dec 22, 2023
76557b5
fix import
dweinholz Dec 22, 2023
bd4c0b7
fixing imports after thrift compiling not needed anymoore for docker …
dweinholz Dec 22, 2023
2d65558
update test
dweinholz Dec 22, 2023
ef87b9b
Merge branch 'deps/pypi-ansible-vulnerability' into feat/unittest
dweinholz Dec 22, 2023
5245bd0
adding unit test for playbook class
dweinholz Jan 2, 2024
08cc142
feat(UnitTest):finished playbook.yml tests
dweinholz Jan 2, 2024
c414643
Merge branch 'dev' into feat/unittest
dweinholz Jan 2, 2024
73c3080
feat(UnitTest):added bibigrid connector tests
dweinholz Jan 3, 2024
d729592
Merge branch 'feat/unittest' of github.com:deNBI/simplevm-client into…
dweinholz Jan 3, 2024
d4809c3
added more tests openstack
dweinholz Jan 3, 2024
3f90473
added more tests openstack
dweinholz Jan 3, 2024
776dde9
added more tests openstack
dweinholz Jan 3, 2024
b77e1c5
feat(UnitTests):finished openstack connector
dweinholz Jan 4, 2024
64ea82b
added pytest.ini and .coveragerc
dweinholz Jan 4, 2024
6284cbe
more tests
dweinholz Jan 4, 2024
e2881db
feat(UnitTests):added tests for virtualachinehandler
dweinholz Jan 4, 2024
4b7eb24
handler 100%
dweinholz Jan 4, 2024
a84a3df
done
dweinholz Jan 4, 2024
0affae4
fixed missing
dweinholz Jan 4, 2024
cd36574
Merge branch 'dev' into feat/unittest
dweinholz Jan 5, 2024
b62d454
Merge branch 'dev' into feat/unittest
dweinholz Jan 5, 2024
c55be14
merged dev
dweinholz Jan 5, 2024
12e01c3
Branch was auto-updated.
github-actions[bot] Jan 6, 2024
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
18 changes: 18 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[run]
omit =
simple_vm_client/VirtualMachineService.py
simple_vm_client/constants.py
simple_vm_client/test_openstack_connector.py
simple_vm_client/ttypes.py
simple_vm_client/forc_connector/template/test_templates.py
simple_vm_client/util/logger.py
simple_vm_client/forc_connector/test_forc_connector.py
simple_vm_client/VirtualMachineServer.py

check_env.py

[report]
exclude_lines =
pragma: no cover
raise NotImplementedError
if __name__ == .__main__.:
33 changes: 33 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will install dependencies, create coverage tests and run Pytest Coverage Comment
# For more information see: https://github.com/MishaKav/pytest-coverage-comment/
name: pytest-coverage-comment
on:
pull_request:
branches:
- '*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

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

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pytest-cov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Build coverage file
run: |
pytest --junitxml=pytest.xml | tee pytest-coverage.txt

- name: Pytest coverage comment
uses: MishaKav/pytest-coverage-comment@main
with:
pytest-coverage-path: ./pytest-coverage.txt
junitxml-path: ./pytest.xml
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ repos:
- id: end-of-file-fixer
- id: check-yaml

- repo: https://github.com/psf/black
rev: 23.12.1
- repo: https://github.com/psf/black-pre-commit-mirror

rev: 23.12.0
hooks:
- id: black
language_version: python3.11
- repo: https://github.com/sondrelg/pep585-upgrade
rev: 'v1.0.1' # Use the sha / tag you want to point at
hooks:
Expand Down
25 changes: 19 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
FROM python:3.11.4-buster
RUN apt-get update -y
RUN apt-get install -y build-essential

RUN apt-get update -y \
&& apt-get install -y build-essential \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /code
ADD requirements.txt /code

# Copy requirements and install them first to leverage Docker cache
COPY requirements.txt /code
RUN pip install -r requirements.txt
ADD requirements.yml /code
ADD ansible.cfg /etc/ansible/

COPY requirements.yml /code
COPY ansible.cfg /etc/ansible/
RUN ansible-galaxy install -r requirements.yml
ADD . /code

# Copy the entire project
COPY . /code

# Set PYTHONPATH to include the project root
ENV PYTHONPATH /code

WORKDIR /code/simple_vm_client
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ thrift_py: ## Builds python code from thrift file
thrift --gen py portal_client.thrift
cp -a gen-py/VirtualMachineService/. simple_vm_client
rm -rf gen-py
@echo Remember to fix the imports: for pip relative imports are needed, for others absolute imports

dev-build: ## Build and Start the docker-compose.dev.yml
docker-compose -f docker-compose.dev.yml up --build
Expand Down
6 changes: 6 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# pytest.ini

[pytest]
addopts = --cov=. --cov-config=.coveragerc

# Add other configuration options as needed
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ warn_unused_configs = True
[coverage:run]
include = simple_vm_client/*
omit = *migrations*, *tests*
plugins =
django_coverage_plugin
55 changes: 28 additions & 27 deletions simple_vm_client/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@
"""
from __future__ import annotations

from typing import TYPE_CHECKING

from bibigrid_connector.bibigrid_connector import BibigridConnector
from forc_connector.forc_connector import ForcConnector
from openstack_connector.openstack_connector import OpenStackConnector
from util import thrift_converter
from util.logger import setup_custom_logger
from VirtualMachineService import Iface

if TYPE_CHECKING:
from ttypes import (
VM,
Backend,
ClusterInfo,
ClusterInstance,
CondaPackage,
Flavor,
Image,
PlaybookResult,
ResearchEnvironmentTemplate,
Snapshot,
Volume,
)

from simple_vm_client.bibigrid_connector.bibigrid_connector import BibigridConnector
from simple_vm_client.forc_connector.forc_connector import ForcConnector
from simple_vm_client.openstack_connector.openstack_connector import OpenStackConnector
from simple_vm_client.util import thrift_converter
from simple_vm_client.util.logger import setup_custom_logger

from .ttypes import (
VM,
Backend,
ClusterInfo,
ClusterInstance,
CondaPackage,
Flavor,
Image,
PlaybookResult,
ResearchEnvironmentTemplate,
Snapshot,
Volume,
)
from .VirtualMachineService import Iface

logger = setup_custom_logger(__name__)

Expand Down Expand Up @@ -143,10 +141,13 @@ def get_server(self, openstack_id: str) -> VM:
return server

def get_servers(self) -> list[VM]:
serv = thrift_converter.os_to_thrift_servers(
openstack_servers=self.openstack_connector.get_servers()
)
return serv
servers = openstack_servers = self.openstack_connector.get_servers()
servers_full = []

for server in servers:
servers_full.append(self.forc_connector.get_playbook_status(server=server))
serv = thrift_converter.os_to_thrift_servers(openstack_servers=servers)
return servers_full

def get_servers_by_ids(self, server_ids: list[str]) -> list[VM]:
return thrift_converter.os_to_thrift_servers(
Expand Down
16 changes: 3 additions & 13 deletions simple_vm_client/VirtualMachineServer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import signal
import ssl
import sys
Expand All @@ -8,8 +7,9 @@
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket, TSSLSocket, TTransport
from VirtualMachineHandler import VirtualMachineHandler
from VirtualMachineService import Processor

from simple_vm_client.VirtualMachineHandler import VirtualMachineHandler
from simple_vm_client.VirtualMachineService import Processor

USERNAME = "OS_USERNAME"
PASSWORD = "OS_PASSWORD"
Expand Down Expand Up @@ -72,15 +72,5 @@ def catch_shutdown(signal: int, frame: object) -> None:
server.serve()


def check_environment_variables(envs: list[str]) -> None:
def check_env(var: str) -> None:
if var not in os.environ:
click.echo(f"ERROR: There is no {var} set in environment.")
click.echo("Please make sure you have sourced your OpenStack rc file")
sys.exit()

list(map(lambda var: check_env(var), envs))


if __name__ == "__main__":
startServer()
28 changes: 22 additions & 6 deletions simple_vm_client/VirtualMachineService.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion simple_vm_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ['ttypes', 'constants', 'VirtualMachineService']
__all__ = ["ttypes", "constants", "VirtualMachineService"]
46 changes: 26 additions & 20 deletions simple_vm_client/bibigrid_connector/bibigrid_connector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import requests
import yaml
from ttypes import ClusterInfo, ClusterInstance
from util.logger import setup_custom_logger

from simple_vm_client.ttypes import ClusterInfo, ClusterInstance
from simple_vm_client.util.logger import setup_custom_logger

logger = setup_custom_logger(__name__)

Expand Down Expand Up @@ -53,27 +54,32 @@ def load_config_yml(self, config_file: str) -> None:

def get_cluster_status(self, cluster_id: str) -> dict[str, str]:
logger.info(f"Get Cluster {cluster_id} status")

headers = {"content-Type": "application/json"}
body = {"mode": "openstack"}
request_url = self._BIBIGRID_URL + "info/" + cluster_id
response = requests.get(
url=request_url,
json=body,
headers=headers,
verify=self._PRODUCTION,
)
logger.info(f"Cluster {cluster_id} status: {str(response.content)} ")
json_resp: dict[str, str] = response.json(strict=False)
try:
json_resp["log"] = str(json_resp["log"])
except Exception:
logger.info(f"No Logs for Cluster - {cluster_id}")
request_url = f"{self._BIBIGRID_URL}info/{cluster_id}"

try:
json_resp["msg"] = str(json_resp["msg"])
except Exception:
logger.info(f"No msg for Cluster - {cluster_id}")
response = requests.get(
url=request_url,
json=body,
headers=headers,
verify=self._PRODUCTION,
)
response.raise_for_status() # Raise an exception for HTTP errors (4xx and 5xx)
json_resp = response.json(strict=False)

# Convert log and msg keys to strings, handling the case where they might not exist
json_resp["log"] = str(json_resp.get("log", ""))
json_resp["msg"] = str(json_resp.get("msg", ""))

return json_resp
logger.info(f"Cluster {cluster_id} status: {json_resp}")

return json_resp

except requests.RequestException as e:
logger.exception("Error while getting Cluster status")
return {"error": str(e)}

def get_cluster_info(self, cluster_id: str) -> ClusterInfo:
logger.info(f"Get Cluster info from {cluster_id}")
Expand Down Expand Up @@ -127,7 +133,7 @@ def is_bibigrid_available(self) -> bool:
logger.error(f"Bibigrid returned status code {response.status_code}")
return False

except requests.RequestException as e:
except requests.RequestException:
logger.exception("Error while checking Bibigrid availability")
return False

Expand Down
Loading