From c36cb8d99ea594e214bef40cfd726d720c2c663c Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Fri, 25 May 2018 12:55:43 +0300 Subject: [PATCH] [connection] Made test mixins reusable (for extensions) --- openwisp_controller/connection/tests/base.py | 83 +++++++++++++++++++ .../connection/tests/test_models.py | 83 +------------------ setup.cfg | 2 +- 3 files changed, 88 insertions(+), 80 deletions(-) create mode 100644 openwisp_controller/connection/tests/base.py diff --git a/openwisp_controller/connection/tests/base.py b/openwisp_controller/connection/tests/base.py new file mode 100644 index 000000000..a57464e4f --- /dev/null +++ b/openwisp_controller/connection/tests/base.py @@ -0,0 +1,83 @@ +import os + +from django.conf import settings +from mockssh import Server as SshServer +from openwisp_controller.config.models import Config, Device +from openwisp_controller.config.tests import CreateConfigTemplateMixin + +from openwisp_users.tests.utils import TestOrganizationMixin + +from .. import settings as app_settings +from ..models import Credentials, DeviceConnection, DeviceIp + + +class CreateConnectionsMixin(CreateConfigTemplateMixin, TestOrganizationMixin): + device_model = Device + config_model = Config + + def _create_credentials(self, **kwargs): + opts = dict(name='Test credentials', + connector=app_settings.CONNECTORS[0][0], + params={'username': 'root', + 'password': 'password', + 'port': 22}) + opts.update(kwargs) + if 'organization' not in opts: + opts['organization'] = self._create_org() + c = Credentials(**opts) + c.full_clean() + c.save() + return c + + def _create_credentials_with_key(self, username='root', port=22, **kwargs): + opts = dict(name='Test SSH Key', + params={'username': username, + 'key': self._SSH_PRIVATE_KEY, + 'port': port}) + return self._create_credentials(**opts) + + def _create_device_connection(self, **kwargs): + opts = dict(enabled=True, + params={}) + opts.update(kwargs) + if 'credentials' not in opts: + opts['credentials'] = self._create_credentials() + org = opts['credentials'].organization + if 'device' not in opts: + opts['device'] = self._create_device(organization=org) + self._create_config(device=opts['device']) + dc = DeviceConnection(**opts) + dc.full_clean() + dc.save() + return dc + + def _create_device_ip(self, **kwargs): + opts = dict(address='10.40.0.1', + priority=1) + opts.update(kwargs) + if 'device' not in opts: + dc = self._create_device_connection() + opts['device'] = dc.device + ip = DeviceIp(**opts) + ip.full_clean() + ip.save() + return ip + + +class SshServerMixin(object): + _TEST_RSA_KEY_PATH = os.path.join(settings.BASE_DIR, 'test-key.rsa') + _SSH_PRIVATE_KEY = None + + @classmethod + def setUpClass(cls): + with open(cls._TEST_RSA_KEY_PATH, 'r') as f: + cls._SSH_PRIVATE_KEY = f.read() + cls.ssh_server = SshServer({'root': cls._TEST_RSA_KEY_PATH}) + cls.ssh_server.__enter__() + + @classmethod + def tearDownClass(cls): + try: + cls.ssh_server.__exit__() + except OSError: + pass diff --git a/openwisp_controller/connection/tests/test_models.py b/openwisp_controller/connection/tests/test_models.py index 4f8ec61c2..b9d0acc72 100644 --- a/openwisp_controller/connection/tests/test_models.py +++ b/openwisp_controller/connection/tests/test_models.py @@ -1,89 +1,14 @@ -import os - import paramiko -from django.conf import settings from django.core.exceptions import ValidationError from django.test import TestCase -from mockssh import Server as SshServer -from openwisp_controller.config.models import Config, Device -from openwisp_controller.config.tests import CreateConfigTemplateMixin - -from openwisp_users.tests.utils import TestOrganizationMixin from .. import settings as app_settings -from ..models import Credentials, DeviceConnection, DeviceIp +from ..models import Credentials, DeviceIp from ..utils import get_interfaces +from .base import CreateConnectionsMixin, SshServerMixin -class TestConnectionMixin(CreateConfigTemplateMixin, TestOrganizationMixin): - device_model = Device - config_model = Config - _TEST_RSA_KEY_PATH = os.path.join(settings.BASE_DIR, 'test-key.rsa') - with open(_TEST_RSA_KEY_PATH, 'r') as f: - _SSH_PRIVATE_KEY = f.read() - - @classmethod - def setUpClass(cls): - cls.ssh_server = SshServer({'root': cls._TEST_RSA_KEY_PATH}) - cls.ssh_server.__enter__() - - @classmethod - def tearDownClass(cls): - try: - cls.ssh_server.__exit__() - except OSError: - pass - - def _create_credentials(self, **kwargs): - opts = dict(name='Test credentials', - connector=app_settings.CONNECTORS[0][0], - params={'username': 'root', - 'password': 'password', - 'port': 22}) - opts.update(kwargs) - if 'organization' not in opts: - opts['organization'] = self._create_org() - c = Credentials(**opts) - c.full_clean() - c.save() - return c - - def _create_credentials_with_key(self, username='root', port=22, **kwargs): - opts = dict(name='Test SSH Key', - params={'username': username, - 'key': self._SSH_PRIVATE_KEY, - 'port': port}) - return self._create_credentials(**opts) - - def _create_device_connection(self, **kwargs): - opts = dict(enabled=True, - params={}) - opts.update(kwargs) - if 'credentials' not in opts: - opts['credentials'] = self._create_credentials() - org = opts['credentials'].organization - if 'device' not in opts: - opts['device'] = self._create_device(organization=org) - self._create_config(device=opts['device']) - dc = DeviceConnection(**opts) - dc.full_clean() - dc.save() - return dc - - def _create_device_ip(self, **kwargs): - opts = dict(address='10.40.0.1', - priority=1) - opts.update(kwargs) - if 'device' not in opts: - dc = self._create_device_connection() - opts['device'] = dc.device - ip = DeviceIp(**opts) - ip.full_clean() - ip.save() - return ip - - -class TestModels(TestConnectionMixin, TestCase): +class TestModels(SshServerMixin, CreateConnectionsMixin, TestCase): def test_connection_str(self): c = Credentials(name='Dev Key', connector=app_settings.CONNECTORS[0][0]) self.assertIn(c.name, str(c)) @@ -127,7 +52,7 @@ def test_device_connection_auto_update_strategy_missing_config(self): try: self._create_device_connection(device=device) except ValidationError as e: - self.assertIn('inferred from', str(e)) + self.assertIn('inferred from', str(e)) else: self.fail('ValidationError not raised') diff --git a/setup.cfg b/setup.cfg index 601777923..ffc1f985e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,4 +6,4 @@ known_third_party = django,django_netjsonconfig,django_x509 known_first_party = openwisp_users, openwisp_utils line_length=110 default_section = THIRDPARTY -skip = migrations \ No newline at end of file +skip = migrations