Exemplo n.º 1
0
    def test_complex_resource_unpacking(self):
        """Test a TestCase with all the ways to request resources.

        * Way 1 - overriding 'resources'.
        * Way 2 - declaring fields with BaseResource instance.
        """
        TempSuccessCase.resources = (request('test_resource',
                                             DemoComplexResource), )

        case = self._run_case(TempSuccessCase)

        self.assertTrue(self.result.wasSuccessful(),
                        'Case failed when it should have succeeded')

        self.assertFalse(hasattr(case, 'demo1'),
                         "Resource unpacked though it shouldn't have")

        TempSuccessCase.resources = (request('test_resource',
                                             DemoComplexResource).unpack(), )

        case = self._run_case(TempSuccessCase)

        self.assertTrue(self.result.wasSuccessful(),
                        'Case failed when it should have succeeded')

        self.assertTrue(hasattr(case, 'demo1'),
                        "Resource didn't unpacked though it should have")
Exemplo n.º 2
0
    def test_dynamic_resources_locking(self):
        """Test that cases can dynamically lock resources.

        * Run a test that dynamically requests resources.
        * Validate that the resources were initialized and finalized.
        """
        TempDynamicResourceLockingCase.resources = (request(
            'test_resource', DemoResource, name=RESOURCE_NAME), )
        dynamic_resource_name = 'available_resource2'
        TempDynamicResourceLockingCase.dynamic_resources = (request(
            'dynamic_resource', DemoResource, name=dynamic_resource_name), )

        case = self._run_case(TempDynamicResourceLockingCase)

        self.assertTrue(self.result.wasSuccessful(),
                        'Case failed when it should have succeeded')

        # === Validate case data object ===
        self.assertTrue(case.data.success)

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
        self.validate_resource(test_resource)
        test_resource = DemoResourceData.objects.get(
            name=dynamic_resource_name)
        self.validate_resource(test_resource)
Exemplo n.º 3
0
    def test_error_in_setup(self):
        """Test a TestCase on setup error.

        * Defines the registered resource as required resource.
        * Runs the test under a test suite.
        * Validates that the test fails.
        * Validates the case's data object.
        * Validates the resource's state.
        """
        TempErrorInSetupCase.resources = (request('test_resource',
                                                  DemoResource,
                                                  name=RESOURCE_NAME), )

        case = self._run_case(TempErrorInSetupCase)

        self.assertFalse(self.result.wasSuccessful(),
                         'Case succeeded when it should have failed')

        # === Validate case data object ===
        self.assertFalse(case.data.success,
                         'Case data result should have been False')

        self.assertEqual(
            case.data.exception_type, TestOutcome.ERROR,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.ERROR, case.data.exception_type))

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)

        self.validate_resource(test_resource, initialized=True, finalized=True)
Exemplo n.º 4
0
    def test_skip_initialize(self):
        """Tests the force_initialize flag when False.

        Note:
            DemoResource fixture sets the 'validate_flag' as False.

        * Defines a resource as required resource & set force_initialize.
        * Runs the test under a test suite.
        * Validates that 'validate' method was called.
        * Validates that 'initialize' method was not called.
        """
        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
        self.assertFalse(test_resource.validate_flag)

        test_resource.validation_result = True
        test_resource.save()
        TempSuccessCase.resources = (request(resource_name='validate_resource',
                                             resource_class=DemoResource,
                                             name=RESOURCE_NAME), )

        self._run_case(TempSuccessCase)

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
        test_resource.validation_result = False
        test_resource.save()
        self.assertTrue(test_resource.validate_flag,
                        "Resource wasn't validated as expected")
        self.assertFalse(test_resource.initialization_flag,
                         "Resource was unexpectedly initialized")
