eclipse-volttron / volttron-platform-driver

Apache License 2.0
0 stars 5 forks source link

Add tests using volttron-testing #11

Open bonicim opened 2 years ago

kefeimo commented 1 year ago

One potential solution is to use the volttron-testing's TestServer (from volttrontesting.server_mock import TestServer)

So that, we can do things like the following

def test_send_alert():
    """ Test that an agent can send an alert through the pubsub message bus."""

    # Create an agent to run the test with
    # agent = Agent(identity='test-health')

    agent = PlatformDriverAgent(driver_config_list=[], identity="platform.driver")

    # Create the server and connect the agent with the server
    ts = TestServer()
    ts.connect_agent(agent=agent)

    # The health.send_alert should send a pubsub message through the message bus
    agent.vip.health.send_alert("my_alert", Status.build(STATUS_BAD, "no context"))

Note that without ts = TestServer(); ts.connect_agent(agent=agent) will get error like the following

>       agent.vip.health.send_alert("my_alert", Status.build(STATUS_BAD, "no context"))

test_platform_driver_w_server.py:30: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../volttron-core/src/volttron/client/vip/agent/subsystems/health.py:80: in send_alert
    self._owner.vip.pubsub.publish("pubsub",
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <volttron.client.vip.agent.subsystems.pubsub.PubSub object at 0x7f0b917535e0>
peer = 'pubsub', topic = 'alerts/PlatformDriverAgent/None_platform_driver'
...
        args = ["publish", topic, dict(bus=bus, headers=headers, message=message)]
>       self.vip_socket.send_vip("", "pubsub", args, result.ident, copy=False)
E       AttributeError: 'NoneType' object has no attribute 'send_vip'

../../volttron-core/src/volttron/client/vip/agent/subsystems/pubsub.py:513: AttributeError