def __parse_config_file(): """ Helper method that sets up the ConfigurationParser. """ config = ConfigurationParser() fil = open("xmppmoterc", "a+") config.parse(fil)
def __setup_parser(self): mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" config = ConfigurationParser() config.parse(mock_file)
def __get_pidfile(): """ Helper method that reads an (optionally) configured pidfile from the configuration file. """ config = ConfigurationParser() try: pidfile = config.get("general", "pidfile") except (NoOptionError, NoSectionError): pidfile = None if not pidfile: pidfile = "/tmp/xmppmote.pid" return pidfile
def test_getting_existing_commandhandlers(self): """ If any of the two known command handlers are configured, an instance of the named command handler should be returned by get_command_handler """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" self.mox.StubOutWithMock(SafeConfigParser, "get") config = ConfigurationParser() config.parse(mock_file) # case this one wierdly just to make sure that character casing is taken # into consideration when parsing the string.. config.get("general", "handler").AndReturn("rEstrIctEd") config.get("general", "handler").AndReturn("pAssthrU") self.mox.ReplayAll() expected_type = commandhandlers.RestrictedCommandHandler() self.assertEquals(type(get_command_handler()), type(expected_type)) expected_type = commandhandlers.UnsafeCommandHandler() self.assertEquals(type(get_command_handler()), type(expected_type))
def test_getting_defined_restricted_set(self): """ Make sure that properly formed commands are parsed into a list of command tuples. """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" self.mox.StubOutWithMock(SafeConfigParser, "has_section") self.mox.StubOutWithMock(SafeConfigParser, "items") config = ConfigurationParser() config.parse(mock_file) config.has_section("commands").AndReturn(True) config.items("commands").AndReturn([ ("foo", "ls::List files"), ("bar", "df:-h:Disk space usage (human readable)"), ("baz", "du:-sh .:"), ("foz", "pwd") ]) self.mox.ReplayAll() self.assertEquals(restricted_set(), [ ("ls", None, "List files"), ("df", ["-h"], "Disk space usage (human readable)"), ("du", ["-sh ."], ""), ("pwd", None, "") ])
def test_getting_nonexisting_commandhandler(self): """ If the command handler returned by the configuration is unknown to get_command_handler, an UnknownHandler exception should be raised. """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" self.mox.StubOutWithMock(SafeConfigParser, "get") config = ConfigurationParser() config.parse(mock_file) config.get("general", "handler").AndReturn("foobar") self.mox.ReplayAll() self.assertRaises(UnknownHandler, get_command_handler)
def test_restricted_set_missing_section(self): """ If there is no commands section in the configuration file, an empty list should be returned. """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" self.mox.StubOutWithMock(SafeConfigParser, "has_section") self.mox.StubOutWithMock(SafeConfigParser, "items") config = ConfigurationParser() config.parse(mock_file) config.has_section("commands").AndReturn(False) self.mox.ReplayAll() self.assertEquals(restricted_set(), [])
def test_parse_config_file(self): """ Test parsing the configuration, when it exists. """ mock_file = self.mox.CreateMockAnything() mock_file.name = "xmppmoterc" self.mox.StubOutWithMock(__builtin__, "open") self.mox.StubOutWithMock(ConfigurationParser, "parse") self.mox.StubOutWithMock(xmppmoted.XMPPMoteDaemon, "_XMPPMoteDaemon__get_pidfile") self.mox.StubOutWithMock(Daemon, "__init__") config = ConfigurationParser() open("xmppmoterc", "a+").AndReturn(mock_file) config.parse(mock_file) xmppmoted.XMPPMoteDaemon._XMPPMoteDaemon__get_pidfile().AndReturn(None) Daemon.__init__(mox.IgnoreArg(), None) self.mox.ReplayAll() daemon = xmppmoted.XMPPMoteDaemon()
def __init__(self): self.__command = None self.__interval = None self.__previous_result = None parser = ConfigurationParser() logger = logging.getLogger() if parser.has_section("status"): try: logger.info("reading configured status command") self.__command = parser.get("status", "command") self.__interval = int(parser.get("status", "interval")) logger.info("executing '%s' every %d sec" % (self.__command, self.__interval)) except NoOptionError: pass
def test_getting_malformed_restricted_set(self): """ If there is a malformed command defined in the commands section, a MalformedCommand should be raised. """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" self.mox.StubOutWithMock(SafeConfigParser, "has_section") self.mox.StubOutWithMock(SafeConfigParser, "items") config = ConfigurationParser() config.parse(mock_file) config.has_section("commands").AndReturn(True) config.items("commands").AndReturn([("foo", "")]) self.mox.ReplayAll() self.assertRaises(MalformedCommand, restricted_set)
def test_getting_commandhandler_undefined_in_config(self): """ If either the section or the option that details the command handler is missing, an UnknownHandler exception should be raised. """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" self.mox.StubOutWithMock(SafeConfigParser, "get") config = ConfigurationParser() config.parse(mock_file) config.get("general", "handler").AndRaise(NoSectionError("general")) config.get("general", "handler").AndRaise(NoOptionError("general", "handler")) self.mox.ReplayAll() self.assertRaises(UnknownHandler, get_command_handler) self.assertRaises(UnknownHandler, get_command_handler)