File: test_generic_async_driver.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 (155 lines) | stat: -rw-r--r-- 5,595 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
import pytest

from scrapli.driver.generic.base_driver import ReadCallback
from scrapli.exceptions import ScrapliValueError


async def test_get_prompt(monkeypatch, async_generic_driver):
    async def _get_prompt(cls):
        return "scrapli>"

    # stupid test w/ the patch, but want coverage and in the future maybe the driver actually
    # does something to the prompt it gets from the channel
    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.get_prompt", _get_prompt)
    assert await async_generic_driver.get_prompt() == "scrapli>"


async def test__send_command(monkeypatch, async_generic_driver):
    async def _send_input(cls, **kwargs):
        return b"raw", b"processed"

    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.send_input", _send_input)
    actual_response = await async_generic_driver._send_command(command="nada")
    assert actual_response.failed is False
    assert actual_response.result == "processed"
    assert actual_response.raw_result == b"raw"


async def test__send_command_no_base_transport_args(async_generic_driver):
    async_generic_driver._base_transport_args = None
    with pytest.raises(ScrapliValueError):
        await async_generic_driver._send_command(command="nada")


async def test_send_command(monkeypatch, async_generic_driver):
    async def _send_input(cls, **kwargs):
        return b"raw", b"processed"

    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.send_input", _send_input)
    actual_response = await async_generic_driver.send_command(command="nada")
    assert actual_response.failed is False
    assert actual_response.result == "processed"
    assert actual_response.raw_result == b"raw"


async def test_send_commands(monkeypatch, async_generic_driver):
    async def _send_input(cls, **kwargs):
        return b"raw", b"processed"

    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.send_input", _send_input)
    actual_response = await async_generic_driver.send_commands(commands=["nada", "nada2"])
    assert len(actual_response) == 2
    assert actual_response.failed is False
    assert actual_response[0].failed is False
    assert actual_response[0].result == "processed"
    assert actual_response[0].raw_result == b"raw"


async def test_send_commands_from_file(
    fs_, monkeypatch, real_ssh_commands_file_path, async_generic_driver
):
    fs_.add_real_file(source_path=real_ssh_commands_file_path, target_path="/commands")

    async def _send_input(cls, **kwargs):
        return b"raw", b"processed"

    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.send_input", _send_input)
    actual_response = await async_generic_driver.send_commands_from_file(file="commands")
    assert actual_response.failed is False
    assert actual_response[0].result == "processed"
    assert actual_response[0].raw_result == b"raw"


async def test_send_and_read(monkeypatch, async_generic_driver):
    async def _send_input_and_read(cls, **kwargs):
        return b"raw", b"processed"

    monkeypatch.setattr(
        "scrapli.channel.async_channel.AsyncChannel.send_input_and_read", _send_input_and_read
    )
    actual_response = await async_generic_driver.send_and_read(channel_input="nada")
    assert actual_response.failed is False
    assert actual_response.result == "processed"
    assert actual_response.raw_result == b"raw"


async def test_send_and_read_no_base_transport_args(async_generic_driver):
    async_generic_driver._base_transport_args = None
    with pytest.raises(ScrapliValueError):
        await async_generic_driver.send_and_read(channel_input="nada")


async def test_send_interactive(monkeypatch, async_generic_driver):
    async def _send_inputs_interact(cls, **kwargs):
        return b"raw", b"processed"

    monkeypatch.setattr(
        "scrapli.channel.async_channel.AsyncChannel.send_inputs_interact", _send_inputs_interact
    )

    actual_response = await async_generic_driver.send_interactive(
        interact_events=[("nada", "scrapli>")]
    )
    assert actual_response.failed is False
    assert actual_response.result == "processed"
    assert actual_response.raw_result == b"raw"


async def test_send_interact_no_base_transport_args(async_generic_driver):
    async_generic_driver._base_transport_args = None
    with pytest.raises(ScrapliValueError):
        await async_generic_driver.send_interactive(interact_events=[])


async def test_readcallback_basic(monkeypatch, async_generic_driver):
    async def _read(cls):
        return b"rtr1#"

    def _write(cls, channel_input, redacted=False):
        return

    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.read", _read)
    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.write", _write)

    callback_one_counter = 0
    callback_two_counter = 0

    async def callback_one(cls, read_output):
        nonlocal callback_one_counter

        callback_one_counter += 1

    async def callback_two(cls, read_output):
        nonlocal callback_two_counter

        callback_two_counter += 1

    callbacks = [
        ReadCallback(
            contains="rtr1#",
            callback=callback_one,
            name="call1",
            case_insensitive=False,
            only_once=True,
        ),
        ReadCallback(
            contains_re=r"^rtr1#",
            callback=callback_two,
            complete=True,
        ),
    ]

    await async_generic_driver.read_callback(callbacks=callbacks, initial_input="nada")

    assert callback_one_counter == 1
    assert callback_two_counter == 1