File: test_cas.py

package info (click to toggle)
python-pyepics 3.5.7%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,336 kB
  • sloc: python: 10,539; makefile: 112; javascript: 104; sh: 53
file content (180 lines) | stat: -rw-r--r-- 5,258 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import time
import pytest
import subprocess
from tempfile import NamedTemporaryFile as NTF
import epics


cas_test_db = '''
                record(ao, "test:ao") {
                    field(ASG, "rps_threshold")
                    field(DRVH, "10")
                    field(DRVL, "0")
                }

                record(bo, "test:bo") {
                    field(ASG, "rps_lock")
                    field(ZNAM, "OUT")
                    field(ONAM, "IN")
                }

                record(ao, "test:ao2") {
                    field(DRVH, "5")
                    field(DRVL, "1")
                }

                record(bo, "test:permit") {
                    field(VAL, "0")
                    field(PINI, "1")
                    field(ZNAM, "DISABLED")
                    field(ONAM, "ENABLED")
                }
            '''

cas_rules = '''
                ASG(DEFAULT) {
                    RULE(1,READ)
                    RULE(1,WRITE,TRAPWRITE)
                }

                ASG(rps_threshold) {
                    INPA("$(P):permit")
                    RULE(1, READ)
                    RULE(0, WRITE, TRAPWRITE) {
                        CALC("A=1")
                    }
                    RULE(1, WRITE, TRAPWRITE) {
                        CALC("A=0")
                    }
                }

                ASG(rps_lock) {
                    INPA("$(P):permit")
                    RULE(1, READ)
                    RULE(1, WRITE, TRAPWRITE) {
                        CALC("A=0")
                    }
                }
            '''

# use yield_fixture() for compatibility with pytest < 2.10
@pytest.yield_fixture(scope='module')
def softioc():
    with NTF(mode='w+') as cf, NTF(mode='w+') as df:
        cf.write(cas_rules)
        cf.flush()
        df.write(cas_test_db)
        df.flush()

        proc = subprocess.Popen(['softIoc', '-D',
                                 '/home/travis/mc/envs/testenv/epics/dbd/softIoc.dbd',
                                 '-m', 'P=test', '-a', cf.name,
                                 '-d', df.name],
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
        yield proc

        try:
            proc.kill()
            proc.wait()
        except OSError:
            pass


@pytest.yield_fixture(scope='module')
def pvs():
    pvlist = ['test:ao', 'test:ao.DRVH', 'test:bo', 'test:ao2',
              'test:permit']
    pvs = dict()
    for name in pvlist:
        pv = epics.get_pv(name)
        pv.wait_for_connection()
        pvs[pv.pvname] = pv

    yield pvs
    for pv in pvs.values():
        pv.disconnect()


def test_connected(softioc, pvs):
    for pv in pvs.values():
        assert pv.connected

def test_permit_disabled(softioc, pvs):
    # with the permit disabled, all test pvs should be readable/writable
    for pv in pvs.values():
        assert pv.read_access and pv.write_access

def test_permit_enabled(softioc, pvs):
    # set the run-permit
    pvs['test:permit'].put(1, wait=True)
    assert pvs['test:permit'].get(as_string=True, use_monitor=False) == 'ENABLED'

    # rps_lock rule should disable write access
    assert pvs['test:bo'].write_access is False
    with pytest.raises(epics.ca.CASeverityException):
        pvs['test:bo'].put(1, wait=True)

    # rps_threshold rule should disable write access to metadata, not VAL
    assert pvs['test:ao'].write_access is True
    assert pvs['test:ao.DRVH'].write_access is False
    with pytest.raises(epics.ca.CASeverityException):
        pvs['test:ao.DRVH'].put(100, wait=True)

def test_pv_access_event_callback(softioc, pvs):
    # clear the run-permit
    pvs['test:permit'].put(0, wait=True)
    assert pvs['test:permit'].get(as_string=True, use_monitor=False) == 'DISABLED'

    def lcb(read_access, write_access, pv=None):
        assert pv.read_access == read_access
        assert pv.write_access == write_access
        pv.flag = True

    bo = epics.get_pv('test:bo', access_callback=lcb)
    bo.flag = False

    # set the run-permit to trigger an access rights event
    pvs['test:permit'].put(1, wait=True)
    assert pvs['test:permit'].get(as_string=True, use_monitor=False) == 'ENABLED'

    assert bo.flag is True
    bo.access_callbacks = []


def test_ca_access_event_callback(softioc, pvs):
    # clear the run-permit
    pvs['test:permit'].put(0, wait=True)
    assert pvs['test:permit'].get(as_string=True, use_monitor=False) == 'DISABLED'

    bo_id = epics.ca.create_channel('test:bo')
    assert bo_id is not None

    def lcb(read_access, write_access):
        assert read_access and write_access
        lcb.sentinal = True

    lcb.sentinal = False
    epics.ca.replace_access_rights_event(bo_id, callback=lcb)

    assert lcb.sentinal is True
    epics.ca.clear_channel(bo_id)


def test_connection_callback(softioc, pvs):
    results = []

    def callback(conn, **kwargs):
        results.append(conn)

    pv = epics.PV('test:ao', connection_callback=callback)
    pv.wait_for_connection()
    softioc.kill()
    softioc.wait()

    t0 = time.time()
    while pv.connected and (time.time() - t0) < 5:
        time.sleep(0.1)

    assert True in results
    assert False in results