Esempio n. 1
0
    def setUp(self):
        """Initialize two clients and connect them to resource manager."""
        super(TwoClientsParallelCase, self).setUp()

        self.client1 = ClientResourceManager(LOCALHOST)
        self.client2 = ClientResourceManager(LOCALHOST)

        self.client1.connect()
        self.client2.connect()
Esempio n. 2
0
    def create_resource_manager(self):
        """Create a new resource manager client instance.

        Returns:
            ClientResourceManager. new resource manager client.
        """
        return ClientResourceManager()
Esempio n. 3
0
def main():
    django.setup()
    create_result()

    print("Creating client")
    BaseResource._SHELL_CLIENT = ClientResourceManager()

    print("""Done! You can now lock resources and run tests, e.g.
    resource1 = ResourceClass.lock(skip_init=True, name='resource_name')
    resource2 = ResourceClass.lock(name='resource_name', config='config.json')
    shared_data['resource'] = resource1
    run_test(ResourceBlock, parameter=5)
    run_test(ResourceBlock.params(parameter=6), resource=resource2)
    run_test(SomeTestCase, debug=True)
    """)

    startup_commands = [IMPORT_BLOCK_UTILS, IMPORT_RESOURCE_LOADER]
    startup_commands.append("imported_resources = get_resources();"
                            "print('Importing resources:'); "
                            "print(', '.join(imported_resources.keys()));"
                            "globals().update(imported_resources)")
    startup_commands.extend(SHELL_STARTUP_COMMANDS)
    try:
        IPython.start_ipython(["-i", "--no-banner",
                               "-c", ";".join(startup_commands)])

    finally:
        print("Releasing locked resources...")
        BaseResource._SHELL_CLIENT.disconnect()
Esempio n. 4
0
    def lock(cls, config=None, skip_init=False, **kwargs):
        """Lock an instance of this resource class.

        Args:
            config (str): path to the json config file.
            skip_init (bool): whether to skip initialization or not.
            kwargs (dict): additional query parameters for the request,
                e.g. name or group.

        Returns:
            BaseResource. locked and initialized resource, ready for work.
        """
        # These runtime imports are done to avoid cyclic imports.
        from rotest.core.runner import parse_config_file
        from rotest.management.client.manager import (ClientResourceManager,
                                                      ResourceRequest)

        if BaseResource._SHELL_CLIENT is None:
            BaseResource._SHELL_CLIENT = ClientResourceManager()

        resource_request = ResourceRequest(BaseResource._SHELL_REQUEST_NAME,
                                           cls,
                                           **kwargs)

        config_dict = None
        if config is not None:
            config_dict = parse_config_file(config)

        result = BaseResource._SHELL_CLIENT.request_resources(
                                                        [resource_request],
                                                        config=config_dict,
                                                        skip_init=skip_init,
                                                        use_previous=False)

        return result[BaseResource._SHELL_REQUEST_NAME]
Esempio n. 5
0
def main():
    django.setup()

    print("Creating client")
    BaseResource._SHELL_CLIENT = ClientResourceManager()
    BaseResource._SHELL_CLIENT.connect()
    LogDebugHandler(None, sys.stdout, None)  # Activate log to screen

    print("""Done! You can now lock resources and run tests, e.g.
    resource1 = ResourceClass.lock(skip_init=True, name='resource_name')
    resource2 = ResourceClass.lock(name='resource_name', config='config.json')
    shared_data['resource'] = resource1
    run_block(ResourceBlock, parameter=5)
    run_block(ResourceBlock.params(parameter=6), resource=resource2)
    """)

    startup_commands = [IMPORT_BLOCK_UTILS, IMPORT_RESOURCE_LOADER]
    for app_name in SHELL_APPS:
        startup_commands.append("globals().update(get_resources(%r))" %
                                app_name)

    startup_commands.extend(SHELL_STARTUP_COMMANDS)
    try:
        IPython.start_ipython(["-i", "-c", ";".join(startup_commands)])

    finally:
        print("Releasing locked resources...")
        BaseResource._SHELL_CLIENT.disconnect()
Esempio n. 6
0
    def create_resource_manager():
        """Create a new resource manager client instance.

        Returns:
            ClientResourceManager. new resource manager client.
        """
        return ClientResourceManager(logger=core_log)
Esempio n. 7
0
    def setUp(self):
        """Initialize clients and connect them to the resource manager."""
        super(MultipleClientsParallelCase, self).setUp()

        self.clients = [
            ClientResourceManager(LOCALHOST)
            for _ in range(self.NUM_OF_CLIENTS)
        ]

        for client in self.clients:
            client.connect()