Exemplo n.º 5
0
    def test_unexpected_success_case_run(self):
        """Test a TestCase run on unexpected success.

        * Defines the registered resource as required resource.
        * Runs the test under a test suite.
        * Validates that the test succeeds.
        * Validates the case's data object.
        * Validates the resource's state.
        """
        TempUnexpectedSuccessCase.resources = (request('test_resource',
                                                       DemoResource,
                                                       name=RESOURCE_NAME), )

        case = self._run_case(TempUnexpectedSuccessCase)
        self.assertFalse(self.result.wasSuccessful(),
                         'Case succeeded when it should have failed')

        # === Validate case data object ===
        self.assertFalse(case.data.success,
                         'Case data result should have been True')

        self.assertEqual(
            case.data.exception_type, TestOutcome.UNEXPECTED_SUCCESS,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.UNEXPECTED_SUCCESS, case.data.exception_type))

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)

        self.validate_resource(test_resource)
Exemplo n.º 6
0
class ResourceIdRegistrationCase(BasicMultiprocessCase):
    """Saves the ID of the locked resource to a queue."""
    resources = (request('res1', DemoResource, ip_address=IP_ADDRESS1), )

    def test_method(self):
        """Register the resource's ID to the shared queue."""
        self.register_id(id(self.res1))
        time.sleep(0.5)  # Make sure the case won't be taken by the same worker
Exemplo n.º 7
0
    def test_locked_resource(self):
        """Test a TestCase with a locked required resource.

        * Locks a resource using the resource manager.
        * Defines one available and one locked resource as required resources.
        * Runs the test under a test suite.
        * Validates the result is failure and the resources' state.
        """
        available_resource_name = 'available_resource2'
        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
        test_resource.owner = self.FAKE_OWNER
        test_resource.save()

        TempSuccessCase.resources = (request('available_resource1',
                                             DemoResource,
                                             name=available_resource_name),
                                     request('locked_resource',
                                             DemoResource,
                                             name=RESOURCE_NAME))

        case = self._run_case(TempSuccessCase)

        # === Validate case data object ===
        self.assertFalse(case.data.success)
        self.assertEqual(
            case.data.exception_type, TestOutcome.ERROR,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.ERROR, case.data.exception_type))

        available_resource = DemoResourceData.objects.get(
            name=available_resource_name)
        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)

        self.validate_resource(test_resource,
                               validated=False,
                               initialized=False,
                               finalized=False)

        self.validate_resource(available_resource,
                               validated=False,
                               initialized=False,
                               finalized=False)
Exemplo n.º 8
0
class CheckResourceBlock(MockBlock):
    """Mock block, checks the version of its locked resource."""
    __test__ = False

    resources = (request('res', DemoResource, version=VERSION1), )

    EXPECTED_VERSION = VERSION1

    def test_version(self):
        """Verify the version of the locked resource."""
        self.assertEqual(self.res.data.version, self.EXPECTED_VERSION)
Exemplo n.º 9
0
    def test_expect_raises_and_assert_case_run(self):
        """Test a TestCase that uses both expect raises and assert.

        * Defines the registered resource as required resource.
        * Runs the test under a test suite.
        * Validates that the test failed with three tracebacks.
        * Validates the case's data object.
        * Validates the resource's state.
        """
        TempExpectRaisesCase.resources = (request('test_resource',
                                                  DemoResource,
                                                  name=RESOURCE_NAME), )

        case = self._run_case(TempExpectRaisesCase)

        self.assertFalse(self.result.wasSuccessful(),
                         'Case succeeded when it should have failed')

        # === Validate case data object ===
        self.assertFalse(case.data.success,
                         'Case data result should have been False')

        self.assertEqual(
            case.data.exception_type, TestOutcome.FAILED,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.FAILED, case.data.exception_type))

        expected_traceback = "%s.*%s.*%s.*" %\
                             (TempExpectRaisesCase.FAILURE_MESSAGE,
                              TempExpectRaisesCase.FAILURE_MESSAGE,
                              TempExpectRaisesCase.ASSERTION_MESSAGE)

        match = re.search(expected_traceback,
                          case.data.traceback,
                          flags=re.DOTALL)

        self.assertIsNotNone(
            match, "Unexpected traceback, "
            "%r doesn't match the expression %r" %
            (case.data.traceback, expected_traceback))

        test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]

        self.assertTrue(isinstance(test_resource, DemoResource),
                        "State resource type should have been 'DemoResource'")

        self.assertEqual(
            test_resource.mode, DemoResourceData.BOOT_MODE,
            "State resource mode attribute should "
            "have been 'boot'")

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)

        self.validate_resource(test_resource)
