File: test_drivers_network_async.py

package info (click to toggle)
python-scrapli 2023.7.30-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,536 kB
  • sloc: python: 14,459; makefile: 72
file content (224 lines) | stat: -rw-r--r-- 8,706 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
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
import time

import pytest
from expected import get_expected, write_expected


class TestAsyncNetworkDevice:
    @pytest.fixture(autouse=True)
    def __setup(self, test_cases):
        self.test_cases = test_cases

    @pytest.mark.parametrize(
        "priv_level",
        ["exec", "privilege_exec", "configuration"],
        ids=["exec", "privilege_exec", "configuration"],
    )
    async def test_get_prompt_and_acquire_priv(self, async_conn, priv_level, device_type, update):
        if (device_type in ("cisco_nxos", "cisco_iosxr") and priv_level == "exec") or (
            device_type == "juniper_junos" and priv_level == "privilege_exec"
        ):
            pytest.skip(f"Priv level {priv_level} for device type {device_type} not tested")

        await async_conn.acquire_priv(priv_level)
        actual = await async_conn.get_prompt()

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    @pytest.mark.parametrize(
        "strip_prompt",
        [True, False],
        ids=["strip_prompt", "no_strip_prompt"],
    )
    async def test_send_command_short(self, async_conn, device_type, strip_prompt, update):
        command = self.test_cases[device_type]["send_command_short"]["command"]
        sanitize_response = self.test_cases[device_type]["sanitize_response"]

        response = await async_conn.send_command(command=command, strip_prompt=strip_prompt)

        actual = sanitize_response(response.result)

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    @pytest.mark.parametrize(
        "strip_prompt",
        [True, False],
        ids=["strip_prompt", "no_strip_prompt"],
    )
    async def test_send_command_long(self, async_conn, device_type, strip_prompt, update):
        command = self.test_cases[device_type]["send_command_long"]["command"]
        sanitize_response = self.test_cases[device_type]["sanitize_response"]

        response = await async_conn.send_command(command=command, strip_prompt=strip_prompt)

        actual = sanitize_response(response.result)

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    @pytest.mark.parametrize(
        "strip_prompt",
        [True, False],
        ids=["strip_prompt", "no_strip_prompt"],
    )
    async def test_send_commands(self, async_conn, device_type, strip_prompt, update):
        commands = [
            self.test_cases[device_type]["send_command_short"]["command"],
            self.test_cases[device_type]["send_command_long"]["command"],
        ]
        sanitize_response = self.test_cases[device_type]["sanitize_response"]

        response = await async_conn.send_commands(commands=commands, strip_prompt=strip_prompt)

        actual = sanitize_response(response.result)

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    @pytest.mark.parametrize(
        "strip_prompt",
        [True, False],
        ids=["strip_prompt", "no_strip_prompt"],
    )
    async def test_send_commands_from_file(self, async_conn, device_type, strip_prompt, update):
        file = self.test_cases[device_type]["send_commands_from_file"]["file"]
        sanitize_response = self.test_cases[device_type]["sanitize_response"]

        response = await async_conn.send_commands_from_file(file=file, strip_prompt=strip_prompt)

        actual = sanitize_response(response.result)

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    async def test_send_commands_stop_on_failed(self, async_conn, device_type):
        commands = self.test_cases[device_type]["send_commands_error"]["commands"]

        responses = await async_conn.send_commands(commands=commands, stop_on_failed=True)

        assert len(responses) == 2
        assert responses[0].failed is False
        assert responses[1].failed is True

    async def test_send_interactive_normal_response(self, async_conn, device_type, update):
        if self.test_cases[device_type]["send_interactive_normal_response"] is None:
            pytest.skip(
                f"send_interactive_normal_response for device type {device_type} not tested"
            )
        interact = self.test_cases[device_type]["send_interactive_normal_response"]["command"]

        response = await async_conn.send_interactive(interact_events=interact)

        actual = response.result

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    @pytest.mark.parametrize(
        "strip_prompt",
        [True, False],
        ids=["strip_prompt", "no_strip_prompt"],
    )
    async def test_send_config(self, async_conn, device_type, strip_prompt, update):
        config = self.test_cases[device_type]["send_config"]["configs"]
        teardown_configs = self.test_cases[device_type]["send_config"]["teardown_configs"]
        sanitize_response = self.test_cases[device_type]["sanitize_response"]

        response = await async_conn.send_config(config=config)
        if isinstance(teardown_configs, list):
            await async_conn.send_configs(configs=teardown_configs)
        else:
            await async_conn.send_config(config=teardown_configs)

        actual = sanitize_response(response.result)

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    @pytest.mark.parametrize(
        "strip_prompt",
        [True, False],
        ids=["strip_prompt", "no_strip_prompt"],
    )
    async def test_send_configs(self, async_conn, device_type, strip_prompt, update):
        configs = self.test_cases[device_type]["send_configs"]["configs"]
        teardown_configs = self.test_cases[device_type]["send_config"]["teardown_configs"]
        sanitize_response = self.test_cases[device_type]["sanitize_response"]

        response = await async_conn.send_configs(configs=configs)
        if isinstance(teardown_configs, list):
            await async_conn.send_configs(configs=teardown_configs)
        else:
            await async_conn.send_config(config=teardown_configs)

        actual = sanitize_response(response.result)

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    @pytest.mark.parametrize(
        "strip_prompt",
        [True, False],
        ids=["strip_prompt", "no_strip_prompt"],
    )
    async def test_send_configs_from_file(self, async_conn, device_type, strip_prompt, update):
        file = self.test_cases[device_type]["send_configs_from_file"]["file"]
        teardown_configs = self.test_cases[device_type]["send_config"]["teardown_configs"]
        sanitize_response = self.test_cases[device_type]["sanitize_response"]

        response = await async_conn.send_configs_from_file(file=file)
        if isinstance(teardown_configs, list):
            await async_conn.send_configs(configs=teardown_configs)
        else:
            await async_conn.send_config(config=teardown_configs)

        actual = sanitize_response(response.result)

        if update:
            write_expected(f=__file__, result=actual)

        assert actual == get_expected(f=__file__)

    async def test_send_configs_stop_on_failed(self, async_conn, device_type):
        if self.test_cases[device_type]["send_configs_error"] is None:
            pytest.skip(
                f"test_send_configs_stop_on_failed for device type {device_type} not tested yet, need to overhaul privilege levels..."
            )

        configs = self.test_cases[device_type]["send_configs_error"]["configs"]
        teardown_configs = self.test_cases[device_type]["send_configs_error"]["teardown_configs"]
        responses = await async_conn.send_configs(configs=configs, stop_on_failed=True)

        assert len(responses) == 2
        assert responses[0].failed is False
        assert responses[1].failed is True
        if isinstance(teardown_configs, list):
            await async_conn.send_configs(configs=teardown_configs)
        else:
            await async_conn.send_config(config=teardown_configs)

    async def test_isalive_and_close(self, async_conn, device_type):
        assert async_conn.isalive() is True
        await async_conn.close()
        # unsure why but w/out a tiny sleep pytest just plows ahead and the connection doesnt
        # close in time for the next assert
        time.sleep(0.5)
        assert async_conn.isalive() is False