def monitor_stable_link_state(device, wait_for_linkup=True): """Raises an exception if it detects that the device link state changes.""" if wait_for_linkup: with waitfor.waitfor_linkup(device): pass iface_properties = iface(device).properties() original_state = iface_properties['state'] try: with monitor.Monitor(groups=('link', )) as mon: yield finally: state_changes = (e['state'] for e in mon if e['name'] == device) for state in state_changes: if state != original_state: raise pytest.fail('{} link state changed: {} -> {}'.format( device, original_state, state))
def test_iterate_while_events(self): """Tests if monitor is able to catch event while iterating. Before the iteration we start _set_and_remove_device, which is delayed for .2 seconds. Then iteration starts and wait for new dummy. """ dummy = Dummy() dummy_name = dummy.create() def _set_and_remove_device(): time.sleep(.2) dummy.up() dummy.remove() with monitor.Monitor(timeout=self.TIMEOUT) as mon: add_device_thread = start_thread(_set_and_remove_device) for event in mon: if event.get('name') == dummy_name: break add_device_thread.join()
def _wait_for_event(iface, expected_event, timeout=10): with monitor.Monitor(groups=('ipv4-ifaddr', 'ipv6-ifaddr'), timeout=timeout) as mon: try: yield finally: caught_events = [] try: for event in mon: caught_events.append(event) if _is_subdict(expected_event, event): return except monitor.MonitorError as e: if e[0] == monitor.E_TIMEOUT: logging.warning( 'Expected event "%s" of interface "%s" ' 'was not caught within the given timeout. ' 'Caught events: %s', expected_event, iface, caught_events) else: raise
def wait_for_ipv6(iface, wait_for_scopes=None): """Wait for iface to get their IPv6 addresses with netlink Monitor""" if not wait_for_scopes: wait_for_scopes = ['global', 'link'] try: with monitor.Monitor(groups=('ipv6-ifaddr', ), timeout=20) as mon: yield for event in mon: dev_name = event.get('label') if (dev_name == iface and event.get('event') == 'new_addr' and event.get('scope') in wait_for_scopes): wait_for_scopes.remove(event.get('scope')) if not wait_for_scopes: return except monitor.MonitorError as e: if e[0] == monitor.E_TIMEOUT: raise Exception('IPv6 addresses has not been caught within the ' 'given timeout.\n') else: raise
def test_passing_invalid_groups(self): with self.assertRaises(AttributeError): monitor.Monitor(groups=('blablabla', )) with self.assertNotRaises(): monitor.Monitor(groups=('link', ))
def test_timeout_silent(self): with monitor.Monitor(timeout=.01, silent_timeout=True) as mon: for event in mon: pass self.assertTrue(mon.is_stopped())
def test_events_keys(self): def _simplify_event(event): """ Strips event keys except event, address, name, destination, family. """ allow = set(['event', 'address', 'name', 'destination', 'family']) return {k: v for (k, v) in event.items() if k in allow} def _expected_events(nic, address, cidr): events_add = [{ 'event': 'new_link', 'name': nic }, { 'event': 'new_addr', 'address': address + '/' + cidr }, { 'event': 'new_link', 'name': nic }] events_del = [{ 'address': address + '/' + cidr, 'event': 'del_addr' }, { 'destination': address, 'event': 'del_route' }, { 'event': 'del_link', 'name': nic }] events_ipv6 = [{ 'event': 'new_addr', 'family': 'inet6' }, { 'event': 'del_addr', 'family': 'inet6' }] if is_disabled_ipv6(): return deque(events_add + events_del) else: return deque(events_add + events_ipv6 + events_del) with monitor.Monitor(timeout=self.TIMEOUT, silent_timeout=True) as mon: dummy = Dummy() dummy_name = dummy.create() dummy.set_ip(IP_ADDRESS, IP_CIDR) dummy.up() dummy.remove() expected_events = _expected_events(dummy_name, IP_ADDRESS, IP_CIDR) _expected = list(expected_events) _caught = [] expected = expected_events.popleft() for event in mon: _caught.append(event) if _is_subdict(expected, event): expected = expected_events.popleft() if len(expected_events) == 0: break self.assertEqual( 0, len(expected_events), 'Expected events have not ' 'been caught (in the right order).\n' 'Expected:\n%s.\nCaught:\n%s.' % ('\n'.join([str(d) for d in _expected]), '\n'.join( [str(_simplify_event(d)) for d in _caught])))
def test_passing_invalid_groups(self): with pytest.raises(AttributeError): monitor.Monitor(groups=('blablabla', )) monitor.Monitor(groups=('link', ))