Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# test that raises AddrFormatError when wrong format
self.assertRaises(AddrFormatError, napalm.base.helpers.ip, "fake")
self.assertRaises(
ValueError,
napalm.base.helpers.ip,
"2001:db8:85a3::8a2e:370:7334",
version=4,
)
self.assertRaises(ValueError, napalm.base.helpers.ip, "192.168.17.1", version=6)
self.assertEqual(
napalm.base.helpers.ip("2001:0dB8:85a3:0000:0000:8A2e:0370:7334"),
"2001:db8:85a3::8a2e:370:7334",
)
self.assertEqual(
napalm.base.helpers.ip("2001:0DB8::0003", version=6), "2001:db8::3"
)
def test_mac(self):
"""
Tests the helper function ```mac```:
* check if raises AddrFormatError when invalid MAC
* check if MAC address returned as expected
"""
self.assertTrue(HAS_NETADDR)
# test that raises AddrFormatError when wrong format
self.assertRaises(AddrFormatError, napalm.base.helpers.mac, "fake")
self.assertEqual(napalm.base.helpers.mac("0123456789ab"), "01:23:45:67:89:AB")
self.assertEqual(napalm.base.helpers.mac("0123.4567.89ab"), "01:23:45:67:89:AB")
self.assertEqual(napalm.base.helpers.mac("123.4567.89ab"), "01:23:45:67:89:AB")
"""
Tests helper function ```convert```:
* cast of non-int str to int returns default value
* cast of str to float returns desired float-type value
* cast of None obj to string does not cast, but returns default
"""
self.assertTrue(
napalm.base.helpers.convert(int, "non-int-value", default=-100) == -100
)
# default value returned
self.assertIsInstance(napalm.base.helpers.convert(float, "1e-17"), float)
# converts indeed to float
self.assertFalse(napalm.base.helpers.convert(str, None) == "None")
# should not convert None-type to 'None' string
self.assertTrue(napalm.base.helpers.convert(str, None) == "")
# should return empty unicode
def test_run_with_incorrect_config_parameter_using_replace_method(self, mock_method):
# set parameter to fail
self.default_kwargs['config_text'] = 'invalid command'
self.default_kwargs['method'] = 'replace'
# prepare mock processing
err_msg = 'syntax error'
mock_device = mock.MagicMock()
mock_device.load_replace_candidate.side_effect = exceptions.ReplaceConfigException(err_msg)
mock_device.__enter__.return_value = mock_device
mock_driver = mock.MagicMock()
mock_driver.return_value = mock_device
mock_method.return_value = mock_driver
action = self.get_action_instance(self.full_config)
with self.assertRaisesRegexp(exceptions.ReplaceConfigException, err_msg):
action.run(**self.default_kwargs)
def test_as_number(self):
"""Test the as_number helper function."""
self.assertEqual(napalm.base.helpers.as_number("64001"), 64001)
self.assertEqual(napalm.base.helpers.as_number("1.0"), 65536)
self.assertEqual(napalm.base.helpers.as_number("1.100"), 65636)
self.assertEqual(napalm.base.helpers.as_number("1.65535"), 131071)
self.assertEqual(napalm.base.helpers.as_number("65535.65535"), 4294967295)
self.assertEqual(napalm.base.helpers.as_number(64001), 64001)
def test_basic(self):
d = driver("blah", "bleh", "blih", optional_args=optional_args)
assert d.is_alive() == {"is_alive": False}
d.open()
assert d.is_alive() == {"is_alive": True}
d.close()
assert d.is_alive() == {"is_alive": False}
with pytest.raises(napalm.base.exceptions.ConnectionClosedException) as excinfo:
d.get_facts()
assert "connection closed" in str(excinfo.value)
def test_context_manager(self):
with pytest.raises(napalm.base.exceptions.ConnectionException) as e, driver(
"blah", "bleh", "blih", optional_args=fail_args
) as d:
pass
assert "You told me to do this" in str(e.value)
with pytest.raises(AttributeError) as e, driver(
"blah", "bleh", "blih", optional_args=optional_args
) as d:
assert d.is_alive() == {"is_alive": True}
d.__fake_call()
assert d.is_alive() == {"is_alive": False}
assert "object has no attribute" in str(e.value)
def test_canonical_interface_name(self):
"""Test the canonical_interface_name helper function."""
self.assertEqual(
napalm.base.helpers.canonical_interface_name("Fa0/1"), "FastEthernet0/1"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("FastEthernet0/1"),
"FastEthernet0/1",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("TenGig1/1/1.5"),
"TenGigabitEthernet1/1/1.5",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("Gi1/2"), "GigabitEthernet1/2"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("HundredGigE105/1/1"),
"HundredGigabitEthernet105/1/1",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("Lo0"), "Loopback0"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("TenGig1/1/1.5"),
"TenGigabitEthernet1/1/1.5",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("Gi1/2"), "GigabitEthernet1/2"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("HundredGigE105/1/1"),
"HundredGigabitEthernet105/1/1",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("Lo0"), "Loopback0"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("lo0"), "Loopback0"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("no_match0/1"), "no_match0/1"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name(
"lo10", addl_name_map={"lo": "something_custom"}
),
"something_custom10",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name(
"uniq0/1/1", addl_name_map={"uniq": "something_custom"}
),
"something_custom0/1/1",
)
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("HundredGigE105/1/1"),
"HundredGigabitEthernet105/1/1",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("Lo0"), "Loopback0"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("lo0"), "Loopback0"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name("no_match0/1"), "no_match0/1"
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name(
"lo10", addl_name_map={"lo": "something_custom"}
),
"something_custom10",
)
self.assertEqual(
napalm.base.helpers.canonical_interface_name(
"uniq0/1/1", addl_name_map={"uniq": "something_custom"}
),
"something_custom0/1/1",
)