Go to the documentation of this file.00001 """
00002 Test an OPC-UA server with freeopcua python client
00003 """
00004 import logging
00005 import sys
00006 import unittest
00007 from concurrent.futures import Future
00008 from datetime import datetime
00009 from datetime import timedelta
00010 import time
00011
00012 from opcua import ua
00013 from opcua import Client
00014
00015
00016 class MySubHandler():
00017
00018 '''
00019 More advanced subscription client using Future, so we can wait for events in tests
00020 '''
00021
00022 def __init__(self):
00023 self.future = Future()
00024
00025 def reset(self):
00026 self.future = Future()
00027
00028 def datachange_notification(self, node, val, data):
00029 self.future.set_result((node, val, data))
00030
00031 def event_notification(self, event):
00032 self.future.set_result(event)
00033
00034
00035 class MySubHandler2(object):
00036 def __init__(self):
00037 self.results = []
00038
00039 def datachange_notification(self, node, val, data):
00040 self.results.append((node, val))
00041
00042 def event_notification(self, event):
00043 self.results.append(event)
00044
00045
00046 def connect(func):
00047 def wrapper(self):
00048 try:
00049 client = Client(URL)
00050 client.connect()
00051 func(self, client)
00052 finally:
00053 client.disconnect()
00054 return wrapper
00055
00056
00057 class Tests(unittest.TestCase):
00058
00059 def test_connect_anonymous(self):
00060 c = Client(URL)
00061 c.connect()
00062 c.disconnect()
00063
00064 def FINISH_test_connect_basic256(self):
00065 c = Client(URL)
00066 c.set_security_string("basic256,sign,XXXX")
00067 c.connect()
00068 c.disconnect()
00069
00070 def test_find_servers(self):
00071 c = Client(URL)
00072 res = c.connect_and_find_servers()
00073 self.assertTrue(len(res) > 0)
00074
00075 def test_find_endpoints(self):
00076 c = Client(URL)
00077 res = c.connect_and_get_server_endpoints()
00078 self.assertTrue(len(res) > 0)
00079
00080 @connect
00081 def test_get_root(self, client):
00082 root = client.get_root_node()
00083 self.assertEqual(root.get_browse_name(), ua.QualifiedName("Root", 0))
00084
00085 @connect
00086 def test_get_root_children(self, client):
00087 root = client.get_root_node()
00088 childs = root.get_children()
00089 self.assertTrue(len(childs) > 2)
00090
00091 @connect
00092 def test_get_namespace_array(self, client):
00093 array = client.get_namespace_array()
00094 self.assertTrue(len(array) > 0)
00095
00096 @connect
00097 def test_get_server_node(self, client):
00098 srv = client.get_server_node()
00099 self.assertEqual(srv.get_browse_name(), ua.QualifiedName("Server", 0))
00100
00101
00102
00103 @connect
00104 def test_browsepathtonodeid(self, client):
00105 root = client.get_root_node()
00106 node = root.get_child(["0:Objects", "0:Server" , "0:ServerArray"])
00107 self.assertEqual(node.get_browse_name(), ua.QualifiedName("ServerArray", 0))
00108
00109 @connect
00110 def test_subscribe_server_time(self, client):
00111 msclt = MySubHandler()
00112
00113 server_time_node = client.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime))
00114 sub = client.create_subscription(200, msclt)
00115 handle = sub.subscribe_data_change(server_time_node)
00116
00117 node, val, data = msclt.future.result()
00118 self.assertEqual(node, server_time_node)
00119 delta = datetime.utcnow() - val
00120 print("Timedelta is ", delta)
00121
00122
00123 sub.unsubscribe(handle)
00124 sub.delete()
00125
00126
00127 if __name__ == "__main__":
00128
00129 logging.basicConfig(level=logging.WARN)
00130
00131
00132 if len(sys.argv) < 2:
00133 print("This script is meant to test compatibilty to a server with freeopcua python client library")
00134 print("Usage: test_server.py url")
00135 sys.exit(1)
00136 else:
00137 URL = sys.argv[1]
00138
00139 unittest.main(verbosity=30, argv=sys.argv[:1])
00140