def main(): #get my command-line arguments cmdline = check_es_insert.getArgs( 'Nagios plugin for checking the total number of documents stored in Elasticsearch' ) #make a calculator my_calc = check_es_insert.Calculator(warn=cmdline['warning'], crit=cmdline['critical'], myfile=cmdline['file'], myaddress=cmdline['address'], threshold=cmdline['threshold'], index=cmdline['index']) #get the current number of documents from Elasticsearch (result, time) = my_calc.getCurrent() #if there's an error, exit with UNKNOWN if result == -1: check_es_insert.printer( "Can't get number of documents from Elasticsearch") check_es_insert.exiter(UNKNOWN) else: #otherwise, thown in some nicely formatted text check_es_insert.printer( "Total number of documents in Elasticsearch (index: %s) is %d | 'es_docs'=%d;%d;%d;;" % (cmdline['index'] if cmdline['index'] != '' else 'all', result, result, cmdline['warning'], cmdline['critical'])) #and exit with the code returned by Calculator (text, exitcode) = my_calc.printandexit(result) check_es_insert.exiter(exitcode)
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 printandexit_runner(self, value, expected_code, index='all'): my_calculator = check_es_insert.Calculator(warn=7, crit=20) (text, exit_code) = my_calculator.printandexit(result=value) self.assertEqual( text, "Number of documents inserted per second (index: %s) is %f | 'es_insert'=%f;7;20;;" % (index, value, value)) self.assertEqual(exit_code, expected_code)
def test_calculate_returns_documentspersecond(self): '''Let's see if it can do some math''' my_calculator = check_es_insert.Calculator(warn=5, crit=7) result = my_calculator.calculate(old_value=7, new_value=37, old_time=1338558183.7, new_time=1338558193.7) self.assertEqual(result, 3.0)
def test_getPrevious_shouldReturnWhateverDiskerReturns(self): '''getPrevious should initialize a Disker object, and ask for the previous value, and return it''' #mock a Disker self.mox.StubOutClassWithMocks(check_es_insert, 'Disker') dummy_disker = check_es_insert.Disker(file='/tmp/check_es_insert') #make Disker.getPrevious it return a value dummy_disker.getPrevious().AndReturn((5, 1338558183)) #replay the mock self.mox.ReplayAll() #now let's test my_calculator = check_es_insert.Calculator(warn=1, crit=2) result = my_calculator.getPrevious() self.assertEqual(result, (5, 1338558183))
def test_getPrevious_DiskerRaisesException_returnsminus2(self): '''getCurrent, in case Disker.getPrevious raises an exception, should return -2 as value and 0 as time''' self.mox.StubOutClassWithMocks(check_es_insert, 'Disker') #mock a Disker dummy_disker = check_es_insert.Disker(file='/tmp/check_es_insert') #make it raise an exception dummy_disker.getPrevious().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.getPrevious() self.assertEqual(result, (-2, 0))
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_main_returnsWhateverRunReturns(self): '''We expect this to create a Calculator, then run the it and print the text and exit with the exit code''' m = mox.Mox() #mock the argument parser m.StubOutWithMock(check_es_insert, "getArgs") #expect to return the needed stuff check_es_insert.getArgs( 'Nagios plugin for checking the number of inserts per second in Elasticsearch' ).AndReturn({ 'critical': 3, 'warning': 2, 'address': 'myhost:1234', 'file': '/tmp/bla', 'threshold': 'lt', 'index': 'articles' }) #mock a calculator m.StubOutClassWithMocks(check_es_insert, 'Calculator') #mock a Calculator dummy_calculator = check_es_insert.Calculator(warn=2, crit=3, myfile='/tmp/bla', myaddress='myhost:1234', threshold='lt', index='articles') #make run() return foo and 3 dummy_calculator.run().AndReturn(("foo", 3)) #mock printer() m.StubOutWithMock(check_es_insert, "printer") #expect to print "foo" check_es_insert.printer("foo") #mock exiter() m.StubOutWithMock(check_es_insert, "exiter") #expect to exit with 3 check_es_insert.exiter(3) #replay all the mocks m.ReplayAll() #now test that stuff check_es_insert.main() #verify if the mocks were called m.VerifyAll() #cleanup m.UnsetStubs() m.ResetAll()
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" )
def test_main_doesWhatPrintAndExitSays_inNormalConditions(self): '''If getCurrent returns a positive value, main() should print the text and exit with the code Calculator.printandexit() says''' m = mox.Mox() #mock an argument parser m.StubOutWithMock(check_es_insert, "getArgs") #expect to be called properly and return some command-line options check_es_insert.getArgs(\ 'Nagios plugin for checking the total number of documents stored in Elasticsearch')\ .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla', 'threshold' : 'lt', 'index' : '' }) #mock a Calculator instance m.StubOutClassWithMocks(check_es_insert, 'Calculator') #mock a dummy Calculator dummy_calculator = check_es_insert.Calculator(warn=5, crit=7, myaddress='myhost:1234', myfile='/tmp/bla', threshold='lt', index='') #make getCurrent return -1 dummy_calculator.getCurrent().AndReturn((3, 1338558185.54)) #make printandexit expect to be called with the result and return something dummy_calculator.printandexit(3).AndReturn(("This is really cool", 2)) #mock printer() m.StubOutWithMock(check_es_insert, "printer") #expect to print "Can't get number of documents from Elasticsearch" check_es_insert.printer( "Total number of documents in Elasticsearch (index: %s) is %d | 'es_docs'=%d;%d;%d;;" % ('all', 3, 3, 5, 7)) #mock exiter() m.StubOutWithMock(check_es_insert, "exiter") #expect to exit with 2 check_es_insert.exiter(2) #replay all mocks m.ReplayAll() #now let's test check_es_docs.main() #verify that all mocks were called as expected m.UnsetStubs() m.VerifyAll() #clean up m.ResetAll()
def test_main_returnsUnknown_ifCalculator_returnsMinus1(self): '''If it can't get the current value from ES, print an error message and exit 3''' m = mox.Mox() #mock an argument parser m.StubOutWithMock(check_es_insert, "getArgs") #expect to be called properly and return some command-line options check_es_insert.getArgs(\ 'Nagios plugin for checking the total number of documents stored in Elasticsearch')\ .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla', 'threshold' : 'lt', 'index' : '' }) #mock a Calculator instance m.StubOutClassWithMocks(check_es_insert, 'Calculator') #mock a dummy Calculator dummy_calculator = check_es_insert.Calculator(warn=5, crit=7, myaddress='myhost:1234', myfile='/tmp/bla', threshold='lt', index='') #make getCurrent return -1 dummy_calculator.getCurrent().AndReturn((-1, 1338558185.54)) #mock printer() m.StubOutWithMock(check_es_insert, "printer") #expect to print "Can't get number of documents from Elasticsearch" check_es_insert.printer( "Can't get number of documents from Elasticsearch") #mock exiter() m.StubOutWithMock(check_es_insert, "exiter") #expect to exit with 3 check_es_insert.exiter(3) #replay all mocks m.ReplayAll() #now let's test check_es_docs.main() #verify that all mocks were called as expected m.UnsetStubs() m.VerifyAll() #clean up m.ResetAll()