Пример #1
0
 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()
Пример #2
0
 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()
Пример #3
0
 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()
Пример #4
0
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)
Пример #5
0
 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()
Пример #6
0
 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()
Пример #7
0
 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()
Пример #8
0
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'])
    #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 is %d | 'es_docs'=%d;%d;%d;;" % (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)