Skip to content

Commit

Permalink
Add test case for CPUCollector with 'simple' flag set
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Cunningham committed Jan 17, 2017
1 parent 85e92f3 commit e32ac29
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/collectors/cpu/test/testcpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,54 @@ def test_should_work_psutil(self, psutil_mock, os_mock, publish_mock):

self.assertPublishedMany(publish_mock, self.expected)


class TestCPUCollectorSimple(CollectorTestCase):

def setUp(self):
self.config = get_collector_config('CPUCollector', {
'interval': 10,
'normalize': False,
'simple': True,
})

self.collector = CPUCollector(self.config, None)

def test_import(self):
self.assertTrue(CPUCollector)

@patch.object(Collector, 'publish')
def test_produces_simple_percent(self, publish_mock):
# when the simple config option is set, we check the CPU values twice
# to calculate a delta, so we need two mock values
m = Mock()
patch_open = patch('__builtin__.open', m)
m.side_effect = [
StringIO('cpu 100 200 300 400 500 0 0 0 0 0'),
StringIO('cpu 110 220 330 440 550 0 0 0 0 0'),
]

patch_open.start()
self.collector.collect()
patch_open.stop()

self.assertPublishedMany(publish_mock, {})

m = Mock()
patch_open = patch('__builtin__.open', m)
m.side_effect = [
StringIO('cpu 110 220 330 440 550 0 0 0 0 0'),
StringIO('cpu 120 230 340 450 560 0 0 0 0 0'),
]

patch_open.start()
self.collector.collect()
patch_open.stop()

self.assertPublishedMany(publish_mock, {
'percent': 75.0
})


##########################################################################
if __name__ == "__main__":
unittest.main()

0 comments on commit e32ac29

Please sign in to comment.