Esempio n. 8
0
def pytest_sessionstart(session):
    """Read and initialize Rotest global fields."""
    config = session.config
    RotestRunContext.CONFIG = AttrDict(
        chain(
            six.iteritems(
                parse_config_file(DEFAULT_CONFIG_PATH, DEFAULT_SCHEMA_PATH)),
            six.iteritems(
                parse_config_file(config.option.config_path,
                                  DEFAULT_SCHEMA_PATH)),
            filter_valid_values({
                'outputs': config.option.outputs,
                'debug': config.option.ipdbugger
            })))

    if not session.config.option.collectonly:
        RotestRunContext.RUN_DATA = RunData(
            config=json.dumps(RotestRunContext.CONFIG))

        RotestRunContext.RESOURCE_MANAGER = ClientResourceManager(
            logger=core_log)

    class AlmightySuite(TestSuite):
        components = [TestCase]

    main_test = AlmightySuite(
        run_data=RotestRunContext.RUN_DATA,
        config=config,
        indexer=RotestRunContext.INDEXER,
        skip_init=RotestRunContext.CONFIG.skip_init,
        save_state=RotestRunContext.CONFIG.save_state,
        enable_debug=RotestRunContext.CONFIG.debug,
        resource_manager=RotestRunContext.RESOURCE_MANAGER)

    RotestRunContext.MAIN_TEST = main_test
    main_test._tests = []
    main_test.name = main_test.get_name()
    main_test.data = SuiteData(name=main_test.name,
                               run_data=RotestRunContext.RUN_DATA)

    if not session.config.option.collectonly:
        RotestRunContext.RUN_DATA.main_test = main_test.data
        RotestRunContext.RESULT = Result(
            stream=sys.stdout,
            outputs=RotestRunContext.CONFIG.outputs,
            main_test=main_test)
Esempio n. 9
0
class TwoClientsParallelCase(AbstractManagerParallelCase):
    """Tests the resource-manager when 2 clients works in parallel."""
    def setUp(self):
        """Initialize two clients and connect them to resource manager."""
        super(TwoClientsParallelCase, self).setUp()

        self.client1 = ClientResourceManager(LOCALHOST)
        self.client2 = ClientResourceManager(LOCALHOST)

        self.client1.connect()
        self.client2.connect()

    def tearDown(self):
        """Disconnect the clients from resource manager."""
        self.client1.disconnect()
        self.client2.disconnect()

        super(TwoClientsParallelCase, self).tearDown()

    def execute_on_clients(self, action, timeout, *args):
        """Execute the given action on each client.

        * Builds execution threads of the given action for each client.
        * Starts the threads and waits until they'll finish their jobs.

        Args:
            action (method): the action to be executed.
            timeout (number): seconds to wait for thread to finish.
            args (tuple): arguments for action.
        """
        clients_threads = [
            self.build_action_thread(action, client, *args)
            for client in (self.client1, self.client2)
        ]
        self.execute_threads_and_wait(clients_threads, timeout)

    def test_lock_resource(self):
        """Validate parallel attempt to lock the same resource."""
        descriptor = Descriptor(DemoResource, name=self.RESOURCE1_NAME)
        self.execute_on_clients(ClientResourceManager._lock_resources,
                                self.ACTION_TIMEOUT, [descriptor],
                                self.ACTION_TIMEOUT - 1)

        resource = DemoResourceData.objects.get(name=self.RESOURCE1_NAME)
        self.assertFalse(
            resource.is_available(), "%r should be locked "
            "but found available" % (self.RESOURCE1_NAME))

        results = [self.client1.result, self.client2.result]
        self.assertEquals(
            results.count(self.SERVER_ERROR_CODE), 1,
            "One client should have failed, actual failures %d" %
            results.count(self.SERVER_ERROR_CODE))

        results.remove(self.SERVER_ERROR_CODE)

        result, = results
        self.assertTrue(
            isinstance(result, list), "LockResource action should "
            "return a list, got '%s'" % (type(result)))

        self.assertEqual(
            len(result), 1, "Only 1 resource should be locked but"
            " %d resources were locked." % (len(result)))

        locked_resource, = result
        self.assertEqual(
            locked_resource.name, self.RESOURCE1_NAME,
            "Expected locked resource name is %r, but got %r" %
            (self.RESOURCE1_NAME, locked_resource.name))

    def test_lock_different_resources(self):
        """Validate parallel attempt to lock different resources."""
        descriptor1 = Descriptor(DemoResource, name=self.RESOURCE1_NAME)
        thread1 = self.build_action_thread(
            ClientResourceManager._lock_resources, self.client1, [descriptor1])

        descriptor2 = Descriptor(DemoResource, name=self.RESOURCE2_NAME)
        thread2 = self.build_action_thread(
            ClientResourceManager._lock_resources, self.client2, [descriptor2])

        self.execute_threads_and_wait([thread1, thread2], self.ACTION_TIMEOUT)

        results = (self.client1.result, self.client2.result)
        expected_names = (self.RESOURCE1_NAME, self.RESOURCE2_NAME)

        for result, expected_name in zip(results, expected_names):
            resource = DemoResourceData.objects.get(name=expected_name)
            self.assertFalse(
                resource.is_available(), "%r should be "
                "locked but found available" % expected_name)

            self.assertTrue(
                isinstance(result, list), "LockResource action "
                "should return a list, got '%s'" % (type(result)))

            self.assertEqual(
                len(result), 1, "Only 1 resource should be locked"
                " but %d resources were locked." % (len(result)))

            self.assertEqual(
                result[0].name, expected_name,
                "Expected locked resource name is %r, but got %r" %
                (expected_name, result[0].name))