Esempio n. 1
0
 def testSysArgv(self):
     argv = [ 'rbuild', 'update', ]
     self.mock(main.RbuildMain, 'main', mock.MockObject())
     self.mock(sys, 'argv', argv)
     main.main()
     mainArgs = main.RbuildMain.main._mock.popCall()[0][0]
     assert mainArgs == argv
     self.unmock()
Esempio n. 2
0
    def testExceptions(self):

        def genRaiseExceptionFn(exception):
            def fn(*args, **kw):
                raise exception
            return fn

        def _testException(exception, debugAll=False):
            self.mock(main.RbuildMain, 'getCommand',
                      genRaiseExceptionFn(exception))
            if debugAll:
                return self.captureOutput(main.main,
                                          ['rbuild', 'help', '--debug-all'])
            return self.captureOutput(main.main,
                                      ['rbuild', 'help'])

        # if rMake isn't running, we get rmake.errors.OpenError
        self.logFilter.add()
        rc, _ = _testException(rmakeerrors.OpenError('Error communicating to server at unix:///var/lib/rmake/socket: Connection refused'))
        self.assertEquals(self.logFilter.records, ['error: Error communicating to server at unix:///var/lib/rmake/socket: Connection refused\n\nCould not contact the rMake server.  Perhaps the rMake service is not\nrunning.  To start the rMake service, as root, try running the command:\nservice rmake restart'])
        self.logFilter.clear()

        # other rmake errors are displayed verbatim, as they are designed for
        self.logFilter.add()
        rc, _ = _testException(rmakeerrors.RmakeError('Dazed and Confused'))
        self.assertEquals(self.logFilter.records, ['error: Dazed and Confused'])
        self.logFilter.clear()

        # pipe errors generally mean EOF when writing to less, e.g.
        rc, _ = _testException(IOError(errno.EPIPE, 'Pipe Error'))
        self.assertEquals(rc, 0)
        self.assertRaises(IOError, _testException, 
                                IOError('Other IO Error'))
        self.assertRaises(RuntimeError, _testException, 
                          RuntimeError('Other IO Error'))
        self.logFilter.add()
        rc, _ = _testException(errors.RbuildError('foo'))
        self.assertEquals(self.logFilter.records, ['error: foo'])
        self.assertEquals(rc, 1)
        self.logFilter.remove()

        # test with --debug-all
        rc, _ = _testException(errors.RbuildError('foo'))
        self.assertEquals(rc, 1)
        self.assertRaises(errors.RbuildError, _testException,
                          errors.RbuildError('foo'), debugAll=True)

        self.mock(main.RbuildMain, 'main', lambda *args, **kw: None)
        assert(main.main(['rbuild', 'help']) == 0)
        self.mock(main.RbuildMain, 'main', lambda *args, **kw: 23)
        assert(main.main(['rbuild', 'help']) == 23)
        oldargv = sys.argv
        try:
            sys.argv = ['rbuild', 'help']
            assert(main.main() == 23)
        finally:
            sys.argv = oldargv
Esempio n. 3
0
 def testSysArgv(self):
     argv = [
         'rbuild',
         'update',
     ]
     self.mock(main.RbuildMain, 'main', mock.MockObject())
     self.mock(sys, 'argv', argv)
     main.main()
     mainArgs = main.RbuildMain.main._mock.popCall()[0][0]
     assert mainArgs == argv
     self.unmock()
Esempio n. 4
0
    def testExceptions(self):
        def genRaiseExceptionFn(exception):
            def fn(*args, **kw):
                raise exception

            return fn

        def _testException(exception, debugAll=False):
            self.mock(main.RbuildMain, 'getCommand',
                      genRaiseExceptionFn(exception))
            if debugAll:
                return self.captureOutput(main.main,
                                          ['rbuild', 'help', '--debug-all'])
            return self.captureOutput(main.main, ['rbuild', 'help'])

        # if rMake isn't running, we get rmake.errors.OpenError
        self.logFilter.add()
        rc, _ = _testException(
            rmakeerrors.OpenError(
                'Error communicating to server at unix:///var/lib/rmake/socket: Connection refused'
            ))
        self.assertEquals(self.logFilter.records, [
            'error: Error communicating to server at unix:///var/lib/rmake/socket: Connection refused\n\nCould not contact the rMake server.  Perhaps the rMake service is not\nrunning.  To start the rMake service, as root, try running the command:\nservice rmake restart'
        ])
        self.logFilter.clear()

        # other rmake errors are displayed verbatim, as they are designed for
        self.logFilter.add()
        rc, _ = _testException(rmakeerrors.RmakeError('Dazed and Confused'))
        self.assertEquals(self.logFilter.records,
                          ['error: Dazed and Confused'])
        self.logFilter.clear()

        # robj errors related to authorization and authentication
        self.logFilter.add()
        rc, _ = _testException(errors.UnauthorizedActionError('act'))
        self.assertEquals(self.logFilter.records,
                          ['error: You are not authorized to act'])
        self.logFilter.clear()

        # pipe errors generally mean EOF when writing to less, e.g.
        rc, _ = _testException(IOError(errno.EPIPE, 'Pipe Error'))
        self.assertEquals(rc, 0)
        self.assertRaises(IOError, _testException, IOError('Other IO Error'))
        self.assertRaises(RuntimeError, _testException,
                          RuntimeError('Other IO Error'))
        self.logFilter.add()
        rc, _ = _testException(errors.RbuildError('foo'))
        self.assertEquals(self.logFilter.records, ['error: foo'])
        self.assertEquals(rc, 1)
        self.logFilter.remove()

        # test with --debug-all
        rc, _ = _testException(errors.RbuildError('foo'))
        self.assertEquals(rc, 1)
        self.assertRaises(errors.RbuildError,
                          _testException,
                          errors.RbuildError('foo'),
                          debugAll=True)

        self.mock(main.RbuildMain, 'main', lambda *args, **kw: None)
        assert (main.main(['rbuild', 'help']) == 0)
        self.mock(main.RbuildMain, 'main', lambda *args, **kw: 23)
        assert (main.main(['rbuild', 'help']) == 23)
        oldargv = sys.argv
        try:
            sys.argv = ['rbuild', 'help']
            assert (main.main() == 23)
        finally:
            sys.argv = oldargv