def failover(request, get_volttron_instances): global primary_config global secondary_config global primary_failover global secondary_failover global vc_uuid 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_services_core("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_services_core("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
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) primary.allow_all_connections() secondary.allow_all_connections() # configure primary listener_primary = 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_services_core("FailoverAgent"), config_file=simple_primary_config) # configure secondary 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_services_core("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 test_repackage_new_dest_dir(volttron_instance): """ Test if we can create a wheel file given an installed agent directory. Test with valid destination directory :param volttron_instance: platform wrapper used to install a test agent and test installation of wheel file generated by repackage """ dest_dir = None try: dest_dir = tempfile.mkdtemp() dest_dir = os.path.join(dest_dir, "subdir") print ("cwd {}".format(os.getcwd())) agent_uuid = volttron_instance.install_agent( agent_dir=os.path.join(get_examples("ListenerAgent"))) agent_dir = os.path.join(volttron_instance.volttron_home, 'agents', agent_uuid, 'listeneragent-3.2') print agent_dir wheel_path = repackage(agent_dir, dest=dest_dir) expeceted_wheel = os.path.join( dest_dir, 'listeneragent-3.2-py2-none-any.whl') assert wheel_path == expeceted_wheel # Check wheel exists and it can be used to install the agent again assert os.path.isfile(wheel_path) volttron_instance.install_agent(agent_wheel=wheel_path) finally: if dest_dir: shutil.rmtree(dest_dir)
def test_repackage_output_to_cwd(volttron_instance): """ Test if we can create a wheel file given an installed agent directory. Test without any explicit destination directory for the wheel file. Wheel file should be created in current working directory :param volttron_instance: platform wrapper used to install a test agent and test installation of wheel file generated by repackage """ dest_dir = None cwd = os.getcwd() try: dest_dir = tempfile.mkdtemp() os.chdir(dest_dir) agent_uuid = volttron_instance.install_agent( agent_dir=os.path.join(cwd, get_examples("ListenerAgent"))) agent_dir = os.path.join(volttron_instance.volttron_home, 'agents', agent_uuid, 'listeneragent-3.2') print agent_dir wheel_name = repackage(agent_dir) assert wheel_name == 'listeneragent-3.2-py2-none-any.whl' wheel = os.path.join(dest_dir, wheel_name) # Check wheel exists and it can be used to install the agent again assert os.path.isfile(wheel) volttron_instance.install_agent(agent_wheel=wheel) finally: os.chdir(cwd) if dest_dir: shutil.rmtree(dest_dir)
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 test_can_install_multiple_listeners(volttron_instance): assert volttron_instance.is_running() volttron_instance.remove_all_agents() uuids = [] num_listeners = 3 try: for x in range(num_listeners): identity = "listener_" + str(x) auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), config_file={ "agentid": identity, "message": "So Happpy" }) assert auuid uuids.append(auuid) time.sleep(4) for u in uuids: assert volttron_instance.is_agent_running(u) agent_list = volttron_instance.dynamic_agent.vip.rpc( 'control', 'list_agents').get(timeout=5) print('Agent List: {}'.format(agent_list)) assert len(agent_list) == num_listeners finally: for x in uuids: try: volttron_instance.remove_agent(x) except: print('COULDN"T REMOVE AGENT')
def test_can_install_listeners(single_instance): assert single_instance.is_running() uuids = [] num_listeners = 5 try: for x in range(num_listeners): identity = "listener_" + str(x) auuid = single_instance.install_agent( agent_dir=get_examples("ListenerAgent"), config_file={ "agentid": identity, "message": "So Happpy"}) assert auuid uuids.append(auuid) for u in uuids: assert single_instance.is_agent_running(u) agent = single_instance.build_agent() agent_list = agent.vip.rpc('control', 'list_agents').get(timeout=5) print('Agent List: {}'.format(agent_list)) assert len(agent_list) == num_listeners finally: for x in uuids: try: single_instance.remove_agent(x) except: print('COULDN"T REMOVE AGENT')
def test_install_arg_matrix(volttron_instance: PlatformWrapper, args: List, use_config: bool): listener_config_file = get_examples("ListenerAgent/config") with with_os_environ(volttron_instance.env): # Don't change the parametrized args that have mutable values. Make copy if changing within test. # parameterized args when used with more than 1 .parametrize() or with another parameterized fixture # fails to rest values correctly # @pytest.mark.parametrize("x,y", (([1, 2], 1), ([3, 4], 1))) - will work fine even if x is changed in test # But # @pytest.mark.parametrize("x,y", (([1,2],1), ([3,4],1))) # @pytest.mark.parametrize("z", [8, 9]) # will fail to reset value of x correctly if x is changed within test vctl_args = copy.copy(args) vctl_args.insert(0, "--json") vctl_args.insert(0, "volttron-ctl") if use_config: vctl_args.extend(["--agent-config", listener_config_file]) response = execute_command(vctl_args, volttron_instance.env) json_response = jsonapi.loads(response) agent_uuid = json_response["agent_uuid"] gevent.sleep(1) response = execute_command(["vctl", "--json", "status", agent_uuid], volttron_instance.env) json_response = jsonapi.loads(response) identity = list(json_response.keys())[0] agent_status_dict = json_response[identity] if "--start" in vctl_args: assert agent_status_dict["status"] if "--tag" in vctl_args: assert agent_status_dict["agent_tag"] tag_name = vctl_args[vctl_args.index("--tag") + 1] assert tag_name == agent_status_dict["agent_tag"] if "--vip-identity" in vctl_args: assert agent_status_dict["identity"] expected_identity = vctl_args[vctl_args.index("--vip-identity") + 1] assert expected_identity == agent_status_dict["identity"] if use_config: with open(listener_config_file) as fp: expected_config = yaml.safe_load(fp.read()) config_path = Path(volttron_instance.volttron_home).joinpath( f"agents/{agent_uuid}/listeneragent-3.3/listeneragent-3.3.dist-info/config" ) with open(config_path) as fp: config_data = yaml.safe_load(fp.read()) assert expected_config == config_data volttron_instance.remove_all_agents()
def test_multiplatform_stop_agent_rpc(request, get_volttron_instances): p1, p2 = get_volttron_instances(2) auuid = p1.install_agent(agent_dir=get_examples("ListenerAgent"), start=True) assert auuid is not None test_agent = p2.build_agent() kwargs = {"external_platform": p1.instance_name} agts = None agts = test_agent.vip.rpc.call(CONTROL, 'list_agents', **kwargs).get(timeout=10) assert agts[0]['identity'].startswith('listener') listener_uuid = agts[0]['uuid'] test_agent.vip.rpc.call(CONTROL, 'stop_agent', listener_uuid, **kwargs).get(timeout=10) agt_status = test_agent.vip.rpc.call(CONTROL, 'agent_status', listener_uuid, **kwargs).get(timeout=10) assert agt_status[1] == 0 def stop(): p1.stop_agent(auuid) p1.remove_agent(auuid) p1.shutdown_platform() test_agent.core.stop() p2.shutdown_platform() request.addfinalizer(stop)
def test_can_install_listeners_on_different_instances(get_volttron_instances): num_instances = 3 wrappers = get_volttron_instances(num_instances, True) wrapper_uuid = [] assert num_instances == len(wrappers) for w in wrappers: assert w.is_running() auuid = w.install_agent( agent_dir=get_examples("ListenerAgent"), config_file={"message": "So Happpy"}, start=True ) assert auuid assert w.is_agent_running(auuid) wrapper_uuid.append((w, auuid)) # Make sure that the installed agents are for different instances for w, aid in wrapper_uuid: for w1, aid1 in wrapper_uuid: if id(w1) == id(w): assert w1.is_agent_running(aid) else: # Note using w to compare the installed agent on w to the agent installed on w1 with pytest.raises(FileNotFoundError): w.get_agent_identity(aid1)
def test_can_get_publickey(volttron_instance): """ Test the control rpc method for retrieving agent publickeys from the :class:`ControlService` @param volttron_instance: """ listener_identity = "listener_test" volttron_instance.is_running() message_bus = os.environ.get("MESSAGEBUS", "zmq") cn = volttron_instance.dynamic_agent id_serverkey_map = cn.vip.rpc.call( "control", "get_all_agent_publickeys").get(timeout=5) auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True, vip_identity=listener_identity, ) assert auuid is not None id_serverkey_map = cn.vip.rpc.call( "control", "get_all_agent_publickeys").get(timeout=5) assert listener_identity in id_serverkey_map assert id_serverkey_map.get(listener_identity) is not None volttron_instance.remove_all_agents()
def test_federation_rpc(two_way_federated_rmq_instances): instance_1, instance_2 = two_way_federated_rmq_instances assert instance_1.is_running() assert instance_2.is_running() auuid = None try: auuid = instance_2.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) assert auuid is not None test_agent = instance_1.dynamic_agent kwargs = {"external_platform": instance_2.instance_name} agts = test_agent.vip.rpc.call(CONTROL, 'list_agents', **kwargs).get(timeout=10) assert agts[0]['identity'].startswith('listener') listener_uuid = agts[0]['uuid'] test_agent.vip.rpc.call(CONTROL, 'stop_agent', listener_uuid, **kwargs).get(timeout=10) agt_status = test_agent.vip.rpc.call(CONTROL, 'agent_status', listener_uuid, **kwargs).get(timeout=10) assert agt_status[1] == 0 finally: if instance_2.is_running: instance_2.remove_agent(auuid)
def test_can_install_listeners_vi(volttron_instance): assert volttron_instance.is_running() uuids = [] num_listeners = 5 try: for x in range(num_listeners): identity = "listener_" + str(x) auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True, config_file={ "agentid": identity, "message": "So Happpy" }) assert auuid uuids.append(auuid) gevent.sleep(0.5) agent = volttron_instance.build_agent() agent_list = agent.vip.rpc('control', 'list_agents').get(timeout=5) print('Agent List: {}'.format(agent_list)) assert len(agent_list) == num_listeners finally: for x in uuids: try: volttron_instance.remove_agent(x) except: print('COULDN"T REMOVE AGENT')
def test_can_install_listener(volttron_instance): clear_messages() vi = volttron_instance assert vi is not None assert vi.is_running() auuid = vi.install_agent(agent_dir=get_examples("ListenerAgent"), start=False) assert auuid is not None started = vi.start_agent(auuid) print('STARTED: ', started) listening = vi.build_agent() listening.vip.pubsub.subscribe(peer='pubsub', prefix='heartbeat/listeneragent', callback=onmessage) # sleep for 10 seconds and at least one heartbeat should have been # published # because it's set to 5 seconds. time_start = time.time() print('Awaiting heartbeat response.') while not messages_contains_prefix( 'heartbeat/listeneragent') and time.time() < time_start + 10: gevent.sleep(0.2) assert messages_contains_prefix('heartbeat/listeneragent') stopped = vi.stop_agent(auuid) print('STOPPED: ', stopped) removed = vi.remove_agent(auuid) print('REMOVED: ', removed)
def test_repackage_new_dest_dir(volttron_instance): """ Test if we can create a wheel file given an installed agent directory. Test with valid destination directory :param volttron_instance: platform wrapper used to install a test agent and test installation of wheel file generated by repackage """ dest_dir = None try: dest_dir = tempfile.mkdtemp() dest_dir = os.path.join(dest_dir, "subdir") print("cwd {}".format(os.getcwd())) agent_uuid = volttron_instance.install_agent( agent_dir=os.path.join(get_examples("ListenerAgent"))) agent_dir = os.path.join(volttron_instance.volttron_home, 'agents', agent_uuid, 'listeneragent-3.2') print agent_dir wheel_path = repackage(agent_dir, dest=dest_dir) expeceted_wheel = os.path.join(dest_dir, 'listeneragent-3.2-py2-none-any.whl') assert wheel_path == expeceted_wheel # Check wheel exists and it can be used to install the agent again assert os.path.isfile(wheel_path) volttron_instance.install_agent(agent_wheel=wheel_path) finally: if dest_dir: shutil.rmtree(dest_dir)
def test_can_cleanup_installed_listener(): try: import psutil except: warnings.warn('No psutil module present for this test') return wrapper = PlatformWrapper() address="tcp://127.0.0.1:{}".format(get_rand_port()) wrapper.startup_platform(address) assert wrapper is not None assert wrapper.is_running() auuid = wrapper.install_agent(agent_dir=get_examples("ListenerAgent"), vip_identity="listener", start=False) assert auuid is not None started = wrapper.start_agent(auuid) assert isinstance(started, int) assert psutil.pid_exists(started) wrapper.shutdown_platform() # give operating system enough time to update pids. gevent.sleep(0.1) assert not psutil.pid_exists(started)
def add_listener(wrapper, config={}, **kwargs): agent_uuid = wrapper.install_agent( config_file=config, agent_dir=get_examples("ListenerAgent"), **kwargs ) return agent_uuid
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) primary.allow_all_connections() secondary.allow_all_connections() # configure primary listener_primary = 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_services_core("FailoverAgent"), config_file=simple_primary_config) # configure secondary 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_services_core("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 test_agent_filters(volttron_instance): auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) buuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) # Verify all installed agents show up in list with with_os_environ(volttron_instance.env): p = subprocess.Popen( ["volttron-ctl", "list"], env=volttron_instance.env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, ) agent_list = p.communicate() assert "listeneragent-3.3_1" in str(agent_list) assert "listeneragent-3.3_2" in str(agent_list) # Filter agent based on agent uuid with with_os_environ(volttron_instance.env): p = subprocess.Popen( ["volttron-ctl", "list", str(auuid)], env=volttron_instance.env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, ) agent_list = p.communicate() assert "listeneragent-3.3_1" in str(agent_list) assert "listeneragent-3.3_2" not in str(agent_list) # Filter agent based on agent name with with_os_environ(volttron_instance.env): p = subprocess.Popen( ["volttron-ctl", "list", "listeneragent-3.3_1"], env=volttron_instance.env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, ) agent_list = p.communicate() assert "listeneragent-3.3_1" in str(agent_list) assert "listeneragent-3.3_2" not in str(agent_list)
def test_installagent(auto_registered_local): # To install the agent we need to simulate the browser's interface # for passing files. This means we have to have a base-64 representation # of the wheel file to send across as well as the correct structure # of parameters. # params['files'] must exist as a list of files # each file must have a file_name and file entry # the file name is the name of the agent wheel file the file is the base64 # encoded wheel file. # f['file_name'] # f['file webapi = auto_registered_local agent_wheel = webapi._wrapper.build_agentpackage( get_examples("ListenerAgent")) assert os.path.exists(agent_wheel) import base64 import random # with open(agent_wheel, 'r+b') as f: # hold = f.read() # file_str = str(hold).encode('utf-8') # decoded_str = str(base64.decodestring(hold)) # # From the web this is what is added to the file. # filestr = "base64," + file_str # # filestr = "base64,"+str(base64.b64encode(hold)) with open(agent_wheel, 'r+b') as f: # From the web this is what is added to the file. hold = f.read() print(f"Package is {hold}") filestr = "base64," + base64.b64encode(hold).decode('utf-8') print(f"file string is {filestr}") vip_id = f"test_listener_{random.randint(1,100000)}" file_props = dict(file_name=os.path.basename(agent_wheel), file=filestr, vip_identity=vip_id, force=True) gevent.sleep(5) platform = webapi.list_platforms()[0] agents = webapi.list_agents(platform['uuid']) assert agents agent = webapi.install_agent(platform['uuid'], fileargs=file_props) assert agent assert agent.get('uuid') agents_after = webapi.list_agents(platform['uuid']) assert len(agents) + 1 == len(agents_after) webapi.remove_agent(platform['uuid'], agent.get('uuid')) agents_after = webapi.list_agents(agent.get('uuid')) assert len(agents) == len(agents_after)
def test_can_install_listener_on_two_platforms(get_volttron_instances): wrapper1, wrapper2 = get_volttron_instances(2) global messages clear_messages() auuid = wrapper1.install_agent( agent_dir=get_examples("ListenerAgent"), start=False) assert auuid is not None started = wrapper1.start_agent(auuid) print('STARTED: ', started) listening = wrapper1.build_agent() listening.vip.pubsub.subscribe(peer='pubsub', prefix='heartbeat/listeneragent', callback=onmessage) # sleep for 10 seconds and at least one heartbeat should have been # published # because it's set to 5 seconds. time_start = time.time() clear_messages() auuid2 = wrapper2.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) assert auuid2 is not None started2 = wrapper2.start_agent(auuid2) print('STARTED: ', started2) listening = wrapper2.build_agent() listening.vip.pubsub.subscribe(peer='pubsub', prefix='heartbeat/listeneragent', callback=onmessage) # sleep for 10 seconds and at least one heartbeat should have been # published # because it's set to 5 seconds. time_start = time.time() print('Awaiting heartbeat response.') while not messages_contains_prefix( 'heartbeat/listeneragent') and time.time() < time_start + 10: gevent.sleep(0.2) assert messages_contains_prefix('heartbeat/listeneragent')
def test_expired_server_cert_after_vstart(request, instance): """ Test error when server cert expires after volttron has started :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) crts.create_signed_cert_files(server_cert_name, cert_type='server', fqdn=fqdn, valid_days=0.0004) # 34.5 seconds print("current time:{}".format(datetime.datetime.utcnow())) instance.startup_platform(vip_address=get_rand_vip()) print("current time:{}".format(datetime.datetime.utcnow())) agent = instance.install_agent( agent_dir=get_examples("ListenerAgent"), vip_identity="listener1", start=True) gevent.sleep(20) print("Attempting agent install after server certificate expiry") with pytest.raises(RuntimeError) as exec_info: agent = instance.install_agent( agent_dir=get_examples("ListenerAgent"), vip_identity="listener2", start=True) pytest.fail("Agent install should fail") assert exec_info.type is RuntimeError # Restore server cert and restart rmq ssl, wait for 30 seconds for volttron to reconnect crts.create_signed_cert_files(server_cert_name, cert_type='server', fqdn=fqdn) restart_ssl(rmq_home=instance.rabbitmq_config_obj.rmq_home, env=instance.env) gevent.sleep(15) # test setup sets the volttron reconnect wait to 5 seconds # status of first agent would still be fine and it would # continue to publish hearbeat. assert instance.is_agent_running(agent) instance.remove_agent(agent) except Exception as e: pytest.fail("Test failed with exception: {}".format(e))
def test_installagent(auto_registered_local): # To install the agent we need to simulate the browser's interface # for passing files. This means we have to have a base-64 representation # of the wheel file to send across as well as the correct structure # of parameters. # params['files'] must exist as a list of files # each file must have a file_name and file entry # the file name is the name of the agent wheel file the file is the base64 # encoded wheel file. # f['file_name'] # f['file webapi = auto_registered_local agent_wheel = webapi._wrapper.build_agentpackage(get_examples("ListenerAgent")) assert os.path.exists(agent_wheel) import base64 import random with open(agent_wheel, 'r+b') as f: # From the web this is what is added to the file. filestr = "base64,"+base64.b64encode(f.read()) file_props = dict( file_name=os.path.basename(agent_wheel), file=filestr, vip_identity='bar.full.{}'.format(random.randint(1, 100000)) ) platform = webapi.list_platforms()[0] agents = webapi.list_agents(platform['uuid']) assert agents agent = webapi.install_agent(platform['uuid'], fileargs=file_props) assert agent assert agent.get('uuid') agents_after = webapi.list_agents(platform['uuid']) assert len(agents) + 1 == len(agents_after) webapi.remove_agent(platform['uuid'], agent.get('uuid')) agents_after = webapi.list_agents(agent.get('uuid')) assert len(agents) == len(agents_after) # @pytest.mark.vc # def test_login_rejected_for_foo(vc_instance): # vc_jsonrpc = vc_instance[2] # with pytest.raises(AssertionError): # tester = APITester(vc_jsonrpc, "foo", "")
def test_can_handle_agent_upgrade(simple_failover): global listener_primary primary, secondary = simple_failover primary.remove_agent(listener_primary) listener_primary = primary.install_agent(agent_dir=get_examples("ListenerAgent"), vip_identity="listener", start=False) gevent.sleep(SLEEP_TIME) assert all_agents_running(primary) assert not all_agents_running(secondary)
def test_can_get_identity(volttron_instance): """ Based upon the agent uuid retrieve the IDENTITY. Use the VolttronCentralPlatform as the test agent. @param volttron_instance: """ auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True, vip_identity="test_can_get_identity") assert auuid is not None cn = volttron_instance.build_connection(peer='control') identity = cn.call('agent_vip_identity', auuid) assert identity == 'test_can_get_identity'
def test_repackage_invalid_dest_dir(volttron_instance): """ Test if we can create a wheel file given an installed agent agent_dir. Test with invalid destination agent_dir. Expected result - AgentPackageError :param volttron_instance: platform wrapper used to install a test agent and test installation of wheel file generated by repackage """ dest_dir = "/abcdef/ghijkl" try: agent_uuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent")) agent_dir = os.path.join(volttron_instance.volttron_home, 'agents', agent_uuid, 'listeneragent-3.2') repackage(agent_dir, dest=dest_dir) pytest.fail("Expecting AgentPackageError but code completed " "successfully") except AgentPackageError as a: assert a.message.find("Unable to create destination directory " "{}".format(dest_dir)) != -1 try: dest_dir = tempfile.mkdtemp() os.chmod(dest_dir, stat.S_IREAD) agent_uuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent")) agent_dir = os.path.join(volttron_instance.volttron_home, 'agents', agent_uuid, 'listeneragent-3.2') repackage(agent_dir, dest=dest_dir) pytest.fail("Expecting AgentPackageError but code completed " "successfully") except Exception as a: assert str(a).find("Permission denied") != -1
def test_identity_is_uuid(volttron_instance): """ The identity is uuid for an agent that doesn't include a specific identity. @param volttron_instance: @return: """ auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) assert auuid is not None agent = volttron_instance.build_agent() identity = agent.vip.rpc.call('control', 'agent_vip_identity', auuid).get(timeout=2) assert identity == "listeneragent-3.2_2"
def test_identity_is_uuid(volttron_instance): """The identity is uuid for an agent that doesn't include a specific identity. @param volttron_instance: @return: """ auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) assert auuid is not None agent = volttron_instance.dynamic_agent identity = agent.vip.rpc.call("control", "agent_vip_identity", auuid).get(timeout=2) assert identity == "listeneragent-3.3_1" volttron_instance.remove_all_agents()
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_can_install_listener(volttron_instance): vi = volttron_instance assert vi is not None assert vi.is_running() # agent identity should be auuid = vi.install_agent(agent_dir=get_examples("ListenerAgent"), start=False) assert auuid is not None started = vi.start_agent(auuid) assert started assert vi.is_agent_running(auuid) listening = vi.build_agent() listening.callback = MagicMock(name="callback") listening.callback.reset_mock() assert listening.core.identity listening.vip.pubsub.subscribe(peer='pubsub', prefix='heartbeat/{}'.format( vi.get_agent_identity(auuid)), callback=listening.callback) # default heartbeat for core listener is 5 seconds. # sleep for 10 just in case we miss one. gevent.sleep(10) assert listening.callback.called call_args = listening.callback.call_args[0] # peer, sender, bus, topic, headers, message assert call_args[0] == 'pubsub' # TODO: This hard coded value should be changed with a platformwrapper call to a function # get_agent_identity(uuid) assert call_args[1] == vi.get_agent_identity(auuid) assert call_args[2] == '' assert call_args[3].startswith('heartbeat/listeneragent') assert 'max_compatible_version' in call_args[4] assert 'min_compatible_version' in call_args[4] assert 'TimeStamp' in call_args[4] assert 'GOOD' in call_args[5] stopped = vi.stop_agent(auuid) print('STOPPED: ', stopped) removed = vi.remove_agent(auuid) print('REMOVED: ', removed) listening.core.stop()
def test_can_get_identity(volttron_instance): """Based upon the agent uuid retrieve the IDENTITY. Use the VolttronCentralPlatform as the test agent. @param volttron_instance: """ auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True, vip_identity="test_can_get_identity", ) assert auuid is not None identity = volttron_instance.dynamic_agent.vip.rpc.call( "control", "agent_vip_identity", auuid).get(5) assert identity == "test_can_get_identity" volttron_instance.remove_all_agents()
def test_agent_versions(volttron_instance): auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) assert auuid is not None agent = volttron_instance.build_agent() version = agent.vip.rpc.call('control', 'agent_version', auuid).get(timeout=2) assert version == "3.2" versions = agent.vip.rpc.call('control', 'agent_versions').get(timeout=2) assert isinstance(versions, dict) assert len(versions) == 1 k = versions.keys()[0] versions = versions[k] assert versions[0] == 'listeneragent-3.2' assert versions[1] == '3.2'
def test_installagent(vc_vcp_platforms): vc, vcp = vc_vcp_platforms # To install the agent we need to simulate the browser's interface # for passing files. This means we have to have a base-64 representation # of the wheel file to send across as well as the correct structure # of parameters. # params['files'] must exist as a list of files # each file must have a file_name and file entry # the file name is the name of the agent wheel file the file is the base64 # encoded wheel file. # f['file_name'] # f['file agent_wheel = vc.build_agentpackage(get_examples("ListenerAgent")) assert os.path.exists(agent_wheel) import base64 import random with open(agent_wheel, 'r+b') as f: # From the web this is what is added to the file. filestr = "base64,"+base64.b64encode(f.read()) file = dict( file_name=os.path.basename(agent_wheel), file=filestr, vip_identity='bar.full.{}'.format(random.randint(1, 100000)) ) api = APITester(vc.jsonrpc_endpoint) platform = api.list_platforms()[0] agents = api.list_agents(platform['uuid']) assert agents agent = api.install_agent(platform['uuid'], fileargs=file) assert agent assert agent.get('uuid') agents_after = api.list_agents(platform['uuid']) assert len(agents) + 1 == len(agents_after)
def test_agent_versions(volttron_instance): auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True) assert auuid is not None agent = volttron_instance.dynamic_agent version = agent.vip.rpc.call("control", "agent_version", auuid).get(timeout=2) assert version == "3.3" versions = agent.vip.rpc.call("control", "agent_versions").get(timeout=2) assert isinstance(versions, dict) assert len(versions) == 1 k = list(versions.keys())[0] versions = versions[k] assert versions[0] == "listeneragent-3.3" assert versions[1] == "3.3" volttron_instance.remove_all_agents()
def test_can_remove_agent(volttron_instance): """ Confirms that 'volttron-ctl remove' removes agent as expected. """ assert volttron_instance is not None assert volttron_instance.is_running() # Install ListenerAgent as the agent to be removed. agent_uuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=False) assert agent_uuid is not None started = volttron_instance.start_agent(agent_uuid) assert started is not None pid = volttron_instance.agent_pid(agent_uuid) assert pid is not None and pid > 0 # Now attempt removal volttron_instance.remove_agent(agent_uuid) # Confirm that it has been removed. pid = volttron_instance.agent_pid(agent_uuid) assert pid is None
def test_can_get_publickey(volttron_instance): """ Test the control rpc method for retrieving agent publickeys from the :class:`ControlService` @param volttron_instance: """ listener_identity = "listener_test" volttron_instance.is_running() cn = volttron_instance.build_connection(peer='control') assert cn.is_peer_connected() id_serverkey_map = cn.call('get_all_agent_publickeys') auuid = volttron_instance.install_agent( agent_dir=get_examples("ListenerAgent"), start=True, vip_identity=listener_identity) assert auuid is not None id_serverkey_map = cn.call('get_all_agent_publickeys') assert listener_identity in id_serverkey_map assert id_serverkey_map.get(listener_identity) is not None
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
def publish_agent(request, volttron_instance1): """ Fixture used for setting up the environment. 1. Creates fake driver configs 2. Starts the master driver agent with the created fake driver agents 3. Starts the actuator agent 4. Creates an instance Agent class for publishing and returns it :param request: pytest request object :param volttron_instance1: instance of volttron in which test cases are run :return: an instance of fake agent used for publishing """ # Reset master driver config store cmd = ['volttron-ctl', 'config', 'delete', PLATFORM_DRIVER, '--all'] process = Popen(cmd, env=volttron_instance1.env, cwd='scripts/scalability-testing', stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = process.wait() print(result) assert result == 0 # Add master driver configuration files to config store. cmd = ['volttron-ctl', 'config', 'store',PLATFORM_DRIVER, 'fake.csv', 'fake_unit_testing.csv', '--csv'] process = Popen(cmd, env=volttron_instance1.env, cwd='scripts/scalability-testing', stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = process.wait() print(result) assert result == 0 config_name = "devices/fakedriver" cmd = ['volttron-ctl', 'config', 'store', PLATFORM_DRIVER, config_name, 'fake_unit_testing.config', '--json'] process = Popen(cmd, env=volttron_instance1.env, cwd='scripts/scalability-testing', stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = process.wait() print(result) assert result == 0 # Start the master driver agent which would intern start the fake driver # using the configs created above master_uuid = volttron_instance1.install_agent( agent_dir=get_services_core("MasterDriverAgent"), config_file={}, start=True) print("agent id: ", master_uuid) gevent.sleep(2) # wait for the agent to start and start the devices # Start the actuator agent through which publish agent should communicate # to fake device. Start the master driver agent which would intern start # the fake driver using the configs created above actuator_uuid = volttron_instance1.install_agent( agent_dir=get_services_core("ActuatorAgent"), config_file=get_services_core("ActuatorAgent/tests/actuator.config"), start=True) print("agent id: ", actuator_uuid) gevent.sleep(2) example_uuid = volttron_instance1.install_agent( agent_dir=get_examples("ConfigActuation"), config_file={}, vip_identity="config_actuation") gevent.sleep(2) # 3: Start a fake agent to publish to message bus publish_agent = volttron_instance1.build_agent(identity=TEST_AGENT) # 4: add a tear down method to stop sqlhistorian agent and the fake agent # \that published to message bus def stop_agent(): print("In teardown method of module") volttron_instance1.stop_agent(actuator_uuid) volttron_instance1.stop_agent(master_uuid) volttron_instance1.stop_agent(example_uuid) volttron_instance1.remove_agent(actuator_uuid) volttron_instance1.remove_agent(master_uuid) volttron_instance1.remove_agent(example_uuid) publish_agent.core.stop() request.addfinalizer(stop_agent) return publish_agent