def test_getCurrent_givesSumOfAllDocuments(self):
        '''This should take the dictionary reply of pyes.ES.status() and pull out a sum of num_docs'''
        #mock pyes.ES.status()
        self.mox.StubOutClassWithMocks(pyes, "ES")
        dummyESconn = pyes.ES(['myeshost:12345'])
        dummyESconn.status().AndReturn({
            "indices": {
                "4534": {
                    "docs": {
                        "num_docs": 500000
                    }
                },
                "4535": {
                    "docs": {
                        "num_docs": 50000
                    }
                }
            }
        })
        self.mox.ReplayAll()

        #now to test
        my_es = check_es_insert.Elasticsearcher('myeshost:12345')
        result = my_es.getCurrent()
        self.assertEqual(result, 550000)
 def test_run_getPreviousReturnsMinus2_shouldReturnUNKNOWNbutWriteCurrentDataIfItCan(
         self):
     '''If old number of docs can't be retrieved, return UNKNOWN and an error message'''
     self.mox.StubOutClassWithMocks(check_es_insert, 'Elasticsearcher')
     #mock an Elasticsearcher
     dummy_elasticsearcher = check_es_insert.Elasticsearcher(
         address='localhost:9200')
     #make it return a value
     dummy_elasticsearcher.getCurrent('').AndReturn(7)
     #mock timer()
     self.mox.StubOutWithMock(check_es_insert, "timer")
     check_es_insert.timer().AndReturn(1338558185.54)
     self.mox.StubOutClassWithMocks(check_es_insert, 'Disker')
     #mock a Disker
     dummy_disker = check_es_insert.Disker(file='/tmp/check_es_insert')
     #make getPrevious raise and exception
     dummy_disker.getPrevious().AndRaise(
         BaseException("Something bad happened"))
     #make writeCurrent() work
     dummy_disker.writeCurrent(7, 1338558185.54)
     #replay all mocks
     self.mox.ReplayAll()
     #now let's test
     my_calculator = check_es_insert.Calculator(warn=2, crit=3)
     result = my_calculator.run()
     self.assertEqual(result[1], 3)
     self.assertEqual(
         result[0],
         "There was an issue getting the previous results from file.")
 def test_run_writeCurrentThrowsException_shouldExitWithUnknown(self):
     '''If it can't write the current results to the file, it should return UNKNOWN to alert the user'''
     self.mox.StubOutClassWithMocks(check_es_insert, 'Elasticsearcher')
     #mock an Elasticsearcher
     dummy_elasticsearcher = check_es_insert.Elasticsearcher(
         address='localhost:9200')
     #make it return a value
     dummy_elasticsearcher.getCurrent('').AndReturn(7)
     #mock timer()
     self.mox.StubOutWithMock(check_es_insert, "timer")
     check_es_insert.timer().AndReturn(1338558185.54)
     self.mox.StubOutClassWithMocks(check_es_insert, 'Disker')
     #mock a Disker
     dummy_disker = check_es_insert.Disker(file='/tmp/check_es_insert')
     #make getPrevious() work
     dummy_disker.getPrevious().AndReturn((5, 1338558183))
     #make writeCurrent() raise an exception
     dummy_disker.writeCurrent(7, 1338558185.54).AndRaise(
         BaseException("Something weird happened"))
     #replay all mocks
     self.mox.ReplayAll()
     #now let's test
     my_calculator = check_es_insert.Calculator(warn=2, crit=3)
     result = my_calculator.run()
     self.assertEqual(result[1], 3)
     self.assertEqual(
         result[0],
         "There was an issue writing the current results to file.")
 def test_run_shoulddoeverything(self):
     '''This method should go through all the methods'''
     self.mox.StubOutClassWithMocks(check_es_insert, 'Elasticsearcher')
     #mock an Elasticsearcher
     dummy_elasticsearcher = check_es_insert.Elasticsearcher(
         address='localhost:9200')
     #make it return a value
     dummy_elasticsearcher.getCurrent('').AndReturn(7)
     #mock timer()
     self.mox.StubOutWithMock(check_es_insert, "timer")
     check_es_insert.timer().AndReturn(1338558185.54)
     self.mox.StubOutClassWithMocks(check_es_insert, 'Disker')
     #mock a Disker
     dummy_disker = check_es_insert.Disker(file='/tmp/check_es_insert')
     #make getPrevious work
     dummy_disker.getPrevious().AndReturn((5, 1338558183))
     #make writeCurrent() work
     dummy_disker.writeCurrent(7, 1338558185.54)
     #replay all mocks
     self.mox.ReplayAll()
     #now let's test
     my_calculator = check_es_insert.Calculator(warn=2, crit=3)
     result = my_calculator.run()
     self.assertEqual(result[1], 0)
     self.assertEqual(
         result[0],
         "Number of documents inserted per second (index: %s) is %f | 'es_insert'=%f;%d;%d;;"
         % ('all', 0.787402, 0.787402, 2, 3))
 def test_getCurrent_ElasticsearcherRaisesException_returnsminus1(self):
     '''getCurrent, in case Elasticsearcher.__init__ raises an exception, should return -1 as value and 0 as time'''
     self.mox.StubOutClassWithMocks(check_es_insert, 'Elasticsearcher')
     #mock an Elasticsearcher
     dummy_elasticsearcher = check_es_insert.Elasticsearcher(
         address='localhost:9200')
     #make it raise an exception
     dummy_elasticsearcher.getCurrent('').AndRaise(
         BaseException("Something nasty happened"))
     #replay the mocks
     self.mox.ReplayAll()
     #now let's test
     my_calculator = check_es_insert.Calculator(warn=1, crit=2)
     result = my_calculator.getCurrent()
     self.assertEqual(result, (-1, 0))
 def test_getCurrent_shouldReturnWhateverElasticsearcherReturns(self):
     '''getCurrent should initialize an Elasticsearcher object, and ask for the current value, and return it, along with the current timestamp'''
     #mock an Elasticsearcher
     self.mox.StubOutClassWithMocks(check_es_insert, 'Elasticsearcher')
     dummy_elasticsearcher = check_es_insert.Elasticsearcher(
         address='localhost:9200')
     #make it return a value
     dummy_elasticsearcher.getCurrent('').AndReturn(7)
     #mock timer()
     self.mox.StubOutWithMock(check_es_insert, "timer")
     check_es_insert.timer().AndReturn(1338558185.54)
     #replay the mocks
     self.mox.ReplayAll()
     #now let's test
     my_calculator = check_es_insert.Calculator(warn=1, crit=2)
     result = my_calculator.getCurrent()
     self.assertEqual(result, (7, 1338558185.54))
 def test_run_getCurrentReturnsMinus1_shouldntWriteCurrentValuesToFile(
         self):
     '''If current number of docs can't be retrieved, return UNKNOWN and an error message. Don't try to write -1 to the old file'''
     self.mox.StubOutClassWithMocks(check_es_insert, 'Elasticsearcher')
     #mock an Elasticsearcher
     dummy_elasticsearcher = check_es_insert.Elasticsearcher(
         address='localhost:9200')
     #make it return a value
     dummy_elasticsearcher.getCurrent('').AndRaise(
         BaseException("Something bad happened"))
     #replay all mocks
     self.mox.ReplayAll()
     #now let's test
     my_calculator = check_es_insert.Calculator(warn=2, crit=3)
     result = my_calculator.run()
     self.assertEqual(result[1], 3)
     self.assertEqual(
         result[0],
         "There was an issue getting the status - and number of docs - from Elasticsearch. Check that ES is running and you have pyes installed"
     )