Exemplo n.º 10
0
class LongSuccessBlock(MockBlock):
    """Test that waits a while and then passes."""
    __test__ = False

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

    WAIT_TIME = 0.5

    def test_method(self):
        """Wait a while and then pass."""
        time.sleep(self.WAIT_TIME)
Exemplo n.º 11
0
    def test_error_in_resource_initialize_bad_first(self):
        """Test a TestCase on resource initialization error on the first one.

        * Defines the registered resources as required resource.
        * Runs the test under a test suite.
        * Validates that the test fails.
        * Validates the case's data object.
        * Validates the resources' state.
        """
        fail_resource_name = 'fail_initialize_resource'
        TempSuccessCase.resources = (request('fail_resource',
                                             DemoResource,
                                             name=fail_resource_name),
                                     request('ok_resource',
                                             DemoResource,
                                             name=RESOURCE_NAME))

        case = self._run_case(TempSuccessCase)

        self.assertFalse(self.result.wasSuccessful(),
                         'Case succeeded when it should have failed')

        # === Validate case data object ===
        self.assertFalse(case.data.success,
                         'Case data result should have been False')

        self.assertEqual(
            case.data.exception_type, TestOutcome.ERROR,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.ERROR, case.data.exception_type))

        ok_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
        fail_resource = DemoResourceData.objects.get(name=fail_resource_name)

        self.validate_resource(fail_resource,
                               initialized=False,
                               finalized=True)
        self.validate_resource(ok_resource,
                               validated=False,
                               initialized=False,
                               finalized=False)
Exemplo n.º 12
0
class MockCase(TestCase):
    """Mock case for unit testing Rotest.

    This case is used by Rotest's unit tests as a mock case which doesn't
    doesn't need a real resource manager in order to get resources.
    """
    # Setting class fixture
    resources = (request('res1', DemoResource, ip_address=IP_ADDRESS1),
                 request('res2', DemoResource, ip_address=IP_ADDRESS2))

    def create_resource_manager(self):
        """Create a new resource manager client instance.

        The resource client is overridden so it wouldn't need an actual
        resource manager in order to lock resources. This client provides the
        resources from the DB without asking any server for them.

        Returns:
            ClientResourceManager. new resource manager client.
        """
        return MockResourceClient()
Exemplo n.º 13
0
class ForceReleaseCase(SuccessCase):
    """Inherit class and override force initialize flag."""
    __test__ = False

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

    def request_resources(self,
                          resources_to_request,
                          use_previous=False,
                          force_initialize=False):
        return super(ForceReleaseCase,
                     self).request_resources(resources_to_request,
                                             use_previous, True)
Exemplo n.º 14
0
class ModifyResourceBlock(MockBlock):
    """Mock block, changes the version of its locked resource."""
    __test__ = False

    resources = (request('res', DemoResource, version=VERSION1), )

    FIELD_TO_CHANGE = NotImplemented
    VALUE_TO_SET = NotImplemented

    def test_change_version(self):
        """Alter a field of the locked resource."""
        setattr(self.res.data, self.FIELD_TO_CHANGE, self.VALUE_TO_SET)
        self.res.data.save()
Exemplo n.º 15
0
    def test_save_state_on_passed_test(self):
        """Test the usage of the save_state flag when a test has passed.

        When a test has passed it is expected that it WILL NOT save the state.
        """
        resource_name = 'save_state_resource'
        TempSuccessCase.resources = (request(resource_name=resource_name,
                                             resource_class=DemoResource,
                                             name=RESOURCE_NAME), )

        case = self._run_case(TempSuccessCase, save_state=True)
        expected_state_path = os.path.join(case.work_dir,
                                           TempSuccessCase.STATE_DIR_NAME)

        self.assertFalse(os.path.exists(expected_state_path))
