def test_shutdown_ip(): class MockBcfProxy(BcfProxy): def __init__(self): self.endpoints = None def get_endpoints(self): return self.endpoints def shutdown_endpoint(self, tenant, segment, name, mac, shutdown): pass bcf = MockBcfProxy() filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant': 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment': 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint': 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric': 'sample_span_fabric.json', # %22 = url-encoded double quotes '/data/controller/applications/bcf/span-fabric[name=%22vent%22]': 'sample_span_fabric.json', } proxy = None endpoints = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', { 'username': username, 'password': password }) endpoints = proxy.get_endpoints() bcf.endpoints = endpoints ret_val = bcf.shutdown_ip('10.0.0.1') answer = list([{ 'ip-address': '10.0.0.1', 'ip-state': 'learned', 'mac': '00:00:00:00:00:01', 'segment': 'poseidon', 'tenant': 'poseidon', 'name': None }]) assert str(answer) == str(ret_val) ret_val = bcf.shutdown_ip('10.0.0.1', mac_addr='00:00:00:00:00:01') answer = list([{ 'mac': '00:00:00:00:00:01', 'name': None, 'tenant': 'poseidon', 'segment': 'poseidon' }]) assert str(answer) == str(ret_val)
def first_run(self): ''' do some pre-run setup/configuration ''' if self.configured: self.controller['TYPE'] = str( self.mod_configuration['controller_type']) if self.controller['TYPE'] == 'bcf': self.controller['URI'] = str( self.mod_configuration['controller_uri']) self.controller['USER'] = str( self.mod_configuration['controller_user']) self.controller['PASS'] = str( self.mod_configuration['controller_pass']) myauth = {} myauth['password'] = self.controller['PASS'] myauth['user'] = self.controller['USER'] try: self.sdnc = BcfProxy(self.controller['URI'], auth=myauth) except BaseException: self.logger.error( 'BcfProxy could not connect to {0}'.format( self.controller['URI'])) elif self.controller['TYPE'] == 'faucet': try: if 'controller_uri' in self.mod_configuration: self.controller['URI'] = str( self.mod_configuration['controller_uri']) if 'controller_user' in self.mod_configuration: self.controller['USER'] = str( self.mod_configuration['controller_user']) if 'controller_pass' in self.mod_configuration: self.controller['PASS'] = str( self.mod_configuration['controller_pass']) if 'controller_config_file' in self.mod_configuration: self.controller['CONFIG_FILE'] = str( self.mod_configuration['controller_config_file']) if 'controller_log_file' in self.mod_configuration: self.controller['LOG_FILE'] = str( self.mod_configuration['controller_log_file']) if 'controller_mirror_ports' in self.mod_configuration: self.controller['MIRROR_PORTS'] = ast.literal_eval( self.mod_configuration['controller_mirror_ports']) self.sdnc = FaucetProxy( host=self.controller['URI'], user=self.controller['USER'], pw=self.controller['PASS'], config_file=self.controller['CONFIG_FILE'], log_file=self.controller['LOG_FILE'], mirror_ports=self.controller['MIRROR_PORTS']) except BaseException as e: # pragma: no cover self.logger.error( 'FaucetProxy could not connect to {0} because {1}'. format(self.controller['URI'], e)) else: self.logger.error('Unknown SDN controller type {0}'.format( self.controller['TYPE'])) else: pass
def test_BcfProxy(): """ Tests bcf """ filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant' : 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment' : 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint' : 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric' : 'sample_span_fabric.json', '/data/controller/applications/bcf/span-fabric[name=%22vent%22]': 'sample_span_fabric.json', # %22 = url-encoded double quotes } proxy = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', {'username': username, 'password': password}) endpoints = proxy.get_endpoints() assert endpoints switches = proxy.get_switches() assert switches tenants = proxy.get_tenants() assert tenants segments = proxy.get_segments() assert segments span_fabric = proxy.get_span_fabric() assert span_fabric span_fabric = proxy.get_span_fabric(span_name="vent") assert span_fabric with HTTMock(mock_factory2(r'.*')): # Normally shutdown_endpoint does not return a value. # You should call get_endpoint() afterwards to verify that a shutdown request went through. # In addition, the mock endpoint generated does not check for duplicates. # TODO: ***This code below is temporary.*** r = proxy.shutdown_endpoint(tenant="TENANT", segment="SEGMENT", endpoint_name="test", mac="00:00:00:00:00:00", shutdown=True) assert r r = proxy.shutdown_endpoint(tenant="TENANT", segment="SEGMENT", endpoint_name="test", mac="00:00:00:00:00:00", shutdown=False) assert r r = proxy.mirror_traffic(seq=2, mirror=True, tenant="TENANT", segment="SEGMENT") assert r r = proxy.mirror_traffic(seq=2, mirror=False) assert r r = lambda: None r.text = "" BcfProxy.parse_json(r) proxy.session.cookies.clear_session_cookies() proxy.base_uri = "http://jsonplaceholder.typicode.com" r = proxy.post_resource('posts') r.raise_for_status() r = proxy.request_resource(method="PUT", url="http://jsonplaceholder.typicode.com/posts/1") r.raise_for_status()
def test_unmirror_ip(): class MockBcfProxy(BcfProxy): def __init__(self): self.endpoints = None self.span_fabric = None def get_endpoints(self): return self.endpoints def mirror_traffic(self, seq, mirror=True, span_name='vent', s_dict=None, fabric_span_endpoint='', **target_kwargs): pass def get_span_fabric(self): return self.span_fabric def shutdown_endpoint(self, tenant, segment, name, mac, shutdown): pass bcf = MockBcfProxy() filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant': 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment': 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint': 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric': 'sample_span_fabric.json', # %22 = url-encoded double quotes '/data/controller/applications/bcf/span-fabric[name=%22vent%22]': 'sample_span_fabric.json', } proxy = None endpoints = None span_fabric = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', { 'username': username, 'password': password }) endpoints = proxy.get_endpoints() span_fabric = proxy.get_span_fabric() bcf.endpoints = endpoints bcf.span_fabric = span_fabric ret_val = bcf.unmirror_ip('10.0.0.2')
def test_get_highest(): class MockBcfProxy(BcfProxy): def __init__(self): self.endpoints = None self.span_fabric = None # def get_endpoints(self): # return self.endpoints # def get_span_fabric(self): # return self.span_fabric # def shutdown_endpoint(self, tenant, segment, name, mac, shutdown): # pass bcf = MockBcfProxy() filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant': 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment': 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint': 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22]': 'sample_span_fabric.json', # %22 = url-encoded double quotes '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22][dest-interface-group=%22INTERFACE_GROUP%22]': 'sample_span_fabric.json', } proxy = None endpoints = None span_fabric = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', { 'username': username, 'password': password }, span_fabric_name='SPAN_FABRIC', interface_group='INTERFACE_GROUP') endpoints = proxy.get_endpoints() span_fabric = proxy.get_span_fabric() bcf.endpoints = endpoints bcf.span_fabric = span_fabric ret_val = bcf.get_highest(span_fabric) answer = 3 assert answer == ret_val
def test_get_byip(): class MockBcfProxy(BcfProxy): def __init__(self): self.endpoints = None self.logger = MockLogger().logger self.poseidon_logger = MockLogger().poseidon_logger def get_endpoints(self): return self.endpoints bcf = MockBcfProxy() filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant': 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment': 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint': 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22]': 'sample_span_fabric.json', # %22 = url-encoded double quotes '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22][dest-interface-group=%22INTERFACE_GROUP%22]': 'sample_span_fabric.json', } proxy = None endpoints = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', { 'username': username, 'password': password }, span_fabric_name='SPAN_FABRIC', interface_group='INTERFACE_GROUP') endpoints = proxy.get_endpoints() bcf.endpoints = endpoints ret_val = bcf.get_byip('10.0.0.1') answer = list([{ 'ip-address': '10.0.0.1', 'ip-state': 'learned', 'mac': '00:00:00:00:00:01', 'segment': 'poseidon', 'tenant': 'poseidon', 'name': None }]) assert str(answer) == str(ret_val)
def test_get_seq_by_ip(): class MockBcfProxy(BcfProxy): def __init__(self): self.endpoints = None self.span_fabric = None # def get_endpoints(self): # return self.endpoints def get_span_fabric(self): return self.span_fabric # def shutdown_endpoint(self, tenant, segment, name, mac, shutdown): # pass bcf = MockBcfProxy() filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant': 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment': 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint': 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric': 'sample_span_fabric.json', # %22 = url-encoded double quotes '/data/controller/applications/bcf/span-fabric[name=%22vent%22]': 'sample_span_fabric.json', } proxy = None endpoints = None span_fabric = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', { 'username': username, 'password': password }) endpoints = proxy.get_endpoints() span_fabric = proxy.get_span_fabric() bcf.endpoints = endpoints bcf.span_fabric = span_fabric ret_val = bcf.get_seq_by_ip('10.0.0.2') answer = list() assert answer == ret_val
def test_unmirror_mac(): class MockBcfProxy(BcfProxy): def __init__(self): self.endpoints = None self.span_fabric = None self.logger = MockLogger().logger self.poseidon_logger = MockLogger().poseidon_logger def get_span_fabric(self): return self.span_fabric bcf = MockBcfProxy() filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant': 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment': 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint': 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22]': 'sample_span_fabric.json', # %22 = url-encoded double quotes '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22][dest-interface-group=%22INTERFACE_GROUP%22]': 'sample_span_fabric.json', } proxy = None endpoints = None span_fabric = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', { 'username': username, 'password': password }, span_fabric_name='SPAN_FABRIC', interface_group='INTERFACE_GROUP') endpoints = proxy.get_endpoints() span_fabric = proxy.get_span_fabric() bcf.endpoints = endpoints bcf.span_fabric = span_fabric ret_val = bcf.unmirror_mac('00:00:00:00:00:01')
def first_run(self): ''' do some pre-run setup/configuration ''' if self.configured: self.controller['URI'] = str( self.mod_configuration['controller_uri']) self.controller['USER'] = str( self.mod_configuration['controller_user']) self.controller['PASS'] = str( self.mod_configuration['controller_pass']) myauth = {} myauth['password'] = self.controller['PASS'] myauth['user'] = self.controller['USER'] try: self.bcf = BcfProxy(self.controller['URI'], auth=myauth) except BaseException: self.logger.error('BcfProxy coult not connect to {0}'.format( self.controller['URI'])) else: pass
def first_run(self): ''' do some pre-run setup/configuration ''' if self.configured: self.controller['TYPE'] = str( self.mod_configuration['controller_type']) if self.controller['TYPE'] == 'bcf': self.controller['URI'] = str( self.mod_configuration['controller_uri']) self.controller['USER'] = str( self.mod_configuration['controller_user']) self.controller['PASS'] = str( self.mod_configuration['controller_pass']) if 'controller_span_fabric_name' in self.mod_configuration: self.controller['SPAN_FABRIC_NAME'] = str( self.mod_configuration['controller_span_fabric_name']) if 'controller_interface_group' in self.mod_configuration: self.controller['INTERFACE_GROUP'] = str( self.mod_configuration['controller_interface_group']) myauth = {} myauth['password'] = self.controller['PASS'] myauth['user'] = self.controller['USER'] try: self.sdnc = BcfProxy( self.controller['URI'], auth=myauth, span_fabric_name=self.controller['SPAN_FABRIC_NAME'], interface_group=self.controller['INTERFACE_GROUP']) except BaseException as e: # pragma: no cover self.logger.error( 'BcfProxy could not connect to {0} because {1}'.format( self.controller['URI'], e)) elif self.controller['TYPE'] == 'faucet': try: if 'learn_public_addresses' in self.mod_configuration: if self.mod_configuration[ 'learn_public_addresses'] == 'Yes': self.controller['LEARN_PUBLIC_ADDRESSES'] = True else: self.controller['LEARN_PUBLIC_ADDRESSES'] = False if 'controller_uri' in self.mod_configuration: self.controller['URI'] = str( self.mod_configuration['controller_uri']) if 'controller_user' in self.mod_configuration: self.controller['USER'] = str( self.mod_configuration['controller_user']) if 'controller_pass' in self.mod_configuration: self.controller['PASS'] = str( self.mod_configuration['controller_pass']) if 'controller_config_file' in self.mod_configuration: self.controller['CONFIG_FILE'] = str( self.mod_configuration['controller_config_file']) if 'controller_log_file' in self.mod_configuration: self.controller['LOG_FILE'] = str( self.mod_configuration['controller_log_file']) if 'controller_mirror_ports' in self.mod_configuration: self.controller['MIRROR_PORTS'] = ast.literal_eval( self.mod_configuration['controller_mirror_ports']) if 'rabbit_enabled' in self.mod_configuration: self.controller['RABBIT_ENABLED'] = ast.literal_eval( self.mod_configuration['rabbit_enabled']) self.sdnc = FaucetProxy( host=self.controller['URI'], user=self.controller['USER'], pw=self.controller['PASS'], config_file=self.controller['CONFIG_FILE'], log_file=self.controller['LOG_FILE'], mirror_ports=self.controller['MIRROR_PORTS'], rabbit_enabled=self.controller['RABBIT_ENABLED'], learn_pub_adds=self. controller['LEARN_PUBLIC_ADDRESSES'], reinvestigation_frequency=self. reinvestigation_frequency, max_concurrent_reinvestigations=self. max_concurrent_reinvestigations) except BaseException as e: # pragma: no cover self.logger.error( 'FaucetProxy could not connect to {0} because {1}'. format(self.controller['URI'], e)) else: self.logger.error('Unknown SDN controller type {0}'.format( self.controller['TYPE']))
def test_BcfProxy(): """ Tests bcf """ filemap = { '/data/controller/applications/bcf/info/fabric/switch': 'sample_switches.json', '/data/controller/applications/bcf/info/endpoint-manager/tenant': 'sample_tenants.json', '/data/controller/applications/bcf/info/endpoint-manager/segment': 'sample_segments.json', '/data/controller/applications/bcf/info/endpoint-manager/endpoint': 'sample_endpoints.json', '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22]': 'sample_span_fabric.json', # %22 = url-encoded double quotes '/data/controller/applications/bcf/span-fabric[name=%22SPAN_FABRIC%22][dest-interface-group=%22INTERFACE_GROUP%22]': 'sample_span_fabric.json', '/data/controller/applications/bcf/span-fabric[name=%22empty%22][dest-interface-group=%22empty%22]': 'sample_span_fabric_empty.json', } proxy = None with HTTMock(mock_factory(r'.*', filemap)): proxy = BcfProxy('http://localhost', 'login', { 'username': username, 'password': password }, span_fabric_name='SPAN_FABRIC', interface_group='INTERFACE_GROUP') endpoints = proxy.get_endpoints() assert endpoints switches = proxy.get_switches() assert switches tenants = proxy.get_tenants() assert tenants segments = proxy.get_segments() assert segments span_fabric = proxy.get_span_fabric() assert span_fabric span_fabric = proxy.get_span_fabric(span_name='SPAN_FABRIC') assert span_fabric span_fabric = proxy.get_span_fabric(span_name='empty', interface_group='empty') assert not span_fabric with HTTMock(mock_factory2(r'.*')): # Normally shutdown_endpoint does not return a value. # You should call get_endpoint() afterwards to verify that a shutdown request went through. # In addition, the mock endpoint generated does not check for duplicates. # TODO: ***This code below is temporary.*** r = proxy.shutdown_endpoint(tenant='TENANT', segment='SEGMENT', endpoint_name='test', mac='00:00:00:00:00:00', shutdown=True) assert r r = proxy.shutdown_endpoint(tenant='TENANT', segment='SEGMENT', endpoint_name='test', mac='00:00:00:00:00:00', shutdown=False) assert r r = proxy.mirror_traffic(seq=2, mirror=True, tenant='TENANT', segment='SEGMENT') assert r r = proxy.mirror_traffic(seq=2, mirror=False) assert r def r(): return True r.text = '' # cover object assert r() BcfProxy.parse_json(r) proxy.session.cookies.clear_session_cookies() proxy.base_uri = 'http://jsonplaceholder.typicode.com' r = proxy.post_resource('posts') r.raise_for_status() r = proxy.request_resource( method='PUT', url='http://jsonplaceholder.typicode.com/posts/1') r.raise_for_status()