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): """ 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 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(): """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(): """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_send_receive_with_none_context(): """ Test attempting to start a TCP server using context values, with no context set. Verify expected ValueError is raised. """ with path.TemporaryDirectory() as runpath: client = TCPClient(name='client', host=context('server', '{{host}}'), port=context('server', '{{port}}'), runpath=runpath) with pytest.raises(ValueError): client.start()
def test_send_receive_with_none_context(runpath): """ Test attempting to start a TCP server using context values, with no context set. Verify expected ValueError is raised. """ client = TCPClient( name="client", host=context("server", "{{host}}"), port=context("server", "{{port}}"), runpath=runpath, ) with pytest.raises(ValueError): client.start()
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 tcp_client(tcp_server, runpath): """Start and yield a TCP client driver.""" client = TCPClient(name="client", host=tcp_server.host, port=tcp_server.port, runpath=os.path.join(runpath, "client")) with client: yield client
def tcp_client(tcp_server, runpath_module): """Start and yield a TCP client driver.""" client = TCPClient( name="client", host=tcp_server.host, port=tcp_server.port, runpath=runpath_module, ) with client: yield client
def tcp_client(tcp_server): """Start and yield a TCP client driver.""" with path.TemporaryDirectory() as runpath: client = TCPClient( name="client", host=tcp_server.host, port=tcp_server.port, runpath=runpath, ) with client: yield 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 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 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(): 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 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 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(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 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 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 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): # 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}}"), ), ], ))