1
|
1 |
|
import unittest |
2
|
|
|
|
3
|
1 |
|
from opcua import Client |
4
|
1 |
|
from opcua import Server |
5
|
1 |
|
from opcua import ua |
6
|
|
|
|
7
|
1 |
|
from tests_common import CommonTests, add_server_methods |
8
|
|
|
|
9
|
1 |
|
port_num1 = 48510 |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
class TestClient(unittest.TestCase, CommonTests): |
13
|
|
|
|
14
|
|
|
''' |
15
|
|
|
Run common tests on client side |
16
|
|
|
Of course we need a server so we start a server in another |
17
|
|
|
process using python Process module |
18
|
|
|
Tests that can only be run on client side must be defined here |
19
|
|
|
''' |
20
|
1 |
|
@classmethod |
21
|
|
|
def setUpClass(self): |
22
|
|
|
# start our own server |
23
|
1 |
|
self.srv = Server() |
24
|
1 |
|
self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num1) |
25
|
1 |
|
add_server_methods(self.srv) |
26
|
1 |
|
self.srv.start() |
27
|
|
|
|
28
|
|
|
# start admin client |
29
|
1 |
|
self.clt = Client('opc.tcp://admin@localhost:%d' % port_num1) |
30
|
1 |
|
self.clt.connect() |
31
|
1 |
|
self.opc = self.clt |
32
|
|
|
|
33
|
|
|
# start anonymous client |
34
|
1 |
|
self.ro_clt = Client('opc.tcp://localhost:%d' % port_num1) |
35
|
1 |
|
self.ro_clt.connect() |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
@classmethod |
40
|
|
|
def tearDownClass(self): |
41
|
|
|
#stop our clients |
42
|
1 |
|
self.ro_clt.disconnect() |
43
|
1 |
|
self.clt.disconnect() |
44
|
|
|
# stop the server |
45
|
1 |
|
self.srv.stop() |
46
|
|
|
|
47
|
1 |
|
def test_service_fault(self): |
48
|
1 |
|
request = ua.ReadRequest() |
49
|
1 |
|
request.TypeId = ua.FourByteNodeId(999) # bad type! |
50
|
1 |
|
with self.assertRaises(ua.UaStatusCodeError): |
51
|
1 |
|
self.clt.uaclient._uasocket.send_request(request) |
52
|
|
|
|
53
|
1 |
|
def test_objects_anonymous(self): |
54
|
1 |
|
objects = self.ro_clt.get_objects_node() |
55
|
1 |
|
with self.assertRaises(ua.UaStatusCodeError): |
56
|
1 |
|
objects.set_attribute(ua.AttributeIds.WriteMask, ua.DataValue(999)) |
57
|
1 |
|
with self.assertRaises(ua.UaStatusCodeError): |
58
|
1 |
|
f = objects.add_folder(3, 'MyFolder') |
59
|
|
|
|
60
|
1 |
|
def test_folder_anonymous(self): |
61
|
1 |
|
objects = self.clt.get_objects_node() |
62
|
1 |
|
f = objects.add_folder(3, 'MyFolderRO') |
63
|
1 |
|
f_ro = self.ro_clt.get_node(f.nodeid) |
64
|
1 |
|
self.assertEqual(f, f_ro) |
65
|
1 |
|
with self.assertRaises(ua.UaStatusCodeError): |
66
|
1 |
|
f2 = f_ro.add_folder(3, 'MyFolder2') |
67
|
|
|
|
68
|
1 |
|
def test_variable_anonymous(self): |
69
|
1 |
|
objects = self.clt.get_objects_node() |
70
|
1 |
|
v = objects.add_variable(3, 'MyROVariable', 6) |
71
|
1 |
|
v.set_value(4) #this should work |
72
|
1 |
|
v_ro = self.ro_clt.get_node(v.nodeid) |
73
|
1 |
|
with self.assertRaises(ua.UaStatusCodeError): |
74
|
1 |
|
v_ro.set_value(2) |
75
|
1 |
|
self.assertEqual(v_ro.get_value(), 4) |
76
|
1 |
|
v.set_writable(True) |
77
|
1 |
|
v_ro.set_value(2) #now it should work |
78
|
1 |
|
self.assertEqual(v_ro.get_value(), 2) |
79
|
1 |
|
v.set_writable(False) |
80
|
1 |
|
with self.assertRaises(ua.UaStatusCodeError): |
81
|
1 |
|
v_ro.set_value(9) |
82
|
1 |
|
self.assertEqual(v_ro.get_value(), 2) |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
|