Exemple #1
0
    def test(self,
             ctxtype,
             config=None,
             assert_on_fail=True,
             username=None,
             password=None,
             always_collect_logs=False):
        """Test the topology.

        Submits the topology for testing and verifies the test conditions are met and the job remained healthy through its execution.

        The submitted application (job) is monitored for the test conditions and
        will be canceled when all the conditions are valid or at least one failed.
        In addition if a local check was specified using :py:meth:`local_check` then
        that callable must complete before the job is cancelled.

        The test passes if all conditions became valid and the local check callable (if present) completed without
        raising an error.

        The test fails if the job is unhealthy, any condition fails or the local check callable (if present) raised an exception.
        In the event that the test fails when submitting to the `STREAMING_ANALYTICS_SERVICE` context, the application logs are retrieved as
        a tar file and are saved to the current working directory. The filesystem path to the application logs is saved in the
        tester's result object under the `application_logs` key, i.e. `tester.result['application_logs']`

        Args:
            ctxtype(str): Context type for submission.
            config: Configuration for submission.
            assert_on_fail(bool): True to raise an assertion if the test fails, False to return the passed status.
            username(str): **Deprecated** 
            password(str): **Deprecated**
            always_collect_logs(bool): True to always collect the console log and PE trace files of the test.

        Attributes:
            result: The result of the test. This can contain exit codes, application log paths, or other relevant test information.
            submission_result: Result of the application submission from :py:func:`~streamsx.topology.context.submit`.
            streams_connection(StreamsConnection): Connection object that can be used to interact with the REST API of
                the Streaming Analytics service or instance.

        Returns:
            bool: `True` if test passed, `False` if test failed if `assert_on_fail` is `False`.

        .. deprecated:: 1.8.3
            ``username`` and ``password`` parameters. When required for
             a distributed test use the environment variables
             ``STREAMS_USERNAME`` and ``STREAMS_PASSWORD`` to define
             the Streams user.
        """
        if username or password:
            warnings.warn(
                "Set username and password with environment variables",
                DeprecationWarning,
                stacklevel=2)
        if config is None:
            config = {}
        config['topology.alwaysCollectLogs'] = always_collect_logs
        config[
            'originator'] = 'tester-' + __version__ + ':python-' + platform.python_version(
            )

        # Look for streamsx.testing plugins
        # Each action that plugin attached to the test is
        # called passing Tester, TestCase, context type and config
        if isinstance(config, _TestConfig):
            test_ = config._test
            actions = test_._streamsx_testing_actions if hasattr(
                test_, '_streamsx_testing_actions') else None
            if actions:
                for action in actions:
                    _logger.debug(
                        "Adding nose plugin action %s to topology %s.",
                        str(action), self.topology.name)
                    action(self, test_, ctxtype, config)

        if stc.ContextTypes.DISTRIBUTED == ctxtype:
            Tester._check_setup_distributed(config)

        # Add the conditions into the graph as sink operators
        _logger.debug("Adding conditions to topology %s.", self.topology.name)
        for ct in self._conditions.values():
            condition = ct[1]
            stream = ct[0]
            condition._attach(stream)

        # Standalone uses --kill-after parameter.
        if self._run_for and stc.ContextTypes.STANDALONE != ctxtype:
            rfn = 'run_for_' + str(int(self._run_for)) + 's'
            run_cond = sttrt._RunFor(self._run_for, rfn)
            self.add_condition(None, run_cond)
            cond_run_time = self.topology.source(run_cond, name=rfn)
            cond_run_time.category = 'Tester'
            cond_run_time._op()._layout(hidden=True)

        _logger.debug("Starting test topology %s context %s.",
                      self.topology.name, ctxtype)

        if stc.ContextTypes.STANDALONE == ctxtype:
            passed = self._standalone_test(config)
        elif stc.ContextTypes.DISTRIBUTED == ctxtype:
            passed = self._distributed_test(config, username, password)
        elif stc.ContextTypes.STREAMING_ANALYTICS_SERVICE == ctxtype or stc.ContextTypes.ANALYTICS_SERVICE == ctxtype:
            passed = self._streaming_analytics_test(ctxtype, config)
        else:
            raise NotImplementedError("Tester context type not implemented:",
                                      ctxtype)

        if hasattr(self, 'result') and self.result.get('conditions'):
            for cn, cnr in self.result['conditions'].items():
                c = self._conditions[cn][1]
                cdesc = cn
                if hasattr(c, '_desc'):
                    cdesc = c._desc

                if 'Fail' == cnr:
                    _logger.error("Condition: %s : %s", cnr, cdesc)
                elif 'NotValid' == cnr:
                    _logger.warning("Condition: %s : %s", cnr, cdesc)
                elif 'Valid' == cnr:
                    _logger.info("Condition: %s : %s", cnr, cdesc)

        if assert_on_fail:
            assert passed, "Test failed for topology: " + self.topology.name
        if passed:
            _logger.info("Test topology %s passed for context:%s",
                         self.topology.name, ctxtype)
        else:
            _logger.error("Test topology %s failed for context:%s",
                          self.topology.name, ctxtype)

        return passed
