From f835997f49d68634d32e6375667834c830095014 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Fri, 29 Apr 2022 05:23:50 +0000 Subject: [PATCH] switch runners and use redis cache during testing --- .github/workflows/ci-tests.yml | 46 +++++++++++++++---------- api/tacticalrmm/tacticalrmm/cache.py | 6 ---- api/tacticalrmm/tacticalrmm/settings.py | 7 ---- api/tacticalrmm/tacticalrmm/test.py | 12 ++----- 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index 73aa0f59..4f66e692 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -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 diff --git a/api/tacticalrmm/tacticalrmm/cache.py b/api/tacticalrmm/tacticalrmm/cache.py index f487bbb0..5297728e 100644 --- a/api/tacticalrmm/tacticalrmm/cache.py +++ b/api/tacticalrmm/tacticalrmm/cache.py @@ -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 diff --git a/api/tacticalrmm/tacticalrmm/settings.py b/api/tacticalrmm/tacticalrmm/settings.py index 00f7daaf..459a47ff 100644 --- a/api/tacticalrmm/tacticalrmm/settings.py +++ b/api/tacticalrmm/tacticalrmm/settings.py @@ -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", diff --git a/api/tacticalrmm/tacticalrmm/test.py b/api/tacticalrmm/tacticalrmm/test.py index a824ac72..c5244ec9 100644 --- a/api/tacticalrmm/tacticalrmm/test.py +++ b/api/tacticalrmm/tacticalrmm/test.py @@ -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)