switch runners and use redis cache during testing

This commit is contained in:
wh1te909 2022-04-29 05:23:50 +00:00
parent a597dba775
commit f835997f49
4 changed files with 31 additions and 40 deletions

View File

@ -10,23 +10,36 @@ on:
jobs:
test:
runs-on: self-hosted
runs-on: ubuntu-latest
name: Tests
strategy:
matrix:
python-version: ['3.10.2', '3.10.4']
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup virtual env and install requirements
- uses: harmon758/postgresql-action@v1
with:
postgresql version: '14'
postgresql db: 'pipeline'
postgresql user: 'pipeline'
postgresql password: 'pipeline123456'
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install redis
run: |
sudo apt update
sudo apt install -y redis
redis-server --version
- name: Install requirements
working-directory: api/tacticalrmm
run: |
sudo -u postgres psql -c 'DROP DATABASE IF EXISTS pipeline'
sudo -u postgres psql -c 'DROP DATABASE IF EXISTS test_pipeline'
sudo -u postgres psql -c 'CREATE DATABASE pipeline'
sudo -u postgres psql -c "SET client_encoding = 'UTF8'" pipeline
pwd
rm -rf /actions-runner/_work/trmm-actions/trmm-actions/api/env
cd api
python3.10 -m venv env
source env/bin/activate
cd tacticalrmm
python --version
SETTINGS_FILE="tacticalrmm/settings.py"
SETUPTOOLS_VER=$(grep "^SETUPTOOLS_VER" "$SETTINGS_FILE" | awk -F'[= "]' '{print $5}')
@ -38,19 +51,16 @@ jobs:
- name: Run django tests
env:
GHACTIONS: "yes"
working-directory: api/tacticalrmm
run: |
cd api/tacticalrmm
source ../env/bin/activate
rm -f .coverage coverage.lcov
pytest
if [ $? -ne 0 ]; then
exit 1
fi
- name: Codestyle black
working-directory: api/tacticalrmm
run: |
cd api
source env/bin/activate
black --exclude migrations/ --check tacticalrmm
if [ $? -ne 0 ]; then
exit 1

View File

@ -1,6 +1,5 @@
from typing import Optional
from django.core.cache.backends.dummy import DummyCache
from django.core.cache.backends.redis import RedisCache
@ -14,8 +13,3 @@ class TacticalRedisCache(RedisCache):
# just for debugging
def show_everything(self, version: Optional[int] = None) -> list[bytes]:
return self._cache.get_client().keys(f":{version if version else 1}:*")
class TacticalDummyCache(DummyCache):
def delete_many_pattern(self, pattern: str, version: Optional[int] = None) -> None:
return None

View File

@ -155,13 +155,6 @@ CACHES = {
}
}
if "GHACTIONS" in os.environ:
CACHES = {
"default": {
"BACKEND": "tacticalrmm.cache.TacticalDummyCache",
}
}
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",

View File

@ -10,6 +10,7 @@ from accounts.models import User
from agents.models import Agent
from automation.models import Policy
from core.models import CoreSettings
from core.utils import clear_entire_cache
if TYPE_CHECKING:
from agents.models import Agent
@ -17,16 +18,7 @@ if TYPE_CHECKING:
from checks.models import Check
from scripts.models import Script
TEST_CACHE = {
"default": {
"BACKEND": "tacticalrmm.cache.TacticalDummyCache",
}
}
@override_settings(
CACHES=TEST_CACHE,
)
class TacticalTestCase(TestCase):
client: APIClient
@ -102,6 +94,7 @@ class TacticalTestCase(TestCase):
def check_not_authorized(
self, method: str, url: str, data: Optional[Dict[Any, Any]] = {}
) -> None:
clear_entire_cache()
try:
r = getattr(self.client, method)(url, data, format="json")
self.assertEqual(r.status_code, 403)
@ -111,6 +104,7 @@ class TacticalTestCase(TestCase):
def check_authorized(
self, method: str, url: str, data: Optional[Dict[Any, Any]] = {}
) -> Any:
clear_entire_cache()
try:
r = getattr(self.client, method)(url, data, format="json")
self.assertNotEqual(r.status_code, 403)