def testMultipleItems(self): current_time_secs = 1560000000 last_seen1 = (current_time_secs - 1) * (10**6) last_seen2 = (current_time_secs - 4 * 60 * 60 * 24) * (10**6) client1 = ClientListTest._MockClient('foo', 'host1', last_seen1) client2 = ClientListTest._MockClient('bar', 'host2', last_seen2) clients = representer.ClientList([client1, client2]) out = io.StringIO() with mock.patch.object(time, 'time', return_value=current_time_secs): clients._repr_pretty_(pretty.PrettyPrinter(out), cycle=False) expected = """ 🌕 foo @ host1 (1 seconds ago) 🌑 bar @ host2 (4 days ago) """ self.assertEqual(out.getvalue(), expected)
def search(cls, ip: Optional[Text] = None, mac: Optional[Text] = None, host: Optional[Text] = None, version: Optional[int] = None, labels: Optional[List[Text]] = None, user: Optional[Text] = None) -> Sequence['Client']: """Searches for clients specified with keywords. Args: ip: Client IP address. mac: Client MAC address. host: Client hostname. version: Client version. labels: Client labels. user: Client username. Returns: A sequence of clients. """ def format_keyword(key: Text, value: Text) -> Text: return '{}:{}'.format(key, value) keywords = [] if ip is not None: keywords.append(format_keyword('ip', ip)) if mac is not None: keywords.append(format_keyword('mac', mac)) if host is not None: keywords.append(format_keyword('host', host)) if version is not None: keywords.append(format_keyword('client', str(version))) if labels: for label in labels: keywords.append(format_keyword('label', label)) if user is not None: keywords.append(format_keyword('user', user)) query = ' '.join(keywords) clients = _api.get().SearchClients(query) return representer.ClientList([cls(_) for _ in clients])
def testSlice(self): client1 = ClientListTest._MockClient() client2 = ClientListTest._MockClient() clients = representer.ClientList([client1, client2]) self.assertIsInstance(clients[:1], representer.ClientList)
def testCycle(self): cs = representer.ClientList([]) out = io.StringIO() with self.assertRaises(AssertionError): cs._repr_pretty_(pretty.PrettyPrinter(out), cycle=True)
def testEmptyResults(self): cs = representer.ClientList([]) out = io.StringIO() cs._repr_pretty_(pretty.PrettyPrinter(out), cycle=False) self.assertEqual(out.getvalue(), 'No results.')