1
|
|
|
""" |
2
|
|
|
Test an OPC-UA server with freeopcua python client |
3
|
|
|
""" |
4
|
|
|
import sys |
5
|
|
|
import pytest |
6
|
|
|
import asyncio |
7
|
|
|
import logging |
8
|
|
|
from datetime import datetime |
9
|
|
|
|
10
|
|
|
from asyncua import ua |
11
|
|
|
from asyncua import Client |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class MySubHandler: |
15
|
|
|
""" |
16
|
|
|
More advanced subscription client using Future, so we can wait for events in tests |
17
|
|
|
""" |
18
|
|
|
|
19
|
|
|
def __init__(self): |
20
|
|
|
self.future = asyncio.Future() |
21
|
|
|
|
22
|
|
|
def reset(self): |
23
|
|
|
self.future = asyncio.Future() |
24
|
|
|
|
25
|
|
|
def datachange_notification(self, node, val, data): |
26
|
|
|
self.future.set_result((node, val, data)) |
27
|
|
|
|
28
|
|
|
def event_notification(self, event): |
29
|
|
|
self.future.set_result(event) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class MySubHandler2(object): |
33
|
|
|
def __init__(self): |
34
|
|
|
self.results = [] |
35
|
|
|
|
36
|
|
|
def datachange_notification(self, node, val, data): |
37
|
|
|
self.results.append((node, val)) |
38
|
|
|
|
39
|
|
|
def event_notification(self, event): |
40
|
|
|
self.results.append(event) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def connect(func): |
44
|
|
|
def wrapper(self): |
45
|
|
|
try: |
46
|
|
|
client = Client(URL) |
47
|
|
|
client.connect() |
48
|
|
|
func(self, client) |
49
|
|
|
finally: |
50
|
|
|
client.disconnect() |
51
|
|
|
return wrapper |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def test_connect_anonymous(self): |
56
|
|
|
c = Client(URL) |
57
|
|
|
c.connect() |
58
|
|
|
c.disconnect() |
59
|
|
|
|
60
|
|
|
def FINISH_test_connect_basic256(self): |
61
|
|
|
c = Client(URL) |
62
|
|
|
c.set_security_string("basic256,sign,XXXX") |
63
|
|
|
c.connect() |
64
|
|
|
c.disconnect() |
65
|
|
|
|
66
|
|
|
def test_find_servers(self): |
67
|
|
|
c = Client(URL) |
68
|
|
|
res = c.connect_and_find_servers() |
69
|
|
|
assert len(res) > 0 |
70
|
|
|
|
71
|
|
|
def test_find_endpoints(self): |
72
|
|
|
c = Client(URL) |
73
|
|
|
res = c.connect_and_get_server_endpoints() |
74
|
|
|
assert len(res) > 0 |
75
|
|
|
|
76
|
|
|
# @connect |
77
|
|
|
def test_get_root(self, client): |
78
|
|
|
root = client.get_root_node() |
79
|
|
|
self.assertEqual(root.get_browse_name(), ua.QualifiedName("Root", 0)) |
80
|
|
|
|
81
|
|
|
# @connect |
82
|
|
|
def test_get_root_children(self, client): |
83
|
|
|
root = client.get_root_node() |
84
|
|
|
childs = root.get_children() |
85
|
|
|
assert len(childs) > 2 |
86
|
|
|
|
87
|
|
|
# @connect |
88
|
|
|
async def test_get_namespace_array(self, client): |
89
|
|
|
array = await client.get_namespace_array() |
90
|
|
|
assert len(array) > 0 |
91
|
|
|
|
92
|
|
|
# @connect |
93
|
|
|
def test_get_server_node(self, client): |
94
|
|
|
srv = client.get_server_node() |
95
|
|
|
self.assertEqual(srv.get_browse_name(), ua.QualifiedName("Server", 0)) |
96
|
|
|
#childs = srv.get_children() |
97
|
|
|
#assert len(childs) > 4) |
98
|
|
|
|
99
|
|
|
# @connect |
100
|
|
|
def test_browsepathtonodeid(self, client): |
101
|
|
|
root = client.get_root_node() |
102
|
|
|
node = root.get_child(["0:Objects", "0:Server" , "0:ServerArray"]) |
103
|
|
|
self.assertEqual(node.get_browse_name(), ua.QualifiedName("ServerArray", 0)) |
104
|
|
|
|
105
|
|
|
# @connect |
106
|
|
|
def test_subscribe_server_time(self, client): |
107
|
|
|
msclt = MySubHandler() |
108
|
|
|
|
109
|
|
|
server_time_node = client.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime)) |
110
|
|
|
sub = client.create_subscription(200, msclt) |
111
|
|
|
handle = sub.subscribe_data_change(server_time_node) |
112
|
|
|
|
113
|
|
|
node, val, data = msclt.future.result() |
114
|
|
|
self.assertEqual(node, server_time_node) |
115
|
|
|
delta = datetime.utcnow() - val |
116
|
|
|
print("Timedelta is ", delta) |
117
|
|
|
#assert delta < timedelta(seconds=2)) |
118
|
|
|
|
119
|
|
|
sub.unsubscribe(handle) |
120
|
|
|
sub.delete() |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
if __name__ == "__main__": |
124
|
|
|
logging.basicConfig(level=logging.WARN) |
125
|
|
|
# FIXME add better arguments parsing with possibility to specify |
126
|
|
|
# username and password and encryption |
127
|
|
|
if len(sys.argv) < 2: |
128
|
|
|
print("This script is meant to test compatibilty to a server with freeopcua python client library") |
129
|
|
|
print("Usage: test_server.py url") |
130
|
|
|
sys.exit(1) |
131
|
|
|
else: |
132
|
|
|
URL = sys.argv[1] |
133
|
|
|
|
134
|
|
|
unittest.main(verbosity=30, argv=sys.argv[:1]) |
135
|
|
|
|
136
|
|
|
|