def test_events_keys(self): def _expected_events(nic, address, cidr): return deque([ {'event': 'new_link', 'name': nic}, {'event': 'new_addr', 'address': address + '/' + cidr}, {'event': 'new_link', 'name': nic}, {'event': 'new_link', 'name': nic}, {'event': 'new_addr', 'family': 'inet6'}, {'event': 'new_link', 'name': nic}, {'event': 'del_neigh'}, {'event': 'del_addr', 'family': 'inet6'}, {'address': address + '/' + cidr, 'event': 'del_addr'}, {'destination': address, 'event': 'del_route'}, {'event': 'del_link', 'name': nic}]) with monitor.Monitor(timeout=self.TIMEOUT, silent_timeout=True) as mon: dummy_name = dummy.create() dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR) dummy.setLinkUp(dummy_name) dummy.remove(dummy_name) expected_events = _expected_events(dummy_name, IP_ADDRESS, IP_CIDR) expected = expected_events.popleft() for event in mon: if _is_subdict(expected, event): expected = expected_events.popleft() if len(expected_events) == 0: break self.assertEqual(0, len(expected_events), '%d expected events have not' ' been caught (in the right order)' % (1 + len(expected_events)))
def test_events_keys(self): def _expected_events(nic, address, cidr): return deque([ {'event': 'new_link', 'name': nic}, {'event': 'new_addr', 'address': address + '/' + cidr}, {'event': 'new_link', 'name': nic}, {'event': 'new_addr', 'family': 'inet6'}, {'event': 'new_link', 'name': nic}, {'event': 'del_neigh'}, {'event': 'del_addr', 'family': 'inet6'}, {'address': address + '/' + cidr, 'event': 'del_addr'}, {'destination': address, 'event': 'del_route'}, {'event': 'del_link', 'name': nic}]) with monitor.Monitor(timeout=self.TIMEOUT, silent_timeout=True) as mon: dummy_name = dummy.create() dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR) dummy.setLinkUp(dummy_name) dummy.remove(dummy_name) expected_events = _expected_events(dummy_name, IP_ADDRESS, IP_CIDR) expected = expected_events.popleft() for event in mon: if _is_subdict(expected, event): expected = expected_events.popleft() if len(expected_events) == 0: break self.assertEqual(0, len(expected_events), '%d expected events have not' ' been caught (in the right order)' % (1 + len(expected_events)))
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_neigh'}, {'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_name = dummy.create() dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR) dummy.setLinkUp(dummy_name) dummy.remove(dummy_name) 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_event_groups(self): with monitor.Monitor(timeout=self.TIMEOUT, groups=("ipv4-ifaddr",)) as mon_a: with monitor.Monitor(timeout=self.TIMEOUT, groups=("link", "ipv4-route")) as mon_l_r: dummy_name = dummy.create() dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR) dummy.setLinkUp(dummy_name) dummy.remove(dummy_name) for event in mon_a: self.assertIn("_addr", event["event"], "Caught event '%s' is not " "related to address." % event["event"]) for event in mon_l_r: link_or_route = "_link" in event["event"] or "_route" in event["event"] self.assertTrue(link_or_route, "Caught event '%s' is not related " "to link or route." % event["event"])
def test_event_groups(self): with monitor.Monitor(timeout=self.TIMEOUT, groups=('ipv4-ifaddr',)) as mon_a: with monitor.Monitor(timeout=self.TIMEOUT, groups=('link', 'ipv4-route')) as mon_l_r: dummy_name = dummy.create() dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR) dummy.setLinkUp(dummy_name) dummy.remove(dummy_name) for event in mon_a: self.assertIn('_addr', event['event'], "Caught event '%s' is not " "related to address." % event['event']) for event in mon_l_r: link_or_route = ('_link' in event['event'] or '_route' in event['event']) self.assertTrue(link_or_route, "Caught event '%s' is not related " "to link or route." % event['event'])
def test_events_keys(self): 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_neigh"}, {"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_name = dummy.create() dummy.setIP(dummy_name, IP_ADDRESS, IP_CIDR) dummy.setLinkUp(dummy_name) dummy.remove(dummy_name) expected_events = _expected_events(dummy_name, IP_ADDRESS, IP_CIDR) expected = expected_events.popleft() for event in mon: if _is_subdict(expected, event): expected = expected_events.popleft() if len(expected_events) == 0: break self.assertEqual( 0, len(expected_events), "%d expected events have not" " been caught (in the right order)" % (1 + len(expected_events)), )