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_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_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_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_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.")