def analyzer_models(api_client, protocol_counters=None, flow_counters=None): ana1 = analyzer_model(api_client, protocol_counters, flow_counters) ana2 = analyzer_model(api_client, protocol_counters, flow_counters) ana2.source_id = get_second_port_id(api_client) return [ana1, ana2] with description('Packet Analyzer,', 'packet_analyzer') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.PacketAnalyzersApi(service.client()) if not check_modules_exists(service.client(), 'packet-analyzer'): self.skip() with description('invalid HTTP methods,'): with description('/packet/analyzers,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/packet/analyzers', 'PUT')).to( raise_api_exception( 405, headers={'Allow': "DELETE, GET, POST"})) with description('/packet/analyzer-results,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/packet/analyzer-results', 'PUT')).to(
ipv6_interface, make_interface_protocols) from common.matcher import be_valid_interface, raise_api_exception CONFIG = Config(os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) BULK_OP_SIZE = 4 with description('Interfaces,', 'interfaces') as self: with description ('REST API,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.InterfacesApi(service.client()) with description('list,'): with description('Eth,'): with before.each: intf = self.api.create_interface(example_ipv4_interface(self.api.api_client)) expect(intf).to(be_valid_interface) self.intf, self.cleanup = intf, intf with description('unfiltered,'): with it('succeeds'): intfs = self.api.list_interfaces() expect(intfs).not_to(be_empty) for intf in intfs: expect(intf).to(be_valid_interface) expect([ i for i in intfs if i.id == self.intf.id ]).not_to(be_empty)
be_valid_dynamic_results) CONFIG = Config(os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) def approximately_equal(a, b): return abs(a - b) < 0.01 with description('CPU Generator Module', 'cpu') as self: with before.all: service = Service(CONFIG.service()) self._process = service.start() self._api = client.api.CpuGeneratorApi(service.client()) if not check_modules_exists(service.client(), 'cpu'): self.skip() with after.all: try: for gen in self._api.list_cpu_generators(): if gen.running: self._api.stop_cpu_generator(gen.id) self._api.delete_cpu_generator(gen.id) self._process.terminate() self._process.wait() except AttributeError: pass
def pcap_icmp_echo_request_lengths(pcap_file): lengths = [] icmp_type = scapy.all.ICMP(type='echo-request').type for packet in scapy.all.rdpcap(pcap_file): if 'ICMP' in packet and packet['ICMP'].type == icmp_type: lengths.append(len(packet)) return lengths with description('Packet Capture,', 'packet_capture') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service('dataplane')) self.process = service.start() self.api = client.api.PacketCapturesApi(service.client()) self.intf_api = client.api.InterfacesApi(self.api.api_client) # By default, ping is a privileged process. We need it unprivileged # to use LD_PRELOAD, so just make a copy as a regular user. self.temp_dir = tempfile.mkdtemp() shutil.copy(PING, self.temp_dir) self.temp_ping = os.path.join(self.temp_dir, os.path.basename(PING)) expect(os.path.isfile(self.temp_ping)) with description('invalid HTTP methods,'): with description('/packet/captures,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/packet/captures', 'PUT')).to(
gen1 = generator_model(api_client) gen1.target_id = ports[0].id gen2 = generator_model(api_client) gen2.target_id = ports[1].id return [gen1, gen2] with description('Packet Generator,', 'packet_generator') as self: with description('REST API'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.PacketGeneratorsApi(service.client()) with description('invalid HTTP methods,'): with description('/packet/generators,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api('/packet/generators', 'PUT')).to( raise_api_exception(405, headers={'Allow': "DELETE, GET, POST"})) with description('/packet/generator-results,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api('/packet/generator-results', 'PUT')).to( raise_api_exception(405, headers={'Allow': "DELETE, GET"})) with description('/packet/tx-flows,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api('/packet/tx-flows', 'PUT')).to(
bulk_stop_model, wait_for_file_initialization_done) from common.matcher import (be_valid_block_device, be_valid_block_file, be_valid_block_generator, be_valid_block_generator_result, has_location, raise_api_exception, be_valid_dynamic_results) CONFIG = Config( os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('Block,', 'block') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.BlockGeneratorApi(service.client()) if not check_modules_exists(service.client(), 'block'): self.skip() with description('invalid HTTP methods,'): with description('/block-devices,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/block-devices', 'PUT')).to( raise_api_exception(405, headers={'Allow': "GET"})) with description('/block-files,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/block-files', 'PUT')).to( raise_api_exception(405,
with open(os.devnull, 'w') as null: p = subprocess.Popen(ping_command(ping_binary, dst_ip, domain), stdout=null, stderr=null, env={'LD_PRELOAD': shim.config.path, 'OP_BINDTODEVICE': src_id}) p.wait() expect(p.returncode).to(equal(0)) with description('Dataplane,', 'dataplane') as self: with before.all: service = Service(CONFIG.service('dataplane')) self.process = service.start() self.api = client.api.InterfacesApi(service.client()) if not check_modules_exists(service.client(), 'packetio'): self.skip() with description('ipv4,', 'dataplane:ipv4'): with description('ping,', 'dataplane:ipv4:ping'): with before.all: # By default, ping is a privileged process. We need it unprivileged # to use LD_PRELOAD, so just make a copy as a regular user. self.temp_dir = tempfile.mkdtemp() shutil.copy(PING, self.temp_dir) self.temp_ping = os.path.join(self.temp_dir, os.path.basename(PING)) expect(os.path.isfile(self.temp_ping)) with description('client interface,'): with it('succeeds'):
def analyzer_models(api_client, protocol_counters=None, flow_counters=None): ana1 = analyzer_model(api_client, protocol_counters, flow_counters) ana2 = analyzer_model(api_client, protocol_counters, flow_counters) ana2.source_id = get_second_port_id(api_client) return [ana1, ana2] with description('Packet Analyzer,', 'packet_analyzer') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.PacketAnalyzersApi(service.client()) with description('invalid HTTP methods,'): with description('/packet/analyzers,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/packet/analyzers', 'PUT')).to( raise_api_exception( 405, headers={'Allow': "DELETE, GET, POST"})) with description('/packet/analyzer-results,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/packet/analyzer-results', 'PUT')).to( raise_api_exception( 405, headers={'Allow': "DELETE, GET"}))
flow = gen_api.get_tx_flow(flow_id) expect(flow).to(be_valid_transmit_flow) expect([f.id for f in gen_api.list_tx_flows() if f.id in gen_result.flows]).not_to(be_empty) ### # Begin test proper ### with description('Packet back to back', 'packet_b2b') as self: with description('generation and analysis,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.client = service.client() self.analyzer_api = client.api.PacketAnalyzersApi(service.client()) self.generator_api = client.api.PacketGeneratorsApi(service.client()) with description('with single traffic definition,'): with description('without signatures,'): with it('succeeds'): ana_result, gen_result = configure_and_run_test(self.client, ANALYZER_CONFIG_NO_SIGS, GENERATOR_CONFIG_NO_SIGS) # Validate results exp_flow_count = 1 expect(len(ana_result.flows)).to(equal(exp_flow_count)) expect(len(gen_result.flows)).to(equal(exp_flow_count))
import os import client.api import client.models from common import Config, Service from common.matcher import be_valid_stack, raise_api_exception CONFIG = Config( os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('Stacks,', 'stacks') as self: with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.StacksApi(service.client()) with description('list,'): with it('returns valid stacks'): stacks = self.api.list_stacks() expect(stacks).not_to(be_empty) for stack in stacks: expect(stack).to(be_valid_stack) with description('unsupported method'): with it('returns 405'): expect( lambda: self.api.api_client.call_api('/stacks', 'PUT')).to( raise_api_exception(405, headers={'Allow': "GET"})) with description('get,'):
import client.models from common import Config, Service from common.matcher import be_valid_port, raise_api_exception from common.helper import check_modules_exists CONFIG = Config( os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('Ports,', 'ports') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.PortsApi(service.client()) if not check_modules_exists(service.client(), 'packetio'): self.skip() with description('list,'): with description('unfiltered,'): with it('returns valid ports'): ports = self.api.list_ports() expect(ports).not_to(be_empty) for port in ports: expect(port).to(be_valid_port) with description('filtered,'): with description('known existing kind,'): with it('returns valid ports of that kind'): ports = self.api.list_ports(kind='dpdk')
class has_location(Matcher): def __init__(self, expected): self._expected = expected def _match(self, subject): expect(subject).to(have_key('Location')) return subject['Location'] == self._expected, [] has_json_content_type = _has_json_content_type() with description('Memory Generator Module', 'memory') as self: with before.all: service = Service(CONFIG.service()) self._process = service.start() self._api = client.api.MemoryGeneratorApi(service.client()) with after.all: try: self._process.terminate() self._process.wait() except AttributeError: pass with description('/memory-info'): with context('GET'): with before.all: self._result = self._api.memory_info_with_http_info( _return_http_data_only=False) with it('succeeded'):
class has_location(Matcher): def __init__(self, expected): self._expected = expected def _match(self, subject): expect(subject).to(have_key('Location')) return subject['Location'] == self._expected, [] has_json_content_type = _has_json_content_type() with description('CPU Generator Module', 'cpu') as self: with before.all: service = Service(CONFIG.service()) self._process = service.start() self._api = client.api.CpuGeneratorApi(service.client()) with after.all: try: for gen in self._api.list_cpu_generators(): if gen.running: self._api.stop_cpu_generator(gen.id) self._api.delete_cpu_generator(gen.id) self._process.terminate() self._process.wait() except AttributeError: pass with description('/cpu-info'): with context('GET'):
class has_location(Matcher): def __init__(self, expected): self._expected = CONFIG.service().base_url + expected def _match(self, subject): expect(subject).to(have_key('Location')) return subject['Location'] == self._expected, [] with description('Block,', 'block') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.BlockGeneratorApi(service.client()) with description('invalid HTTP methods,'): with description('/block-devices,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/block-devices', 'PUT')).to( raise_api_exception(405, headers={'Allow': "GET"})) with description('/block-files,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/block-files', 'PUT')).to( raise_api_exception(405, headers={'Allow': "GET, POST"}))
return s def get_system_timesource(id=None): s = client.models.TimeSource() s.kind = 'system' s.id = 'test-source-' + str(id) return s with description('Timesync,', 'timesync') as self: with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.TimeSyncApi(service.client()) with description('counters,'): with description('unsupported method,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/time-counters', 'PUT')).to( raise_api_exception(405, headers={'Allow': "GET"})) with description('GET,'): with description('list,'): with it('succeeds'): counters = self.api.list_time_counters() expect(counters).not_to(be_empty) for counter in counters: expect(counter).to(be_valid_counter)
timestamps = pcap_packet_timestamps(pacp_file) prev_ts = None for ts in timestamps: expect(ts).to(be_above(0)) if (prev_ts): expect(ts).to(be_above_or_equal(prev_ts)) prev_ts = ts with description('Packet Capture,', 'packet_capture') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service('dataplane')) self.process = service.start() self.api = client.api.PacketCapturesApi(service.client()) self.intf_api = client.api.InterfacesApi(self.api.api_client) if not check_modules_exists(service.client(), 'packetio', 'packet-capture'): self.skip() # By default, ping is a privileged process. We need it unprivileged # to use LD_PRELOAD, so just make a copy as a regular user. self.temp_dir = tempfile.mkdtemp() shutil.copy(PING, self.temp_dir) self.temp_ping = os.path.join(self.temp_dir, os.path.basename(PING)) expect(os.path.isfile(self.temp_ping)) with description('invalid HTTP methods,'): with description('/packet/captures,'):
gen1 = generator_model(api_client) gen1.target_id = ports[0].id gen2 = generator_model(api_client) gen2.target_id = ports[1].id return [gen1, gen2] with description('Packet Generator,', 'packet_generator') as self: with description('REST API'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.PacketGeneratorsApi(service.client()) if not check_modules_exists(service.client(), 'packet-generator'): self.skip() with description('invalid HTTP methods,'): with description('/packet/generators,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/packet/generators', 'PUT')).to( raise_api_exception( 405, headers={'Allow': "DELETE, GET, POST"})) with description('/packet/generator-results,'): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/packet/generator-results', 'PUT')).to(
wait_for_buffer_initialization_done) from common.matcher import (has_location, has_json_content_type, raise_api_exception, be_valid_memory_info, be_valid_memory_generator, be_valid_memory_generator_result, be_valid_dynamic_results) CONFIG = Config( os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('Memory Generator Module', 'memory') as self: with before.all: service = Service(CONFIG.service()) self._process = service.start() self._api = client.api.MemoryGeneratorApi(service.client()) if not check_modules_exists(service.client(), 'memory'): self.skip() with after.all: try: for gen in self.api.list_memory_generators(): if gen.running: self.api.stop_memory_generator(gen.id) self.api.delete_memory_generator(gen.id) except AttributeError: pass try: self._process.terminate() self._process.wait()
import os import client.api import client.models from common import Config, Service from common.matcher import be_valid_module, raise_api_exception CONFIG = Config( os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('Modules, ', 'modules') as self: with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.ModulesApi(service.client()) with description('list, '): with description('all, '): with it('returns list of modules'): modules = self.api.list_modules() expect(modules).not_to(be_empty) for module in modules: expect(module).to(be_valid_module) with description('unsupported method, '): with it('returns 405'): expect(lambda: self.api.api_client.call_api( '/modules', 'PUT')).to( raise_api_exception(405, headers={'Allow': "GET"}))
import client.api import client.models from common import Config, Service from common.matcher import be_valid_stack, raise_api_exception from common.helper import check_modules_exists CONFIG = Config( os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('Stacks,', 'stacks') as self: with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.StacksApi(service.client()) if not check_modules_exists(service.client(), 'packet-stack'): self.skip() with description('list,'): with it('returns valid stacks'): stacks = self.api.list_stacks() expect(stacks).not_to(be_empty) for stack in stacks: expect(stack).to(be_valid_stack) with description('unsupported method'): with it('returns 405'): expect( lambda: self.api.api_client.call_api('/stacks', 'PUT')).to( raise_api_exception(405, headers={'Allow': "GET"}))
import client.api import client.models from common import Config, Service from common.matcher import be_valid_port, raise_api_exception CONFIG = Config(os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('Ports,', 'ports') as self: with description('REST API,'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.api = client.api.PortsApi(service.client()) with description('list,'): with description('unfiltered,'): with it('returns valid ports'): ports = self.api.list_ports() expect(ports).not_to(be_empty) for port in ports: expect(port).to(be_valid_port) with description('filtered,'): with description('known existing kind,'): with it('returns valid ports of that kind'): ports = self.api.list_ports(kind='dpdk') expect(ports).not_to(be_empty) for port in ports:
expect(ana).to(be_valid_packet_analyzer) return ana CONFIG = Config( os.path.join(os.path.dirname(__file__), os.environ.get('MAMBA_CONFIG', 'config.yaml'))) with description('MAC Learning,', 'learning') as self: with description('REST API'): with before.all: service = Service(CONFIG.service()) self.process = service.start() self.ana_api = client.api.PacketAnalyzersApi(service.client()) self.gen_api = client.api.PacketGeneratorsApi(service.client()) self.intf_api = client.api.InterfacesApi(service.client()) if not check_modules_exists(service.client(), 'packet-generator', 'packet-analyzer'): self.skip() with description('packet-generator, '): with description('IPv4, '): with before.each: self.source_ip = "192.168.22.10" self.source_mac = "aa:bb:cc:dd:ee:01" self.target_ip = "192.168.22.1" self.intf1 = ipv4_interface(self.intf_api.api_client, method="static", ipv4_address=self.source_ip,