Skip to content

Commit

Permalink
Merge pull request #152 from gregoil/enable_sending_config_and_resour…
Browse files Browse the repository at this point in the history
…ces_to_tests

Enable sending config and resources to tests
  • Loading branch information
osherdp committed Jul 9, 2019
2 parents a013a32 + 2f40ad0 commit cd3f1d9
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 30 deletions.
28 changes: 19 additions & 9 deletions src/rotest/core/abstract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,28 @@ def request_resources(self, resources_to_request, use_previous=False,
force_initialize (bool): whether the resources will be initialized
even if the validation succeeds and skip_init is True.
"""
if len(resources_to_request) == 0:
# No resources to requested
new_requests = []

for resource_request in resources_to_request:
if resource_request.name in self.all_resources:
self.logger.debug("Already has a resource named %r, "
"skipping request", resource_request)

else:
new_requests.append(resource_request)

if len(new_requests) == 0:
# No resources to request
return

requested_resources = self.resource_manager.request_resources(
config=self.config,
skip_init=self.skip_init,
use_previous=use_previous,
base_work_dir=self.work_dir,
requests=resources_to_request,
enable_debug=self.enable_debug,
force_initialize=force_initialize)
config=self.config,
skip_init=self.skip_init,
use_previous=use_previous,
base_work_dir=self.work_dir,
requests=new_requests,
enable_debug=self.enable_debug,
force_initialize=force_initialize)

self.add_resources(requested_resources)
self.locked_resources.update(requested_resources)
Expand Down
7 changes: 6 additions & 1 deletion src/rotest/core/flow_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from rotest.common.config import ROTEST_WORK_DIR
from rotest.core.abstract_test import AbstractTest
from rotest.management.common.errors import ServerError
from rotest.management.base_resource import BaseResource
from rotest.core.models.case_data import TestOutcome, CaseData


Expand Down Expand Up @@ -423,7 +424,11 @@ def _set_parameters(self, override_previous=True, **parameters):
if override_previous or (name not in self.__dict__ and
name not in self._pipes):

setattr(self, name, value)
if isinstance(value, BaseResource):
self.add_resources({name: value})

else:
setattr(self, name, value)

def validate_inputs(self, extra_inputs=[]):
"""Validate that all the required inputs of the component were passed.
Expand Down
10 changes: 10 additions & 0 deletions src/rotest/core/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ def __init__(self, tests=(), indexer=count(),

core_log.debug("Initialized %r test-suite successfully", self.data)

def add_resources(self, resources):
"""Add the resources to the child tests.
Args:
resources (dict): dictionary of attributes name to resources
instance.
"""
for test in self:
test.add_resources(resources)

@classmethod
def get_name(cls):
"""Return test name as used in Django DB.
Expand Down
71 changes: 51 additions & 20 deletions src/rotest/management/utils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import django
import IPython
from attrdict import AttrDict
from future.builtins import object
from future.builtins import object, next

from rotest.core.suite import TestSuite
from rotest.core.result.result import Result
from rotest.common.config import SHELL_STARTUP_COMMANDS
Expand All @@ -17,20 +18,16 @@
from rotest.core.runner import parse_config_file, DEFAULT_CONFIG_PATH
from rotest.core.result.handlers.stream.log_handler import LogDebugHandler


# Mock tests result object for running blocks
result_object = Result(stream=None, descriptions=None,
outputs=[], main_test=None)


# Mock tests configuration for running blocks
default_config = AttrDict(parse_config_file(DEFAULT_CONFIG_PATH))


# Container for data shared between blocks
shared_data = {}


ENABLE_DEBUG = False
IMPORT_BLOCK_UTILS = \
"from rotest.management.utils.shell import shared_data, run_test"
Expand Down Expand Up @@ -67,11 +64,13 @@ def request_resources(self, resources_to_request, *args, **kwargs):
for name in request_names})


def _run_block(block_class, debug=ENABLE_DEBUG, **kwargs):
def _run_block(block_class, config=default_config,
debug=ENABLE_DEBUG, **kwargs):
"""Run a block of the given class, passing extra parameters as arguments.
Args:
block_class (type): class inheriting from AbstractFlowComponent.
config (dict): run configuration dict.
debug (bool): whether to run the test in debug mode or not.
kwargs (dict): additional arguments that will be passed as parameters
to the block (overriding shared data).
Expand All @@ -82,7 +81,7 @@ def _run_block(block_class, debug=ENABLE_DEBUG, **kwargs):
parent = ShellMockFlow()
block_class = block_class.params(**shared_kwargs)

block = block_class(config=default_config,
block = block_class(config=config,
parent=parent,
enable_debug=debug,
resource_manager=BaseResource._SHELL_CLIENT,
Expand All @@ -91,31 +90,63 @@ def _run_block(block_class, debug=ENABLE_DEBUG, **kwargs):
parent.work_dir = block.work_dir
block.validate_inputs()
block.run(result_object)
return block


def run_test(test_class, debug=ENABLE_DEBUG, **kwargs):
def _run_suite(test_class, config=default_config, debug=ENABLE_DEBUG,
**kwargs):
"""Run a test of the given class, passing extra parameters as arguments.
Args:
test_class (type): class inheriting from AbstractTest.
config (dict): run configuration dict.
debug (bool): whether to run the test in debug mode or not.
kwargs (dict): additional arguments that will be passed as parameters
if the test is a block or flow (overriding shared data).
kwargs (dict): resources to use for the test.
"""
if issubclass(test_class, AbstractFlowComponent):
return _run_block(test_class, debug=debug, **kwargs)
test = test_class(config=config,
enable_debug=debug,
resource_manager=BaseResource._SHELL_CLIENT)

if not test_class.IS_COMPLEX:
class AlmightySuite(TestSuite):
components = [test_class]
test.add_resources(kwargs)
test.run(result_object)
return test

test_class = AlmightySuite

test = test_class(config=default_config,
enable_debug=debug,
resource_manager=BaseResource._SHELL_CLIENT)
def _run_case(test_class, config=default_config, debug=ENABLE_DEBUG, **kwargs):
"""Run a test of the given class, passing extra parameters as arguments.
test.run(result_object)
Args:
test_class (type): class inheriting from AbstractTest.
config (dict): run configuration dict.
debug (bool): whether to run the test in debug mode or not.
kwargs (dict): resources to use for the test.
"""
class AlmightySuite(TestSuite):
components = [test_class]

suite_instance = _run_suite(AlmightySuite, config, debug, **kwargs)

return next(iter(suite_instance))


def run_test(test_class, config=default_config, debug=ENABLE_DEBUG, **kwargs):
"""Run a test of the given class, passing extra parameters as arguments.
Args:
test_class (type): class inheriting from AbstractTest.
config (dict): run configuration dict.
debug (bool): whether to run the test in debug mode or not.
kwargs (dict): additional arguments that will be passed as parameters
if the test is a block or flow (overriding shared data),
or resources to use for the test.
"""
if issubclass(test_class, AbstractFlowComponent):
return _run_block(test_class, config, debug, **kwargs)

if test_class.IS_COMPLEX:
return _run_suite(test_class, config, debug, **kwargs)

return _run_case(test_class, config, debug, **kwargs)


def main():
Expand Down
155 changes: 155 additions & 0 deletions tests/core/test_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""Test Rotest's TestCase class behavior."""
# pylint: disable=missing-docstring,unused-argument,protected-access
# pylint: disable=no-member,no-self-use,too-many-public-methods,invalid-name
from __future__ import absolute_import

from rotest.core import request
from rotest.management.utils.shell import run_test
from rotest.management.models.ut_resources import DemoResource
from rotest.management.models.ut_models import DemoResourceData

from tests.core.utils import (BasicRotestUnitTest, MockCase, MockBlock,
MockFlow, MockTestSuite)

RESOURCE_NAME = 'available_resource1'


class TempResourceCase(MockCase):
"""Inherit class and override resources requests."""
__test__ = False

resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)

def test_method(self):
self.assertEqual(self.test_resource.name, RESOURCE_NAME)


class TempResourceSuite(MockTestSuite):
"""Inherit class and override resources requests."""
__test__ = False

components = [TempResourceCase]


class TempResourceBlock(MockBlock):
"""Inherit class and override resources requests."""
__test__ = False

def test_method(self):
self.assertEqual(self.test_resource.name, RESOURCE_NAME)


class TempResourceFlow(MockFlow):
"""Inherit class and override resources requests."""
__test__ = False

resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)

blocks = [TempResourceBlock]


class TestShell(BasicRotestUnitTest):
"""Test Rotest shell functionality."""
fixtures = ['resource_ut.json']

DEMO_RESOURCE_NAME = 'test_resource'

def test_case_supplying_config(self):
config = {'some': 'value'}
test = run_test(TempResourceCase, config=config)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.config, config, 'Test ran with the wrong config')

def test_case_supplying_resource_via_kwargs(self):
resource = DemoResource(
data=DemoResourceData.objects.get(name=RESOURCE_NAME))

test = run_test(TempResourceCase, test_resource=resource)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.test_resource, resource,
"Test didn't get the supplied resource")

