Download tasks/codeql_python_benchmark51_v2/pyvul-4ac3440b9023e6511a91/patch.diff from Benchanything/checkerbench: direct link, hf CLI and curl.
- Browser
- Download file 8.55 kB
-
https://huggingface.co/datasets/Benchanything/checkerbench/resolve/main/tasks/codeql_python_benchmark51_v2/pyvul-4ac3440b9023e6511a91/patch.diff
- Command line
-
hf download hf://datasets/Benchanything/checkerbench/tasks/codeql_python_benchmark51_v2/pyvul-4ac3440b9023e6511a91/patch.diff
-
curl -L -o patch.diff https://huggingface.co/datasets/Benchanything/checkerbench/resolve/main/tasks/codeql_python_benchmark51_v2/pyvul-4ac3440b9023e6511a91/patch.diff
8.55 kB
| diff --git a/nautobot_device_onboarding/jobs.py b/nautobot_device_onboarding/jobs.py | |
| index cca9e9c..72b843d 100644 | |
| --- a/nautobot_device_onboarding/jobs.py | |
| +++ b/nautobot_device_onboarding/jobs.py | |
| PLUGIN_SETTINGS = settings.PLUGINS_CONFIG["nautobot_device_onboarding"] | |
| class OnboardingTask(Job): | |
| - class Meta: | |
| - """Meta object boilerplate for onboarding.""" | |
| - | |
| - name = "Perform Device Onboarding" | |
| - description = "Login to a device and populate Nautobot device object." | |
| - has_sensitive_variables = False | |
| + """Nautobot Job for onboarding a new device.""" | |
| location = ObjectVar(model=Location, query_params={"": ""}, required=False, description="") | |
| ip_address = IPAddressVar(description="", label="") | |
| class OnboardingTask(Job): | |
| role = ObjectVar(model=Role, query_params={"": ""}, required=False, description="") | |
| device_type = ObjectVar(model=DeviceType, required=False, description="") | |
| + class Meta: # pylint: disable=too-few-public-methods | |
| + """Meta object boilerplate for onboarding.""" | |
| + | |
| + name = "Perform Device Onboarding" | |
| + description = "Login to a device and populate Nautobot device object." | |
| + has_sensitive_variables = False | |
| + | |
| + def __init__(self, *args, **kwargs): | |
| + """Overload init to instantiate class attributes per W0201.""" | |
| + self.username = None | |
| + self.password = None | |
| + self.secret = None | |
| + super().__init__(*args, **kwargs) | |
| + | |
| def run(self, *args, **data): | |
| """Process a single Onboarding Task instance.""" | |
| self.logger.info("START: onboard device") | |
| - credentials = self._parse_credentials(data["credentials"]) | |
| + self._parse_credentials(data["credentials"]) | |
| platform = data["platform"] | |
| netdev = NetdevKeeper( | |
| hostname=data["ip_address"], | |
| port=data["port"], | |
| timeout=data["timeout"], | |
| - username=credentials["username"], | |
| - password=credentials["password"], | |
| - secret=credentials["secret"], | |
| + username=self.username, | |
| + password=self.password, | |
| + secret=self.secret, | |
| napalm_driver=platform.napalm_driver if platform and platform.napalm_driver else None, | |
| optional_args=platform.napalm_args if platform and platform.napalm_args else settings.NAPALM_ARGS, | |
| ) | |
| class OnboardingTask(Job): | |
| def _parse_credentials(self, credentials): | |
| """Parse and return dictionary of credentials.""" | |
| - | |
| if credentials: | |
| - self.username = (credentials.secrets.get(secret_type=SecretsGroupSecretTypeChoices.TYPE_USERNAME),) | |
| - self.password = (credentials.secrets.get(secret_type=SecretsGroupSecretTypeChoices.TYPE_PASSWORD),) | |
| - self.secret = (None,) | |
| + self.username = credentials.secrets.get(secret_type=SecretsGroupSecretTypeChoices.TYPE_USERNAME) | |
| + self.password = credentials.secrets.get(secret_type=SecretsGroupSecretTypeChoices.TYPE_PASSWORD) | |
| secret = credentials.secrets.filter(secret_type=SecretsGroupSecretTypeChoices.TYPE_SECRET) | |
| if secret.exists(): | |
| self.secret = secret.first() | |
| else: | |
| - self.username = (settings.NAPALM_USERNAME,) | |
| - self.password = (settings.NAPALM_PASSWORD,) | |
| - self.secret = (settings.NAPALM_ARGS.get("secret", None),) | |
| + self.username = settings.NAPALM_USERNAME | |
| + self.password = settings.NAPALM_PASSWORD | |
| + self.secret = settings.NAPALM_ARGS.get("secret", None) | |
| register_jobs(OnboardingTask) | |
| diff --git a/nautobot_device_onboarding/tests/test_netdev_keeper.py b/nautobot_device_onboarding/tests/test_netdev_keeper.py | |
| deleted file mode 100644 | |
| index 91fbde6..0000000 | |
| --- a/nautobot_device_onboarding/tests/test_netdev_keeper.py | |
| +++ /dev/null | |
| -"""Unit tests for nautobot_device_onboarding.netdev_keeper module and its classes.""" | |
| - | |
| -from socket import gaierror | |
| -from unittest import mock | |
| - | |
| -from django.contrib.contenttypes.models import ContentType | |
| -from django.test import TestCase | |
| - | |
| -from nautobot.dcim.models import Device, Location, LocationType, Platform | |
| -from nautobot.extras.models import Role, Status | |
| - | |
| -from nautobot_device_onboarding.exceptions import OnboardException | |
| -from nautobot_device_onboarding.helpers import onboarding_task_fqdn_to_ip | |
| -# from nautobot_device_onboarding.models import OnboardingTask | |
| - | |
| - | |
| -class NetdevKeeperTestCase(TestCase): | |
| - """Test the NetdevKeeper Class.""" | |
| - | |
| - def setUp(self): | |
| - """Create a superuser and token for API calls.""" | |
| - role_content_type = ContentType.objects.get_for_model(Device) | |
| - status = Status.objects.get(name="Active") | |
| - location_type = LocationType.objects.create(name="site") | |
| - self.site1 = Location.objects.create(name="USWEST", location_type=location_type, status=status) | |
| - self.device_role1 = Role.objects.create(name="Firewall") | |
| - self.device_role1.content_types.set([role_content_type]) | |
| - | |
| - self.platform1 = Platform.objects.create(name="JunOS", napalm_driver="junos") | |
| - # self.platform2 = Platform.objects.create(name="Cisco NX-OS", slug="cisco-nx-os") | |
| - | |
| - # self.onboarding_task4 = OnboardingTask.objects.create( | |
| - # ip_address="ntc123.local", location=self.site1, role=self.device_role1, platform=self.platform1 | |
| - # ) | |
| - | |
| - # self.onboarding_task5 = OnboardingTask.objects.create( | |
| - # ip_address="bad.local", location=self.site1, role=self.device_role1, platform=self.platform1 | |
| - # ) | |
| - | |
| - # self.onboarding_task7 = OnboardingTask.objects.create( | |
| - # ip_address="192.0.2.1/32", location=self.site1, role=self.device_role1, platform=self.platform1 | |
| - # ) | |
| - | |
| - # @mock.patch("nautobot_device_onboarding.helpers.socket.gethostbyname") | |
| - # def test_check_ip(self, mock_get_hostbyname): | |
| - # """Check DNS to IP address.""" | |
| - # # Look up response value | |
| - # mock_get_hostbyname.return_value = "192.0.2.1" | |
| - | |
| - # # FQDN -> IP | |
| - # onboarding_task_fqdn_to_ip(onboarding_task=self.onboarding_task4) | |
| - | |
| - # # Run the check to change the IP address | |
| - # self.assertEqual(self.onboarding_task4.ip_address, "192.0.2.1") | |
| - | |
| - # @mock.patch("nautobot_device_onboarding.helpers.socket.gethostbyname") | |
| - # def test_failed_check_ip(self, mock_get_hostbyname): | |
| - # """Check DNS to IP address failing.""" | |
| - # # Look up a failed response | |
| - # mock_get_hostbyname.side_effect = gaierror(8) | |
| - | |
| - # # Check for bad.local raising an exception | |
| - # with self.assertRaises(OnboardException) as exc_info: | |
| - # onboarding_task_fqdn_to_ip(onboarding_task=self.onboarding_task5) | |
| - # self.assertEqual(exc_info.exception.message, "ERROR failed to complete DNS lookup: bad.local") | |
| - # self.assertEqual(exc_info.exception.reason, "fail-dns") | |
| - | |
| - # # Check for exception with prefix address entered | |
| - # with self.assertRaises(OnboardException) as exc_info: | |
| - # onboarding_task_fqdn_to_ip(onboarding_task=self.onboarding_task7) | |
| - # self.assertEqual(exc_info.exception.reason, "fail-prefix") | |
| - # self.assertEqual(exc_info.exception.message, "ERROR appears a prefix was entered: 192.0.2.1/32") | |
| diff --git a/nautobot_device_onboarding/tests/test_onboarding.py b/nautobot_device_onboarding/tests/test_onboarding.py | |
| index d7a938c..f427bba 100644 | |
| --- a/nautobot_device_onboarding/tests/test_onboarding.py | |
| +++ b/nautobot_device_onboarding/tests/test_onboarding.py | |
| """Unit tests for nautobot_device_onboarding.netdev_keeper module and its classes.""" | |
| -from unittest import mock | |
| +# from unittest import mock | |
| from django.conf import settings | |
| -from django.test import TestCase | |
| -from django.contrib.contenttypes.models import ContentType | |
| +# from django.test import TestCase | |
| +# from django.contrib.contenttypes.models import ContentType | |
| -from nautobot.dcim.models import Device, Location, LocationType, Platform | |
| -from nautobot.extras.models import Status | |
| -# from nautobot_device_onboarding.models import OnboardingTask | |
| -# from nautobot_device_onboarding.onboard import OnboardingManager | |
| +# from nautobot.dcim.models import Device, Location, LocationType, Platform | |
| +# from nautobot.extras.models import Status | |
| + | |
| PLUGIN_SETTINGS = settings.PLUGINS_CONFIG["nautobot_device_onboarding"] | |