def test_get_endpoints_and_messages_combines_endpoints_and_messages_from_all_addons( self): endpoint1 = EndpointId('extension1', '') endpoint2 = EndpointId('extension2', '') msg1 = MessageId('msg1', '') msg2 = MessageId('msg2', '') msg3 = MessageId('msg3', '') @FrameworkExtension('extension1', endpoints_and_messages={endpoint1: [msg1, msg2]}) class Extension1(): pass @FrameworkExtension('extension2', endpoints_and_messages={endpoint2: [msg3]}) class Extension2(): pass self.em.add_extension(Extension1) self.em.add_extension(Extension2) self.em.enable_all_extensions() actual_endpoints_and_messages = self.em.get_endpoints_and_messages() self.assertEqual(actual_endpoints_and_messages[endpoint1], [msg1, msg2]) self.assertEqual(actual_endpoints_and_messages[endpoint2], [msg3])
from enum import Enum from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId from k2.sut import SUT GUDE_IP = ConfigOptionId('gude.ip', 'The gude IP for the SUT', at=SUT, default=None) GUDE_PORT = ConfigOptionId( 'gude.port', 'The gude port for the SUT', at=SUT, default=None, option_type=int) GUDE_POWER_SWITCH_ENDPOINT = EndpointId( 'gudepowerswitch', 'Endpoint for power switch when using a Gude power switch') GUDE_POWER_METER_ENDPOINT = EndpointId( 'gudepowermeter', 'Endpoint for power meter when using a Gude power switch') class GudePowerState(Enum): OFF = 0 ON = 1
import json from threading import Event from zaf.component.decorator import component, requires from zaf.messages.message import EndpointId from gudemock.gudemock import GudeMock from k2.sut import SUT_RECOVERY_PERFORM SYSTEST = EndpointId('systest', 'systest endpoint') @component @requires(zk2='Zk2') class zk2PowerMeter(object): def __init__(self, zk2): self.zk2 = zk2 def __call__(self, server_port, json=False, expected_exit_code=0): return self.zk2(['powermeter', 'gude'], 'powermeter --suts-ids box ' '--suts-box@powermeter gude ' '--suts-box@gude-ip localhost:{port} ' '--suts-box@gude-port 4 ' '{json}'.format(port=server_port, json='--json' if json else ''), expected_exit_code=expected_exit_code) @requires(powermeter=zk2PowerMeter) def test_powermeter_power(powermeter):
from collections import Counter from unittest import TestCase from unittest.mock import MagicMock, Mock from zaf.builtin.unittest.harness import ComponentMock, ExtensionTestHarness from zaf.config.manager import ConfigManager from zaf.messages.message import EndpointId from k2.sut import SUT, SUT_RESET_DONE from monitor import MONITOR_ENDPOINT, PERFORM_MEASUREMENT from ..cpu import CPU_USAGE_MONITOR_ENABLED, SystemCpuMonitorError, SystemCpuTicksCollector, \ SystemCpuUsage, SystemCpuUsageMonitor MOCK_ENDPOINT = EndpointId('mock', 'Mock endpoint') class TestSystemCpuTicksCollector(TestCase): def setUp(self): self.exec = MagicMock() self.collector = SystemCpuTicksCollector(self.exec) def test_raises_cpu_monitor_error_if_exec_raises(self): with self.assertRaisesRegex(SystemCpuMonitorError, 'Could not collect CPU ticks data'): self.exec.send_line = MagicMock( side_effect=Exception('remote peer said no')) self.collector.collect() def test_raises_cpu_monitor_error_on_missing_data(self): with self.assertRaisesRegex(SystemCpuMonitorError,
from zaf.messages.message import EndpointId, MessageId MONITOR_ENDPOINT = EndpointId('monitor', """\ The K2 monitor addon endpoint. """) PERFORM_MEASUREMENT = MessageId( 'PERFORM_MEASUREMENT', """ Request that a monitor performs its measurements. data: None """)
from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId, MessageId SCHEDULER_ENDPOINT = EndpointId( 'scheduler', """\ Handles the scheduling of test cases. The run queue can be modified during the run using a set of messages. """) TESTS_INCLUDE = ConfigOptionId( 'tests.include', 'Include test cases where the start of the qualified name matches this value. ' 'Syntax is "package[.module[[.class].testcase]]."', multiple=True, ) TESTS_INCLUDE_REGEX = ConfigOptionId( 'tests.include.regex', 'Include test cases where the qualified name matches this regex.', multiple=True, ) TESTS_EXCLUDE = ConfigOptionId( 'tests.exclude', 'Exclude test cases where the start of the qualified name matches this value. ' 'Syntax is "package[.module[[.class].testcase]]."', multiple=True, ) TESTS_EXCLUDE_REGEX = ConfigOptionId( 'tests.exclude.regex',
from collections import namedtuple from zaf.messages.message import MessageId, EndpointId DocTemplate = namedtuple('DocTemplate', ['package', 'template_dir', 'filename']) DOCGEN_COMMAND_ENDPOINT = EndpointId('DOCGEN_COMMAND_ENDPOINT', """Endpoint for the docgen command.""") GET_CUSTOM_LOGGING_DOCS = MessageId( 'GET_CUSTOM_LOGGING_DOCS', """ Get path to template file with custom logging documentation that should be injected into the generated documentation. return: list of DocTemplate """) GET_CUSTOM_DOCS = MessageId( 'GET_CUSTOM_DOCS', """ Get list of paths to templates that should be rendered and included in the generated documentation. return: list of DocTemplate """) GET_CUSTOM_DOC_FILES = MessageId( 'GET_CUSTOM_DOC_FILES', """ Get list of files that need to be put in the same directory as the documentation. return: list of DocTemplate
from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId, MessageId INSTANCES = ConfigOptionId('ids', 'description instance', multiple=True, namespace='instances', entity=True) MY_EXTENSION_ENDPOINT = EndpointId( 'MY_EXTENSION_ENDPOINT', """\ This is my extensions endpoint """) MY_EXTENSION_MESSAGE = MessageId( 'MY_EXTENSION_MESSAGE', """ This is my extensions message data: None """)
'blocker.init.enabled', 'Enable blocking on initialization of zaf. ID for blocking is "init".', option_type=bool, default=False, hidden=True, application_contexts=ApplicationContext.EXTENDABLE) BLOCKER_INIT_TIMEOUT = ConfigOptionId( 'blocker.init.timeout', 'Timeout for init blocking.', option_type=float, default=5.0, hidden=True, application_contexts=ApplicationContext.EXTENDABLE) BLOCKER_ENDPOINT = EndpointId('blocker', 'Endpoint for blocker extension') StartBlockingInfo = namedtuple( 'StartBlockingInfo', ['message_id', 'endpoint_id', 'entity', 'timeout']) START_BLOCKING_ON_MESSAGE = MessageId( 'START_BLOCKING_ON_MESSAGE', """ Starts blocking on message described by the message data. Send as request to receive an ID that should be used as entity when sending the STOP_BLOCKING_ON_MESSAGE data: StartBlockingInfo """) STOP_BLOCKING_ON_MESSAGE = MessageId( 'STOP_BLOCKING_ON_MESSAGE', """
from zaf.messages.message import EndpointId, MessageId HEALTH_CHECK_ENDPOINT = EndpointId( 'healthcheck', """\ The K2 health check addon endpoint. """) PERFORM_HEALTH_CHECK = MessageId( 'PERFORM_HEALTH_CHECK', """ Tells subscribers to this message to run their health check data: None """) PERFORM_HEALTH_CHECKS = MessageId( 'PERFORM_HEALTH_CHECK', """ Send to the healthcheck endpoint to trigger running all health checks data: None """) class HealthCheckError(Exception): """Raise from a dispatcher for the PERFORM_HEALTH_CHECK request to indicate check failure.""" pass
from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId, MessageId METRICS_ENDPOINT = EndpointId('metrics', """\ The K2 metrics addon endpoint. """) CREATE_METRIC = MessageId( 'CREATE_METRIC', """ Store a metric. data: A metrics.messages.RegisterMetric instance. """) COLLECT_METRICS_REQUEST = MessageId( 'COLLECT_METRICS_REQUEST', """ Retreive metrics collected so far. data: A metrics.messages.CollectMetrics instance. """) GENERATE_METRICS_AGGREGATE = MessageId( 'GENERATE_METRICS_AGGREGATE', """ Request that metrics aggregates are generated. data: None """) GENERATE_METRICS_REPORT = MessageId( 'GENERATE_METRICS_REPORT', """ Request that metrics reports are generated.
import pickle from zaf.messages.message import EndpointId, MessageId MESSAGE = MessageId('message', '') PICKLED_MESSAGE = pickle.dumps(MESSAGE) PICKLED_MESSAGES = pickle.dumps([MESSAGE]) ENDPOINT = EndpointId('endpoint', '') PICKLED_ENDPOINT = pickle.dumps(ENDPOINT) PICKLED_ENDPOINTS = pickle.dumps([ENDPOINT]) DATA = {'a': 'b', 3: 4, MESSAGE: ENDPOINT} PICKLED_DATA = pickle.dumps(DATA) ENTITY = 'entity' PICKLED_ENTITY = pickle.dumps(ENTITY) PICKLED_ENTITIES = pickle.dumps([ENTITY])
'error', 'Set named logger to log at error level to the file', default=[], at=LOG_FILES, multiple=True, hidden=True) LOG_FILE_TYPE = ConfigOptionId( 'type', 'What type of log file to write. Choose from text or json', option_type=LOG_TYPE_CHOICE, default='text', at=LOG_FILES, hidden=True) LOG_END_POINT = EndpointId('logging', 'The Logging endpoint') ENTER_LOG_SCOPE = MessageId( 'ENTER_LOG_SCOPE', """ Entering log scope data: LogScopeMessageData """) EXIT_LOG_SCOPE = MessageId( 'EXIT_LOG_SCOPE', """ Exiting log scope data: LogScopeMessageData """)
from zaf.messages.message import EndpointId, MessageId RESULTS_ENDPOINT = EndpointId( 'results', """\ Collects test results and triggers TEST_RESULTS_COLLECTED when test run is completed """) TEST_RESULTS_COLLECTED = MessageId( 'TEST_RESULTS_COLLECTED', """\ Message that is triggered when the test run is completed with a complete collection of the test results data: k2.results.results.TestResult """)
'Generate a Z2 report', option_type=bool, default=False) Z2_REPORTS_FILE = ConfigOptionId( 'reports.z2.file', 'Write the report to this path. If no path is given the report will be stored in output_dir', default='${output.dir}/reports/z2/z2-results.json', option_type=Path()) Z2_REPORTS_URL = ConfigOptionId('reports.z2.url', 'Upload the report to this Z2 instance.') Z2_REPORTS_JOB_NAME = ConfigOptionId( 'reports.z2.job.name', 'Name of the job that initiated this K2 run.') Z2_REPORTS_BUILD_NUMBER = ConfigOptionId( 'reports.z2.build.number', 'Build number of the job that initiated this K2 run.', option_type=int) Z2_REPORTS_ENDPOINT = EndpointId('Z2_REPORTER_ENDPOINT', 'The Z2 reporter endpoint') Z2_INTERNAL_PUBLISH_REPORT_REQUEST = MessageId( 'Z2_INTERNAL_PUBLISH_REPORT_REQUEST', """\ Request sent internally to indicate when a report should be published. data: The Z2 report to publish. """)
from zaf.config.options import ConfigOptionId from zaf.config.types import Choice, ConfigChoice, Flag, Path from zaf.messages.message import EndpointId from k2.sut import SUT ANSIBLE_ENDPOINT = EndpointId('ansible', 'Ansible Manager') ANSIBLE_ENABLED = ConfigOptionId( 'ansible.enabled', 'Should Ansible support be enabled', option_type=Flag(), default=False, ) ANSIBLE_BACKEND = ConfigOptionId( 'ansible.backend', 'Backend to test against', option_type=Choice(['docker']), default='docker', ) ANSIBLE_CONFIGS = ConfigOptionId( 'ansible.configs', 'KEY=value pairs of ansible config. The key should be the Ansible environment variable name of the config option.', option_type=str, multiple=True, ) ANSIBLE_CONFIG_FILE = ConfigOptionId( 'ansible.configfile',
import queue from contextlib import contextmanager from unittest.mock import DEFAULT, MagicMock, patch from zaf.component.decorator import component from zaf.component.factory import Factory from zaf.component.manager import ComponentManager, create_registry from zaf.component.scope import Scope from zaf.config.manager import ConfigManager, ConfigView from zaf.extensions.manager import _is_extension_active from zaf.messages.decorator import get_dispatcher_descriptors from zaf.messages.dispatchers import LocalMessageQueue from zaf.messages.message import EndpointId from zaf.messages.messagebus import MessageBus HARNESS_ENDPOINT = EndpointId('harness', '') class ComponentMock(object): def __init__(self, name, mock, scope=None, can=None, provided_by_extension=None): self.name = name self.mock = mock self.scope = scope self.can = can self.provided_by_extension = provided_by_extension
option_type=int, default=18861, hidden=True, application_contexts=ApplicationContext.EXTENDABLE) REMOTE_CLIENT_ENABLED = ConfigOptionId( 'remoteclient.enabled', 'Enabled remote messagebus client', option_type=bool, default=False, hidden=True, application_contexts=ApplicationContext.EXTENDABLE) REMOTE_CLIENT_PORT = ConfigOptionId( 'remoteclient.port', 'The port to use to connect to the remote interface', option_type=int, default=18861, hidden=True, application_contexts=ApplicationContext.EXTENDABLE) REMOTE_CLIENT_HOST = ConfigOptionId( 'remoteclient.host', 'The host to use to connect to the remote interface', default='localhost', hidden=True, application_contexts=ApplicationContext.EXTENDABLE) REMOTE_ENDPOINT = EndpointId('REMOTE_ENDPOINT', 'Endpoint for remote messagebus interface')
ZNAIL_IP = ConfigOptionId('znail.ip', 'IP number to the Znail device', at=SUT) ZNAIL_PORT = ConfigOptionId('znail.port', 'Port number to connect to', option_type=int, default=80, at=SUT) ZNAIL_TIMEOUT = ConfigOptionId( 'znail.timeout', 'Timeout when communicating with the Znail device, in seconds', option_type=int, default=10, at=SUT) ZNAIL_CONNECTION_CHECK_ENDPOINT = EndpointId( 'znailcc', 'Endpoint for the Znail connection check') ZNAIL_CONNECTION_CHECK_ENABLED = ConfigOptionId( 'znailcc.enabled', 'Should Znail connection check be enabled', at=SUT, option_type=bool, default=True) ZNAIL_CONNECTION_CHECK_REQUIRED = ConfigOptionId( 'znailcc.required', 'Require Znail connection to be working', at=SUT, option_type=bool, default=True)
from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId, MessageId EXTENSION_NAME = 'testrunner' RUNNER_ENDPOINT = EndpointId('runner', """\ The K2 default test runner """) TEST_RUN_STARTED = MessageId( 'TEST_RUN_STARTED', """\ Message that is triggered when the test runner has started. data: k2.runner.messages.TestRunStarted """) TEST_RUN_FINISHED = MessageId( 'TEST_RUN_FINISHED', """\ Message that is triggered when the test runner has finished. data: k2.runner.messages.TestRunFinished """) TEST_CASE_STARTED = MessageId( 'TEST_CASE_STARTED', """\ Message that is triggered when the test case is started. data: k2.runner.messages.TestCaseStarted """) TEST_CASE_FINISHED = MessageId(
from zaf.builtin.blocker import BLOCKER_ENDPOINT, BLOCKING_COMPLETED, BLOCKING_STARTED, \ START_BLOCKING_ON_MESSAGE, STOP_BLOCKING_ON_MESSAGE, StartBlockingInfo from zaf.commands.command import CommandId from zaf.component.decorator import requires from zaf.extensions.extension import AbstractExtension, FrameworkExtension from zaf.messages.decorator import concurrent_dispatcher from zaf.messages.dispatchers import LocalMessageQueue from zaf.messages.message import EndpointId, MessageId BLOCK_MESSAGE = MessageId('BLOCK_MESSAGE', '') START = MessageId('START', '') CONTINUE = MessageId('CONTINUE', '') ENDPOINT = EndpointId('ENDPOINT', '') def remote_and_blocker_command(core): core.messagebus.send_request(START, receiver_endpoint_id=ENDPOINT) core.component_factory.call(_remote_and_blocker_command, core.session_scope) @requires(client='RemoteClient') def _remote_and_blocker_command(client): # Request that waits for CONTINUE to have been received by the test case # which indicates that test case message and endpoint have been defined client.send_request(CONTINUE, ENDPOINT).wait(timeout=1)[0].result(timeout=1) # Register the blocking and receive the blocking ID so that it can be used in the # local message queue registration blocking_id = client.send_request(
BEFORE_COMMAND = MessageId( 'BEFORE_COMMAND', """ Event triggered before the command has been started data: command name """) AFTER_COMMAND = MessageId( 'AFTER_COMMAND', """ Event triggered after the command has been finished data: command name """) APPLICATION_ENDPOINT = EndpointId( 'application', """ The Zaf application endpoint """) MESSAGEBUS_TIMEOUT = ConfigOptionId( 'application.messagebus.timeout', 'Specifies how long application should wait for the messagebus to finish on shutdown, in seconds', option_type=float, default=3, hidden=True, application_contexts=[ApplicationContext.EXTENDABLE]) APPLICATION_NAME = ConfigOptionId( 'application.name', 'Name of the application', application_contexts=ApplicationContext.INTERNAL) APPLICATION_ROOT = ConfigOptionId(
from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId, MessageId TEST_SOURCES = ConfigOptionId( 'test.sources', 'One or more test sources. Can be files, modules, classes etc', multiple=True, argument=True) FINDER_ENDPOINT = EndpointId('testfinder', """\ The test finder """) FIND_TEST_CASES = MessageId( 'FIND_TEST_CASES', """\ Request to find test cases. returns: List of TestCase """)
from zaf.extensions.extension import FrameworkExtension, get_logger_name from zaf.messages.message import EndpointId, MessageId from k2.sut import SUT logger = logging.getLogger(get_logger_name('k2', 'runcommand')) logger.addHandler(logging.NullHandler()) EXIT_CODE_FROM_VERDICT = ConfigOptionId( 'exitcode.from.verdict', 'Give the verdict of the test run as exit code', option_type=bool, default=False) RUN_COMMAND_ENDPOINT = EndpointId('runcommand', """ The K2 run command """) PRE_INITIALIZE_SUT = MessageId( 'PRE_INITIALIZE_SUT', """ Triggered before the initialize the SUT. This message is sent once for each sut entity. data: None """) INITIALIZE_SUT = MessageId( 'INITIALIZE_SUT', """ Triggered when the run command is ready to initialize the SUT. Initializing the SUT should done be as a callback. This message is sent once for each sut entity.
from zaf.builtin.unittest.harness import ExtensionTestHarness from zaf.config.manager import ConfigManager from zaf.messages.dispatchers import CallbackDispatcher from zaf.messages.message import EndpointId from k2.cmd.run import POST_INITIALIZE_SUT, RUN_COMMAND_ENDPOINT from k2.sut import SUT, SUT_RECOVERY_PERFORM from .. import CONNECTIONCHECK_ENABLED, CONNECTIONCHECK_RUN_CHECK, CONNECTIONCHECK_SHOULD_RECOVER from ..connectioncheck import ConnectionCheck, ConnectionCheckResult MOCK_CONNECTION_CHECK_ENDPOINT = EndpointId('mockcc', 'Mock connection check') MOCK_SUT_RECOVER = EndpointId('mocksr', 'Mock sut recover') def create_harness(enabled=True, should_recover=True, sut=['entity']): config = ConfigManager() entity = sut[0] config.set(SUT, sut) config.set(CONNECTIONCHECK_ENABLED, enabled, entity=entity) config.set(CONNECTIONCHECK_SHOULD_RECOVER, should_recover, entity=entity) return ExtensionTestHarness(ConnectionCheck, endpoints_and_messages={ RUN_COMMAND_ENDPOINT: [POST_INITIALIZE_SUT], MOCK_CONNECTION_CHECK_ENDPOINT: [CONNECTIONCHECK_RUN_CHECK], MOCK_SUT_RECOVER: [SUT_RECOVERY_PERFORM], }, config=config)
from k2.sut import SUT K2_POWER_COMPONENT = 'PowerSwitch' POWER_SWITCH_TIMEOUT = 15 AVAILABLE_POWER_SWITCHES = ConfigOptionId( 'powerswitch.available', 'A collection of all availabe powerswitch options', multiple=True) POWER_SWITCH = ConfigOptionId( 'powerswitch', 'The type of the power switch', at=SUT, option_type=ConfigChoice(AVAILABLE_POWER_SWITCHES)) POWER_SWITCH_CONNECTION_CHECK_ENDPOINT = EndpointId( 'powerswitchcc', 'Endpoint for power switch connection check') POWER_SWITCH_CONNECTION_CHECK_ENABLED = ConfigOptionId( 'powerswitchcc.enabled', 'Should powerswitch connection check be enabled', at=SUT, option_type=bool, default=True) POWER_SWITCH_CONNECTION_CHECK_REQUIRED = ConfigOptionId( 'powerswitchcc.required', 'Require powerswitch connection to be working', at=SUT, option_type=bool, default=True)
from k2.sut import SUT K2_POWER_METER_COMPONENT = 'PowerMeter' POWER_METER_TIMEOUT = 15 AVAILABLE_POWER_METERS = ConfigOptionId( 'powermeter.available', 'A collection of all availabe power meter options', multiple=True) POWER_METER = ConfigOptionId('powermeter', 'The type of the power meter', at=SUT, option_type=ConfigChoice(AVAILABLE_POWER_METERS)) POWER_METER_CONNECTION_CHECK_ENDPOINT = EndpointId( 'powermetercc', 'Endpoint for power meter connection check') POWER_METER_CONNECTION_CHECK_ENABLED = ConfigOptionId( 'powermetercc.enabled', 'Should powermeter connection check be enabled', at=SUT, option_type=bool, default=True) POWER_METER_CONNECTION_CHECK_REQUIRED = ConfigOptionId( 'powermetercc.required', 'Require powermeter connection to be working', at=SUT, option_type=bool, default=True)
from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId, MessageId from k2.sut import SUT CONNECTIONCHECK_ENDPOINT = EndpointId( 'connectioncheck', 'Endpoint for the connection check messages') CONNECTIONCHECK_RUN_CHECK = MessageId( 'CONNECTIONCHECK_RUN_CHECK', """\ Tells subscribers to the message to run their checks and return the result as a ConnectionCheckResult. """) CONNECTIONCHECK_RUN_CHECKS = MessageId( 'CONNECTIONCHECK_RUN_CHECKS', """\ Send to connectioncheck to trigger all connection checks. Raises exception if any of the required checks fail. """) CONNECTIONCHECK_ENABLED = ConfigOptionId('connectioncheck.enabled', 'Enable the connection check', option_type=bool, default=True, at=SUT) CONNECTIONCHECK_SHOULD_RECOVER = ConfigOptionId( 'connectioncheck.should.recover', 'Attempt to recover the SUT on failed connection check', option_type=bool, default=True,
from zaf.config.options import ConfigOptionId from zaf.config.types import Flag from zaf.messages.message import EndpointId ABORT_ON_FAIL_ENABLED = ConfigOptionId('abort.on.fail', 'Enable abort on fail addon', option_type=Flag(), default=False) ABORT_ON_FAIL_ENDPOINT = EndpointId('abortonfailaddon', """ Abort on fail """) ABORT_ON_UNEXPECTED_SUT_RESET_ENDPOINT = EndpointId( 'abortonunexpectedsutreset', """ Abort on unexpected sut reset """) ABORT_ON_UNEXPECTED_SUT_RESET = ConfigOptionId( 'abort.on.unexpected.sut.reset', 'Abort a test run when an unexpected sut reset occurs.', option_type=Flag(), default=False)
from zaf.config.options import ConfigOptionId from zaf.messages.message import EndpointId EXCLUDE_IDS = ConfigOptionId('exclude.ids', '', multiple=True) INCLUDE_IDS = ConfigOptionId('include.ids', '', multiple=True) ASCIIDOCTOR_IDS_ENDPOINT = EndpointId('ASCIIDOCTOR_IDS_ENDPOINT', 'Endpoint that handles ids')