summaryrefslogtreecommitdiff
path: root/tests/modules/test_vy_device.py
blob: 096e924265db1324a5071f775204961a3c8b181b (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import random
import string

import pytest
import requests

from pyvyos import VyDevice
from pyvyos.rest import RestClient


def test_device_invalid_configuration():
    with pytest.raises(ValueError):
        VyDevice(
            "tst",
            "123",
            "123",
            "123",
            2,
        )


def test_device_configuration(test_device):
    assert isinstance(test_device.hostname, str)
    assert isinstance(test_device.apikey, str)
    assert isinstance(test_device.port, int)
    assert isinstance(test_device.protocol, str)
    assert isinstance(test_device.verify, bool)


def test_device_retrieve_show_config(monkeypatch, test_device):
    def mock_retrieve_show_config(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": {
                "config-management": {"commit-revisions": "100"},
                "host-name": "pyvyos-device",
                "name-server": "eth0",
            },
            "error": None,
        }

        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_retrieve_show_config,
    )

    api_resp = test_device.retrieve_show_config(["system"])
    assert api_resp.result
    assert isinstance(api_resp.result, dict)


def test_device_configure_set(monkeypatch, test_device):
    def mock_configure_set(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": {},
            "error": None,
        }

        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_configure_set,
    )

    api_resp = test_device.configure_set(
        path=["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"]
    )
    assert isinstance(api_resp.result, dict)


def test_device_retrieve_return_values(monkeypatch, test_device):
    def mock_retrieve_return_values(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": ["192.168.56.100/24"],
            "error": None,
        }

        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_retrieve_return_values,
    )

    api_resp = test_device.retrieve_return_values(
        path=["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"]
    )
    assert api_resp.result
    assert isinstance(api_resp.result, list)


def test_device_configure_delete(monkeypatch, test_device):
    def mock_configure_delete(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": {},
            "error": None,
        }

        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_configure_delete,
    )

    api_resp = test_device.configure_delete(path=["interfaces", "dummy", "dum1"])
    assert isinstance(api_resp.result, dict)


def test_device_generate(monkeypatch, test_device):
    def mock_configure_delete(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": f"""'Generating public/private rsa key pair.\n'
                   'Your identification has been saved in '
                   '/tmp/key_{keyrand}\n'
                   'Your public key has been saved in '
                   '/tmp/key_39skBBzxc5NvIfE8GzQ7.pub\n'
                   'The key fingerprint is:\n'
                   'SHA256:7vfukWp093kyngUB+iH6'
                   'root@pyvyos-device\n'
                   "The key's randomart image is:\n"
                   '+---[RSA 3072]----+\n'
                   '|        .   .    |\n'
                   '|       o + . - . |\n'
                   '|      . = = = +  |\n'
                   '|       o + O - o.|\n'
                   '|      . S B . .oo|\n'
                   '|       o *. ..oo.|\n'
                   '|      . ....o. Eo|\n'
                   '|     . +...+ .o+o|\n'
                   '|      ..o=o++.oo.|\n'
                   '+----[SHA256]-----+\n'""",
            "error": None,
        }

        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_configure_delete,
    )

    randstring = "".join(
        random.choice(string.ascii_letters + string.digits) for _ in range(20)
    )
    keyrand = f"/tmp/key_{randstring}"

    api_resp = test_device.generate(path=["ssh", "client-key", keyrand])
    assert isinstance(api_resp.result, str)
    assert keyrand in api_resp.result


def test_device_show(monkeypatch, test_device):
    def mock_show(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": """'Name                   Default boot    Running  \n'
                   '------------------------  --------------  --------- \n'
                   '1.5-rolling-202407300021       Yes           Yes    \n'
            """,
            "error": None,
        }
        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_show,
    )

    api_resp = test_device.show(path=["system", "image"])
    assert isinstance(api_resp.result, str)


def test_device_reset(monkeypatch, test_device):
    def mock_reset(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": """'conntrack-sync is not configured!\n'""",
            "error": None,
        }
        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_reset,
    )

    api_resp = test_device.reset(path=["system", "image"])
    assert isinstance(api_resp.result, str)


def test_device_config_file_save(monkeypatch, test_device):
    def mock_config_file_save(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": "",
            "error": None,
        }
        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_config_file_save,
    )

    api_resp = test_device.config_file_save(file="/config/teste.config")
    assert isinstance(api_resp.result, str)


def test_device_config_file_load(monkeypatch, test_device):
    def mock_config_file_save(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": None,
            "error": None,
        }
        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_config_file_save,
    )

    api_resp = test_device.config_file_load(file="/config/teste.config")
    assert api_resp


def test_device_configure_multiple_op(monkeypatch, test_device):
    def mock_config_file_save(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": None,
            "error": None,
        }
        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_config_file_save,
    )

    api_resp = test_device.configure_multiple_op(
        op_path=[
            {
                "op": "set",
                "path": ["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"],
            },
            {
                "op": "delete",
                "path": ["interfaces", "dummy", "dum1"],
            },
        ]
    )
    assert api_resp


def test_device_invalid_path_multiple_op(monkeypatch, test_device):
    def mock_configure_set(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.json = lambda: {
            "success": True,
            "data": {},
            "error": None,
        }

        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_configure_set,
    )
    with pytest.raises(ValueError):
        test_device.configure_multiple_op(op_path="interfaces")


def test_device_invalid_request(monkeypatch, test_device):
    def mock_invalid_request(*args, **kwargs):
        response = requests.Response()
        response.status_code = 400
        response.json = lambda: {
            "success": False,
            "data": None,
            "error": "Bad Request",
        }
        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_invalid_request,
    )

    api_resp = test_device.show(path=["invalid", "path"])
    assert not api_resp.result
    assert api_resp.status == 400
    assert "HTTP Error" in api_resp.error


def test_device_error_json_response(monkeypatch, test_device):
    def mock_error_json_response(*args, **kwargs):
        response = requests.Response()
        response.status_code = 200
        response.data = "This is not a JSON response"
        return response

    monkeypatch.setattr(
        RestClient,
        "_execute_request",
        mock_error_json_response,
    )

    api_resp = test_device.show(path=["system", "image"])
    assert not api_resp.result
    assert "Invalid response format" in api_resp.error