如你所见,本地源码呗挂载/编写在vagrant虚拟机中。在这也有网络定义。一个作为集成测试的物理网络用来配置VLANs(注意:auto_confi => false option)和其他用来测试代码通信。
当开发者运行一段测试时发生了什么?
实际上是在网关虚拟机上运行了测试。使用了本地挂载代码来创建应用对象,调用对象,然后使用 fabric在测试机器上远程运行网络工具来ping/sniff/trace/accept 所有通过和返回给网关的流量的种类。
下面来看个简单的例子,简化了很多的:
class TestVlansBase(unittest.TestCase): def setUp(self): self._initialize_tester_machines() def tearDown(self): for tester_name, vlan in self.dct_interfaces_to_remove.iteritems(): self._remove_interface_from_host(tester_name, vlan) class TestVlans(TestVlansBase): def _test_connection( self, server_name, server_vlan, server_ip, server_port, protocol, client_name, client_dst_ip=None, client_dst_port=None): self.assertTrue(protocol in ('tcp', 'udp'), 'protocol should be tcp or udp') client_dst_ip = client_dst_ip or server_ip client_dst_port = client_dst_port or server_port if protocol == 'tcp': server = self._create_server(server_name, server_ip, server_port) elif protocol == 'udp': filter_exp = '{0} port {1}'.format(protocol, server_port) server = self._create_sniffer_on_host(server_name, server_vlan, filter_exp, 1) client = self._connect_to_host(client_name, client_dst_ip, client_dst_port, protocol) client.runner.join() if server.runner.exitcode is None: # this means that the process did not exit hence no packets were seen server.runner.terminate() server.runner.join() self.assertEqual(client.runner.exitcode, 0) self.assertEqual(server.runner.exitcode, 0) def test_reroute_http_traffic(self): self._configure_testers() self.gateway.configure() self._test_connection( 'tester3', 93, '10.180.0.3', 88, 'tcp', 'tester2', client_dst_ip='10.10.10.10', client_dst_port=80) self._test_connection( 'tester3', 93, '10.180.0.3', 88, 'tcp', 'tester2', client_dst_ip='10.10.10.10', client_dst_port=44444) self._test_connection( 'tester3', 93, '10.180.0.3', 88, 'tcp', 'tester2', client_dst_ip='10.10.10.10', client_dst_port=88)所有的从网关(测试运行的地方)到测试者机器的远程调用使用的是fabric。
一个可以在测试上运行的简单命令:
class FabricProcessProxy(object): __metaclass__ = ABCMeta def __init__(self, *args, **kwargs): self.kwargs = kwargs self.args = args self.out_q = multiprocessing.Queue() # self.runner = multiprocessing.Process(target=lambda: execute(self.run, *self.args, **self.kwargs)) self.runner = multiprocessing.Process( target=lambda: self.out_q.put(execute(self.run, *self.args, **self.kwargs))) def execute(self, hosts): self.kwargs['hosts'] = hosts self.runner.start() return self.runner @abstractmethod def run(self): raise NotImplementedError() class Ping(FabricProcessProxy): def run(self, target, iface, count): str_iface = '-I {0} '.format(iface) if iface else ' ' return run('ping -c {count}{iface}-W 1 {target}'.format(count=count, iface=str_iface, target=target)) def _ping_from_host(self, host, dst_ip, through_iface=None, num_pings=1, b_verify_success=True): ping = Ping(dst_ip, through_iface, num_pings) ping.execute(['vagrant@{0}'.format(host)]).join() if b_verify_success: self.assertEqual(ping.runner.exitcode, 0) return ping.runner.exitcode _ping_from_host('tester2', '10.180.0.3')既然这个基础结构已经建好了,我们就不在回头看它了。我们今天所拥有的网关是一等公民,而且只要通过了测试,我们就不怕重构它,添加新的功能和做出其他改变。