def test_description_units(self): """Test description and units""" sf = io.StringIO(general_config) database.init(sf) i = database.get_raw_item("ROLL") self.assertEqual(i.description, "Roll Angle") self.assertEqual(i.units, "deg")
def test_missing_description_units(self): """Test missing description and units""" sf = io.StringIO(general_config) database.init(sf) i = database.get_raw_item("DUMMY") self.assertEqual(i.description, '') self.assertEqual(i.units, '')
def test_Minimal_Database_Build(self): """Test minimal database build""" sf = io.StringIO(minimal_config) database.init(sf) l = database.listkeys() l.sort() self.assertEqual(l, minimal_list)
def test_string_datatype(self): """test writing a string to an item""" sf = io.StringIO(general_config) database.init(sf) database.write("DUMMY", "test string") x = database.read("DUMMY") self.assertEqual(x[0], "test string")
def test_quality_bits(self): """Test quality bits""" sf = io.StringIO(general_config) database.init(sf) i = database.get_raw_item("OILP1") database.write("OILP1", 15.4) x = database.read("OILP1") self.assertEqual(x, (15.4, False, False, False, False, False)) i.annunciate = True x = database.read("OILP1") self.assertEqual(x, (15.4, True, False, False, False, False)) i.annunciate = False x = database.read("OILP1") self.assertEqual(x, (15.4, False, False, False, False, False)) i.fail = True x = database.read("OILP1") self.assertEqual(x, (15.4, False, False, False, True, False)) i.fail = False x = database.read("OILP1") self.assertEqual(x, (15.4, False, False, False, False, False)) i.bad = True x = database.read("OILP1") self.assertEqual(x, (15.4, False, False, True, False, False)) i.bad = False x = database.read("OILP1") self.assertEqual(x, (15.4, False, False, False, False, False))
def test_database_callbacks(self): """Test database callback routines""" sf = io.StringIO(general_config) database.init(sf) rval = None def test_cb(key, val, udata): # Use a closure for our callback nonlocal rval rval = (key, val) database.callback_add("test", "PITCH", test_cb, None) database.write("PITCH", -11.4) self.assertEqual(rval, ("PITCH", (-11.4, False, False, False, False, False))) database.write("PITCH", 10.2) self.assertEqual(rval, ("PITCH", (10.2, False, False, False, False, False))) i = database.get_raw_item("PITCH") i.fail = True self.assertEqual(rval, ("PITCH", (10.2, False, False, False, True, False))) i.annunciate = True self.assertEqual(rval, ("PITCH", (10.2, True, False, False, True, False))) i.bad = True self.assertEqual(rval, ("PITCH", (10.2, True, False, True, True, False))) time.sleep(0.250) database.update() # force the update self.assertEqual(rval, ("PITCH", (10.2, True, True, True, True, False)))
def test_aux_data_creation(self): """Test database auxillary data creation""" sf = io.StringIO(general_config) database.init(sf) tests = ["Min", "Max", "0g", "Warn", "Stall"] tests.sort() i = database.get_raw_item("AOA") l = i.get_aux_list() l.sort() self.assertEqual(l, tests)
def test_wrong_datatype(self): """test using wrong datatype for item""" sf = io.StringIO(general_config) database.init(sf) database.write("DUMMY", 1234) x = database.read("DUMMY") self.assertEqual(x[0], "1234") database.write("PITCH", "23.4") x = database.read("PITCH") self.assertEqual(x[0], 23.4)
def test_aux_data_read_write(self): """Test database auxillary data reading and writing""" sf = io.StringIO(general_config) database.init(sf) tests = [("Min", -160.0), ("Max", -130.0), ("0g", 10.0), ("Warn", 23.4), ("Stall", 45.6)] for test in tests: x = database.write("AOA." + test[0], test[1]) x = database.read("AOA." + test[0]) self.assertEqual(x, test[1])
def test_similar_aux_items(self): """it would be easy for a single aux array to be pointed to by different database items.""" sf = io.StringIO(variable_config) database.init(sf) database.write("EGT11.Max", 700) database.write("EGT12.Max", 800) x = database.read("EGT11.Max") y = database.read("EGT12.Max") self.assertNotEqual(y, x)
def setUp(self): sf = io.StringIO(db_config) database.init(sf) cc = yaml.load(config) import fixgw.plugins.compute self.pl = fixgw.plugins.compute.Plugin("compute", cc) self.pl.start() time.sleep(0.1) # Give plugin a chance to get started
def test_database_aux_data_bounds(self): """Test database aux data bounds checking""" sf = io.StringIO(general_config) database.init(sf) tests = [(0.0, 0.0), (-180.0, -180.0), (-180.1, -180.0), (0.0, 0, 0), (180.0, 180.0), (180.1, 180.0)] for test in tests: database.write("AOA.Warn", test[0]) x = database.read("AOA.Warn") self.assertEqual(x, test[1])
def test_timeout_lifetime(self): """Test item timeout lifetime""" sf = io.StringIO(general_config) database.init(sf) database.write("PITCH", -11.4) x = database.read("PITCH") self.assertEqual(x, (-11.4, False, False, False, False, False)) time.sleep(0.250) x = database.read("PITCH") self.assertEqual(x, (-11.4, False, True, False, False, False)) database.write("PITCH", -11.4) x = database.read("PITCH") self.assertEqual(x, (-11.4, False, False, False, False, False))
def test_Variable_Expansion(self): """Test database variable expansion""" sf = io.StringIO(variable_config) database.init(sf) l = database.listkeys() l.sort() self.assertEqual(l, variable_list) for e in range(4): for c in range(6): key = "EGT{}{}".format(e+1,c+1) item = database.get_raw_item(key) s = "Exhaust Gas Temp Engine {}, Cylinder {}".format(e+1,c+1) self.assertEqual(item.description, s)
def setUp(self): sf = io.StringIO(db_config) database.init(sf) nc = yaml.load(netfix_config) import fixgw.plugins.netfix self.pl = fixgw.plugins.netfix.Plugin("netfix", nc) self.pl.start() time.sleep(0.1) # Give plugin a chance to get started # Grab a client socket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.settimeout(0.5) self.sock.connect(("127.0.0.1", 3490))
def setUp(self): sf = io.StringIO(db_config) database.init(sf) nc = yaml.load(netfix_config) import fixgw.plugins.netfix self.pl = fixgw.plugins.netfix.Plugin("netfix", nc) self.pl.start() time.sleep(0.1) # Give plugin a chance to get started # Grab a client socket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.settimeout(1.0) self.sock.connect(("127.0.0.1", 3490))
def test_database_aux_data_bounds(self): """Test database aux data bounds checking""" sf = io.StringIO(general_config) database.init(sf) tests = [(0.0, 0.0), (-180.0, -180.0), (-180.1, -180.0), (0.0, 0,0), (180.0, 180.0), (180.1, 180.0)] for test in tests: database.write("AOA.Warn", test[0]) x = database.read("AOA.Warn") self.assertEqual(x, test[1])
def setUp(self): # Use the default database sf = open("fixgw/config/database.yaml") database.init(sf) import fixgw.plugins.canfix cc = yaml.load(config) self.pl = fixgw.plugins.canfix.Plugin("canfix",cc) self.pl.start() self.interface = cc['interface'] self.channel = cc['channel'] self.node = cc['node'] self.device = cc['device'] self.revision = cc['revision'] self.model = cc['model'] self.bus = can.Bus(self.channel, bustype = self.interface) time.sleep(0.1) # Give plugin a chance to get started
def test_bool_write(self): """test using wrong datatype for item""" sf = io.StringIO(general_config) database.init(sf) # Test actual booleans database.write("BTN1", True) x = database.read("BTN1") self.assertEqual(x[0], True) database.write("BTN1", False) x = database.read("BTN1") self.assertEqual(x[0], False) # Test strings database.write("BTN1", "True") x = database.read("BTN1") self.assertEqual(x[0], True) database.write("BTN1", "False") x = database.read("BTN1") self.assertEqual(x[0], False) database.write("BTN1", "1") x = database.read("BTN1") self.assertEqual(x[0], True) database.write("BTN1", "0") x = database.read("BTN1") self.assertEqual(x[0], False) database.write("BTN1", "Yes") x = database.read("BTN1") self.assertEqual(x[0], True) database.write("BTN1", "No") x = database.read("BTN1") self.assertEqual(x[0], False) # Test integers database.write("BTN1", 1) x = database.read("BTN1") self.assertEqual(x[0], True) database.write("BTN1", 0) x = database.read("BTN1") self.assertEqual(x[0], False)
def main_setup(): global config_path global log # Look for our configuration file in the list of directories for directory in path_options: # store the first match that we find d = directory.format(USER=user_home, PREFIX=prefix_path) if os.path.isfile("{}/{}".format(d, config_filename)): config_path = d break config_file = "{}/{}".format(config_path, config_filename) parser = argparse.ArgumentParser(description='FIX Gateway') parser.add_argument('--debug', '-d', action='store_true', help='Run in debug mode') parser.add_argument('--verbose', '-v', action='store_true', help='Run in verbose mode') parser.add_argument('--daemonize', '-D', action='store_true', help='Run program in the background') parser.add_argument('--config-file', type=argparse.FileType('r'), help='Alternate configuration file') parser.add_argument('--log-config', type=argparse.FileType('r'), help='Alternate logger configuration file') args, unknown_args = parser.parse_known_args() # if we passed in a configuration file on the command line... if args.config_file: cf = args.config_file config_file = cf.name elif config_path is not None: # otherwise use the default cf = open(config_file) else: # If all else fails copy the configuration from the package # to ~/.makerplane/fixgw/config create_config_dir("{USER}/.makerplane/fixgw".format(USER=user_home)) # Reset this stuff like we found it config_file = "{USER}/.makerplane/fixgw/config/{FILE}".format( USER=user_home, FILE=config_filename) cf = open(config_file) config_path = os.path.dirname(cf.name) config = yaml.safe_load(cf) # Either load the config file given as a command line argument or # look in the configuration for the logging object if args.log_config: logging.config.fileConfig(args.log_config) elif 'logging' in config: logging.config.dictConfig(config['logging']) else: logging.basicConfig() log = logging.getLogger("fixgw") if args.verbose: log.setLevel(logging.INFO) if args.debug: log.setLevel(logging.DEBUG) log.info("Starting FIX Gateway") log.info("Configuration Found at {}".format(config_file)) # Open database definition file and send to database initialization try: ddfile = config["database file"].format(CONFIG=config_path) f = open(ddfile, 'r') except: log.critical("Unable to open database definition file - " + ddfile) raise try: database.init(f) except Exception as e: log.error("Database failure, Exiting:" + str(e)) raise if "initialization files" in config and config["initialization files"]: ifiles = config["initialization files"] for fn in ifiles: filename = fn.format(CONFIG=config_path) log.info("Setting Initial Values - {}".format(filename)) try: f = open(filename, 'r') for line in f.readlines(): l = line.strip() if l and l[0] != '#': x = l.split("=") if len(x) >= 2: database.write(x[0].strip(), x[1].strip()) except Exception as e: log.error( "Problem setting initial values from configuration - {0}". format(e)) raise # TODO: Need to do some more thorough error checking here # run through the plugin_list dict and find all the plugins that are # configured to be loaded and load them. for each in config['connections']: if config['connections'][each]['load']: module = config['connections'][each]["module"] try: load_plugin(each, module, config['connections'][each]) except Exception as e: logging.critical("Unable to load module - " + module + ": " + str(e)) if args.debug: raise ss = {"Configuration File": config_file, "Configuration Path": config_path} status.initialize(plugins, ss) if not args.daemonize: signal.signal(signal.SIGINT, sig_int_handler) signal.signal(signal.SIGTERM, sig_int_handler) return args
def main(): global config_path # Look for our configuration file in the list of directories for directory in path_options: # store the first match that we find d = directory.format(USER=user_home, PREFIX=prefix_path) if os.path.isfile("{}/{}".format(d, config_filename)): config_path = d break config_file = "{}/{}".format(config_path, config_filename) parser = argparse.ArgumentParser(description='FIX Gateway') parser.add_argument('--debug', '-d', action='store_true', help='Run in debug mode') parser.add_argument('--verbose', '-v', action='store_true', help='Run in verbose mode') parser.add_argument('--config-file', type=argparse.FileType('r'), help='Alternate configuration file') parser.add_argument('--log-config', type=argparse.FileType('r'), help='Alternate logger configuration file') args, unknown_args = parser.parse_known_args() # if we passed in a configuration file on the command line... if args.config_file: cf = args.config_file config_file = cf.name elif config_path is not None: # otherwise use the default cf = open(config_file) else: # If all else fails copy the configuration from the package # to ~/.makerplane/fixgw/config create_config_dir("{USER}/.makerplane/fixgw".format(USER=user_home)) # Reset this stuff like we found it config_file = "{USER}/.makerplane/fixgw/config/{FILE}".format(USER=user_home, FILE=config_filename) cf = open(config_file) config_path = os.path.dirname(cf.name) config = yaml.safe_load(cf) # Either load the config file given as a command line argument or # look in the configuration for the logging object if args.log_config: logging.config.fileConfig(args.log_config) elif 'logging' in config: logging.config.dictConfig(config['logging']) else: logging.basicConfig() log = logging.getLogger() if args.verbose: log.setLevel(logging.INFO) if args.debug: log.setLevel(logging.DEBUG) log.info("Starting FIX Gateway") log.info("Configuration Found at {}".format(config_file)) # Open database definition file and send to database initialization try: ddfile = config["database file"].format(CONFIG=config_path) f = open(ddfile,'r') except: log.critical("Unable to open database definition file - " + ddfile) raise try: database.init(f) except Exception as e: log.error("Database failure, Exiting:" + str(e)) raise if "initialization files" in config and config["initialization files"]: log.info("Setting Initial Values") ifiles = config["initialization files"] for fn in ifiles: try: f = open(fn.format(CONFIG=config_path), 'r') for line in f.readlines(): l = line.strip() if l and l[0] != '#': x = l.split("=") if len(x) >= 2: database.write(x[0].strip(), x[1].strip()) except Exception as e: log.error("Problem setting initial values from configuration - {0}".format(e)) raise # TODO: Need to do some more thorough error checking here # run through the plugin_list dict and find all the plugins that are # configured to be loaded and load them. for each in config['connections']: if config['connections'][each]['load']: module = config['connections'][each]["module"] try: load_plugin(each, module, config['connections'][each]) except Exception as e: logging.critical("Unable to load module - " + module + ": " + str(e)) if args.debug: raise ss = {"Configuration File": config_file, "Configuration Path": config_path} status.initialize(plugins, ss) def sig_int_handler(signum, frame): plugin.jobQueue.put("QUIT") signal.signal(signal.SIGINT, sig_int_handler) signal.signal(signal.SIGTERM, sig_int_handler) for each in plugins: log.debug("Attempting to start plugin {0}".format(each)) try: plugins[each].start() except Exception as e: log.error("Problem Starting Plugin: {} - {}".format(each,e)) if args.debug: raise e # If we have debuggin set we'll raise this exception iteration = 0 while True: try: job = plugin.jobQueue.get(timeout=1.0) if job == "QUIT": break except KeyboardInterrupt: # This should be broken by the signal handler log.info("Termination from keybaord received") break except queue.Empty: pass iteration += 1 # Every four times through the loop we do some stuff if iteration % 4 == 0: # check how many plugins are running and exit if zero running_count = 0 for each in plugins: if plugins[each].is_running(): running_count += 1 if running_count == 0: log.info("No plugins running, quitting") break cleanstop = True for each in plugins: log.debug("Attempting to stop plugin {0}".format(each)) try: plugins[each].shutdown() except plugin.PluginFail: log.warning("Plugin {0} did not shutdown properly".format(each)) cleanstop = False if cleanstop == True: log.info("FIX Gateway Exiting Normally") else: log.info("FIX Gateway Exiting Forcefully") os._exit(-1)
def main(): global config_path # Look for our configuration file in the list of directories for directory in path_options: # store the first match that we find d = directory.format(USER=user_home, PREFIX=prefix_path) if os.path.isfile("{}/{}".format(d, config_filename)): config_path = d break config_file = "{}/{}".format(config_path, config_filename) parser = argparse.ArgumentParser(description='FIX Gateway') parser.add_argument('--debug', '-d', action='store_true', help='Run in debug mode') parser.add_argument('--verbose', '-v', action='store_true', help='Run in verbose mode') parser.add_argument('--config-file', type=argparse.FileType('r'), help='Alternate configuration file') parser.add_argument('--log-config', type=argparse.FileType('r'), help='Alternate logger configuration file') args, unknown_args = parser.parse_known_args() # if we passed in a configuration file on the command line... if args.config_file: cf = args.config_file config_file = cf.name elif config_path is not None: # otherwise use the default cf = open(config_file) else: # If all else fails copy the configuration from the package # to ~/.makerplane/fixgw/config create_config_dir("{USER}/.makerplane/fixgw".format(USER=user_home)) # Reset this stuff like we found it config_file = "{USER}/.makerplane/fixgw/config/{FILE}".format(USER=user_home, FILE=config_filename) cf = open(config_file) config_path = os.path.dirname(cf.name) config = yaml.safe_load(cf) # Either load the config file given as a command line argument or # look in the configuration for the logging object if args.log_config: logging.config.fileConfig(args.log_config) elif 'logging' in config: logging.config.dictConfig(config['logging']) else: logging.basicConfig() log = logging.getLogger() if args.verbose: log.setLevel(logging.INFO) if args.debug: log.setLevel(logging.DEBUG) log.info("Starting FIX Gateway") log.info("Configuration Found at {}".format(config_file)) # Open database definition file and send to database initialization try: ddfile = config["database file"].format(CONFIG=config_path) f = open(ddfile,'r') except: log.critical("Unable to open database definition file - " + ddfile) raise try: database.init(f) except Exception as e: log.error("Database failure, Exiting:" + str(e)) raise if "initialization files" in config and config["initialization files"]: log.info("Setting Initial Values") ifiles = config["initialization files"] for fn in ifiles: try: f = open(fn.format(CONFIG=config_path), 'r') for line in f.readlines(): l = line.strip() if l and l[0] != '#': x = l.split("=") if len(x) >= 2: database.write(x[0].strip(), x[1].strip()) except Exception as e: log.error("Problem setting initial values from configuration - {0}".format(e)) raise # TODO: Need to do some more thorough error checking here # run through the plugin_list dict and find all the plugins that are # configured to be loaded and load them. for each in config['connections']: if config['connections'][each]['load']: module = config['connections'][each]["module"] try: load_plugin(each, module, config['connections'][each]) except Exception as e: logging.critical("Unable to load module - " + module + ": " + str(e)) if args.debug: raise ss = {"Configuration File": config_file, "Configuration Path": config_path} status.initialize(plugins, ss) def sig_int_handler(signum, frame): plugin.jobQueue.put("QUIT") signal.signal(signal.SIGINT, sig_int_handler) signal.signal(signal.SIGTERM, sig_int_handler) for each in plugins: log.debug("Attempting to start plugin {0}".format(each)) try: plugins[each].start() except Exception as e: if args.debug: raise e # If we have debuggin set we'll raise this exception iteration = 0 while True: try: job = plugin.jobQueue.get(timeout=1.0) if job == "QUIT": break except KeyboardInterrupt: # This should be broken by the signal handler log.info("Termination from keybaord received") break except queue.Empty: pass iteration += 1 # Every four times through the loop we do some stuff if iteration % 4 == 0: # check how many plugins are running and exit if zero running_count = 0 for each in plugins: if plugins[each].is_running(): running_count += 1 if running_count == 0: log.info("No plugins running, quitting") break cleanstop = True for each in plugins: log.debug("Attempting to stop plugin {0}".format(each)) try: plugins[each].shutdown() except plugin.PluginFail: log.warning("Plugin {0} did not shutdown properly".format(each)) cleanstop = False if cleanstop == True: log.info("FIX Gateway Exiting Normally") else: log.info("FIX Gateway Exiting Forcefully") os._exit(-1)