def test_switches(): with mock.patch('ryu.controller.controller.Datapath.set_state'): #*** Set up fake switch datapaths: datapath1 = controller.Datapath(sock_mock, addr_mock) datapath1.id = 12345 datapath1.address = ('172.16.1.10', 12345) datapath2 = controller.Datapath(sock_mock, addr_mock) datapath2.id = 67890 datapath2.address = ('172.16.1.11', 23456) #*** Should have 0 switches: assert len(switches.switches) == 0 assert switches.switches_col.count() == 0 #*** Add switches assert switches.add(datapath1) == 1 assert switches.add(datapath2) == 1 #*** Should have 2 switches: assert len(switches.switches) == 2 assert switches.switches_col.count() == 2 #*** Delete switch assert switches.delete(datapath2) == 1 #*** Should have 1 switch: assert len(switches.switches) == 1 assert switches.switches_col.count() == 1
def _test_ports_accessibility(self, ofproto_parser, msgs_len): with mock.patch('ryu.controller.controller.Datapath.set_state'): # Ignore warnings with warnings.catch_warnings(record=True) as msgs: warnings.simplefilter('always') # Test target sock_mock = mock.Mock() addr_mock = mock.Mock() dp = controller.Datapath(sock_mock, addr_mock) dp.ofproto_parser = ofproto_parser # Create dp.ports = {} # Update port_mock = mock.Mock() dp.ports[0] = port_mock # Read & Delete del dp.ports[0] self.assertEqual(len(msgs), msgs_len) for msg in msgs: self.assertTrue(issubclass(msg.category, UserWarning))
def domain_features_handler(self, ev): msg = ev.msg domain = msg.domain domain.id = msg.domain_id domain.sbp_proto_type = msg.proto_type domain.sbp_proto_version = msg.sbp_version oxproto_parser = domain.oxproto_parser self.logger.debug('domain features ev %s', msg) set_config = oxproto_parser.OXPSetConfig(domain, CONF.oxp_flags, CONF.oxp_period, CONF.oxp_miss_send_len) domain.send_msg(set_config) get_config_request = oxproto_parser.OXPGetConfigRequest(domain) domain.send_msg(get_config_request) ev.msg.domain.set_state(MAIN_DISPATCHER) # build a fake datapath for parsing OF packet. if self.fake_datapath is None: self.fake_datapath = controller.Datapath(domain.socket, domain.address) if domain.sbp_proto_type == oxproto_v1_0.OXPS_OPENFLOW: if domain.sbp_proto_version == 4: self.fake_datapath.ofproto = ofproto_v1_3 self.fake_datapath.ofproto_parser = ofproto_v1_3_parser elif domain.sbp_proto_version == 1: self.fake_datapath.ofproto = ofproto_v1_0 self.fake_datapath.ofproto_parser = ofproto_v1_0_parser
def test_switches(): with mock.patch('ryu.controller.controller.Datapath.set_state'): #*** Set up fake switch datapaths: datapath1 = controller.Datapath(sock_mock, addr_mock) datapath1.id = 12345 datapath2 = controller.Datapath(sock_mock, addr_mock) datapath2.id = 67890 #*** Add switches assert switches.add(datapath1) == 1 assert switches.add(datapath2) == 1 #*** Look up by DPID: assert switches.datapath(datapath1.id) == datapath1 assert switches.datapath(datapath2.id) == datapath2 #*** Run function to test single switch use cases: _switch_test(switches[datapath1.id]) #*** Run function to test multiple switch use cases: _switches_test(switches[datapath1.id], switches[datapath2.id])
def test_recv_loop(self, app_manager_mock): # Prepare test data test_messages = [ "4-6-ofp_features_reply.packet", "4-14-ofp_echo_reply.packet", "4-14-ofp_echo_reply.packet", "4-4-ofp_packet_in.packet", "4-14-ofp_echo_reply.packet", "4-14-ofp_echo_reply.packet", ] this_dir = os.path.dirname(sys.modules[__name__].__file__) packet_data_dir = os.path.join(this_dir, '../../packet_data/of13') json_dir = os.path.join(this_dir, '../ofproto/json/of13') packet_buf = bytearray() expected_json = list() for msg in test_messages: # Construct the received packet buffer as one packet data in order # to test the case of the OpenFlow messages composed in one packet. packet_data_file = os.path.join(packet_data_dir, msg) packet_buf += open(packet_data_file, 'rb').read() json_data_file = os.path.join(json_dir, msg + '.json') expected_json.append(json.load(open(json_data_file))) # Prepare mock for socket class SocketMock(mock.MagicMock): buf = bytearray() random = None def recv(self, bufsize): size = self.random.randint(1, bufsize) out = self.buf[:size] self.buf = self.buf[size:] return out # Prepare mock ofp_brick_mock = mock.MagicMock(spec=app_manager.RyuApp) app_manager_mock.lookup_service_brick.return_value = ofp_brick_mock sock_mock = SocketMock() sock_mock.buf = packet_buf sock_mock.random = random.Random('Ryu SDN Framework') addr_mock = mock.MagicMock() # Prepare test target dp = controller.Datapath(sock_mock, addr_mock) dp.set_state(handler.MAIN_DISPATCHER) ofp_brick_mock.reset_mock() # Test dp._recv_loop() # Assert calls output_json = list() for call in ofp_brick_mock.send_event_to_observers.call_args_list: args, kwargs = call ev, state = args if not hasattr(ev, 'msg'): continue output_json.append(ev.msg.to_jsondict()) self.assertEqual(state, handler.MAIN_DISPATCHER) self.assertEqual(kwargs, {}) self.assertEqual(expected_json, output_json)