Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ omit =
/*/__init__.py
/setup.py
/*/migrations/*
source = openwisp_controller
parallel = true
concurrency = multiprocessing
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ jobs:
image: redis
ports:
- 6379:6379
postgres:
image: mdillon/postgis:11-alpine
env:
POSTGRES_PASSWORD: openwisp2
POSTGRES_USER: openwisp2
POSTGRES_DB: openwisp2
ports:
- 5432:5432

strategy:
fail-fast: false
Expand Down Expand Up @@ -73,7 +81,9 @@ jobs:
./run-qa-checks
- name: Tests
run: |
coverage run --source=openwisp_controller runtests.py
coverage run runtests.py --parallel
POSTGRESQL=1 coverage run runtests.py --parallel --keepdb
coverage combine
Comment thread
nemesifier marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try combining these changes with the changes in openwisp/openwisp-users#340 (move flags to rc file and run tests in parallel), in the second call to coverage, in order to avoid increasing the build time excessively, we could run only the tests of the subdivision app, in this case --keepdb should help speed up things a bit.

POSTGRESQL=1 coverage run /tests/manage.py test openwisp_controller.subnet_division.tests

It should work!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using POSTGRESQL=1 only a subset of tests are executed. The abstraction is done in runtests.py to allow running the tests locally as well.

if os.environ.get('POSTGRESQL', False):
args.extend(['--tag', 'db_tests'])

# SAMPLE tests
SAMPLE_APP=1 ./runtests.py --keepdb
env:
Expand Down
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ Navigate into the cloned repository:

cd openwisp-controller/

Launch Redis:
Launch Redis and PostgreSQL:

.. code-block:: shell

docker-compose up -d redis
docker-compose up -d redis postgres

Setup and activate a virtual-environment. (we'll be using `virtualenv <https://pypi.org/project/virtualenv/>`_)

Expand Down Expand Up @@ -305,6 +305,8 @@ Run tests with:
.. code-block:: shell

./runtests.py --parallel
# To run database tests against PostgreSQL backend
POSTGRESQL=1 ./runtests.py --parallel

Run quality assurance tests with:

Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ services:
ports:
- "6379:6379"
entrypoint: redis-server --appendonly yes

postgres:
image: mdillon/postgis:11-alpine
environment:
POSTGRES_PASSWORD: openwisp2
POSTGRES_USER: openwisp2
POSTGRES_DB: openwisp2
ports:
- 5432:5432
8 changes: 6 additions & 2 deletions openwisp_controller/subnet_division/rule_types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from operator import attrgetter

from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.db import connection, transaction
from django.dispatch import Signal
from django.utils.translation import gettext_lazy as _
from netaddr import IPNetwork
Expand Down Expand Up @@ -164,11 +164,15 @@ def get_config(cls, instance):

@staticmethod
def get_max_subnet(master_subnet, division_rule):
# Only PostgreSQL supports ordering queryset using the "subnet"
# field. If the project is using any other database backend, then
# "created" field is used for ordering the queryset.
order_field = '-subnet' if connection.vendor == 'postgresql' else '-created'
try:
max_subnet = (
# Get the highest subnet created for this master_subnet
Subnet.objects.filter(master_subnet_id=master_subnet.id)
.order_by('-created')
.order_by(order_field)
.first()
.subnet
)
Expand Down
30 changes: 28 additions & 2 deletions openwisp_controller/subnet_division/tests/test_rule.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
from django.test import TestCase
from django.db import connection
from django.test import TestCase, tag
from openwisp_ipam.tests import CreateModelsMixin as SubnetIpamMixin
from swapper import load_model

from ..rule_types.base import BaseSubnetDivisionRuleType
from ..rule_types.vpn import VpnSubnetDivisionRuleType

SubnetDivisionRule = load_model('subnet_division', 'SubnetDivisionRule')

class TestBaseSubnetDivisionRuleType(TestCase):

class TestBaseSubnetDivisionRuleType(SubnetIpamMixin, TestCase):
def test_should_create_subnets_ips(self):
with self.assertRaises(NotImplementedError):
BaseSubnetDivisionRuleType.should_create_subnets_ips(instance=None)

def test_provision_for_existing_objects(self):
with self.assertRaises(NotImplementedError):
BaseSubnetDivisionRuleType.provision_for_existing_objects(rule_obj=None)

@tag('db_tests')
def test_get_max_subnet(self):
rule = SubnetDivisionRule(
**{
'label': 'OW',
'size': 28,
'number_of_ips': 2,
'number_of_subnets': 2,
'type': VpnSubnetDivisionRuleType,
}
)
master_subnet = self._create_subnet(subnet='10.0.0.0/16')
self._create_subnet(subnet='10.0.0.16/28', master_subnet=master_subnet)
self._create_subnet(subnet='10.0.0.0/28', master_subnet=master_subnet)
max_subnet = VpnSubnetDivisionRuleType.get_max_subnet(master_subnet, rule)
if connection.vendor == 'postgresql':
self.assertEqual(str(max_subnet), '10.0.0.16/28')
else:
self.assertEqual(str(max_subnet), '10.0.0.0/28')
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ django_redis~=4.12
mock-ssh-server~=0.9.1
responses~=0.12.1
selenium~=3.141.0
psycopg2-binary~=2.8.0
9 changes: 8 additions & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

if __name__ == '__main__':
sys.path.insert(0, 'tests')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openwisp2.settings')
if os.environ.get('POSTGRESQL', False):
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openwisp2.postgresql_settings')
else:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openwisp2.settings')
from django.core.management import execute_from_command_line

args = sys.argv
Expand All @@ -18,6 +21,10 @@
args.insert(2, 'openwisp_controller')
else:
args.insert(2, 'openwisp2')

if os.environ.get('POSTGRESQL', False):
args.extend(['--tag', 'db_tests'])

execute_from_command_line(args)

if not os.environ.get('SAMPLE_APP', False):
Expand Down
12 changes: 12 additions & 0 deletions tests/openwisp2/postgresql_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .settings import *

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'openwisp2',
'USER': 'openwisp2',
'PASSWORD': 'openwisp2',
'HOST': '127.0.0.1',
'PORT': '5432',
},
}