Esempio n. 1
0
def build_agent(platform: PlatformWrapper, identity=None):
    """Builds an agent instance with the passed platform as its bus.

    The agent identity will be set.  If the identity is set to None
    then a random identity will be created.
    """
    os.environ['VOLTTRON_HOME'] = platform.volttron_home
    agent = platform.build_agent(identity)
    gevent.sleep(0.1)  # switch context for a bit
    os.environ.pop('VOLTTRON_HOME')
    return agent
Esempio n. 2
0
def test_channel_send_file(volttron_instance: PlatformWrapper):

    if not volttron_instance.messagebus == "zmq":
        pytest.skip("Channel only available for zmq message bus")
        return

    # Create
    with tarfile.open("/tmp/tmptar.tar", mode="w") as tf:
        for x in range(1, 50):
            with open(f"/tmp/data{x}", "w") as fin:
                fin.write("x" * 50)

            tf.add(f"/tmp/data{x}")

            os.remove(f"/tmp/data{x}")

    sender = volttron_instance.build_agent(agent_class=ChannelSender,
                                           identity="sender_agent",
                                           enable_channel=True)
    receiver = volttron_instance.build_agent(agent_class=ChannelReceiver,
                                             identity="receiver_agent",
                                             enable_channel=True)

    if os.path.exists(receiver.receiver_file_path):
        os.remove(receiver.receiver_file_path)

    sender.send_file(receiver.core.identity, "/tmp/tmptar.tar")

    assert os.path.isfile(
        receiver.receiver_file_path
    ), f"Couldn't find file {receiver.receiver_file_path}"

    assert hashlib.sha256(open("/tmp/tmptar.tar",
                               'rb').read()).hexdigest() == hashlib.sha256(
                                   open(receiver.receiver_file_path,
                                        'rb').read()).hexdigest()

    sender.core.stop()
    receiver.core.stop()
Esempio n. 3
0
def test_channel_send_data(volttron_instance: PlatformWrapper):

    if not volttron_instance.messagebus == "zmq":
        pytest.skip("Channel only available for zmq message bus")
        return

    data = "x" * 50

    sender = volttron_instance.build_agent(agent_class=ChannelSender,
                                           identity="sender_agent",
                                           enable_channel=True)
    receiver = volttron_instance.build_agent(agent_class=ChannelReceiver,
                                             identity="receiver_agent",
                                             enable_channel=True)

    sender.do_send(peer=receiver.core.identity,
                   data=data.encode('utf-8'),
                   channel_name="foo_data")

    assert sender.responses
    assert receiver.the_data
    assert receiver.the_data == data.encode('utf-8')
    sender.core.stop()
    receiver.core.stop()
Esempio n. 4
0
def test_encryption():
    addr = 'tcp://127.0.0.1:55055'
    pub, sec = curve_keypair()
    publickey, secretkey = encode_key(pub), encode_key(sec)
    auth = {'allow': [{'credentials': 'CURVE:{}'.format(publickey)}]}

    plat = PlatformWrapper()
    plat.startup_platform(vip_address=addr, auth_dict=auth, encrypt=True)

    agent_addr = '{}?serverkey={}&publickey={}&secretkey=' \
                 '{}'.format(addr, plat.publickey, publickey, secretkey)

    agent1 = plat.build_agent(agent_addr, identity='agent1')
    peers = agent1.vip.peerlist.list().get(timeout=2)
    plat.shutdown_platform()
    print('PEERS: ', peers)
    assert len(peers) > 0
Esempio n. 5
0
def build_agent_with_key(platform: PlatformWrapper, identity=None):
    """Create an agent instance that has a generated public and private key.

    The passed platform will be the vip-address of the agent and the
     identity will be set.  If the identity is set to None then a random
     identity will be created.
    """
    os.environ['VOLTTRON_HOME'] = platform.volttron_home
    keys = KeyStore(os.path.join(platform.volttron_home, identity + '.keys'))
    keys.generate()
    agent = platform.build_agent(identity=identity,
                                 serverkey=platform.publickey,
                                 publickey=keys.public,
                                 secretkey=keys.secret)
    # Make publickey easily accessible for these tests
    agent.publickey = keys.public
    gevent.sleep(0.1)  # switch context for a bit
    os.environ.pop('VOLTTRON_HOME')
    return agent