Exemplo n.º 16
0
    def test_save_state(self):
        """Test the save_sate flag.

        * Defines a resource as required resource that not save state.
        * Runs the test under a test suite.
        * Validates store_state method wasn't called.
        """
        resource_name = 'save_state_resource'
        TempSuccessCase.resources = (request(resource_name=resource_name,
                                             resource_class=DemoResource,
                                             name=RESOURCE_NAME), )

        case = self._run_case(TempSuccessCase, save_state=True)
        expected_state_path = os.path.join(case.work_dir,
                                           TempSuccessCase.STATE_DIR_NAME)

        self.assertFalse(os.path.exists(expected_state_path))
Exemplo n.º 17
0
class MockFlow(TestFlow):
    """Mock test flow for unit-testing blocks behavior."""
    __test__ = False

    resources = (request('res1', DemoResource, ip_address=IP_ADDRESS1), )

    def create_resource_manager(self):
        """Create a new resource manager client instance.

        The resource client is overridden so it wouldn't need an actual
        resource manager in order to lock resources. This client provides the
        resources from the DB without asking any server for them.

        Returns:
            ClientResourceManager. new resource manager client.
        """
        return MockResourceClient()
Exemplo n.º 18
0
    def test_store_state(self):
        """Test the resource store sate method.

        * Define a resource as required resource.
        * Run the test under a test suite.
        * Validate store_state method was called (it writes a file).
        """
        resource_name = 'store_resource'
        TempErrorCase.resources = (request(resource_name=resource_name,
                                           resource_class=DemoResource,
                                           name=RESOURCE_NAME), )

        case = self._run_case(TempErrorCase, save_state=True)

        expected_state_path = os.path.join(case.work_dir,
                                           TempErrorCase.STATE_DIR_NAME)

        self.assertTrue(os.path.exists(expected_state_path))
Exemplo n.º 19
0
    def test_missing_resource(self):
        """Test a TestCase with a missing required resource.

        * Defines a missing resource as required resource.
        * Runs the test under a test suite
        * Validates that the test failed.
        """
        TempSuccessCase.resources = (request('missing_resource',
                                             NonExistingResource,
                                             name='missing'), )

        case = self._run_case(TempSuccessCase)

        # === Validate case data object ===
        self.assertFalse(case.data.success)
        self.assertEqual(
            case.data.exception_type, TestOutcome.ERROR,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.ERROR, case.data.exception_type))
Exemplo n.º 20
0
    def test_store_state(self):
        """Test the resource store sate method.

        * Define a resource as required resource.
        * Run the test under a test suite.
        * Validate store_state method was called (it writes a file).
        """
        resource_name = 'store_resource'
        TempSuccessCase.resources = (request(resource_name=resource_name,
                                             resource_class=DemoResource,
                                             name=RESOURCE_NAME), )

        case = self._run_case(TempSuccessCase)

        test_resource = case.all_resources.values()[0]
        expected_state_path = os.path.join(
            test_resource.work_dir, ClientResourceManager.DEFAULT_STATE_DIR)

        self.assertTrue(os.path.exists(expected_state_path))
Exemplo n.º 21
0
    def test_error_and_stored_failure_case_run(self):
        """Test a TestCase on that stores a failure and raises an exception.

        * Defines the registered resource as required resource.
        * Runs the test under a test suite.
        * Validates that the test fails.
        * Validates the case's data object.
        * Validates the resource's state.
        """
        TempStoreFailureErrorCase.resources = (request('test_resource',
                                                       DemoResource,
                                                       name=RESOURCE_NAME), )

        case = self._run_case(TempStoreFailureErrorCase)

        self.assertFalse(self.result.wasSuccessful(),
                         'Case succeeded when it should have failed')

        # === Validate case data object ===
        self.assertFalse(case.data.success,
                         'Case data result should have been False')

        self.assertEqual(
            case.data.exception_type, TestOutcome.ERROR,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.ERROR, case.data.exception_type))

        expected_traceback = "%s.*%s.*%s.*" % (
            TempStoreFailureErrorCase.FAILURE_MESSAGE, CaseData.TB_SEPARATOR,
            TempStoreFailureErrorCase.ERROR_MESSAGE)

        match = re.search(expected_traceback,
                          case.data.traceback,
                          flags=re.DOTALL)

        self.assertIsNotNone(
            match, "Unexpected traceback, %r doesn't match the expression %r" %
            (case.data.traceback, expected_traceback))

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)

        self.validate_resource(test_resource)
