Skip to content

Commit

Permalink
Merge pull request #1485 from DataDog/quentin/test_system_core
Browse files Browse the repository at this point in the history
[system_core] Add AgentCheckTest mocked test
  • Loading branch information
LeoCavaille committed Mar 31, 2015
2 parents 5301ec6 + c771b33 commit e4612a2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
7 changes: 4 additions & 3 deletions checks.d/system_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import psutil
from checks import AgentCheck


class SystemCore(AgentCheck):

def check(self, instance):
Expand All @@ -10,7 +11,7 @@ def check(self, instance):
for i, cpu in enumerate(cpu_times):
for key, value in cpu._asdict().iteritems():
self.rate(
"system.core.{0}".format(key),
100.0 * value,
"system.core.{0}".format(key),
100.0 * value,
tags=["core:{0}".format(i)]
)
)
62 changes: 62 additions & 0 deletions tests/test_system_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import mock
import psutil

from tests.common import AgentCheckTest
from util import Platform

if Platform.is_mac():
CHECK_RATES = [
'system.core.idle',
'system.core.nice',
'system.core.system',
'system.core.user',
]
MOCK_PSUTIL_CPU_TIMES = [
psutil._psosx.scputimes(user=7877.29, nice=0.0, system=7469.72, idle=38164.81),
psutil._psosx.scputimes(user=3826.74, nice=0.0, system=2701.6, idle=46981.39),
psutil._psosx.scputimes(user=7486.51, nice=0.0, system=5991.36, idle=40031.88),
psutil._psosx.scputimes(user=3964.85, nice=0.0, system=2862.37, idle=46682.5)
]
else:
CHECK_RATES = [
'system.core.idle',
'system.core.nice',
'system.core.system',
'system.core.user',
'system.core.iowait',
'system.core.irq',
'system.core.softirq',
'system.core.steal',
'system.core.guest',
'system.core.guest_nice',
]
MOCK_PSUTIL_CPU_TIMES = [
psutil._pslinux.scputimes(user=1805.64, nice=0.01, system=298.66, idle=14177.28,
iowait=3.23, irq=0.05, softirq=33.28, steal=0.0,
guest=0.0, guest_nice=0.0),
psutil._pslinux.scputimes(user=1724.18, nice=0.04, system=235.61, idle=14381.94,
iowait=3.55, irq=0.0, softirq=6.94, steal=0.0,
guest=0.0, guest_nice=0.0),
psutil._pslinux.scputimes(user=1737.58, nice=0.03, system=230.61, idle=14382.33,
iowait=2.69, irq=0.0, softirq=6.12, steal=0.0,
guest=0.0, guest_nice=0.0),
psutil._pslinux.scputimes(user=1696.18, nice=0.0, system=218.36, idle=14610.06,
iowait=2.43, irq=0.0, softirq=3.8, steal=0.0,
guest=0.0, guest_nice=0.0)
]


class SystemCoreTestCase(AgentCheckTest):

CHECK_NAME = 'system_core'

@mock.patch('psutil.cpu_times', return_value=MOCK_PSUTIL_CPU_TIMES)
def test_system_core(self, mock_cpu_times):
self.run_check_twice({"instances": [{}]})
self.assertMetric('system.core.count', value=4, count=1)

for i in range(4):
for rate in CHECK_RATES:
self.assertMetric(rate, count=1, tags=['core:{0}'.format(i)])

self.coverage_report()
2 changes: 1 addition & 1 deletion util.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def is_darwin(name=None):

@staticmethod
def is_mac(name=None):
Platform.is_darwin(name)
return Platform.is_darwin(name)

@staticmethod
def is_freebsd(name=None):
Expand Down

0 comments on commit e4612a2

Please sign in to comment.