def test_on_rmq_reconnect(volttron_instance_rmq, publisher_agent, subscriber_agent): """ Test the fix for issue# 1702 :param request: :param volttron_instance_rmq: :return: """ publisher_agent.vip.pubsub.publish(peer='pubsub', topic='test/test_message', headers={}, message="This is test message") gevent.sleep(0.5) assert subscriber_agent.callback.call_count == 1 # Stop RabbitMQ server rmq_cfg = RMQConfig() stop_rabbit(rmq_cfg.rmq_home, env=volttron_instance_rmq.env) gevent.sleep(1) # Start RabbitMQ server again start_rabbit(rmq_cfg.rmq_home, env=volttron_instance_rmq.env) gevent.sleep(8) publisher_agent.vip.pubsub.publish( peer='pubsub', topic='test/test_message', headers={}, message="This is test message after rmq reconnect") gevent.sleep(0.1) assert subscriber_agent.callback.call_count == 2
def test_vctl_shutdown_on_rmq_stop(request): """ Test for fix issue# 1886 :param volttron_instance_rmq: :return: """ address = get_rand_vip() volttron_instance = build_wrapper(address, messagebus='rmq', ssl_auth=True) agent_uuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) assert agent_uuid is not None agent_pid = volttron_instance.agent_pid(agent_uuid) assert agent_pid is not None and agent_pid > 0 # Stop RabbitMQ server rmq_cfg = RMQConfig() stop_rabbit(rmq_home=rmq_cfg.rmq_home, env=volttron_instance.env) gevent.sleep(5) # Shtudown platform cmd = ['volttron-ctl', 'shutdown', '--platform'] execute_command(cmd, env=volttron_instance.env) gevent.sleep(2) # Check that installed agent and platform is not running assert not psutil.pid_exists(agent_pid) assert volttron_instance.is_running() == False
def instance(request): instance = PlatformWrapper(messagebus='rmq', ssl_auth=True) yield instance if instance.is_running(): instance.shutdown_platform() # In case platform was just killed stop_rabbit(rmq_home=instance.rabbitmq_config_obj.rmq_home, env=instance.env, quite=True)
def test_expired_ca_cert_after_vstart(request, instance): """ Test error when CA cert expires after volttron has started. Once CA cert expires, can't install agent or can't get agent status. CA certificate needs to be recreated and client certs have to :param request: pytest request object :param instance: instance of volttron using rmq and ssl """ crts = instance.certsobj try: (root_ca, server_cert_name, admin_cert_name) = \ Certs.get_admin_cert_names(instance.instance_name) data = {'C': 'US', 'ST': 'Washington', 'L': 'Richland', 'O': 'pnnl', 'OU': 'volttron', 'CN': instance.instance_name + "_root_ca"} crts.create_root_ca(valid_days=0.0005, **data) print("current time after root ca:{}".format(datetime.datetime.utcnow())) copy(crts.cert_file(crts.root_ca_name), crts.cert_file(crts.trusted_ca_name)) crts.create_signed_cert_files(server_cert_name, cert_type='server', fqdn=fqdn) crts.create_signed_cert_files(admin_cert_name, cert_type='client') instance.startup_platform(vip_address=get_rand_vip()) print("current time after platform start:{}".format(datetime.datetime.utcnow())) gevent.sleep(30) # wait for CA to expire # Can't install new agent with pytest.raises(RuntimeError) as exec_info: agent = instance.install_agent( agent_dir=get_examples("ListenerAgent"), vip_identity="listener2", start=True) assert exec_info.type is RuntimeError except Exception as e: pytest.fail("Test failed with exception: {}".format(e)) finally: # can't do clean shutdown with expired ca. terminate process # and clean up manually instance.p_process.terminate() stop_rabbit(rmq_home=instance.rabbitmq_config_obj.rmq_home, env=instance.env, quite=True) if not instance.skip_cleanup: shutil.rmtree(instance.volttron_home)
def test_rmq_reconnect_with_publish(volttron_instance_rmq, publisher_agent, subscriber_agent): """ Test the fix for issue# 1702 :param request: :param volttron_instance_rmq: :return: """ subscriber_agent.callback.reset_mock() gevent.sleep(0.2) publisher_agent.vip.pubsub.publish(peer='pubsub', topic='test/test_message', headers={}, message="This is test message") gevent.sleep(0.2) assert subscriber_agent.callback.call_count == 1 # Stop RabbitMQ server rmq_cfg = RMQConfig() stop_rabbit(rmq_cfg.rmq_home, env=volttron_instance_rmq.env) gevent.sleep(2) # Start RabbitMQ server start_rabbit(rmq_cfg.rmq_home, env=volttron_instance_rmq.env) gevent.sleep(2) for i in range(5): try: publisher_agent.vip.pubsub.publish(peer='pubsub', topic='test/test_message', headers={}, message="This is test message") except Unreachable: # Apply back pressure and try again after sleep gevent.sleep(1) publisher_agent.vip.pubsub.publish( peer='pubsub', topic='test/test_message', headers={}, message="This is test message after rmq reconnect") gevent.sleep(0.1) assert subscriber_agent.callback.call_count >= 2
def federated_rmq_instances(request, **kwargs): """ Create two rmq based volttron instances. One to act as producer of data and one to act as consumer of data :return: 2 volttron instances - (producer, consumer) that are federated """ upstream_vip = get_rand_vip() upstream = build_wrapper(upstream_vip, ssl_auth=True, messagebus='rmq', should_start=False, **kwargs) downstream_vip = get_rand_vip() downstream = build_wrapper(downstream_vip, ssl_auth=True, messagebus='rmq', should_start=False, **kwargs) # exchange CA certs stop_rabbit(rmq_home=upstream.rabbitmq_config_obj.rmq_home, env=upstream.env, quite=True) stop_rabbit(rmq_home=downstream.rabbitmq_config_obj.rmq_home, env=downstream.env, quite=True) with open( os.path.join(upstream.certsobj.cert_dir, upstream.instance_name + "-root-ca.crt"), "r") as uf: with open( os.path.join(downstream.certsobj.cert_dir, downstream.instance_name + "-trusted-cas.crt"), "a") as df: df.write(uf.read()) with open( os.path.join(downstream.certsobj.cert_dir, downstream.instance_name + "-root-ca.crt"), "r") as df: with open( os.path.join(upstream.certsobj.cert_dir, upstream.instance_name + "-trusted-cas.crt"), "a") as uf: uf.write(df.read()) start_rabbit(rmq_home=downstream.rabbitmq_config_obj.rmq_home, env=downstream.env) gevent.sleep(1) start_rabbit(rmq_home=upstream.rabbitmq_config_obj.rmq_home, env=upstream.env) gevent.sleep(1) try: # add downstream user ON UPSTREAM and give permissions # ~/rabbitmq_server/rabbitmq_server-3.7.7/sbin/rabbitmqctl add_user <user> <password> # ~/rabbitmq_server/rabbitmq_server-3.7.7/sbin/rabbitmqctl set_permissions -p <vhost> <user> ".*" ".*" ".*" cmd = [ os.path.join(upstream.rabbitmq_config_obj.rmq_home, "sbin/rabbitmqctl") ] cmd.extend(['add_user', downstream.instance_name + "-admin", "test"]) execute_command(cmd, env=upstream.env, err_prefix="Error creating user in upstream server") cmd = [ os.path.join( upstream.rabbitmq_config_obj.rabbitmq_config['rmq-home'], "sbin/rabbitmqctl") ] cmd.extend([ 'set_permissions', "-p", upstream.rabbitmq_config_obj.rabbitmq_config["virtual-host"] ]) cmd.extend([downstream.instance_name + "-admin", ".*", ".*", ".*"]) execute_command( cmd, env=upstream.env, err_prefix="Error setting user permission in upstream server") gevent.sleep(1) upstream.startup_platform(upstream_vip) gevent.sleep(2) print("After upstream start") downstream.startup_platform(downstream_vip) gevent.sleep(2) # create federation config and setup federation content = """federation-upstream: {host}: port: {port} virtual-host: {vhost} """ config_path = os.path.join(downstream.volttron_home, "federation.config") with open(config_path, 'w') as conf: conf.write( content.format( host=upstream.rabbitmq_config_obj.rabbitmq_config["host"], port=upstream.rabbitmq_config_obj. rabbitmq_config["amqp-port-ssl"], vhost=upstream.rabbitmq_config_obj. rabbitmq_config["virtual-host"])) downstream.setup_federation(config_path) except Exception as e: print("Exception setting up federation: {}".format(e)) upstream.shutdown_platform() downstream.shutdown_platform() raise e yield upstream, downstream upstream.shutdown_platform() downstream.shutdown_platform()