Skip to content

Commit

Permalink
Merge pull request #168 from gregoil/better_request_and_register
Browse files Browse the repository at this point in the history
Better request and register
  • Loading branch information
osherdp committed Oct 22, 2019
2 parents 6615572 + c052cd5 commit 12b315d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup, find_packages


__version__ = "7.13.1"
__version__ = "7.14.0"

result_handlers = [
"db = rotest.core.result.handlers.db_handler:DBHandler",
Expand Down
4 changes: 2 additions & 2 deletions src/rotest/api/resource_control/lock_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@


USER_NOT_EXIST = "User {} has no matching object in the DB"
INVALID_RESOURCES = "No existing resource meets the requirements: {!r}"
UNAVAILABLE_RESOURCES = "No available resource meets the requirements: {!r}"
INVALID_RESOURCES = "No existing resource meets the requirements: {}"
UNAVAILABLE_RESOURCES = "No available resource meets the requirements: {}"


class LockResources(DjangoRequestView):
Expand Down
28 changes: 26 additions & 2 deletions src/rotest/management/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Used in order to modify the appearance of tables in the admin site.
"""
# pylint: disable=too-many-public-methods
# pylint: disable=too-many-public-methods,protected-access
from __future__ import absolute_import
from django.contrib import admin
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as utext
from django.db.models import ForeignKey, OneToOneField, ManyToManyField, Model

from rotest.management.models import ResourceData
from rotest.common.django_utils.common import linked_unicode
Expand Down Expand Up @@ -99,7 +101,7 @@ def has_add_permission(self, request):
admin.site.register(ResourceData, ResourceDataAdmin)


def register_resource_to_admin(resource_type, attr_list=(), link_list=()):
def register_resource_to_admin(resource_type, attr_list=None, link_list=None):
"""Create a admin-view class using the given arguments.
Args:
Expand All @@ -110,6 +112,24 @@ def register_resource_to_admin(resource_type, attr_list=(), link_list=()):
Returns:
ResourceAdmin. resource admin view class.
"""
if attr_list is None and link_list is None:
attr_list = []
link_list = []

fields = [field for field in
resource_type._meta.fields + resource_type._meta.many_to_many
if field.model != ResourceData and '_ptr' not in field.name]

for field in fields:
if isinstance(field, (ForeignKey, OneToOneField, ManyToManyField)):
link_list.append(field.name)

else:
attr_list.append(field.name)

attr_list = [] if attr_list is None else attr_list
link_list = [] if link_list is None else link_list

# Create link properties to be displayed for the relations.
link_properties = []
for field_name in link_list:
Expand All @@ -120,6 +140,10 @@ def link_method(self, field_to_convert=field_name):
if instance is None:
return '(None)'

if not isinstance(instance, Model):
return mark_safe(', '.join(linked_unicode(value)
for value in instance.all()))

return linked_unicode(instance)

property_name = '%s_link' % field_name
Expand Down
21 changes: 16 additions & 5 deletions src/rotest/management/client/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
from rotest.common import core_log
from rotest.management.client.client import AbstractClient
from rotest.api.common.responses import FailureResponseModel
from rotest.api.resource_control.lock_resources import USER_NOT_EXIST
from rotest.management.common.resource_descriptor import ResourceDescriptor
from rotest.api.resource_control.lock_resources import (USER_NOT_EXIST,
UNAVAILABLE_RESOURCES)
from rotest.common.config import (RESOURCE_MANAGER_HOST,
ROTEST_WORK_DIR,
SMART_CLIENT)
from rotest.management.common.errors import (ResourceReleaseError,
ResourceUnavailableError,
ResourceDoesNotExistError,
UnknownUserError)
from rotest.api.resource_control import (LockResources,
QueryResources,
Expand All @@ -50,6 +52,8 @@ class ClientResourceManager(AbstractClient):
keep_resources (bool): whether to keep the resources locked until
they are not needed.
"""
REQUEST_RETRY_INTERVAL = 0.5 # Seconds

def __init__(self, host=None, logger=core_log,
keep_resources=SMART_CLIENT):
"""Initialize the resource client."""
Expand Down Expand Up @@ -186,11 +190,18 @@ def _wait_until_resources_are_locked(self, descriptors, timeout):
if match:
raise UnknownUserError(response.details)

if time.time() - start_time > timeout:
raise ResourceUnavailableError(response.details)
match = re.match(UNAVAILABLE_RESOURCES.format(".*"),
response.details)
if match:
if time.time() - start_time >= timeout:
raise ResourceUnavailableError(response.details)

time.sleep(self.REQUEST_RETRY_INTERVAL)
continue

raise ResourceDoesNotExistError(response.details)

else:
break
break

return response

Expand Down
12 changes: 6 additions & 6 deletions tests/management/test_resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_lock_non_existing_name_resource(self):
% (self.NON_EXISTING_NAME1, resources_num))

descriptor = Descriptor(DemoResource, name=self.NON_EXISTING_NAME1)
self.assertRaises(ResourceUnavailableError,
self.assertRaises(ResourceDoesNotExistError,
self.client._lock_resources,
descriptors=[descriptor],
timeout=self.LOCK_TIMEOUT)
Expand All @@ -321,7 +321,7 @@ def test_lock_non_existing_field_resource(self):
descriptor = Descriptor(DemoResource, name=self.FREE1_NAME)
descriptor.properties[self.NON_EXISTING_FIELD] = 0

self.assertRaises(ResourceUnavailableError,
self.assertRaises(ResourceDoesNotExistError,
self.client._lock_resources,
descriptors=[descriptor],
timeout=self.LOCK_TIMEOUT)
Expand Down Expand Up @@ -963,7 +963,7 @@ def test_locking_other_group_resource(self):

descriptor = Descriptor(DemoResource, name=self.OTHER_GROUP_RESOURCE)

with self.assertRaises(ResourceUnavailableError):
with self.assertRaises(ResourceDoesNotExistError):
self.client._lock_resources(descriptors=[descriptor],
timeout=self.LOCK_TIMEOUT)

Expand Down Expand Up @@ -1782,7 +1782,7 @@ def test_lock_non_existing_name_resource(self):
% (self.NON_EXISTING_NAME1, resources_num))

descriptor = Descriptor(DemoResource, name=self.NON_EXISTING_NAME1)
self.assertRaises(ResourceUnavailableError,
self.assertRaises(ResourceDoesNotExistError,
self.client._lock_resources,
descriptors=[descriptor],
timeout=self.LOCK_TIMEOUT)
Expand All @@ -1799,7 +1799,7 @@ def test_lock_non_existing_field_resource(self):
descriptor = Descriptor(DemoResource, name=self.FREE1_NAME)
descriptor.properties[self.NON_EXISTING_FIELD] = 0

self.assertRaises(ResourceUnavailableError,
self.assertRaises(ResourceDoesNotExistError,
self.client._lock_resources,
descriptors=[descriptor],
timeout=self.LOCK_TIMEOUT)
Expand Down Expand Up @@ -2011,7 +2011,7 @@ def test_locking_other_group_resource(self):

descriptor = Descriptor(DemoResource, name=self.OTHER_GROUP_RESOURCE)

with self.assertRaises(ResourceUnavailableError):
with self.assertRaises(ResourceDoesNotExistError):
self.client._lock_resources(descriptors=[descriptor],
timeout=self.LOCK_TIMEOUT)

Expand Down

0 comments on commit 12b315d

Please sign in to comment.