def simple_failover(request, get_volttron_instances): global simple_primary_config global simple_secondary_config global uuid_primary global uuid_secondary global listener_primary primary, secondary = get_volttron_instances(2) if primary.messagebus != 'zmq': pytest.skip("Failover only valid for zmq instances.") return primary.allow_all_connections() secondary.allow_all_connections() # configure primary listener_primary = add_listener(primary, start=True, vip_identity="listener") # primary.install_agent(agent_dir=get_examples("ListenerAgent"), # vip_identity="listener", # start=False) simple_primary_config["remote_vip"] = secondary.vip_address simple_primary_config["remote_serverkey"] = secondary.serverkey uuid_primary = primary.install_agent(agent_dir=get_ops("FailoverAgent"), config_file=simple_primary_config) # configure secondary listener_secondary = add_listener(secondary, start=False, vip_identity="listener") # listener_secondary = secondary.install_agent(agent_dir=get_examples("ListenerAgent"), # vip_identity="listener", # start=False) simple_secondary_config["remote_vip"] = primary.vip_address simple_secondary_config["remote_serverkey"] = primary.serverkey uuid_secondary = secondary.install_agent( agent_dir=get_ops("FailoverAgent"), config_file=simple_secondary_config) gevent.sleep(SLEEP_TIME) assert all_agents_running(primary) assert not all_agents_running(secondary) def cleanup(): primary.stop_agent(uuid_primary) primary.stop_agent(listener_primary) primary.shutdown_platform() secondary.stop_agent(uuid_secondary) secondary.stop_agent(listener_secondary) secondary.shutdown_platform() request.addfinalizer(cleanup) return primary, secondary
def agent(request, volttron_instance): global db_connection, agent_version, db_path, alert_uuid assert os.path.exists(get_ops("TopicWatcher")) alert_uuid = volttron_instance.install_agent( agent_dir=get_ops("TopicWatcher"), config_file=WATCHER_CONFIG, vip_identity=PLATFORM_TOPIC_WATCHER) gevent.sleep(2) if volttron_instance.secure_agent_users: db_path = os.path.join( volttron_instance.volttron_home, 'agents', alert_uuid, 'topic_watcheragent-' + agent_version, 'topic-watcheragent-' + agent_version + '.agent-data', 'alert_log.sqlite') else: # agent keeps the same path in insecure mode for backward compatibility db_path = os.path.join(volttron_instance.volttron_home, 'agents', alert_uuid, 'topic_watcheragent-' + agent_version, 'alert_log.sqlite') print("DB PATH: {}".format(db_path)) db_connection = sqlite3.connect(db_path, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) agent = volttron_instance.build_agent() def onmessage(peer, sender, bus, topic, headers, message): global alert_messages alert = jsonapi.loads(message)["context"] try: alert_messages[alert] += 1 except KeyError: alert_messages[alert] = 1 print("In on message: {}".format(alert_messages)) agent.vip.pubsub.subscribe(peer='pubsub', prefix='alerts', callback=onmessage) def stop(): volttron_instance.stop_agent(alert_uuid) agent.core.stop() db_connection.close() request.addfinalizer(stop) return agent
def test_default_config(volttron_instance, publish_agent): """ Test the default configuration file included with the agent """ config_path = os.path.join(get_ops("FileWatchPublisher"), "filewatchpublisher.config") with open(config_path, "r") as config_file: config_json = json.load(config_file) assert isinstance(config_json, dict) watcher_uuid = volttron_instance.install_agent( agent_dir=get_ops("FileWatchPublisher"), config_file=config_json, start=True, vip_identity="health_test") assert publish_agent.vip.rpc.call("health_test", "health.get_status").get(timeout=10).get('status') == STATUS_GOOD volttron_instance.remove_agent(watcher_uuid)
def test_file_watcher(volttron_instance, publish_agent): test_path = os.path.join(get_home(), "test.txt") with open(test_path, "w") as textfile: textfile.write("test_data") test_config = { "files": [ { "file": test_path, "topic": "platform/test_topic" } ] } watcher_uuid = volttron_instance.install_agent( agent_dir=get_ops("FileWatchPublisher"), config_file=test_config, start=True, vip_identity="health_test") with open(test_path, "w+") as textfile: textfile.write("more test_data") gevent.sleep(1) assert publish_agent.callback.call_count == 1 print(publish_agent.callback.call_args) volttron_instance.remove_agent(watcher_uuid) os.remove(test_path)
def add_thresholddetection(wrapper, config, **kwargs): agent_uuid = wrapper.install_agent( config_file=config, agent_dir=get_ops("ThresholdDetectionAgent"), **kwargs ) return agent_uuid
def add_emailer(wrapper, config, **kwargs): agent_uuid = wrapper.install_agent( config_file=config, agent_dir=get_ops("EmailerAgent"), **kwargs ) return agent_uuid
def add_sysmon(wrapper, config, **kwargs): agent_uuid = wrapper.install_agent( config_file=config, agent_dir=get_ops("SysMonAgent"), **kwargs ) return agent_uuid
def threshold_tester_agent(volttron_instance): """ Fixture used for setting up ThresholdDetectionAgent and tester agents """ print("ADDRESS IS: ", volttron_instance.vip_address) threshold_detection_uuid = volttron_instance.install_agent( agent_dir=get_ops("ThresholdDetectionAgent"), config_file=_default_config, start=True) agent = volttron_instance.build_agent(agent_class=AlertWatcher, identity="alert_watcher", enable_store=True) agent.reset_store() # agent.vip.rpc.call(CONFIGURATION_STORE, # 'manage_store', # 'platform.thresholddetection', # 'config', # json.dumps(_test_config), # 'json').get() yield agent agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_delete_store', 'platform.thresholddetection').get() volttron_instance.remove_agent(threshold_detection_uuid)
def threshold_tester_agent(request, volttron_instance): """ Fixture used for setting up ThresholdDetectionAgent and tester agents """ threshold_detection_uuid = volttron_instance.install_agent( agent_dir=get_ops("ThresholdDetectionAgent"), config_file=_default_config, start=True) agent = volttron_instance.build_agent(agent_class=AlertWatcher, enable_store=False) agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_store', 'platform.thresholddetection', 'config', json.dumps(_test_config), 'json').get(timeout=10) def stop_agent(): volttron_instance.stop_agent(threshold_detection_uuid) volttron_instance.remove_agent(threshold_detection_uuid) agent.core.stop() request.addfinalizer(stop_agent) return agent
def agent(request, volttron_instance1): alert_uuid = volttron_instance1.install_agent( agent_dir=get_ops("AlertAgent"), config_file=ALERT_CONFIG) gevent.sleep(2) agent = volttron_instance1.build_agent() def onmessage(peer, sender, bus, topic, headers, message): global alert_messages alert = json.loads(message)["context"] try: alert_messages[alert] += 1 except KeyError: alert_messages[alert] = 1 agent.vip.pubsub.subscribe(peer='pubsub', prefix='alert', callback=onmessage) def stop(): volttron_instance1.stop_agent(alert_uuid) agent.core.stop() request.addfinalizer(stop) return agent
def test_remote_alert_publish(get_volttron_instances): """ Test alert to remote agent with 2 ZMQ instances :return: """ volttron_instance1, volttron_instance2 = get_volttron_instances(2) volttron_instance1.allow_all_connections() volttron_instance2.allow_all_connections() gevent.sleep(3) agent = volttron_instance1.build_agent() def onmessage(peer, sender, bus, topic, headers, message): global alert_messages alert = jsonapi.loads(message)["context"] try: alert_messages[alert] += 1 except KeyError: alert_messages[alert] = 1 print("In on message: {}".format(alert_messages)) agent.vip.pubsub.subscribe(peer='pubsub', prefix='alerts', callback=onmessage) config = { "group1": { "fakedevice": 5, "fakedevice2/all": { "seconds": 5, "points": ["point"] } }, "publish-settings": { "publish-local": False, "publish-remote": True, "remote": { "identity": "remote-agent", "serverkey": volttron_instance1.serverkey, "vip-address": volttron_instance1.vip_address } } } alert_uuid = volttron_instance2.install_agent( agent_dir=get_ops("TopicWatcher"), config_file=config, vip_identity=PLATFORM_TOPIC_WATCHER ) gevent.sleep(6) assert alert_messages alert_messages.clear()
def agent(request, volttron_instance1): global db_connection, agent_version, db_path, alert_uuid assert os.path.exists(get_ops("TopicWatcher")) alert_uuid = volttron_instance1.install_agent( agent_dir=get_ops("TopicWatcher"), config_file=WATCHER_CONFIG, vip_identity=PLATFORM_TOPIC_WATCHER ) gevent.sleep(2) db_path = os.path.join(volttron_instance1.volttron_home, 'agents', alert_uuid, 'topic_watcheragent-' + agent_version, 'topic-watcheragent-' + agent_version + '.agent-data', 'alert_log.sqlite') print ("DB PATH: {}".format(db_path)) db_connection = sqlite3.connect( db_path, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) agent = volttron_instance1.build_agent() def onmessage(peer, sender, bus, topic, headers, message): global alert_messages alert = json.loads(message)["context"] try: alert_messages[alert] += 1 except KeyError: alert_messages[alert] = 1 print("In on message: {}".format(alert_messages)) agent.vip.pubsub.subscribe(peer='pubsub', prefix='alert', callback=onmessage) def stop(): volttron_instance1.stop_agent(alert_uuid) agent.core.stop() db_connection.close() request.addfinalizer(stop) return agent
def test_default_config(platform): """ Test the default configuration file included with the agent """ publish_agent = platform.build_agent(identity="test_agent") gevent.sleep(1) config_path = os.path.join(get_ops("AgentWatcher"), "config") with open(config_path, "r") as config_file: config_json = json.load(config_file) assert isinstance(config_json, dict) assert 'watchlist' in config_json and 'check-period' in config_json assert isinstance( config_json.get('watchlist'), list) and (isinstance(config_json.get('check-period'), int) or isinstance(config_json.get('check-period'), float)) if len(config_json.get('watchlist')) > 0: for watch in config_json.get('watchlist'): assert isinstance(watch, str) platform.install_agent(agent_dir=get_ops("AgentWatcher"), config_file=config_json, start=True, vip_identity="health_test") gevent.sleep(2) if len(config_json.get('watchlist')) > 0: assert f"Agent(s) expected but but not running {config_json.get('watchlist')}" in alert_messages else: assert not alert_messages assert publish_agent.vip.rpc.call( "health_test", "health.get_status").get(timeout=10).get('status') == STATUS_GOOD publish_agent.core.stop() gevent.sleep(2) assert alert_messages
def agent(request, volttron_instance_msgdebug): master_uuid = volttron_instance_msgdebug.install_agent( agent_dir=get_ops("MessageDebuggerAgent"), config_file=DEBUGGER_CONFIG, start=True) gevent.sleep(2) msg_debugger_agent = volttron_instance_msgdebug.build_agent() gevent.sleep(20) # wait for the agent to start def stop(): volttron_instance_msgdebug.stop_agent(master_uuid) msg_debugger_agent.core.stop() request.addfinalizer(stop) return msg_debugger_agent
def test_multiplatform_rpc(request, get_volttron_instances): p1, p2 = get_volttron_instances(2) _default_config = { "test_max": { "threshold_max": 10 } } threshold_detection_uuid = p1.install_agent( agent_dir=get_ops("ThresholdDetectionAgent"), config_file=_default_config, start=True) updated_config = { "updated_topic": { "threshold_max": 10, "threshold_min": 2, } } test_agent = p2.build_agent() kwargs = {"external_platform": 'platform1'} test_agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_store', 'platform.thresholddetection', 'config', json.dumps(updated_config), 'json', **kwargs).get(timeout=10) config = test_agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_get', 'platform.thresholddetection', 'config', raw=True, **kwargs).get(timeout=10) config = json.loads(config) try: assert config == updated_config except KeyError: pytest.fail("Expecting config change : {}".format(config)) def stop(): p1.stop_agent(threshold_detection_uuid) p2.remove_agent(threshold_detection_uuid) p1.shutdown_platform() test_agent.core.stop() p1.shutdown_platform() request.addfinalizer(stop)
def sysmon_tester_agent(request, volttron_instance, tmpdir): """ Fixture used for setting up SysMonAgent and tester agent """ config = tmpdir.mkdir('config').join('config') config.write(jsonapi.dumps(_test_config)) sysmon_uuid = volttron_instance.install_agent( agent_dir=get_ops("SysMonAgent"), config_file=str(config), start=True) agent = volttron_instance.build_agent() def stop_agent(): volttron_instance.stop_agent(sysmon_uuid) volttron_instance.remove_agent(sysmon_uuid) agent.core.stop() request.addfinalizer(stop_agent) return agent
def platform(request, volttron_instance): global listener_uuid listener_uuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), vip_identity="listener", start=True) gevent.sleep(2) watcher_uuid = volttron_instance.install_agent( agent_dir=get_ops("AgentWatcher"), config_file=WATCHER_CONFIG) gevent.sleep(2) agent = volttron_instance.build_agent() def onmessage(peer, sender, bus, topic, headers, message): global alert_messages alert = jsonapi.loads(message)["context"] try: alert_messages[alert] += 1 except KeyError: alert_messages[alert] = 1 agent.vip.pubsub.subscribe(peer='pubsub', prefix='alerts', callback=onmessage) def stop(): volttron_instance.stop_agent(listener_uuid) volttron_instance.stop_agent(watcher_uuid) volttron_instance.remove_agent(listener_uuid) volttron_instance.remove_agent(watcher_uuid) agent.core.stop() alert_messages.clear() request.addfinalizer(stop) return volttron_instance
def test_log_stats(volttron_instance, publish_agent): test_config["file_path"] = volttron_instance.log_path print(f"File path: {test_config['file_path']}") stats_uuid = volttron_instance.install_agent( agent_dir=get_ops("LogStatisticsAgent"), config_file=test_config, start=True, vip_identity="health_test") import gevent gevent.sleep(1) # building another agent should populate the logs volttron_instance.build_agent(identity="log_populate") gevent.sleep(1) assert publish_agent.callback.call_count >= 1 volttron_instance.remove_agent(stats_uuid)
def platform(request, volttron_instance1): global listener_uuid listener_uuid = volttron_instance1.install_agent( agent_dir=get_examples("ListenerAgent"), vip_identity="listener", start=True) gevent.sleep(2) watcher_uuid = volttron_instance1.install_agent( agent_dir=get_ops("AgentWatcher"), config_file=WATCHER_CONFIG) gevent.sleep(2) agent = volttron_instance1.build_agent() def onmessage(peer, sender, bus, topic, headers, message): global alert_messages alert = json.loads(message)["context"] try: alert_messages[alert] += 1 except KeyError: alert_messages[alert] = 1 agent.vip.pubsub.subscribe(peer='pubsub', prefix='alert', callback=onmessage) def stop(): volttron_instance1.stop_agent(listener_uuid) volttron_instance1.stop_agent(watcher_uuid) agent.core.stop() request.addfinalizer(stop) return volttron_instance1
import os import pytest from volttron.platform import jsonapi, get_ops from volttrontesting.utils.utils import poll_gevent_sleep _test_config = { "base_topic": "test1/sysmon", "cpu_check_interval": 1, "memory_check_interval": 1, "disk_check_interval": 1, "disk_path": "/" } config_path = os.path.join(get_ops("SysMonAgent"), "sysmonagent.config") with open(config_path, "r") as config_file: default_config_json = jsonapi.load(config_file) assert isinstance(default_config_json, dict) @pytest.fixture() def sysmon_tester_agent(request, volttron_instance, tmpdir): """ Fixture used for setting up SysMonAgent and tester agent """ config = tmpdir.mkdir('config').join('config') config.write(jsonapi.dumps(_test_config)) sysmon_uuid = volttron_instance.install_agent( agent_dir=get_ops("SysMonAgent"), config_file=_test_config, start=True)
def test_alert_multi_messagebus_publish(volttron_multi_messagebus): """ Test alert to remote agent with multi message bus combinations :return: """ source_instance, destination_instance = volttron_multi_messagebus() destination_instance.allow_all_connections() if destination_instance.messagebus == 'rmq': remote_address = destination_instance.bind_web_address destination_instance.enable_auto_csr() else: remote_address = destination_instance.vip_address gevent.sleep(3) agent = destination_instance.dynamic_agent def onmessage(peer, sender, bus, topic, headers, message): global alert_messages alert = jsonapi.loads(message)["context"] try: alert_messages[alert] += 1 except KeyError: alert_messages[alert] = 1 print("In on message: {}".format(alert_messages)) agent.vip.pubsub.subscribe(peer='pubsub', prefix='alerts', callback=onmessage) config = { "group1": { "fakedevice": 5, "fakedevice2/all": { "seconds": 5, "points": ["point"] } }, "publish-settings": { "publish-local": False, "publish-remote": True, "remote": { "identity": "remote-agent", "serverkey": destination_instance.serverkey, "vip-address": remote_address } } } alert_uuid = source_instance.install_agent( agent_dir=get_ops("TopicWatcher"), config_file=config, vip_identity=PLATFORM_TOPIC_WATCHER ) gevent.sleep(6) assert u"Topic(s) not published within time limit: ['fakedevice', " \ u"'fakedevice2/all', ('fakedevice2/all', 'point')]" in \ alert_messages alert_messages.clear()
def add_sysmon(wrapper, config, **kwargs): agent_uuid = wrapper.install_agent(config_file=config, agent_dir=get_ops("SysMonAgent"), **kwargs) return agent_uuid
def add_emailer(wrapper, config, **kwargs): agent_uuid = wrapper.install_agent(config_file=config, agent_dir=get_ops("EmailerAgent"), **kwargs) return agent_uuid
def add_thresholddetection(wrapper, config, **kwargs): agent_uuid = wrapper.install_agent( config_file=config, agent_dir=get_ops("ThresholdDetectionAgent"), **kwargs) return agent_uuid
def failover(request, get_volttron_instances): global primary_config global secondary_config global primary_failover global secondary_failover global vc_uuid pytest.skip("Coordination with VC not implemted") primary, secondary, vc = get_volttron_instances(3) primary.allow_all_connections() secondary.allow_all_connections() vc.allow_all_connections() # configure vc vc_uuid = vc.install_agent( agent_dir=get_services_core("VolttronCentralPlatform")) # configure primary primary_platform = primary.install_agent( agent_dir=get_services_core("VolttronCentralPlatform")) # register with vc primary_listener = primary.install_agent( agent_dir=get_examples("ListenerAgent"), vip_identity="listener", start=False) primary_config["remote_vip"] = tcp_to(secondary) primary_failover = primary.install_agent( agent_dir=get_ops("FailoverAgent"), config_file=primary_config) # configure secondary secondary_platform = secondary.install_agent( agent_dir=get_services_core("VolttronCentralPlatform")) # register with vc secondary_listener = secondary.install_agent( agent_dir=get_examples("ListenerAgent"), vip_identity="listener", start=False) secondary_config["remote_vip"] = tcp_to(primary) secondary_failover = secondary.install_agent( agent_dir=get_ops("FailoverAgent"), config_file=secondary_config) def stop(): vc.stop_agent(vc_uuid) vc.shutdown_platform() primary.stop_agent(primary_failover) primary.stop_agent(primary_platform) primary.stop_agent(primary_listener) primary.shutdown_platform() secondary.stop_agent(secondary_failover) secondary.stop_agent(secondary_platform) secondary.stop_agent(secondary_listener) secondary.shutdown_platform() request.addfinalizer(stop) return primary, secondary, vc