Exemplo n.º 22
0
    def test_expect_and_assert_case_run(self):
        """Test a TestCase that uses both expect and assert.

        * Defines the registered resource as required resource.
        * Runs the test under a test suite.
        * Validates that the test failed.
        * Validates the case's data object.
        * Validates the resource's state.
        """
        TempStoreFailureCase.resources = (request('test_resource',
                                                  DemoResource,
                                                  name=RESOURCE_NAME), )

        case = self._run_case(TempStoreFailureCase)

        self.assertFalse(self.result.wasSuccessful(),
                         'Case succeeded when it should have failed')

        # === Validate case data object ===
        self.assertFalse(case.data.success,
                         'Case data result should have been False')

        self.assertEqual(
            case.data.exception_type, TestOutcome.FAILED,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.FAILED, case.data.exception_type))

        expected_traceback = "%s.*" % TempStoreFailureCase.FAILURE_MESSAGE
        match = re.search(expected_traceback,
                          case.data.traceback,
                          flags=re.DOTALL)

        self.assertIsNotNone(
            match, "Unexpected traceback, %r doesn't match the expression %r" %
            (case.data.traceback, expected_traceback))

        test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)

        self.validate_resource(test_resource)
Exemplo n.º 23
0
    def test_failed_case_run(self):
        """Test a TestCase on run failure.

        * Defines the registered resource as required resource.
        * Runs the test under a test suite.
        * Validates that the test failed.
        * Validates the case's data object.
        * Validates the resource's state.
        """
        TempFailureCase.resources = (request('test_resource',
                                             DemoResource,
                                             name=RESOURCE_NAME), )

        case = self._run_case(TempFailureCase)

        self.assertFalse(self.result.wasSuccessful(),
                         'Case succeeded when it should have failed')

        # === Validate case data object ===
        self.assertFalse(case.data.success,
                         'Case data result should have been False')

        self.assertEqual(
            case.data.exception_type, TestOutcome.FAILED,
            "Unexpected test outcome, expected %r got %r" %
            (TestOutcome.FAILED, case.data.exception_type))

        test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]

        self.assertTrue(isinstance(test_resource, DemoResource),
                        "State resource type should have been 'DemoResource'")

        self.assertEqual(
            test_resource.mode, DemoResourceData.BOOT_MODE,
            "State resource mode attribute should "
            "have been 'boot'")

        test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)

        self.validate_resource(test_resource)
Exemplo n.º 24
0
class TempComplexAdaptiveResourceCase(SuccessCase):
    """Inherit class and override resources requests."""
    __test__ = False

    resources = (request('res1', DemoAdaptiveComplexResource), )
Exemplo n.º 25
0
 class SharedDynamicResourceLockingBlock(DynamicResourceLockingBlock):
     is_global = True
     outputs = (dynamic_request_name,)
     dynamic_resources = (request(dynamic_request_name,
                                  DemoResource,
                                  name=dynamic_resource_name),)
Exemplo n.º 26
0
 class TempFlow(MockFlow):
     resources = (request('fail_resource',
                          InitializeErrorResource,
                          name=fail_resource_name),)
Exemplo n.º 27
0
 class TempFlow(MockFlow):
     resources = (request('no_resource',
                          DemoResource,
                          name=no_resource_name),)
Exemplo n.º 28
0
class MockFlow2(MockFlow):
    """Mock test flow for unit-testing blocks behavior."""
    __test__ = False

    resources = (request('res1', DemoResource, ip_address=IP_ADDRESS1), )
Exemplo n.º 29
0
class TempComplexRequestCase(SuccessCase):
    """Inherit class and override resources requests."""
    __test__ = False

    resources = (request('res1', DemoResource, name='available_resource1'), )
    res2 = DemoResource.request(name='available_resource2')
Exemplo n.º 30
0
class TempDynamicResourceLockingCase(DynamicResourceLockingCase):
    """Inherit class and override resources requests."""
    __test__ = False

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