Repository: napalm-automation/napalm

Issue:
IOS BGP not active error
```python
In [3]: with IOSDriver("ios01", os.environ.get("NAPALM_USERNAME"), os.environ.get("NAPALM_PASSWORD")) as d:
   ...:     d.get_bgp_neighbors()
   ...:
NAPALM didn't catch this exception. Please, fill a bugfix on https://github.com/napalm-automation/napalm/issues
Don't forget to include this traceback.
---------------------------------------------------------------------------
AddrFormatError                           Traceback (most recent call last)
<ipython-input-3-fc2e1223ed9d> in <module>
      1 with IOSDriver("ios01", os.environ.get("NAPALM_USERNAME"), os.environ.get("NAPALM_PASSWORD")) as d:
----> 2     d.get_bgp_neighbors()
      3

/mnt/c/Users/bewing/PycharmProjects/napalm/napalm/ios/ios.py in get_bgp_neighbors(self)
   1697
   1698         # check the router_id looks like an ipv4 address
-> 1699         router_id = napalm.base.helpers.ip(router_id, version=4)
   1700
   1701         # add parsed data to output dict

/mnt/c/Users/bewing/PycharmProjects/napalm/napalm/base/helpers.py in ip(addr, version)
    328         u'2001:db8:85a3::8a2e:370:7334'
    329     """
--> 330     addr_obj = IPAddress(addr)
    331     if version and addr_obj.version != version:
    332         raise ValueError("{} is not an ipv{} address".format(addr, version))

/mnt/c/Users/bewing/.pyenv/versions/3.6.6/envs/napalm/lib/python3.6/site-packages/netaddr/ip/__init__.py in __init__(self, addr, version, flags)
    304                 if self._module is None:
    305                     raise AddrFormatError('failed to detect a valid IP ' \
--> 306                         'address from %r' % addr)
    307             else:
    308                 #   IP version is explicit.

AddrFormatError: failed to detect a valid IP address from None
```

```
ios01#show bgp all summary
% BGP not active

ios01#
```

Looks like we need to detect this in line 1478.  Do we really want to raise a `CommandErrorException`, or just return an empty dictionary?


Discussion:

Resolving patch:
diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py
--- a/napalm/ios/ios.py
+++ b/napalm/ios/ios.py
@@ -1497,9 +1497,9 @@ def get_bgp_neighbors(self):
         # get summary output from device
         cmd_bgp_all_sum = "show bgp all summary"
         summary_output = self._send_command(cmd_bgp_all_sum).strip()
-
-        if "Invalid input detected" in summary_output:
-            raise CommandErrorException("BGP is not running on this device")
+        bgp_not_running = ["Invalid input", "BGP not active"]
+        if any((s in summary_output for s in bgp_not_running)):
+            return {}
 
         # get neighbor output from device
         neighbor_output = ""


Test patch:
diff --git a/napalm/base/test/getters.py b/napalm/base/test/getters.py
--- a/napalm/base/test/getters.py
+++ b/napalm/base/test/getters.py
@@ -211,7 +211,8 @@ def test_get_environment(self, test_case):
     def test_get_bgp_neighbors(self, test_case):
         """Test get_bgp_neighbors."""
         get_bgp_neighbors = self.device.get_bgp_neighbors()
-        assert "global" in get_bgp_neighbors.keys()
+        if len(get_bgp_neighbors) > 0:
+            assert "global" in get_bgp_neighbors.keys()
 
         for vrf, vrf_data in get_bgp_neighbors.items():
             assert isinstance(vrf_data["router_id"], str)
diff --git a/test/ios/mocked_data/test_get_bgp_neighbors/bgp_inactive/expected_result.json b/test/ios/mocked_data/test_get_bgp_neighbors/bgp_inactive/expected_result.json
new file mode 100644
--- /dev/null
+++ b/test/ios/mocked_data/test_get_bgp_neighbors/bgp_inactive/expected_result.json
@@ -0,0 +1 @@
+{}
diff --git a/test/ios/mocked_data/test_get_bgp_neighbors/bgp_inactive/show_bgp_all_summary.txt b/test/ios/mocked_data/test_get_bgp_neighbors/bgp_inactive/show_bgp_all_summary.txt
new file mode 100644
--- /dev/null
+++ b/test/ios/mocked_data/test_get_bgp_neighbors/bgp_inactive/show_bgp_all_summary.txt
@@ -0,0 +1 @@
+% BGP not active
\ No newline at end of file