def test_heartbeat_sending_status(volttron_instance1): """ Tests the heartbeat message that it has the status. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() agent_prefix = 'heartbeat/Agent' new_agent = volttron_instance1.build_agent(identity='test3') orig_status = Status.from_json(new_agent.vip.health.get_status()) new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) new_agent.vip.heartbeat.start() poll_gevent_sleep( 2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) message = subscription_results[agent_prefix]['message'] headers = subscription_results[agent_prefix]['headers'] d = Status.from_json(message) assert headers[DATE] is not None assert d.last_updated is not None assert orig_status.status == d.status assert orig_status.context == d.context
def test_heartbeat_sending_status(volttron_instance): """ Tests the heartbeat message that it has the status. :param volttron_instance: :return: """ global subscription_results subscription_results.clear() agent_prefix = 'heartbeat/Agent' new_agent = volttron_instance.build_agent(identity='test3') orig_status = new_agent.vip.health.get_status() new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) new_agent.vip.heartbeat.start() poll_gevent_sleep(2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) print subscription_results message = subscription_results[agent_prefix]['message'] headers = subscription_results[agent_prefix]['headers'] d = message assert headers[DATE] is not None assert d["last_updated"] is not None assert orig_status["status"] == d["status"] assert orig_status["context"] == d["context"]
def test_alert_publish(volttron_instance): """ Tests the heartbeat message that it has the status. :param volttron_instance: :return: """ global subscription_results subscription_results.clear() alert_prefix = 'alerts' new_agent = volttron_instance.build_agent(identity='alert1') status = Status.build(BAD_STATUS, "Too many connections!") new_agent.vip.pubsub.subscribe(peer='pubsub', prefix='', callback=onmessage) gevent.sleep(0.3) orig_status = new_agent.vip.health.send_alert("too_many", status) poll_gevent_sleep(2, lambda: messages_contains_prefix(alert_prefix, subscription_results)) print("THE SUBSCRIPTIONS ARE: {}".format(subscription_results)) if not messages_contains_prefix(alert_prefix, subscription_results): pytest.fail('prefix not found') headers = subscription_results['alerts/Agent']['headers'] message = subscription_results['alerts/Agent']['message'] assert "too_many", headers['alert_key'] passed_status = Status.from_json(message) assert status.status == passed_status.status assert status.context == passed_status.context assert status.last_updated == passed_status.last_updated
def test_alert_publish(volttron_instance1): """ Tests the heartbeat message that it has the status. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() alert_prefix = 'alerts' new_agent = volttron_instance1.build_agent(identity='alert1') status = Status.build(BAD_STATUS, "Too many connections!") new_agent.vip.pubsub.subscribe(peer='pubsub', prefix='', callback=onmessage) gevent.sleep(0.3) orig_status = new_agent.vip.health.send_alert("too_many", status) poll_gevent_sleep(2, lambda: messages_contains_prefix(alert_prefix, subscription_results)) print("THE SUBSCRIPTIONS ARE: {}".format(subscription_results)) if not messages_contains_prefix(alert_prefix, subscription_results): pytest.fail('prefix not found') headers = subscription_results['alerts/Agent']['headers'] message = subscription_results['alerts/Agent']['message'] assert "too_many", headers['alert_key'] passed_status = Status.from_json(message) assert status.status == passed_status.status assert status.context == passed_status.context assert status.last_updated == passed_status.last_updated
def test_publish_from_message_handler(volttron_instance): """ Tests the ability to change a status by sending a different status code. This test also tests that the heartbeat is received. :param volttron_instance: :return: """ test_topic = "testtopic1/test" new_agent1 = volttron_instance.build_agent( identity='test_publish1', agent_class=_publish_from_handler_test_agent) new_agent2 = volttron_instance.build_agent(identity='test_publish2') # new_agent1.setup_callback("") new_agent2.vip.pubsub.publish("pubsub", test_topic, headers={}, message="Test message").get() poll_gevent_sleep( 2, lambda: messages_contains_prefix(test_topic, new_agent1. subscription_results)) assert new_agent1.subscription_results[test_topic][ "message"] == "Test message"
def test_above_max(threshold_tester_agent): """Should get alert because values exceed max""" publish(threshold_tester_agent, _test_config, lambda x: x+1) check = lambda: threshold_tester_agent.seen_alert_keys == set(['test_max']) try: assert poll_gevent_sleep(5, check) finally: threshold_tester_agent.clear_keys()
def test_pubsub_not_protected(volttron_instance1_encrypt): '''Tests pubsub without any topic protection ''' agent1, agent2, topic, msgs = build_two_agents_pubsub_agents( volttron_instance1_encrypt) agent2.vip.pubsub.publish('pubsub', topic, message='hello agent').get(timeout=1) assert poll_gevent_sleep( 2, lambda: len(msgs) > 0 and msgs[0] == 'hello agent')
def test_below_min(threshold_tester_agent): """Should get alert because values below min""" publish(threshold_tester_agent, _test_config, lambda x: x - 1, to_max=False) check = lambda: threshold_tester_agent.seen_alert_keys == set(['test_min']) assert poll_gevent_sleep(2, check)
def test_device_publish(threshold_tester_agent): threshold_tester_agent.vip.pubsub.publish('pubsub', 'devices/all', headers={}, message=[{'point': 11}]).get() gevent.sleep(0.5) check = lambda: threshold_tester_agent.seen_alert_keys == set(['devices/all']) try: assert poll_gevent_sleep(5, check) finally: threshold_tester_agent.clear_keys()
def test_remove_from_config_store(threshold_tester_agent): threshold_tester_agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_delete_config', 'platform.thresholddetection', 'config').get() publish(threshold_tester_agent, _test_config, lambda x: x+1) publish(threshold_tester_agent, _test_config, lambda x: x-1, to_max=False) publish(threshold_tester_agent, _default_config, lambda x: x+1) check = lambda: threshold_tester_agent.seen_alert_keys == set(['test_max']) assert poll_gevent_sleep(2, check)
def test_can_set_status(volttron_instance1): """ Tests the ability to change a status by sending a different status code. This test also tests that the heartbeat is received. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() new_agent = volttron_instance1.build_agent(identity='test_status') new_agent.vip.heartbeat.start() orig_status = Status.from_json(new_agent.vip.health.get_status()) assert orig_status.status == STATUS_GOOD assert orig_status.context is None assert orig_status.last_updated is not None print('original status: {}'.format(orig_status.as_json())) new_context = { 'foo': 'A test something when wrong', 'woah': ['blah', 'blah'] } agent_prefix = 'heartbeat/Agent' new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) gevent.sleep(1) new_agent.vip.health.set_status(STATUS_BAD, new_context) poll_gevent_sleep( 2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) new_status = Status.from_json(new_agent.vip.health.get_status()) print('new status: {}'.format(new_status.as_json())) assert new_status.status == STATUS_BAD assert new_status.context == new_context assert new_status.last_updated is not None print("OLD IS: {}".format(orig_status.last_updated)) print("NEW IS: {}".format(new_status.last_updated)) old_date = parse_timestamp_string(orig_status.last_updated) new_date = parse_timestamp_string(new_status.last_updated) assert old_date < new_date
def pubsub_authorized(build_protected_pubsub, topic='foo', regex=None, peer='pubsub'): """Tests pubsub with a protected topic and an agents is authorized to publish to the protected topic. """ setup = build_protected_pubsub(topic, 'can_publish_to_my_topic', regex, add_capabilities=True) agent1 = setup['agent1'] agent2 = setup['agent2'] topic = setup['topic'] msgs = setup['messages'] agent2.vip.pubsub.publish(peer, topic, message='hello agent').get(timeout=2) assert poll_gevent_sleep(2, lambda: 'hello agent' in msgs)
def test_multiplatform_pubsub(request, multi_platform_connection): p1_publisher, p2_listener, p3_listener = multi_platform_connection def callback2(peer, sender, bus, topicdr, headers, message): print message assert message == [{'point': 'value'}] def callback3(peer, sender, bus, topic, headers, message): print message def callback4(peer, sender, bus, topic, headers, message): print message def callback5(peer, sender, bus, topic, headers, message): print message p2_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage, all_platforms=True) gevent.sleep(2) p3_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage) print "publish" prefix = 'devices' for i in range(10): p1_publisher.vip.pubsub.publish(peer='pubsub', topic='devices/campus/building1', message=[{ 'point': 'value' }]) # gevent.sleep(0.1) poll_gevent_sleep( 2, lambda: messages_contains_prefix(prefix, subscription_results)) message = subscription_results['devices/campus/building1']['message'] assert message == [{'point': 'value'}] gevent.sleep(5)
def test_auto_register_platform(vc_instance): vc, vcuuid, jsonrpc = vc_instance adir = "services/core/VolttronCentralPlatform/" pauuid = vc.install_agent(agent_dir=adir, config_file=adir + "config") assert pauuid print(pauuid) tester = APITester(jsonrpc) def redo_request(): response = tester.do_rpc("list_platforms") print('Response is: {}'.format(response.json())) jsonresp = response.json() if len(jsonresp['result']) > 0: p = jsonresp['result'][0] assert p['uuid'] assert p['name'] == vc.vip_address assert isinstance(p['health'], dict) assert STATUS_GOOD == p['health']['status'] return True return len(response.json()['result']) > 0 assert poll_gevent_sleep(6, redo_request) response = tester.do_rpc("list_platforms") assert len(response.json()['result']) > 0 jsondata = response.json() # Specific platform not the same as vcp on the platform platform_uuid = jsondata['result'][0]['uuid'] # Remove the agent. vc.remove_agent(pauuid) assert 1 == len(vc.list_agents()) newpauuid = vc.install_agent(agent_dir=adir, config_file=adir + "config") assert newpauuid != pauuid assert poll_gevent_sleep(6, redo_request) response = tester.do_rpc("list_platforms") jsondata = response.json() # Specific platform not the same as vcp on the platform platform_uuid2 = jsondata['result'][0]['uuid'] assert platform_uuid == platform_uuid2
def pubsub_authorized(volttron_instance_encrypt, topic='foo', regex=None, peer='pubsub'): """Tests pubsub with a protected topic and an agents is authorized to publish to the protected topic. """ setup = build_protected_pubsub(volttron_instance_encrypt, topic, 'can_publish_to_my_topic', regex, add_capabilities=True) agent1 = setup['agent1'] agent2 = setup['agent2'] topic = setup['topic'] msgs = setup['messages'] agent2.vip.pubsub.publish(peer, topic, message='hello agent').get(timeout=2) assert poll_gevent_sleep(2, lambda: 'hello agent' in msgs)
def test_remove_from_config_store(threshold_tester_agent): threshold_tester_agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_delete_config', 'platform.thresholddetection', 'config').get() publish(threshold_tester_agent, _test_config, lambda x: x + 1) publish(threshold_tester_agent, _test_config, lambda x: x - 1, to_max=False) publish(threshold_tester_agent, _default_config, lambda x: x + 1) check = lambda: threshold_tester_agent.seen_alert_keys == set(['test_max']) assert poll_gevent_sleep(2, check)
def test_can_set_status(volttron_instance1): """ Tests the ability to change a status by sending a different status code. This test also tests that the heartbeat is received. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() new_agent = volttron_instance1.build_agent(identity='test_status') new_agent.vip.heartbeat.start() orig_status = Status.from_json(new_agent.vip.health.get_status()) assert orig_status.status == STATUS_GOOD assert orig_status.context is None assert orig_status.last_updated is not None print('original status: {}'.format(orig_status.as_json())) new_context = {'foo': 'A test something when wrong', 'woah': ['blah', 'blah']} agent_prefix = 'heartbeat/Agent' new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) gevent.sleep(1) new_agent.vip.health.set_status(STATUS_BAD, new_context) poll_gevent_sleep(2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) new_status = Status.from_json(new_agent.vip.health.get_status()) print('new status: {}'.format(new_status.as_json())) assert new_status.status == STATUS_BAD assert new_status.context == new_context assert new_status.last_updated is not None print("OLD IS: {}".format(orig_status.last_updated)) print("NEW IS: {}".format(new_status.last_updated)) old_date = parse_timestamp_string(orig_status.last_updated) new_date = parse_timestamp_string(new_status.last_updated) assert old_date < new_date
def test_heartbeat_sending_status(volttron_instance1): """ Tests the heartbeat message that it has the status. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() agent_prefix = 'heartbeat/Agent' new_agent = volttron_instance1.build_agent(identity='test3') orig_status = Status.from_json(new_agent.vip.health.get_status()) new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) new_agent.vip.heartbeat.start() poll_gevent_sleep(2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) message = subscription_results[agent_prefix]['message'] headers = subscription_results[agent_prefix]['headers'] d = Status.from_json(message) assert headers[DATE] is not None assert d.last_updated is not None assert orig_status.status == d.status assert orig_status.context == d.context
def test_multiplatform_pubsub(request, multi_platform_connection): p1_publisher, p2_listener, p3_listener = multi_platform_connection p2_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage, all_platforms=True) p3_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage) gevent.sleep(1) prefix = 'devices' for i in range(10): p1_publisher.vip.pubsub.publish(peer='pubsub', topic='devices/campus/building1', message=[{ 'point': 'value' }]) poll_gevent_sleep( 5, lambda: messages_contains_prefix(prefix, subscription_results)) message = subscription_results['devices/campus/building1']['message'] assert message == [{'point': 'value'}]
def listen(agent, config): """Assert all SysMonAgent topics have been heard""" base_topic = config['base_topic'] short_topics = ['cpu_percent', 'memory_percent', 'disk_percent'] topics = [base_topic + '/' + x for x in short_topics] seen_topics = set() def add_topic(peer, sender, bus, topic, headers, messages): seen_topics.add(topic) agent.vip.pubsub.subscribe('pubsub', base_topic, callback=add_topic) max_wait = 1 + max(value for key, value in _test_config.items() if key.endswith('_interval')) assert poll_gevent_sleep(max_wait, lambda: set(topics) <= seen_topics)
def test_update_config(threshold_tester_agent): updated_config = { "updated_topic": { "threshold_max": 10, "threshold_min": 0, } } print("*******************test_update_config") print("Before Modifying Config Store {}".format( threshold_tester_agent.vip.pubsub.list( threshold_tester_agent.core.identity).get())) # threshold_tester_agent.vip.pubsub.publish('pubsub', topic="alerts/woot", headers={"foo": "bar"}) # threshold_tester_agent.vip .config.set('config', updated_config, True) threshold_tester_agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_store', 'platform.thresholddetection', 'config', jsonapi.dumps(updated_config), 'json').get() gevent.sleep(0.1) # gevent.sleep(1) # Sleep for chance to update from config callback. # config = threshold_tester_agent.vip.config.get() # assert threshold_tester_agent.vip.config.get() == updated_config print("Before Publish {}".format( threshold_tester_agent.vip.pubsub.list( threshold_tester_agent.core.identity).get())) print("Alert keys before: {}".format( threshold_tester_agent.seen_alert_keys)) publish(threshold_tester_agent, updated_config, lambda x: x + 1) print("Alert keys after publish: {}".format( threshold_tester_agent.seen_alert_keys)) print("After Publish Store {}".format( threshold_tester_agent.vip.pubsub.list( threshold_tester_agent.core.identity).get())) print("Checking object id object_id: {}".format( id(threshold_tester_agent))) check = lambda: threshold_tester_agent.seen_alert_keys == set( ['updated_topic']) try: assert poll_gevent_sleep(5, check) finally: print("Done with testing Now!") threshold_tester_agent.clear_keys() threshold_tester_agent.reset_store()
def test_update_config(threshold_tester_agent): updated_config = { "updated_topic": { "threshold_max": 10, "threshold_min": 0, } } threshold_tester_agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_store', 'platform.thresholddetection', 'config', json.dumps(updated_config), 'json').get(timeout=10) publish(threshold_tester_agent, _test_config, lambda x: x + 1) publish(threshold_tester_agent, updated_config, lambda x: x + 1) check = lambda: threshold_tester_agent.seen_alert_keys == set( ['updated_topic']) assert poll_gevent_sleep(2, check)
def test_update_config(threshold_tester_agent): updated_config = { "updated_topic": { "threshold_max": 10, "threshold_min": 0, } } threshold_tester_agent.vip.rpc.call(CONFIGURATION_STORE, 'manage_store', 'platform.thresholddetection', 'config', json.dumps(updated_config), 'json').get(timeout=10) publish(threshold_tester_agent, _test_config, lambda x: x+1) publish(threshold_tester_agent, updated_config, lambda x: x+1) check = lambda: threshold_tester_agent.seen_alert_keys == set(['updated_topic']) assert poll_gevent_sleep(2, check)
def listen(agent, config): """Assert all SysMonAgent topics have been heard""" base_topic = config['base_topic'] short_topics = ['cpu_percent', 'memory_percent', 'disk_percent'] topics = [base_topic + '/' + x for x in short_topics] seen_topics = set() def add_topic(peer, sender, bus, topic, headers, messages): seen_topics.add(topic) agent.vip.pubsub.subscribe('pubsub', base_topic, callback=add_topic) max_wait = 1 + max([value for key, value in _test_config.items() if key.endswith('_interval')]) all_topics_seen = lambda: set(topics) <= seen_topics assert poll_gevent_sleep(max_wait, all_topics_seen)
def test_pubsub_not_protected(volttron_instance1_encrypt): '''Tests pubsub without any topic protection ''' agent1, agent2, topic, msgs = build_two_agents_pubsub_agents(volttron_instance1_encrypt) agent2.vip.pubsub.publish('pubsub', topic, message='hello agent').get(timeout=1) assert poll_gevent_sleep(2, lambda: len(msgs) > 0 and msgs[0] == 'hello agent')
def test_below_min(threshold_tester_agent): """Should get alert because values below min""" publish(threshold_tester_agent, _test_config, lambda x: x-1, to_max=False) check = lambda: threshold_tester_agent.seen_alert_keys == set(['test3']) assert poll_gevent_sleep(2, check)
def test_above_max(threshold_tester_agent): """Should get alert because values exceed max""" publish(threshold_tester_agent, _test_config, lambda x: x+1) # Only test1 should alert because test2 is disabled check = lambda: threshold_tester_agent.seen_alert_keys == set(['test1']) assert poll_gevent_sleep(2, check)
def test_above_max(threshold_tester_agent): """Should get alert because values exceed max""" publish(threshold_tester_agent, _test_config, lambda x: x + 1) # Only test1 should alert because test2 is disabled check = lambda: threshold_tester_agent.seen_alert_keys == set(['test1']) assert poll_gevent_sleep(2, check)
def test_multiplatform_2_publishers(request, five_platform_connection): subscription_results2 = {} subscription_results3 = {} subscription_results4 = {} subscription_results5 = {} p1_publisher, p2_listener, p3_listener, p4_listener, p5_publisher = five_platform_connection def callback2(peer, sender, bus, topic, headers, message): subscription_results2[topic] = {'headers': headers, 'message': message} print("platform2 sub results [{}] = {}".format( topic, subscription_results2[topic])) def callback3(peer, sender, bus, topic, headers, message): subscription_results3[topic] = {'headers': headers, 'message': message} print("platform3 sub results [{}] = {}".format( topic, subscription_results3[topic])) def callback4(peer, sender, bus, topic, headers, message): subscription_results4[topic] = {'headers': headers, 'message': message} print("platform4 sub results [{}] = {}".format( topic, subscription_results4[topic])) def callback5(peer, sender, bus, topic, headers, message): subscription_results5[topic] = {'headers': headers, 'message': message} print("platform4 sub results [{}] = {}".format( topic, subscription_results5[topic])) p2_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=callback2, all_platforms=True) p3_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=callback3, all_platforms=True) gevent.sleep(2) p4_listener.vip.pubsub.subscribe(peer='pubsub', prefix='analysis', callback=callback4, all_platforms=True) p5_publisher.vip.pubsub.subscribe(peer='pubsub', prefix='analysis', callback=callback5) gevent.sleep(2) print "publish" prefix = 'devices' for i in range(5): p1_publisher.vip.pubsub.publish(peer='pubsub', topic='devices/campus/building1', message=[{ 'point': 'value' }]) poll_gevent_sleep( 1, lambda: messages_contains_prefix(prefix, subscription_results2)) message = subscription_results2['devices/campus/building1']['message'] assert message == [{'point': 'value'}] message = subscription_results3['devices/campus/building1']['message'] assert message == [{'point': 'value'}] prefix = 'analysis' for i in range(5): p5_publisher.vip.pubsub.publish( peer='pubsub', topic='analysis/airside/campus/building1', message=[{ 'result': 'pass' }]) # gevent.sleep(0.1) poll_gevent_sleep( 2, lambda: messages_contains_prefix(prefix, subscription_results3)) message = subscription_results4['analysis/airside/campus/building1'][ 'message'] assert message == [{'result': 'pass'}] message = subscription_results5['analysis/airside/campus/building1'][ 'message'] assert message == [{'result': 'pass'}]
def test_device_publish(threshold_tester_agent): threshold_tester_agent.vip.pubsub.publish('pubsub', 'devices/all', headers={}, message=[{'point': 11}]).get() check = lambda: threshold_tester_agent.seen_alert_keys == set(['devices/all']) assert poll_gevent_sleep(2, check)