def main(plan): # Now we are inside a function that will be passed a plan object, we # can add tests to this plan. Here we will add a PyTest instance that # targets the tests in pytest_basics.py. plan.add( py_test.PyTest( name='PyTest', description='PyTest example - pytest basics', target=['pytest_tests.py'], environment=[ TCPServer(name='server', host='localhost', port=0), TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}')), ], ))
def make_multitest(index=0, files=None): """ Creates a new MultiTest that runs TCP connection tests. This will be created inside a remote worker. """ print('Creating a MultiTest on process id {}.'.format(os.getpid())) test = MultiTest(name='TCPMultiTest_{}'.format(index), suites=[TCPTestsuite(files)], environment=[ TCPServer(name='server'), TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}')) ], after_start=after_start) return test
def test_send_receive_with_context(tcp_server): """ Test starting a TCP client with the host/port information extracted from the server via context values. """ with path.TemporaryDirectory() as runpath: client = TCPClient(name='context_client', host=context('server', '{{host}}'), port=context('server', '{{port}}'), runpath=runpath) tcp_server.context.add(client) with client: assert client.host assert client.port send_receive_message(tcp_server, client)
def add_environment_resource(self, env_uid, target_class_name, source_file=None, **kwargs): """ Add a resource to existing environment or to environment maker object. """ final_kwargs = {} compiled = re.compile(r'_ctx_(.+)_ctx_(.+)') context_params = {} for key, value in kwargs.items(): if key.startswith('_ctx_'): matched = compiled.match(key) if not matched or key.count('_ctx_') != 2: raise ValueError('Invalid key: {}'.format(key)) target_key, ctx_key = matched.groups() if target_key not in context_params: context_params[target_key] = {} context_params[target_key][ctx_key] = value else: final_kwargs[key] = value if context_params: from testplan.common.utils.context import context for key in context_params: final_kwargs[key] = context(**context_params[key]) if source_file is None: # Invoke class loader resource = self._resource_loader.load( target_class_name, final_kwargs) try: self.get_environment(env_uid).add(resource) except: self._created_environments[env_uid].add_resource(resource) else: raise Exception('Add from source file is not yet supported.')
def make_multitest(idx=''): def accept_connection(env): # Server accepts client connection. env.server.accept_connection() return MultiTest(name='Test{}'.format(idx), suites=[BasicSuite(), TCPSuite(0), TCPSuite(1)], environment=[ TCPServer(name='server'), TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}')) ], after_start=accept_connection)
def make_multitest(index=0): """ Creates a new MultiTest that runs TCP connection tests. This will be created inside a thread worker. """ print('Creating a MultiTest on {}.'.format( threading.current_thread().name)) test = MultiTest(name='TCPMultiTest_{}'.format(index), suites=[TCPTestsuite()], environment=[ TCPServer(name='server'), TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}'))], after_start=after_start) return test
def get_multitest(name): test = MultiTest( name=name, suites=[ZMQTestsuite()], environment=[ # The server message pattern is defined as ZMQ PAIR. ZMQServer(name='server', host='127.0.0.1', port=0, message_pattern=zmq.PAIR), # The client message pattern is defined as ZMQ PAIR. ZMQClient(name='client', hosts=[context('server', '{{host}}')], ports=[context('server', '{{port}}')], message_pattern=zmq.PAIR) ]) return test
def test_send_receive_with_context(runpath, tcp_server): """ Test starting a TCP client with the host/port information extracted from the server via context values. """ client = TCPClient( name="context_client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), runpath=runpath, ) tcp_server.context.add(client) with client: assert client.host assert client.port send_receive_message(tcp_server, client)
def main(plan): # Add a test with an environment. plan.add(make_multitest(idx="1")) # Add an independent environment. plan.add_environment( LocalEnvironment( "my_env1", [ TCPServer(name="server"), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], ))
def make_multitest(idx=""): def accept_connection(env): env.server.accept_connection() return MultiTest( name="Test{}".format(idx), suites=[BasicSuite(), TCPSuite()], environment=[ TCPServer(name="server"), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], after_start=accept_connection, )
def main(plan): # Now we are inside a function that will be passed a plan object, we # can add tests to this plan. Here we will add a PyTest instance that # targets the tests in pytest_basics.py. plan.add( py_test.PyTest( name="PyTest", description="PyTest example - pytest basics", target=["pytest_tests.py"], environment=[ TCPServer(name="server", host="localhost", port=0), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], ))
def make_multitest(index=0): """ Creates a new MultiTest that runs TCP connection tests. This will be created inside a process worker. """ test = MultiTest( name="TCPMultiTest_{}".format(index), suites=[TCPTestsuite()], environment=[ TCPServer(name="server"), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], after_start=after_start, ) return test
def get_multitest(name): """ Creates and returns a new MultiTest instance to be added to the plan. The environment is a server and a client connecting using the context functionality that retrieves host/port of the server after is started. """ test = MultiTest( name=name, suites=[TCPTestsuite()], environment=[ TCPServer(name="server"), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], ) return test
def get_multitest(name): """ Creates and returns a new MultiTest instance to be added to the plan. The environment is a server and 2 clients connecting using the context functionality that retrieves host/port of the server after is started. """ test = MultiTest(name=name, suites=[TCPTestsuite()], environment=[ TCPServer(name='server'), TCPClient(name='client1', host=context('server', '{{host}}'), port=context('server', '{{port}}'), connect_at_start=False), TCPClient(name='client2', host=context('server', '{{host}}'), port=context('server', '{{port}}'), connect_at_start=False) ]) return test
def make_multitest(index=0, files=None): """ Creates a new MultiTest that runs TCP connection tests. This will be created inside a remote worker. """ print("Creating a MultiTest on process id {}.".format(os.getpid())) test = MultiTest( name="TCPMultiTest_{}".format(index), suites=[TCPTestsuite(files)], environment=[ TCPServer(name="server"), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], after_start=after_start, ) return test
def make_multitest(idx=""): def accept_connection(env): print("Server accepts connection.") idx = env.server.accept_connection() if idx >= 0: print("Connection accepted from client.") return MultiTest( name="Test{}".format(idx), suites=[TCPSuite()], environment=[ TCPServer(name="server"), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], after_start=accept_connection, )
def converter_environment(): """ MultiTest environment that will be made available within the testcases. """ # Server that will respond with FX exchange rates. server = TCPServer(name="server") # Converter application that accepts configuration template that # after install process it will contain the host/port information of # the 'server' to connect to. # It also reads from the output file the address that the converter # listens for incoming connections and makes host/port info available # through its context so that the client can connect as well. converter_name = "converter" config = "converter.cfg" regexps = [ re.compile(r"Converter started."), re.compile(r".*Listener on: (?P<listen_address>.*)"), ] converter = FXConverter( name=converter_name, pre_args=[sys.executable], binary=os.path.join( os.path.dirname(os.path.abspath(__file__)), "converter.py" ), args=[config], install_files=[ os.path.join(os.path.dirname(os.path.abspath(__file__)), config) ], log_regexps=regexps, ) # Client that connects to the converted application using host/port # information that FXConverter driver made available through its context. client = TCPClient( name="client", host=context(converter_name, "{{host}}"), port=context(converter_name, "{{port}}"), ) return [server, converter, client]
def main(plan): # remote_service represents the RPyC server that runs on remote host remote_service = RemoteService( "rmt_svc", REMOTE_HOST, clean_remote=True, ) # add the remote_service to plan so that it gets started, # and cleaned up after plan finished running as well plan.add_remote_service(remote_service) # args to be passed to TCPServer driver tcp_server_args = dict( name="server", host=REMOTE_HOST, ) # define remote driver instance remote_tcp_server = RemoteDriver( remote_service= remote_service, # via which remote_service shall the driver run driver_cls=TCPServer, # what type of driver **tcp_server_args, # args to driver class ) plan.add( MultiTest( name="Remote TCP Server Test", suites=TCPTestsuite(), description="Running a TCP Server on remote host", environment=[ remote_tcp_server, TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], ))
def get_multitest(name): """ Creates and returns a new MultiTest instance to be added to the plan. The environment is a server and a client connecting using the context functionality that retrieves host/port of the server after is started. """ # The HTTPServer can be passed handler_attributes in a dictionary. These # will be accessible in the custom HTTP request handler # (see custom_http_request_handler.py). attributes = {'text_file': 'test.txt'} test = MultiTest(name=name, suites=[HTTPTestsuite()], environment=[ HTTPServer(name='http_server', request_handler=CustomHTTPRequestHandler, handler_attributes=attributes), HTTPClient(name='http_client', host=context('http_server', '{{host}}'), port=context('http_server', '{{port}}')) ]) return test
def get_multitest(): """ Creates and returns a new MultiTest instance to be added to the plan. The environment is a server and a client connecting using the context functionality that retrieves host/port of the server after is started. """ test = MultiTest(name='OverOneSession', suites=[FIXTestsuite()], environment=[ FixServer(name='server', msgclass=FixMessage, codec=CODEC), FixClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}'), sender='TW', target='ISLD', msgclass=FixMessage, codec=CODEC) ]) return test
def test_send_receive_with_context(): rcxt = Environment() server = TCPServer(name='server', host='localhost', port=0) rcxt.add(server) client = TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}')) rcxt.add(client) assert server.port is None for item in rcxt: item.start() item._wait_started() assert item.port != 0 send_receive_message(server, client) for item in reversed(list(rcxt)): item.stop() item._wait_stopped()
def add_fix_test_one_client(): """ docstring """ test = MultiTest( name="FIX_one_client", suites=[fix_one_client_test()], environment=[ FixServer(name="server", msgclass=FixMessage, codec=codec), FixClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), sender="TW", target="ISLD", msgclass=FixMessage, codec=codec, ), ], ) return test
def main(plan): # Since this function is decorated with `@test_plan`, the first # argument will be a `Testplan` instance, to which we attach out test # targets. Here we will add a PyTest instance which targets the tests # in pytest_basics.py. plan.add( py_test.PyTest( name="PyTest", description="PyTest example - pytest basics", target=[ os.path.join(os.path.dirname(__file__), "pytest_tests.py") ], environment=[ TCPServer(name="server", host="localhost", port=0), TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), ), ], ))
def test_create_client_with_context(): rcxt = Environment() server = ZMQServer(name='server', host='127.0.0.1', port=0, message_pattern=zmq.PAIR) rcxt.add(server) client = ZMQClient(name='client', hosts=[context('server', '{{host}}')], ports=[context('server', '{{port}}')], message_pattern=zmq.PAIR) rcxt.add(client) assert server.port is None for item in rcxt: item.start() item._wait_started() assert server.port != 0 assert client.ports != 0 send_receive_message(sender=client, receiver=server, data=b'Hello World') stop_devices(reversed(list(rcxt)))
def get_multitest(name): test = MultiTest( name=name, suites=[ZMQTestsuite()], environment=[ # The server message pattern is defined as ZMQ PAIR. ZMQServer( name="server", host="127.0.0.1", port=0, message_pattern=zmq.PAIR, ), # The client message pattern is defined as ZMQ PAIR. ZMQClient( name="client", hosts=[context("server", "{{host}}")], ports=[context("server", "{{port}}")], message_pattern=zmq.PAIR, ), ], ) return test
def get_multitest(): """ Creates and returns a new MultiTest instance to be added to the plan. The environment is a server and two clients connecting using the context functionality that retrieves host/port of the server after is started. ------------- client1 | server | ------------- client2 """ test = MultiTest( name="OverTwoSessions", suites=[FIXTestsuite()], environment=[ FixServer(name="server", msgclass=FixMessage, codec=CODEC), FixClient( name="client1", host=context("server", "{{host}}"), port=context("server", "{{port}}"), sender="TW", target="ISLD", msgclass=FixMessage, codec=CODEC, ), FixClient( name="client2", host=context("server", "{{host}}"), port=context("server", "{{port}}"), sender="TW2", target="ISLD", msgclass=FixMessage, codec=CODEC, ), ], ) return test
def test_multitest_drivers_in_testplan(runpath): """TODO.""" for idx, opts in enumerate(( dict(name="MyPlan", parse_cmdline=False, runpath=runpath), dict(name="MyPlan", parse_cmdline=False), )): plan = Testplan(**opts) server = TCPServer(name="server") client = TCPClient( name="client", host=context(server.cfg.name, "{{host}}"), port=context(server.cfg.name, "{{port}}"), ) mtest = MultiTest( name="Mtest", suites=[MySuite()], environment=[server, client], initial_context={"test_key": "test_value"}, ) plan.add(mtest) assert server.status.tag == ResourceStatus.NONE assert client.status.tag == ResourceStatus.NONE with log_propagation_disabled(TESTPLAN_LOGGER): plan.run() res = plan.result assert res.run is True if idx == 0: assert plan.runpath == runpath else: assert plan.runpath == default_runpath(plan._runnable) assert mtest.runpath == os.path.join(plan.runpath, mtest.uid()) assert server.runpath == os.path.join(mtest.runpath, server.uid()) assert client.runpath == os.path.join(mtest.runpath, client.uid()) assert server.status.tag == ResourceStatus.STOPPED assert client.status.tag == ResourceStatus.STOPPED
def test_multitest_drivers(runpath): """TODO.""" for idx, opts in enumerate( ( dict(name="Mtest", suites=[MySuite()], runpath=runpath), dict(name="Mtest", suites=[MySuite()]), ) ): server = TCPServer(name="server") client = TCPClient( name="client", host=context(server.cfg.name, "{{host}}"), port=context(server.cfg.name, "{{port}}"), ) opts.update( environment=[server, client], initial_context={"test_key": "test_value"}, stdout_style=defaults.STDOUT_STYLE, test_filter=Filter(), test_sorter=NoopSorter(), ) mtest = MultiTest(**opts) assert server.status.tag == ResourceStatus.NONE assert client.status.tag == ResourceStatus.NONE res = mtest.run() assert res.run is True assert res.report.passed if idx == 0: assert mtest.runpath == runpath else: assert mtest.runpath == default_runpath(mtest) assert server.runpath == os.path.join(mtest.runpath, server.uid()) assert client.runpath == os.path.join(mtest.runpath, client.uid()) assert server.status.tag == ResourceStatus.STOPPED assert client.status.tag == ResourceStatus.STOPPED
def get_multitest(name): # The environment contains two ZMQServers and two ZMQClients connected as # in the diagrams below. This allows us to send messages from one publish # server to many subscription clients and from many subscription clients to # one publish server as in the examples above. # # +------> Client1 # | # Server1 ------+ # | # +------> Client2 # # Server2 -------------> Client1 test = MultiTest( name=name, suites=[ZMQTestsuite()], environment=[ # Both server's message patterns are defined as ZMQ # PUB. ZMQServer( name="server1", host="127.0.0.1", port=0, message_pattern=zmq.PUB, ), ZMQServer( name="server2", host="127.0.0.1", port=0, message_pattern=zmq.PUB, ), # Both client's message patterns are defined as ZMQ # SUB. ZMQClient( name="client1", hosts=[ context("server1", "{{host}}"), context("server2", "{{host}}"), ], ports=[ context("server1", "{{port}}"), context("server2", "{{port}}"), ], message_pattern=zmq.SUB, ), ZMQClient( name="client2", hosts=[context("server1", "{{host}}")], ports=[context("server1", "{{port}}")], message_pattern=zmq.SUB, ), ], after_start=after_start, ) return test
def test_top_level_environment(): with log_propagation_disabled(TESTPLAN_LOGGER): with InteractivePlan(name='InteractivePlan', interactive=True, interactive_block=False, parse_cmdline=False, logger_level=TEST_INFO) as plan: plan.add_environment( LocalEnvironment('env1', [ TCPServer(name='server'), TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}')) ])) plan.run() wait(lambda: bool(plan.i.http_handler_info), 5, raise_on_timeout=True) assert len(plan.resources.environments.envs) == 1 # Create an environment using serializable arguments. # That is mandatory for HTTP usage. plan.i.create_new_environment('env2') plan.i.add_environment_resource('env2', 'TCPServer', name='server') plan.i.add_environment_resource('env2', 'TCPClient', name='client', _ctx_host_ctx_driver='server', _ctx_host_ctx_value='{{host}}', _ctx_port_ctx_driver='server', _ctx_port_ctx_value='{{port}}') plan.i.add_created_environment('env2') assert len(plan.resources.environments.envs) == 2 for env_uid in ('env1', 'env2'): env = plan.i.get_environment(env_uid) assert isinstance(env, Environment) resources = [res.uid() for res in env] assert resources == ['server', 'client'] for resource in env: assert resource.status.tag is None plan.i.start_environment(env_uid) # START # INSPECT THE CONTEXT WHEN STARTED env_context = plan.i.get_environment_context(env_uid) for resource in [res.uid() for res in env]: res_context = \ plan.i.environment_resource_context(env_uid, resource_uid=resource) assert env_context[resource] == res_context assert isinstance(res_context['host'], six.string_types) assert isinstance(res_context['port'], int) assert res_context['port'] > 0 # CUSTOM RESOURCE OPERATIONS plan.i.environment_resource_operation(env_uid, 'server', 'accept_connection') plan.i.environment_resource_operation(env_uid, 'client', 'send_text', msg='hello') received = plan.i.environment_resource_operation( env_uid, 'server', 'receive_text') assert received == 'hello' plan.i.environment_resource_operation(env_uid, 'server', 'send_text', msg='worlds') received = plan.i.environment_resource_operation( env_uid, 'client', 'receive_text') assert received == 'worlds' for resource in env: assert resource.status.tag is resource.STATUS.STARTED plan.i.stop_environment(env_uid) # STOP for resource in env: assert resource.status.tag is resource.STATUS.STOPPED