def test_case_not_supplying_any_resource(self):
test = run_test(TempResourceCase)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

def test_block_supplying_config(self):
config = {'some': 'value'}
resource = DemoResource(
data=DemoResourceData.objects.get(name=RESOURCE_NAME))

test = run_test(TempResourceBlock, config=config,
test_resource=resource)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.config, config, 'Test ran with the wrong config')

def test_block_supplying_resource_via_kwargs(self):
resource = DemoResource(
data=DemoResourceData.objects.get(name=RESOURCE_NAME))

test = run_test(TempResourceBlock, test_resource=resource)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.test_resource, resource,
"Test didn't get the supplied resource")

def test_block_not_supplying_any_resource(self):
test = run_test(TempResourceBlock)
self.assertFalse(test.data.success,
'Block passed even though no resource was supplied')

def test_flow_supplying_config(self):
config = {'some': 'value'}
test = run_test(TempResourceFlow, config=config)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.config, config, "Test ran with the wrong config")

def test_flow_supplying_resource_via_kwargs(self):
resource = DemoResource(
data=DemoResourceData.objects.get(name=RESOURCE_NAME))

test = run_test(TempResourceFlow, test_resource=resource)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.test_resource, resource,
"Test didn't get the supplied resource")

def test_flow_not_supplying_any_resource(self):
test = run_test(TempResourceFlow)
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

def test_suite_supplying_config(self):
config = {'some': 'value'}
test = run_test(TempResourceSuite, config=config)._tests[0]
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.config, config, "Test ran with the wrong config")

def test_suite_supplying_resource_via_kwargs(self):
resource = DemoResource(
data=DemoResourceData.objects.get(name=RESOURCE_NAME))

test = run_test(TempResourceSuite, test_resource=resource)._tests[0]
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

self.assertEqual(test.test_resource, resource,
"Test didn't get the supplied resource")

def test_suite_not_supplying_any_resource(self):
test = run_test(TempResourceSuite)._tests[0]
self.assertTrue(test.data.success,
'Case failed when it should have succeeded')

0 comments on commit cd3f1d9

Please sign in to comment.