torch-subscriber-simple/torch_sub/test/integration_pub_sub.py

62 lines
1.4 KiB
Python

import json
import ssl
import unittest
from datetime import datetime
import paho.mqtt.client as mqtt
from torch_sub import torch_sub
host = "mqtt.example.com"
port = 8883
configPath = "agent-config/"
mqttCaFile = configPath + "ca.crt"
mqttCertFile = configPath + "vagrant.crt"
mqttKeyFile = configPath + "vagrant.key"
def agent_connect():
client = mqtt.Client()
client.tls_set(
ca_certs=mqttCaFile,
certfile=mqttCertFile,
keyfile=mqttKeyFile,
cert_reqs=ssl.CERT_REQUIRED)
client.connect(host, port, 60)
return client
def agent_publish(client_id, onion_hostname):
payload = {
'clientId': client_id,
'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"),
'onionAddress': onion_hostname,
'sshPort': 22
}
client = agent_connect()
topic = "torch/" + client_id + "/wake"
client.publish(topic, json.dumps(payload))
print("Log: Connected to MQTT Broker at %s://%s:%s/%s" % ("mqtts", host, port, topic))
print("Log: Published payload: " + json.dumps(payload))
client.disconnect()
print("Log: Disconnected from MQTT Broker")
pass
class MyTestCase(unittest.TestCase):
def test_something(self):
torch_sub.attach(host, port)
agent_publish("client1", "crazyonion.onion")
self.assertEqual(True, False)
if __name__ == '__main__':
unittest.main()