def test_no_logging_if_permission_denied(self): """ If there is no permission to write to the given directory this does not prevent the script from running. """ options = usage.Options() sys = FakeSysModule(argv=[b"mythingie"]) logs = FilePath(self.mktemp()) logs.makedirs() logs.chmod(0) self.addCleanup(logs.chmod, 0o777) class Script(object): ran = False def main(self, *args, **kwargs): self.ran = True return succeed(None) script = Script() from twisted.test.test_task import _FakeReactor fakeReactor = _FakeReactor() runner = FlockerScriptRunner(script, options, reactor=fakeReactor, sys_module=sys) runner.log_directory = logs try: with attempt_effective_uid('nobody', suppress_errors=True): runner.main() except SystemExit: pass self.assertTrue(script.ran)
def test_main_uses_sysargv(self): """ ``FlockerScriptRunner.main`` uses ``self.sys_module.argv``. """ class SpyOptions(usage.Options): def opt_hello(self, value): self.value = value class SpyScript(object): def main(self, reactor, arguments): self.reactor = reactor self.arguments = arguments return succeed(None) options = SpyOptions() script = SpyScript() sys = FakeSysModule(argv=[b"flocker", b"--hello", b"world"]) # XXX: We shouldn't be using this private fake and Twisted probably # shouldn't either. See https://twistedmatrix.com/trac/ticket/6200 and # https://twistedmatrix.com/trac/ticket/7527 from twisted.test.test_task import _FakeReactor fakeReactor = _FakeReactor() runner = FlockerScriptRunner(script, options, reactor=fakeReactor, sys_module=sys, logging=False) self.assertRaises(SystemExit, runner.main) self.assertEqual(b"world", script.arguments.value)
def test_logs_arguments(self): """ ``FlockerScriptRunner.main`` logs ``self.sys_module.argv``. """ options = usage.Options() sys = FakeSysModule(argv=[b"mythingie", b"--version"]) logs = FilePath(self.mktemp()) from twisted.test.test_task import _FakeReactor fakeReactor = _FakeReactor() runner = FlockerScriptRunner(LoggingScript(), options, reactor=fakeReactor, sys_module=sys) runner.log_directory = logs try: runner.main() except SystemExit: pass path = logs.child(b"mythingie-%d.log" % (getpid(),)) self.assertIn(b"--version", path.getContent())
def test_adds_log_observer(self): """ ``FlockerScriptRunner.main`` logs to the given directory using a filename composed of process name and pid. """ options = usage.Options() sys = FakeSysModule(argv=[b"/usr/bin/mythingie"]) logs = FilePath(self.mktemp()) from twisted.test.test_task import _FakeReactor fakeReactor = _FakeReactor() runner = FlockerScriptRunner(LoggingScript(), options, reactor=fakeReactor, sys_module=sys) runner.log_directory = logs try: runner.main() except SystemExit: pass path = logs.child(b"mythingie-%d.log" % (getpid(),)) self.assertIn(b"it's alive", path.getContent())
def test_disabled_logging(self): """ If ``logging`` is set to ``False``, ``FlockerScriptRunner.main`` does not log to ``sys.stdout``. """ class Script(object): def main(self, reactor, arguments): twisted_log.msg(b"hello!") return succeed(None) script = Script() sys = FakeSysModule(argv=[]) # XXX: We shouldn't be using this private fake and Twisted probably # shouldn't either. See https://twistedmatrix.com/trac/ticket/6200 and # https://twistedmatrix.com/trac/ticket/7527 from twisted.test.test_task import _FakeReactor fakeReactor = _FakeReactor() runner = FlockerScriptRunner(script, usage.Options(), reactor=fakeReactor, sys_module=sys, logging=False) self.assertRaises(SystemExit, runner.main) self.assertEqual(sys.stdout.getvalue(), b"")