|
10 | 10 | # License for the specific language governing permissions and limitations |
11 | 11 | # under the License. |
12 | 12 |
|
| 13 | +import contextlib |
| 14 | +import random |
| 15 | +import time |
| 16 | + |
13 | 17 | from oslo_log import log as logging |
14 | 18 |
|
15 | 19 | from networking_generic_switch.devices import netmiko_devices |
|
18 | 22 | LOG = logging.getLogger(__name__) |
19 | 23 |
|
20 | 24 |
|
| 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 | + |
21 | 61 | class Fake(netmiko_devices.NetmikoSwitch): |
22 | 62 | """Netmiko device driver for Fake switches.""" |
23 | 63 |
|
@@ -62,11 +102,7 @@ class Fake(netmiko_devices.NetmikoSwitch): |
62 | 102 | device output that indicate a failure to apply configuration. |
63 | 103 | """ |
64 | 104 |
|
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