From c28a39fa476bd3607c7cc29969d60b30f0f5c808 Mon Sep 17 00:00:00 2001 From: Shubhank Saxena Date: Thu, 1 Jul 2021 20:35:43 +0530 Subject: [PATCH] Segregation of Dev and Prod envs (#218) Segregation of Dev and Prod envs, addition of tests Co-authored-by: Markos Gogoulos Co-authored-by: Ubuntu --- .github/workflows/python.yml | 54 ++++++++++++++--------------- .gitignore | 1 + Dockerfile-dev | 16 +++++++++ README.md | 2 +- conftest.py | 5 +++ docker-compose-dev.yaml | 66 ++++++++++++++++++++++++++++++++++++ pytest.ini | 6 ++++ requirements-dev.txt | 16 +++++++++ requirements.txt | 15 ++------ tests/test_selenium_smoke.py | 13 +++++++ tests/users/factories.py | 15 ++++++++ tests/users/test_sample.py | 4 +++ 12 files changed, 171 insertions(+), 42 deletions(-) create mode 100644 Dockerfile-dev create mode 100644 conftest.py create mode 100644 docker-compose-dev.yaml create mode 100644 pytest.ini create mode 100644 requirements-dev.txt create mode 100644 tests/test_selenium_smoke.py create mode 100644 tests/users/factories.py create mode 100644 tests/users/test_sample.py diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 913dfcd..d1b32b2 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -1,37 +1,35 @@ name: Python Tests -on: [push] +on: + pull_request: + push: + branches: + - main jobs: build: runs-on: ubuntu-latest - services: - postgres: - image: postgres:12 - env: - POSTGRES_USER: mediacms - POSTGRES_PASSWORD: mediacms - POSTGRES_DB: mediacms - ports: - - 5432:5432 - # needed because the postgres container does not provide a healthcheck - options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 - steps: - - uses: actions/checkout@v1 - - name: Set up Python 3.7 - uses: actions/setup-python@v1 - with: - python-version: 3.7 - - name: psycopg2 prerequisites - run: sudo apt-get install libpq-dev - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - name: Run migrations - run: python manage.py migrate - - name: Run tests - run: py.test + - name: Checkout + uses: actions/checkout@v1 + + - name: Build the Stack + run: docker-compose -f docker-compose-dev.yaml build + + - name: Start containers + run: docker-compose -f docker-compose-dev.yaml up -d + + - name: List containers + run: docker ps + + - name: Sleep for 60 seconds + run: sleep 60s + shell: bash + + - name: Run Django Tests + run: docker-compose -f docker-compose-dev.yaml exec -T web pytest + + - name: Tear down the Stack + run: docker-compose -f docker-compose-dev.yaml down \ No newline at end of file diff --git a/.gitignore b/.gitignore index 665e663..392ce65 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,6 @@ static/ckeditor/ static/debug_toolbar/ static/mptt/ static/rest_framework/ +static/drf-yasg cms/local_settings.py deploy/docker/local_settings.py diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 0000000..2547fc1 --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,16 @@ +FROM mediacms/mediacms:latest + +SHELL ["/bin/bash", "-c"] + +# Set up virtualenv +ENV VIRTUAL_ENV=/home/mediacms.io +ENV PATH="$VIRTUAL_ENV/bin:$PATH" +ENV PIP_NO_CACHE_DIR=1 + +RUN cd /home/mediacms.io && python3 -m venv $VIRTUAL_ENV + +COPY requirements.txt . +COPY requirements-dev.txt . +RUN pip install -r requirements-dev.txt + +WORKDIR /home/mediacms.io/mediacms diff --git a/README.md b/README.md index 9c06cc3..9f84669 100644 --- a/README.md +++ b/README.md @@ -125,4 +125,4 @@ If you like the project, here's a few things you can do - Checkout the [Code of conduct page](CODE_OF_CONDUCT.md) if you want to contribute to this repository ## Contact -info@mediacms.io +info@mediacms.io \ No newline at end of file diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..7f2a2ba --- /dev/null +++ b/conftest.py @@ -0,0 +1,5 @@ +from pytest_factoryboy import register + +from tests.users.factories import UserFactory + +register(UserFactory) diff --git a/docker-compose-dev.yaml b/docker-compose-dev.yaml new file mode 100644 index 0000000..1afca7a --- /dev/null +++ b/docker-compose-dev.yaml @@ -0,0 +1,66 @@ +version: "3" + +services: + # frontend: + # image: node:14 + # volumes: + # - ${PWD}/frontend:/home/mediacms.io/mediacms/frontend/ + # working_dir: /home/mediacms.io/mediacms/frontend/ + # command: bash -c "npm install && npm run start" + # env_file: + # - ${PWD}/frontend/.env + # ports: + # - "8097:8097" + # depends_on: + # - web + web: + build: + context: . + dockerfile: ./Dockerfile-dev + image: mediacms/mediacms-dev:latest + ports: + - "80:80" + volumes: + - ./:/home/mediacms.io/mediacms/ + depends_on: + redis: + condition: service_healthy + db: + condition: service_healthy + selenium_hub: + container_name: selenium_hub + image: selenium/hub + ports: + - "4444:4444" + selenium_chrome: + container_name: selenium_chrome + image: selenium/node-chrome-debug + environment: + - HUB_PORT_4444_TCP_ADDR=selenium_hub + - HUB_PORT_4444_TCP_PORT=4444 + ports: + - "5900:5900" + depends_on: + - selenium_hub + db: + image: postgres + volumes: + - ../postgres_data:/var/lib/postgresql/data/ + restart: always + environment: + POSTGRES_USER: mediacms + POSTGRES_PASSWORD: mediacms + POSTGRES_DB: mediacms + healthcheck: + test: ["CMD-SHELL", "pg_isready -U mediacms"] + interval: 10s + timeout: 5s + retries: 5 + redis: + image: "redis:alpine" + restart: always + healthcheck: + test: ["CMD", "redis-cli","ping"] + interval: 30s + timeout: 10s + retries: 3 \ No newline at end of file diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..a5eac27 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,6 @@ +[pytest] +DJANGO_SETTINGS_MODULE = cms.settings +python_files = test_*.py + +markers = + slow: slow running test \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..dfde72c --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,16 @@ +-r requirements.txt + +rpdb +tqdm +ipython +flake8 +pylint +pep8 +django-silk +pre-commit +pytest-cov +pytest-django +pytest-factoryboy +Faker +selenium +webdriver-manager \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index d3f102c..58ac65d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,17 +28,6 @@ django-celery-email m3u8 django-ckeditor - -django-login-required-middleware==0.6.1 - -# extra nice utilities! -rpdb -tqdm -ipython -flake8 -pylint -pep8 -django-silk django-debug-toolbar -pre-commit -pytest-django + +django-login-required-middleware==0.6.1 \ No newline at end of file diff --git a/tests/test_selenium_smoke.py b/tests/test_selenium_smoke.py new file mode 100644 index 0000000..deb46ba --- /dev/null +++ b/tests/test_selenium_smoke.py @@ -0,0 +1,13 @@ +from django.test import TestCase +from selenium import webdriver +from selenium.webdriver.common.desired_capabilities import DesiredCapabilities + + +class SeleniumTest(TestCase): + def setUp(self): + self.chrome = webdriver.Remote(command_executor='http://selenium_hub:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME) + self.chrome.implicitly_wait(10) + + def test_visit_site_with_chrome(self): + self.chrome.get('http://web/admin') + self.assertIn(self.chrome.title, "Log in | Django site admin") diff --git a/tests/users/factories.py b/tests/users/factories.py new file mode 100644 index 0000000..5097e6c --- /dev/null +++ b/tests/users/factories.py @@ -0,0 +1,15 @@ +import factory +from django.conf import settings +from faker import Faker + +fake = Faker() +User = settings.AUTH_USER_MODEL + + +class UserFactory(factory.django.DjangoModelFactory): + class Meta: + model = User + + description = fake.paragraph(nb_sentences=4) + name = fake.name() + is_editor = True diff --git a/tests/users/test_sample.py b/tests/users/test_sample.py new file mode 100644 index 0000000..e083b96 --- /dev/null +++ b/tests/users/test_sample.py @@ -0,0 +1,4 @@ +def test_new_user(user_factory): + print(user_factory.name) + print(user_factory.description) + assert True