Skip to content

Commit e32ac29

Browse files
author
Scott Cunningham
committed
Add test case for CPUCollector with 'simple' flag set
1 parent 85e92f3 commit e32ac29

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/collectors/cpu/test/testcpu.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,54 @@ def test_should_work_psutil(self, psutil_mock, os_mock, publish_mock):
257257

258258
self.assertPublishedMany(publish_mock, self.expected)
259259

260+
261+
class TestCPUCollectorSimple(CollectorTestCase):
262+
263+
def setUp(self):
264+
self.config = get_collector_config('CPUCollector', {
265+
'interval': 10,
266+
'normalize': False,
267+
'simple': True,
268+
})
269+
270+
self.collector = CPUCollector(self.config, None)
271+
272+
def test_import(self):
273+
self.assertTrue(CPUCollector)
274+
275+
@patch.object(Collector, 'publish')
276+
def test_produces_simple_percent(self, publish_mock):
277+
# when the simple config option is set, we check the CPU values twice
278+
# to calculate a delta, so we need two mock values
279+
m = Mock()
280+
patch_open = patch('__builtin__.open', m)
281+
m.side_effect = [
282+
StringIO('cpu 100 200 300 400 500 0 0 0 0 0'),
283+
StringIO('cpu 110 220 330 440 550 0 0 0 0 0'),
284+
]
285+
286+
patch_open.start()
287+
self.collector.collect()
288+
patch_open.stop()
289+
290+
self.assertPublishedMany(publish_mock, {})
291+
292+
m = Mock()
293+
patch_open = patch('__builtin__.open', m)
294+
m.side_effect = [
295+
StringIO('cpu 110 220 330 440 550 0 0 0 0 0'),
296+
StringIO('cpu 120 230 340 450 560 0 0 0 0 0'),
297+
]
298+
299+
patch_open.start()
300+
self.collector.collect()
301+
patch_open.stop()
302+
303+
self.assertPublishedMany(publish_mock, {
304+
'percent': 75.0
305+
})
306+
307+
260308
##########################################################################
261309
if __name__ == "__main__":
262310
unittest.main()

0 commit comments

Comments
 (0)