def test_load_functions(): # Load valid functions file py_mod = load_functions(filepath=funcfile_good) assert py_mod is not None # No-op py_mod = load_functions(filepath=None) assert py_mod is None # Load missing functions file with pytest.raises(IOError) as excinfo: load_functions(filepath="unknown.txt") assert str(excinfo.value) == "'{}' not found".format("unknown.txt") # Load functions file that is not a python file with pytest.raises(ValueError) as excinfo: load_functions(filepath=configfile_full) assert str( excinfo.value ) == "'{}' does not have the .py or .pyc extension".format(configfile_full) # Load bad functions file with pytest.raises(Exception): load_functions(filepath=funcfile_bad)
def __init__(self, configuration_file, defaults=None): defaults = defaults or {} RawConfigParser.__init__(self) f = codecs.open(configuration_file, 'r', encoding='utf-8') self.read_file(f) f.close() ''' set defaults ''' self.hostname = 'localhost' self.port = 1883 self.username = None self.password = None self.clientid = None self.lwt = None self.skipretained = False self.cleansession = False self.protocol = 3 self.logformat = '%(asctime)-15s %(levelname)-8s [%(name)-25s] %(message)s' self.logfile = None self.loglevel = 'DEBUG' self.functions = None self.num_workers = 1 self.directory = '.' self.ca_certs = None self.tls_version = None self.certfile = None self.keyfile = None self.tls_insecure = False self.tls = False self.__dict__.update(defaults) self.__dict__.update(self.config('defaults')) if HAVE_TLS == False: logger.error("TLS parameters set but no TLS available (SSL)") sys.exit(2) if self.ca_certs is not None: self.tls = True if self.tls_version is not None: if self.tls_version == 'tlsv1_2': self.tls_version = ssl.PROTOCOL_TLSv1_2 if self.tls_version == 'tlsv1_1': self.tls_version = ssl.PROTOCOL_TLSv1_1 if self.tls_version == 'tlsv1': self.tls_version = ssl.PROTOCOL_TLSv1 if self.tls_version == 'sslv3': self.tls_version = ssl.PROTOCOL_SSLv3 self.loglevelnumber = self.level2number(self.loglevel) self.functions = load_functions(self.functions)
def test_load_function(): # Load valid functions file py_mod = load_functions(filepath=funcfile) assert py_mod is not None # Load valid function func = load_function(name='foobar', py_mod=py_mod) assert func is not None # Load invalid function, function name does not exist in "funcfile" with pytest.raises(AttributeError) as excinfo: load_function(name='unknown', py_mod=py_mod) assert re.match("Function 'unknown' does not exist in '{}c?'".format(funcfile), str(excinfo.value))
def __init__(self, configuration_file=None, defaults=None): defaults = defaults or {} self.configuration_path = None RawConfigParser.__init__(self) if configuration_file is not None: f = codecs.open(configuration_file, 'r', encoding='utf-8') self.read_file(f) f.close() self.configuration_path = os.path.dirname(configuration_file) ''' set defaults ''' self.hostname = 'localhost' self.port = 1883 self.username = None self.password = None self.clientid = None self.lwt = None self.skipretained = False self.cleansession = False self.protocol = 3 self.logformat = '%(asctime)-15s %(levelname)-8s [%(name)-26s] %(message)s' self.logfile = "stream://sys.stderr" self.loglevel = 'DEBUG' self.functions = None self.num_workers = 1 self.ca_certs = None self.tls_version = None self.certfile = None self.keyfile = None self.tls_insecure = False self.tls = False self.__dict__.update(defaults) self.__dict__.update(self.config('defaults')) if HAVE_TLS == False: logger.error("TLS parameters set but no TLS available (SSL)") sys.exit(2) if self.ca_certs is not None: self.tls = True if self.tls_version is not None: if self.tls_version == 'tlsv1_2': self.tls_version = ssl.PROTOCOL_TLSv1_2 if self.tls_version == 'tlsv1_1': self.tls_version = ssl.PROTOCOL_TLSv1_1 if self.tls_version == 'tlsv1': self.tls_version = ssl.PROTOCOL_TLSv1 if self.tls_version == 'sslv3': self.tls_version = ssl.PROTOCOL_SSLv3 self.loglevelnumber = self.level2number(self.loglevel) if self.functions is not None and self.functions.strip() != "": logger.info("Loading user-defined functions from %s" % self.functions) # Load function file as given (backward-compatibility). if os.path.isfile(self.functions): functions_file = self.functions # Load function file as given if path is absolute. elif os.path.isabs(self.functions): functions_file = self.functions # Load function file relative to path of configuration file if path is relative. else: functions_file = os.path.join(self.configuration_path, self.functions) self.functions = load_functions(functions_file)