File: test_general.py

package info (click to toggle)
librouteros 3.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 376 kB
  • sloc: python: 1,579; makefile: 141; sh: 4
file content (187 lines) | stat: -rw-r--r-- 5,262 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
181
182
183
184
185
186
187
import socket
import pytest
from time import sleep
from librouteros.query import Key
from librouteros import connect, async_connect
from librouteros.exceptions import LibRouterosError


def test_login(routeros_login):
    port, method = routeros_login("sync")
    api = None
    for _ in range(30):
        try:
            api = connect(
                host="127.0.0.1",
                port=port,
                username="admin",
                password="",
                login_method=method,
            )
            break
        except (LibRouterosError, socket.error, socket.timeout):
            sleep(1)

    data = api("/system/identity/print")
    assert tuple(data)[0]["name"] == "MikroTik"


@pytest.mark.asyncio
async def test_login_async(routeros_login):
    port, method = routeros_login("async")
    api = None
    for _ in range(30):
        try:
            api = await async_connect(
                host="127.0.0.1",
                port=port,
                username="admin",
                password="",
                login_method=method,
                timeout=60,
            )
            break
        except (LibRouterosError, socket.error, socket.timeout):
            sleep(1)

    result = [r async for r in api("/system/identity/print")]
    assert result[0]["name"] == "MikroTik"


def test_query(routeros_api):
    new_address = "172.16.1.1/24"
    result = routeros_api(
        "/ip/address/add",
        address=new_address,
        interface="ether1",
    )
    created_id = tuple(result)[0]["ret"]

    _id = Key(".id")
    address = Key("address")
    query = (
        routeros_api.path("/ip/address")
        .select(_id, address)
        .where(
            _id == created_id,
            address == new_address,
        )
    )
    selected_data = tuple(query)
    assert len(selected_data) == 1
    assert selected_data[0][".id"] == created_id
    assert selected_data[0]["address"] == new_address


@pytest.mark.asyncio
async def test_query_async(routeros_api_async):
    new_address = "172.16.1.1/24"
    result = [
        r
        async for r in routeros_api_async(
            "/ip/address/add",
            address=new_address,
            interface="ether1",
        )
    ]
    created_id = result[0]["ret"]

    _id = Key(".id")
    address = Key("address")
    selected_data = [
        r
        async for r in routeros_api_async.path("/ip/address")
        .select(_id, address)
        .where(
            _id == created_id,
            address == new_address,
        )
    ]
    assert len(selected_data) == 1
    assert selected_data[0][".id"] == created_id
    assert selected_data[0]["address"] == new_address


@pytest.mark.parametrize(
    "addresses",
    (
        {"172.16.1.1/24", "172.16.1.2/24"},
        {"172.16.1.1/24", "172.16.1.2/24", "1.1.1.1/24"},
        {"172.16.1.2/24"},
    ),
)
def test_query_In_operator(routeros_api, addresses):
    addr_path = routeros_api.path("/ip/address")
    for addr in addresses:
        addr_path.add(interface="ether1", address=addr)

    address = Key("address")
    query = addr_path.select(address).where(address.In(*addresses))
    assert addresses == set(row["address"] for row in query)


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "addresses",
    (
        {"172.16.1.1/24", "172.16.1.2/24"},
        {"172.16.1.1/24", "172.16.1.2/24", "1.1.1.1/24"},
        {"172.16.1.2/24"},
    ),
)
async def test_query_In_operator_async(routeros_api_async, addresses):
    addr_path = routeros_api_async.path("/ip/address")
    for addr in addresses:
        await addr_path.add(interface="ether1", address=addr)

    address = Key("address")
    result = [r async for r in addr_path.select(address).where(address.In(*addresses))]
    assert addresses == set(row["address"] for row in result)


def test_long_word(routeros_api):
    r"""
    Assert that when word length is encoded with \x00 in it,
    library should decode this without errors.

    Create a entry with long word length (256)
    resulting in word encoding of \x81\00.
    Check if when reading back, comment is same when set.
    """
    long_value = "a" * (256 - len("=comment="))
    data = routeros_api(
        "/ip/address/add",
        address="172.16.1.1/24",
        interface="ether1",
        comment=long_value,
    )
    _id = tuple(data)[0]["ret"]
    for row in routeros_api("/ip/address/print"):
        if row[".id"] == _id:
            assert row["comment"] == long_value


@pytest.mark.asyncio
async def test_long_word_async(routeros_api_async):
    r"""
    Assert that when word length is encoded with \x00 in it,
    library should decode this without errors.

    Create a entry with long word length (256)
    resulting in word encoding of \x81\00.
    Check if when reading back, comment is same when set.
    """
    long_value = "a" * (256 - len("=comment="))
    data = [
        r
        async for r in routeros_api_async(
            "/ip/address/add",
            address="172.16.1.1/24",
            interface="ether1",
            comment=long_value,
        )
    ]
    _id = data[0]["ret"]
    async for row in routeros_api_async("/ip/address/print"):
        if row[".id"] == _id:
            assert row["comment"] == long_value