Fix handling OctetString for pysnmp

This commit fixes getting MAC address while inspection for iRMC driver.
To remove ord() in retrieving MAC address, it can avoid the following
exception:

  TypeError: ord() expected string of length 1, but int found

Also, this commit sets the following type of mock for UT. So, I add
'pysnmp'[1] into test-requirements.txt and lower-constraints.txt.

  * pysnmp.proto.rfc1902.OctetString
  * pysnmp.proto.rfc1902.Integer32

[1] https://bit.ly/3bDeTCH
Change-Id: I1e013a93854e01a7060e1fc48aac091e7e9b74cb
This commit is contained in:
Yushiro FURUKAWA 2020-09-08 23:54:45 +09:00
parent 2151d660d2
commit 0e399f750a
5 changed files with 25 additions and 6 deletions

View File

@ -110,7 +110,7 @@ def _get_mac_addresses(node):
d_info['irmc_snmp_security'])
node_classes = snmp_client.get_next(NODE_CLASS_OID)
mac_addresses = [':'.join(['%02x' % ord(x) for x in mac])
mac_addresses = [':'.join(['%02x' % x for x in mac])
for mac in snmp_client.get_next(MAC_ADDRESS_OID)]
return [a for c, a in zip(node_classes, mac_addresses)

View File

@ -18,6 +18,8 @@ Test class for iRMC Inspection Driver
from unittest import mock
from pysnmp.proto import rfc1902
from ironic.common import exception
from ironic.common import states
from ironic.common import utils
@ -38,12 +40,23 @@ class IRMCInspectInternalMethodsTestCase(test_common.BaseIRMCTest):
@mock.patch('ironic.drivers.modules.irmc.inspect.snmp.SNMPClient',
spec_set=True, autospec=True)
def test__get_mac_addresses(self, snmpclient_mock):
# NOTE(yushiro): In pysnmp 4.4.12, SNMPClient returns following type:
# node classes: pysnmp.proto.rfc1902.Integer32
# mac addresses: pysnmp.proto.rfc1902.OctetString
snmpclient_mock.return_value = mock.Mock(
**{'get_next.side_effect': [[2, 2, 7],
['\xaa\xaa\xaa\xaa\xaa\xaa',
'\xbb\xbb\xbb\xbb\xbb\xbb',
'\xcc\xcc\xcc\xcc\xcc\xcc']]})
inspected_macs = ['aa:aa:aa:aa:aa:aa', 'bb:bb:bb:bb:bb:bb']
**{'get_next.side_effect': [
[
rfc1902.Integer32(2),
rfc1902.Integer32(2),
rfc1902.Integer32(7)
], [
rfc1902.OctetString('\x90\x1b\x0e\xa5\x70\x37'),
rfc1902.OctetString('\x90\x1b\x0e\xa5\x70\x38'),
rfc1902.OctetString('\x90\x1b\x0e\xa5\x70\x39')
]]}
)
inspected_macs = ['90:1b:0e:a5:70:37', '90:1b:0e:a5:70:38']
with task_manager.acquire(self.context, self.node.uuid,
shared=True) as task:
result = irmc_inspect._get_mac_addresses(task.node)

View File

@ -95,6 +95,7 @@ pyOpenSSL==19.1.0
pyparsing==2.4.7
pyperclip==1.8.0
pysendfile==2.0.0
pysnmp==4.4.12
python-cinderclient==3.3.0
python-dateutil==2.8.1
python-editor==1.0.4

View File

@ -0,0 +1,4 @@
---
fixes:
- |
Fixed iRMC inspection for getting MAC address.

View File

@ -16,6 +16,7 @@ testtools>=2.2.0 # MIT
WebTest>=2.0.27 # MIT
bashate>=0.5.1 # Apache-2.0
pycodestyle>=2.0.0,<2.6.0 # MIT
pysnmp>=4.4.12
flake8-import-order>=0.17.1 # LGPLv3
Pygments>=2.2.0 # BSD
bandit!=1.6.0,>=1.1.0,<2.0.0 # Apache-2.0