Skip to content

Commit c019cc2

Browse files
committed
Fake: support adding a random sleep and injecting failures
This allows us to more closely model real switch devices. Change-Id: Ia28a08b5426cd71c03d51e7b6149f8fda37fc91c
1 parent 5246705 commit c019cc2

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

networking_generic_switch/devices/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
{'name': 'ngs_save_configuration', 'default': True},
5151
# When true try to batch up in flight switch requests
5252
{'name': 'ngs_batch_requests', 'default': False},
53+
# The following three are used in the Fake device driver.
54+
{'name': 'ngs_fake_sleep_min_s'},
55+
{'name': 'ngs_fake_sleep_max_s'},
56+
{'name': 'ngs_fake_failure_prob'},
5357
]
5458

5559

networking_generic_switch/devices/netmiko_devices/fake.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13+
import contextlib
14+
import random
15+
import time
16+
1317
from oslo_log import log as logging
1418

1519
from networking_generic_switch.devices import netmiko_devices
@@ -18,6 +22,42 @@
1822
LOG = logging.getLogger(__name__)
1923

2024

25+
class FakeConnection(object):
26+
"""A Fake netmiko connection object."""
27+
28+
def __init__(self, device):
29+
self.device = device
30+
31+
def enable(self):
32+
pass
33+
34+
def send_config_set(self, config_commands, cmd_verify):
35+
# Allow adding a random sleep to commands.
36+
if self.device.ngs_config.get('ngs_fake_sleep_max_s'):
37+
sleep_min_s = self.device.ngs_config.get('ngs_fake_sleep_min_s', 0)
38+
sleep_max_s = self.device.ngs_config['ngs_fake_sleep_max_s']
39+
sleep_duration = random.uniform(float(sleep_min_s),
40+
float(sleep_max_s))
41+
time.sleep(sleep_duration)
42+
43+
# Allow injecting random failures.
44+
if self.device.ngs_config.get('ngs_fake_failure_prob'):
45+
failure_prob = self.device.ngs_config['ngs_fake_failure_prob']
46+
if random.random() < float(failure_prob):
47+
raise Exception("Random failure!")
48+
49+
for cmd in config_commands:
50+
LOG.info("%s", cmd)
51+
return "Success!"
52+
53+
def save_config(self):
54+
pass
55+
56+
def send_command(self, command):
57+
LOG.info("%s", command)
58+
return "Success!"
59+
60+
2161
class Fake(netmiko_devices.NetmikoSwitch):
2262
"""Netmiko device driver for Fake switches."""
2363

@@ -62,11 +102,7 @@ class Fake(netmiko_devices.NetmikoSwitch):
62102
device output that indicate a failure to apply configuration.
63103
"""
64104

65-
def send_commands_to_device(self, cmd_set):
66-
if not cmd_set:
67-
LOG.debug("Nothing to execute")
68-
return
69-
70-
for cmd in cmd_set:
71-
LOG.info("%s", cmd)
72-
return "Success!"
105+
@contextlib.contextmanager
106+
def _get_connection(self):
107+
"""Context manager providing a netmiko SSH connection object."""
108+
yield FakeConnection(self)

0 commit comments

Comments
 (0)