|
| 1 | +"""Unit tests for netbox_onboarding.onboard module and its classes. |
| 2 | +
|
| 3 | +(c) 2020 Network To Code |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +""" |
| 14 | +from django.test import TestCase |
| 15 | + |
| 16 | +from dcim.models import Platform |
| 17 | +from netbox_onboarding.onboard import NetdevKeeper, OnboardException |
| 18 | + |
| 19 | + |
| 20 | +class NetdevKeeperTestCase(TestCase): |
| 21 | + """Test the NetdevKeeper Class.""" |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + """Create a superuser and token for API calls.""" |
| 25 | + self.platform1 = Platform.objects.create(name="JunOS", slug="junos", napalm_driver="junos") |
| 26 | + self.platform2 = Platform.objects.create(name="Cisco NX-OS", slug="cisco-nx-os") |
| 27 | + |
| 28 | + def test_get_platform_object_from_netbox(self): |
| 29 | + """Test of platform object from netbox.""" |
| 30 | + # Test assigning platform |
| 31 | + platform = NetdevKeeper.get_platform_object_from_netbox("junos", create_platform_if_missing=False) |
| 32 | + self.assertIsInstance(platform, Platform) |
| 33 | + |
| 34 | + # Test creation of missing platform object |
| 35 | + platform = NetdevKeeper.get_platform_object_from_netbox("arista_eos", create_platform_if_missing=True) |
| 36 | + self.assertIsInstance(platform, Platform) |
| 37 | + self.assertEqual(platform.napalm_driver, "eos") |
| 38 | + |
| 39 | + # Test failed unable to find the device and not part of the NETMIKO TO NAPALM keys |
| 40 | + with self.assertRaises(OnboardException) as exc_info: |
| 41 | + platform = NetdevKeeper.get_platform_object_from_netbox("notthere", create_platform_if_missing=True) |
| 42 | + self.assertEqual( |
| 43 | + exc_info.exception.message, |
| 44 | + "ERROR platform not found in NetBox and it's eligible for auto-creation: notthere", |
| 45 | + ) |
| 46 | + self.assertEqual(exc_info.exception.reason, "fail-general") |
| 47 | + |
| 48 | + # Test searching for an object, does not exist, but create_platform is false |
| 49 | + with self.assertRaises(OnboardException) as exc_info: |
| 50 | + platform = NetdevKeeper.get_platform_object_from_netbox("cisco_ios", create_platform_if_missing=False) |
| 51 | + self.assertEqual(exc_info.exception.message, "ERROR platform not found in NetBox: cisco_ios") |
| 52 | + self.assertEqual(exc_info.exception.reason, "fail-general") |
| 53 | + |
| 54 | + # Test NAPALM Driver not defined in NetBox |
| 55 | + with self.assertRaises(OnboardException) as exc_info: |
| 56 | + platform = NetdevKeeper.get_platform_object_from_netbox("cisco-nx-os", create_platform_if_missing=False) |
| 57 | + self.assertEqual(exc_info.exception.message, "ERROR platform is missing the NAPALM Driver: cisco-nx-os") |
| 58 | + self.assertEqual(exc_info.exception.reason, "fail-general") |
0 commit comments