def test_statnode(self, mock_dt, mock_requests, mock_Popen): now = datetime.utcnow() mock_dt.utcnow.return_value = now # predictable Popen().communicate mock_Popen(['ifconfig', 'lo']).communicate.return_value = ('', '') # No psutil -> just don't exception out node = StatNode(log_type='stats_log', api=self.watcher.api) stats = node._gather() expected_stats = { 'cpu_times_percent': [{ 'idle': 90.49773755656109, 'nice': 0.0, 'system': 9.049773755656108, 'user': 0.45248868778280543 }], 'virtual_memory': { 'free': 4, 'percent': 80.0, 'total': 20, 'used': 18 }, 'disk_usage': [{ 'total': 150, 'path': '/', 'used': 40, 'percent': 88.3, 'free': 110 }], 'net_io_counters': [{ 'nic': 'lo', 'B_recv': 30, 'B_sent': 30, 'p_recv': 18, 'p_sent': 18 }], 'starttime': now.isoformat(), 'runtime': '0:00:00', } self.assertEqual(stats, expected_stats) def fetch_request(*args, **kwargs): data = kwargs['data'].read() data_dict = loads(data) # verify the data we're sending matches our expectation self.assertEqual(expected_stats, data_dict) return Mock() mock_requests.request.side_effect = fetch_request # now check the stuff we'll send (trigger the 1 minute interval) with patch('ona_service.log_watcher.utcnow') as mock_now: mock_now.side_effect = lambda: now + timedelta(seconds=61) node.check_data()
def test_statnode(self, mock_dt, mock_requests, mock_Popen): now = datetime.utcnow() mock_dt.utcnow.return_value = now # predictable Popen().communicate mock_Popen(['ifconfig', 'lo']).communicate.return_value = ('', '') # No psutil -> just don't exception out node = StatNode(log_type='stats_log', api=self.watcher.api) stats = node._gather() expected_stats = { 'cpu_times_percent': [ {'idle': 90.49773755656109, 'nice': 0.0, 'system': 9.049773755656108, 'user': 0.45248868778280543} ], 'virtual_memory': { 'free': 4, 'percent': 80.0, 'total': 20, 'used': 18 }, 'disk_usage': [ {'total': 150, 'path': '/', 'used': 40, 'percent': 88.3, 'free': 110} ], 'net_io_counters': [ {'nic': 'lo', 'B_recv': 30, 'B_sent': 30, 'p_recv': 18, 'p_sent': 18} ], 'starttime': now.isoformat(), 'runtime': '0:00:00', } self.assertEqual(stats, expected_stats) def fetch_request(*args, **kwargs): data = kwargs['data'].read() data_dict = loads(data) # verify the data we're sending matches our expectation self.assertEqual(expected_stats, data_dict) return Mock() mock_requests.request.side_effect = fetch_request # now check the stuff we'll send (trigger the 1 minute interval) with patch('ona_service.log_watcher.utcnow') as mock_now: mock_now.side_effect = lambda: now + timedelta(seconds=61) node.check_data()
def test_statnode_nopsutil(self): # No psutil -> just don't exception out node = StatNode(log_type='stats_log', api=self.watcher.api) node.check_data() stats = node._gather() self.assertEqual(stats, {})