Skip to content

Commit

Permalink
Merge pull request #62 from edwardtheharris/15-try-to-increase-test-c…
Browse files Browse the repository at this point in the history
…overage

15 try to increase test coverage

Closes #15
  • Loading branch information
edwardtheharris authored Sep 10, 2024
2 parents b18c96a + 1a1983d commit 7586d6a
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
if [ -e ./secrets/coveralls.gpg ]; then
COVERALLS_REPO_TOKEN="$(gpg -d -q secrets/coveralls.gpg)"
OPENAI_API_KEY="$(gpg -d -q secrets/openai_api_key.gpg)"
SONAR_HOST_URL='https://sonarcloud.io'
SONAR_TOKEN="$(gpg -d -q secrets/sonar-token.gpg)"

export COVERALLS_REPO_TOKEN
export OPENAI_API_KEY
export SONAR_HOST_URL
export SONAR_TOKEN

fi
GNUPGHOME="$(pwd)/.gnupg"
PIPENV_VENV_IN_PROJECT=1
Expand Down
67 changes: 67 additions & 0 deletions .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,80 @@ jobs:
- name: Run tests with coverage
run: |
pytest --cov
pytest --junit-xml=results-${{ matrix.python-version }}.xml
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
check_name: pytest-${{ matrix.python-version }}
comment_title: "Pytest ${{ matrix.python-version }} Results"
files: results-${{ matrix.python-version }}.xml
- name: Coveralls
uses: coverallsapp/github-action@v2
with:
parallel: true
flag-name: run-${{ matrix.python-version }}
pylint:
name: PyLint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@main
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install -U pip pipenv
python -m pipenv requirements --dev > reqs
python -m pip install -r reqs
- name: Run PyLint
run: |
pylint **/*.py --output-format=pylint_junit.JUnitReporter --output=pylint.xml || true
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
check_name: pylint
comment_title: "PyLint Results"
files: pylint.xml
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Setup Python
uses: actions/setup-python@main
with:
python-version: 3.11
- name: Run tox
run: |
python -m pip install -U pip pipenv
python -m pipenv requirements --dev > reqs
python -m pip install -r reqs
pytest --cov --cov-report=xml
pytest --cov
version="$(python -c 'from k8s_agent.__init__ import __version__; print(__version__)')"
sed -i -e "s/__version__/${version}/" sonar-project.properties
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
with:
args: >
-Dsonar.projectKey=edwardtheharris_k8s-operator-agent
-Dsonar.projectName=k8s-operator-agent
-Dsonar.verbose=true
projectBaseDir: .
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

finish:
needs: build
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ __pycache__/
*.py[cod]
*$py.class
.gnupg
.coverage-reports/
reflog
deployment/helm/k8s-agent/charts
markdownlint-cli2-junit.xml
pylint.xml
results.xml
secrets/

# C extensions
Expand Down
5 changes: 3 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
duckduckgo-search = "*"
fastapi = "*"
gitpython = "*"
hypercorn = "*"
Expand All @@ -15,7 +16,6 @@ langserve = "*"
langsmith = "*"
loguru = "*"
pipenv = "*"
duckduckgo-search = "*"
sse-starlette = "*"

[dev-packages]
Expand All @@ -26,12 +26,13 @@ myst-parser = {extras = ["linkify"], version = "*"}
pylint = "*"
pylint-junit = "*"
pytest = "*"
pytest-asyncio = "*"
pytest-cov = "*"
pytest-pylint = "*"
sphinx = "*"
sphinx-autobuild = "*"
sphinx-book-theme = "*"
sphinx-design = "*"
sphinxemoji = "*"
tox = "*"
version-query = "*"
pytest-asyncio = "*"
186 changes: 125 additions & 61 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ date: 2024-08-04
title: Readme
---

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=edwardtheharris_k8s-operator-agent&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=edwardtheharris_k8s-operator-agent)

Kubernetes Operator Agent

## Development
Expand Down
15 changes: 15 additions & 0 deletions k8s_agent/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
"""Kubernetes Agent module."""
from pathlib import Path

import version_query

def get_version():
"""Query the current version for the project."""
try:
repo_path = Path(".").resolve()
ret_value = version_query.git_query.query_git_repo(repo_path).to_str()
except ValueError:
ret_value = version_query.Version.from_str('0.0.0')
ret_value = ret_value.devel_increment()
return ret_value

__version__ = get_version()
4 changes: 4 additions & 0 deletions k8s_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@


class ChatRequest(BaseModel):
"""Define a ChatRequest object."""

input: str
chat_history: Optional[List[Dict[str, str]]]


class Output(BaseModel):
"""Define an Output object."""
output: str


def serialize_history(request: ChatRequest):
"""Serialize the history of a conversation."""
chat_history = request["chat_history"] or []
converted_chat_history = []
for message in chat_history:
Expand Down
1 change: 1 addition & 0 deletions k8s_agent/prompts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Prompts module."""
SYSTEM_PROMPT = """
Act as an advanced AI system specialized in Kubernetes cluster management.
```CurrentClusterState
Expand Down
2 changes: 1 addition & 1 deletion k8s_agent/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
# from fastapi.staticfiles import StaticFiles

from hypercorn.asyncio import serve
from hypercorn.config import Config
Expand Down
10 changes: 10 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
sonar.coverage.exclusions=tests/
sonar.organization=edwardtheharris
sonar.projectKey=edwardtheharris_k8s-operator-agent
sonar.projectName=k8s-operator-agent
sonar.projectVersion=__version__
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.pylint.reportPaths=pylint.yml
sonar.python.version=3.8,3.9,3.10,3.11,3.12
sonar.sourceEncoding=UTF-8
sonar.sources=k8s_agent/
sonar.tests=tests/
49 changes: 49 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Kubernetes Operator Agent init test module."""
# pylint: disable=redefined-outer-name,reimported,import-outside-toplevel
from unittest.mock import patch
from unittest.mock import MagicMock

import version_query

from k8s_agent import get_version
from k8s_agent import __version__

def test_get_version_success():
"""Test get_version when git version query is successful.
Mock the return value of query_git_repo to simulate successful
version retrieval.
"""

mock_version = MagicMock()
mock_version.to_str.return_value = "1.2.3"

with patch("k8s_agent.version_query.git_query.query_git_repo",
return_value=mock_version):
version = get_version()
assert version == "1.2.3"
mock_version.to_str.assert_called_once()

def test_get_version_fallback():
"""Test get_version failure back to default version.
Mock the query_git_repo to raise a ValueError
Mock the Version.from_str and its devel_increment behavior
"""
with patch("k8s_agent.version_query.git_query.query_git_repo",
side_effect=ValueError):
mock_version = MagicMock()
mock_version.devel_increment.return_value = "0.0.1"

with patch("k8s_agent.version_query.Version.from_str",
return_value=mock_version):
version = get_version()
assert version == "0.0.1"
mock_version.devel_increment.assert_called_once()

def test_version_init():
"""Test if __version__ is initialized correctly."""
with patch("k8s_agent.get_version", return_value="1.2.3"):
from k8s_agent import __version__
assert __version__ == version_query.Version.from_str("0.0.1.dev1")

0 comments on commit 7586d6a

Please sign in to comment.