aboutsummaryrefslogtreecommitdiffstats
path: root/pyagl/tests/test_network.py
blob: 53653cfc6906849b23dfc4a78deb90aa38924d4c (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import asyncio
import os
import pytest
import logging
from pyagl.services.base import AFBResponse, AFBT
from pyagl.services.network import NetworkService as NS

pytestmark = [pytest.mark.asyncio, pytest.mark.network]


@pytest.fixture(scope='module')
def event_loop():
    loop = asyncio.get_event_loop()
    yield loop


@pytest.fixture(scope='module')
async def service():
    address = os.environ.get('AGL_TGT_IP', 'localhost')
    port = os.environ.get('AGL_TGT_PORT', None)
    ns = await NS(ip=address, port=port)
    yield ns
    await ns.websocket.close()


@pytest.fixture(scope='module')
def expected_available_techs():
    techs = os.environ.get('AGL_AVAILABLE_TECHS', 'wifi,ethernet,bluetooth').split(',')
    return techs


@pytest.mark.regular
async def test_state(event_loop, service: NS):
    msgid = await service.state()
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info
    assert resp.data == 'online'


@pytest.mark.regular
async def test_global_offline(event_loop, service: NS):
    addr, _ = service.websocket.remote_address
    print(f"Remote address is {addr}")
    if addr != 'localhost' or addr != '127.0.0.1':
        pytest.skip('We want to skip global offline mode until this is ran locally on the board against localhost')
    msgid = await service.offline(True)
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_scan_services(event_loop, service: NS, expected_available_techs):
    scannable_techs = ['wifi', 'bluetooth']
    for t in scannable_techs:
        if t in expected_available_techs:
            msgid = service.scan_services(technology=t)
            resp = await service.afbresponse()
            assert resp.status == 'success', f'scan_services failed for technology {t} - {resp.info}'


@pytest.mark.regular
async def test_global_online(event_loop, service: NS):
    msgid = await service.offline(False)
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
@pytest.mark.dependency
async def test_technologies_verb(event_loop, service: NS):
    msgid = await service.technologies()
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info
    assert 'values' in resp.data


@pytest.mark.regular
@pytest.mark.dependency(depends=['test_technologies_verb'])
async def test_expected_existing_technologies(event_loop, service: NS, expected_available_techs):
    msgid = await service.technologies()
    resp = await service.afbresponse()
    techs = [t['technology'] for t in resp.data['values']]
    for t in expected_available_techs:
        assert t in techs, f'"{t}" technology is expected to be available, but it is not'


@pytest.mark.regular
@pytest.mark.dependency(depends=['test_expected_existing_technologies'])
async def test_get_property(event_loop, service: NS, expected_available_techs):
    for t in expected_available_techs:
        msgid = await service.get_property(t)
        resp = await service.afbresponse()
        assert resp.status == 'success', resp.info
        assert isinstance(resp.data, dict)
        expected_fields = frozenset(['name', 'type', 'powered', 'connected', 'tethering'])
        # diverging_fields = frozenset(expected_fields).symmetric_difference(frozenset(resp.data.keys()))
        diverging_fields = frozenset(expected_fields).difference(frozenset(resp.data.keys()))
        assert len(diverging_fields) == 0, f'the following property fields are diverging from the expected: {diverging_fields}'


@pytest.mark.regular
@pytest.mark.xfail(reason='Expecting this to throw "permission denied" via the API, tethering from connmanctl succeeds')
async def test_enable_wifi_tethering(event_loop, service: NS, expected_available_techs):
    if 'wifi' not in expected_available_techs:
        pytest.skip('Skipping wifi tethering test because its not marked as available technology')
    msgid = await service.set_property('wifi', {'tethering': True,
                                                'TetheringIdentifier': 'AGL',
                                                'TetheringPassphrase': 'agltestwifi'})
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
@pytest.mark.dependency(depends='test_enable_wifi_tethering')
async def test_disable_wifi_tethering(event_loop, service: NS, expected_available_techs):
    if 'wifi' not in expected_available_techs:
        pytest.skip('Skipping wifi tethering test because its not marked as available technology')
    msgid = await service.set_property('wifi', {'tethering': False})
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


#
#@pytest.mark.regular
# async def test_set_property(event_loop, service: NS, expected_available_techs):
#     for t in expected_available_techs:
#         msgid = await service.set_property(t, {'tethering': True})
#         resp = await service.afbresponse()
#         assert resp.status == 'success', resp.info
#         print(resp)


@pytest.mark.regular
async def test_services_verb(event_loop, service: NS):
    msgid = await service.services()
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info
    assert 'values' in resp.data


@pytest.mark.regular
async def test_subscribe_global_state(event_loop, service: NS):
    msgid = await service.subscribe('global_state')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_unsubscribe_global_state(event_loop, service: NS):
    msgid = await service.unsubscribe('global_state')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_subscribe_technologies(event_loop, service: NS):
    msgid = await service.subscribe('technologies')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_unsubscribe_technologies(event_loop, service: NS):
    msgid = await service.unsubscribe('technologies')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_subscribe_tech_props(event_loop, service: NS):
    msgid = await service.subscribe('technology_properties')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_unsubscribe_tech_props(event_loop, service: NS):
    msgid = await service.unsubscribe('technology_properties')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_subscribe_services(event_loop, service: NS):
    msgid = await service.subscribe('services')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_unsubscribe_services(event_loop, service: NS):
    msgid = await service.unsubscribe('services')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_subscribe_service_props(event_loop, service: NS):
    msgid = await service.subscribe('service_properties')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_unsubscribe_service_props(event_loop, service: NS):
    msgid = await service.unsubscribe('service_properties')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_subscribe_agent(event_loop, service: NS):
    msgid = await service.subscribe('agent')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_unsubscribe_agent(event_loop, service: NS):
    msgid = await service.unsubscribe('agent')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_enable_wifi(event_loop, service: NS, expected_available_techs):
    if 'wifi' not in expected_available_techs:
        pytest.skip('Skipping enable_technology for "wifi" because it is not expected to be available')
    msgid = await service.enable_technology('wifi')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_disable_wifi(event_loop, service: NS, expected_available_techs):
    if 'wifi' not in expected_available_techs:
        pytest.skip('Skipping disable_technology for "wifi" because it is not expected to be available')
    msgid = await service.disable_technology('wifi')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_enable_bluetooth(event_loop, service: NS, expected_available_techs):
    if 'bluetooth' not in expected_available_techs:
        pytest.skip('Skipping enable_technology for "bluetooth" because it is not expected to be available')
    msgid = await service.enable_technology('bluetooth')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_disable_bluetooth(event_loop, service: NS, expected_available_techs):
    if 'bluetooth' not in expected_available_techs:
        pytest.skip('Skipping disable_technology for "bluetooth" because it is not expected to be available')
    msgid = await service.disable_technology('bluetooth')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_enable_ethernet(event_loop, service: NS, expected_available_techs):
    if 'ethernet' not in expected_available_techs:
        pytest.skip('Skipping enable_technology for "ethernet" because it is not expected to be available')
    msgid = await service.enable_technology('ethernet')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info


@pytest.mark.regular
async def test_disable_ethernet(event_loop, service: NS, expected_available_techs):
    addr, _ = service.websocket.remote_address
    if addr != 'localhost' or addr != '127.0.0.1':
        pytest.skip('Skipping this test until it is ran locally on the board, '
                    'presuming it is the only available interface to connect to remotely for testing')
    if 'ethernet' not in expected_available_techs:
        pytest.skip('Skipping disable_technology for "ethernet" because it is not expected to be available')

    msgid = await service.disable_technology('ethernet')
    resp = await service.afbresponse()
    assert resp.status == 'success', resp.info