Exemple #2
0
    def test(self, ctxtype, config=None, assert_on_fail=True, username=None, password=None, always_collect_logs=False):
        """Test the topology.

        Submits the topology for testing and verifies the test conditions are met and the job remained healthy through its execution.

        The submitted application (job) is monitored for the test conditions and
        will be canceled when all the conditions are valid or at least one failed.
        In addition if a local check was specified using :py:meth:`local_check` then
        that callable must complete before the job is cancelled.

        The test passes if all conditions became valid and the local check callable (if present) completed without
        raising an error.

        The test fails if the job is unhealthy, any condition fails or the local check callable (if present) raised an exception.
        In the event that the test fails when submitting to the `STREAMING_ANALYTICS_SERVICE` context, the application logs are retrieved as
        a tar file and are saved to the current working directory. The filesystem path to the application logs is saved in the
        tester's result object under the `application_logs` key, i.e. `tester.result['application_logs']`

        Args:
            ctxtype(str): Context type for submission.
            config: Configuration for submission.
            assert_on_fail(bool): True to raise an assertion if the test fails, False to return the passed status.
            username(str): **Deprecated** 
            password(str): **Deprecated**
            always_collect_logs(bool): True to always collect the console log and PE trace files of the test.

        Attributes:
            result: The result of the test. This can contain exit codes, application log paths, or other relevant test information.
            submission_result: Result of the application submission from :py:func:`~streamsx.topology.context.submit`.
            streams_connection(StreamsConnection): Connection object that can be used to interact with the REST API of
                the Streaming Analytics service or instance.

        Returns:
            bool: `True` if test passed, `False` if test failed if `assert_on_fail` is `False`.

        .. deprecated:: 1.8.3
            ``username`` and ``password`` parameters. When required for
             a distributed test use the environment variables
             ``STREAMS_USERNAME`` and ``STREAMS_PASSWORD`` to define
             the Streams user.
        """

        # Add the conditions into the graph as sink operators
        _logger.debug("Adding conditions to topology %s.", self.topology.name)
        for ct in self._conditions.values():
            condition = ct[1]
            stream = ct[0]
            cond_sink = stream.for_each(condition, name=condition.name)
            cond_sink.colocate(stream)
            cond_sink.category = 'Tester'
            cond_sink._op()._layout(hidden=True)

        # Standalone uses --kill-after parameter.
        if self._run_for and stc.ContextTypes.STANDALONE != ctxtype:
            run_cond = sttrt._RunFor(self._run_for)
            self.add_condition(None, run_cond)
            cond_run_time = self.topology.source(run_cond, name="TestRunTime")
            cond_run_time.category = 'Tester'
            cond_run_time._op()._layout(hidden=True)

        if config is None:
            config = {}
        config['topology.alwaysCollectLogs'] = always_collect_logs

        _logger.debug("Starting test topology %s context %s.", self.topology.name, ctxtype)

        if stc.ContextTypes.STANDALONE == ctxtype:
            passed = self._standalone_test(config)
        elif stc.ContextTypes.DISTRIBUTED == ctxtype:
            passed = self._distributed_test(config, username, password)
        elif stc.ContextTypes.STREAMING_ANALYTICS_SERVICE == ctxtype or stc.ContextTypes.ANALYTICS_SERVICE == ctxtype:
            passed = self._streaming_analytics_test(ctxtype, config)
        else:
            raise NotImplementedError("Tester context type not implemented:", ctxtype)

        if self.result.get('conditions'):
            for cn,cnr in self.result['conditions'].items():
                c = self._conditions[cn][1]
                cdesc = cn
                if hasattr(c, '_desc'):
                    cdesc = c._desc

                if 'Fail' == cnr:
                    _logger.error("Condition: %s : %s", cnr, cdesc)
                elif 'NotValid' == cnr:
                    _logger.warning("Condition: %s : %s", cnr, cdesc)
                elif 'Valid' == cnr:
                    _logger.info("Condition: %s : %s", cnr, cdesc)
        
        if assert_on_fail:
            assert passed, "Test failed for topology: " + self.topology.name
        if passed:
            _logger.info("Test topology %s passed for context:%s", self.topology.name, ctxtype)
        else:
            _logger.error("Test topology %s failed for context:%s", self.topology.name, ctxtype)
            
        return passed