def test_multitest_drivers_in_testplan(): """TODO.""" for idx, opts in enumerate((dict(name='MyPlan', parse_cmdline=False, runpath=runpath_maker), 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_maker(None) 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(): """TODO.""" for idx, opts in enumerate( (dict(name='Mtest', suites=[MySuite()], runpath=runpath_maker), 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'}) mtest = MultiTest(**opts) assert server.status.tag == ResourceStatus.NONE assert client.status.tag == ResourceStatus.NONE mtest.run() res = mtest.result assert res.run is True if idx == 0: assert mtest.runpath == runpath_maker(None) 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 test_multitest_drivers_in_testplan(runpath): """TODO.""" for idx, opts in enumerate((dict(name="MyPlan", runpath=runpath), dict(name="MyPlan"))): plan = TestplanMock(**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 plan.run() res = plan.result assert res.run is True assert res.report.passed if idx == 0: assert plan.runpath == runpath assert mtest.runpath == os.path.join(plan.runpath, slugify(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 test_send_receive_with_none_context(): server = TCPServer(name='server', host='localhost', port=0) client = TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}')) assert server.port is None server.start() server._wait_started() assert server.port != 0 should_raise(ValueError, client.start) server.stop() server._wait_stopped()
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=[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 test_send_receive_no_context(): server = TCPServer(name='server', host='localhost', port=0) assert server.port is None server.start() server._wait_started() assert server.port != 0 client = TCPClient(name='client', host=server._host, port=server._port) client.start() client._wait_started() send_receive_message(server, client) client.stop() client._wait_stopped() server.stop() server._wait_stopped()
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 tcp_server(runpath_module): """Start and yield a TCP server driver.""" env = Environment() server = TCPServer( name="server", host="localhost", port=0, runpath=runpath_module ) env.add(server) with server: yield server
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 tcp_server(runpath): """Start and yield a TCP server driver.""" env = Environment() server = TCPServer( name="server", host="localhost", port=0, runpath=os.path.join(runpath, "server"), ) env.add(server) with server: yield server
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 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 test_basic_runpath(): """Test runpath of TCP client and server.""" with path.TemporaryDirectory() as svr_path: # Server runpath server = TCPServer(name='server', runpath=svr_path) assert_obj_runpath(server, svr_path) with path.TemporaryDirectory() as cli_path: # Client runpath client = TCPClient(name='client', runpath=cli_path, host=server._host, port=server._port) assert_obj_runpath(client, cli_path)
def test_basic_runpath(): svr_path = make_runpath("srv_runpath") cli_path = make_runpath("cli_runpath") # Server runpath server = TCPServer(name='server', runpath=svr_path) assert_obj_runpath(server, svr_path) # Client runpath client = TCPClient(name='client', runpath=cli_path, host=server._host, port=server._port) assert_obj_runpath(client, cli_path) shutil.rmtree(svr_path, ignore_errors=True) shutil.rmtree(cli_path, ignore_errors=True)
def tcp_server(): """Start and yield a TCP server driver.""" with path.TemporaryDirectory() as runpath: env = Environment() server = TCPServer( name="server", host="localhost", port=0, runpath=runpath, ) env.add(server) with server: yield server
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 make_multitest(index=0): """ 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()], environment=[ TCPServer(name='server'), TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}')) ], after_start=after_start) return test
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 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 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 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_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 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
def test_top_level_environment(): with InteractivePlan( name="InteractivePlan", interactive_port=0, 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"], str) 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