diff options
-rw-r--r-- | src/resource_inventory/resource_manager.py | 15 | ||||
-rw-r--r-- | src/resource_inventory/tests/test_managers.py | 54 |
2 files changed, 66 insertions, 3 deletions
diff --git a/src/resource_inventory/resource_manager.py b/src/resource_inventory/resource_manager.py index 652e4e3..e94b4ec 100644 --- a/src/resource_inventory/resource_manager.py +++ b/src/resource_inventory/resource_manager.py @@ -6,7 +6,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## - +import re from dashboard.exceptions import ( ResourceExistenceException, @@ -59,6 +59,7 @@ class ResourceManager: for profile in profile_count.keys(): available = Host.objects.filter( booked=False, + working=True, lab=grb.lab, profile=profile ).count() @@ -144,7 +145,7 @@ class ResourceManager: host_full_set = Host.objects.filter(lab__name__exact=labName, profile=genericHost.profile) if not host_full_set.first(): raise ResourceExistenceException("No matching servers found") - host_set = host_full_set.filter(booked=False) + host_set = host_full_set.filter(booked=False, working=True) if not host_set.first(): raise ResourceAvailabilityException("No unbooked hosts match requested hosts") host = host_set.first() @@ -172,3 +173,13 @@ class ResourceManager: self.releaseNetworks(grb, vlan_manager, vlans) for host in hosts: self.releaseHost(host) + + +class HostNameValidator(object): + regex = r'^[A-Za-z0-9][A-Za-z0-9-]*$' + message = "Hostnames can only contain alphanumeric characters and hyphens (-). Hostnames must start with a letter" + pattern = re.compile(regex) + + @classmethod + def is_valid_hostname(cls, hostname): + return len(hostname) < 65 and cls.pattern.fullmatch(hostname) is not None diff --git a/src/resource_inventory/tests/test_managers.py b/src/resource_inventory/tests/test_managers.py index 0e7c673..46cee5a 100644 --- a/src/resource_inventory/tests/test_managers.py +++ b/src/resource_inventory/tests/test_managers.py @@ -11,7 +11,7 @@ from django.test import TestCase from django.contrib.auth.models import User from resource.inventory_manager import InventoryManager -from resource.resource_manager import ResourceManager +from resource.resource_manager import ResourceManager, HostNameValidator from account.models import Lab from resource.models import ( Host, @@ -247,3 +247,55 @@ class ResourceManagerTestCase(TestCase): def test_convert_bundle(self): ResourceManager.getInstance().convertResoureBundle(self.genericBundle, self.lab.name) # verify bundle configuration + + +class HostNameValidatorTestCase(TestCase): + + def test_valid_hostnames(self): + self.assertTrue(HostNameValidator.is_valid_hostname("localhost")) + self.assertTrue(HostNameValidator.is_valid_hostname("Localhost")) + self.assertTrue(HostNameValidator.is_valid_hostname("localHost")) + self.assertTrue(HostNameValidator.is_valid_hostname("LOCALHOST")) + self.assertTrue(HostNameValidator.is_valid_hostname("f")) + self.assertTrue(HostNameValidator.is_valid_hostname("abc123doreyme")) + self.assertTrue(HostNameValidator.is_valid_hostname("F9999999")) + self.assertTrue(HostNameValidator.is_valid_hostname("my-host")) + self.assertTrue(HostNameValidator.is_valid_hostname("My-Host")) + self.assertTrue(HostNameValidator.is_valid_hostname("MY-HOST")) + self.assertTrue(HostNameValidator.is_valid_hostname("a-long-name-for-my-host")) + + def test_invalid_hostnames(self): + self.assertFalse(HostNameValidator.is_valid_hostname("-long-name-for-my-host")) + self.assertFalse(HostNameValidator.is_valid_hostname("546")) + self.assertFalse(HostNameValidator.is_valid_hostname("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) + + def test_invalid_chars(self): + self.assertFalse(HostNameValidator.is_valid_hostname("contains!char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains@char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains#char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains$char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains%char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains^char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains&char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains*char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains(char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains)char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains_char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains=char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains+char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains|char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains\\char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains[char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains]char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains;char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains:char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains'char")) + self.assertFalse(HostNameValidator.is_valid_hostname('contains"char')) + self.assertFalse(HostNameValidator.is_valid_hostname("contains'char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains<char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains>char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains,char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains?char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains/char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains`char")) + self.assertFalse(HostNameValidator.is_valid_hostname("contains~char")) |