import unittest
import logging

from util import get_hostname
from tests.common import load_check
from nose.plugins.attrib import attr
logging.basicConfig()

@attr('process')
class ProcessTestCase(unittest.TestCase):
    offset = 0
    nb_procs = [0, 1, 3, 5, 6]
    def build_config(self, config):
        # 6 possible configurations:
        # C--W--W--C
        # W--C--C--W
        # W--C--W--C
        # C--W--C--W
        # C--C--W--W
        # W--W--C--C
        # 1--2--4--5
        # There is some redundancy but every cases should be tested
        critical_low = [2, 1, 2, 1, 1, 4]
        critical_high = [4, 5, 5, 4, 2, 5]
        warning_low = [1, 2, 1, 2, 4, 1]
        warning_high = [5, 4, 4, 5, 5, 2]

        for i in range(6):
            name = 'test' + str(i)
            config['instances'].append({
                'name': name,
                'search_string': ['test'],
                'thresholds': {
                    'critical': [critical_low[i], critical_high[i]],
                    'warning': [warning_low[i], warning_high[i]]
                }
            })

        # Adding two cases where there is no configuration
        config['instances'].append({
            'name': 'testnothresholds',
            'search_string': ['test']
        })
        config['instances'].append({
            'name': 'testnoranges',
            'search_string': ['test'],
            'thresholds': {
                "test": "test"
            }
        })
        return config

    def find_pids(self, search_string, exact_match=True):
        x = self.nb_procs[self.offset]
        ret = []
        for i in range(x):
            ret.append(0)
        return ret

    def testCheck(self):
        config = {
            'init_config': {},
            'instances': []
        }
        self.agentConfig = {
            'version': '0.1',
            'api_key': 'toto'
        }

        self.check = load_check('process', config, self.agentConfig)

        config = self.build_config(config)
        self.check.find_pids = self.find_pids

        for i in self.nb_procs:
            for j in range(len(config['instances'])):
                self.check.check(config['instances'][j])

            self.offset += 1

        service_checks = self.check.get_service_checks()

        assert service_checks

        self.assertTrue(type(service_checks) == type([]))
        self.assertTrue(len(service_checks) > 0)
        self.assertEquals(len([t for t in service_checks
            if t['status']== 0]), 12, service_checks)
        self.assertEquals(len([t for t in service_checks
            if t['status']== 1]), 6, service_checks)
        self.assertEquals(len([t for t in service_checks
            if t['status']== 2]), 22, service_checks)

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

@@ -13,23 +13,38 @@
 est%0A
-import logging%0A
+%0Afrom checks import AgentCheck
 %0Afro
@@ -146,30 +146,8 @@
 attr
-%0Alogging.basicConfig()
 %0A%0A@a
@@ -199,24 +199,25 @@
 .TestCase):%0A
+%0A
     offset =
@@ -250,16 +250,17 @@
 , 5, 6%5D%0A
+%0A
     def 
@@ -1687,17 +1687,18 @@
 def test
-C
+_c
 heck(sel
@@ -2752,16 +2752,1260 @@
 hecks)%0A%0A
+    def test_check_real_process(self):%0A        %22Check that we detect python running (at least this process)%22%0A        config = %7B%0A            'instances': %5B%7B%22name%22: %22py%22,%0A                           %22search_string%22: %5B%22python%22%5D,%0A                           %22ignored_denied_access%22: True,%0A                           %22thresholds%22: %7B%22warning%22: %5B1, 10%5D, %22critical%22: %5B1, 100%5D%7D,%0A                       %7D%5D%0A        %7D%0A        %0A        self.agentConfig = %7B%0A            'version': '0.1',%0A            'api_key': 'toto'%0A        %7D%0A%0A        self.check = load_check('process', config, self.agentConfig)%0A        self.check.check(config%5B'instances'%5D%5B0%5D)%0A        python_metrics = self.check.get_metrics()%0A        service_checks = self.check.get_service_checks()%0A        assert service_checks%0A        self.assertTrue(len(python_metrics) %3E 0)%0A        # system.process.number %3E= 1%0A        self.assertTrue(%5Bm%5B2%5D for m in python_metrics if m%5B0%5D == %22system.process.number%22%5D %3E= 1)%0A        self.assertTrue(len(%5Bt for t in service_checks if t%5B'status'%5D== AgentCheck.OK%5D) %3E 0)%0A        self.assertEquals(len(%5Bt for t in service_checks if t%5B'status'%5D== AgentCheck.WARNING%5D),  0)%0A        self.assertEquals(len(%5Bt for t in service_checks if t%5B'status'%5D== AgentCheck.CRITICAL%5D), 0)%0A%0A
 if __nam

Add a test for testing this check in a somewhat reliable way