mypy: Address issues with openstack.baremetal, baremetal_introspection

Yet another bug here: we weren't passing 'session' arguments.

Change-Id: Id9c5bafe8bc71024ff6d453870553dd45b6576d1
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2023-07-26 18:01:57 +01:00
parent 5d47d65d00
commit b6cc1d817d
19 changed files with 32 additions and 35 deletions

View File

@ -54,8 +54,6 @@ repos:
| openstack/tests/unit/.*
| openstack/tests/fixtures.py
| openstack/accelerator/.*
| openstack/baremetal/.*
| openstack/baremetal_introspection/.*
| openstack/cloud/.*
| openstack/clustering/.*
| openstack/container_infrastructure_management/.*

View File

@ -20,6 +20,7 @@ import os
import shutil
import subprocess
import tempfile
import typing as ty
@contextlib.contextmanager
@ -100,7 +101,7 @@ def build(
return pack(path)
def pack(path):
def pack(path: str) -> str:
"""Pack a directory with files into a Bare Metal service configdrive.
Creates an ISO image with the files and label "config-2".
@ -112,6 +113,7 @@ def pack(path):
# NOTE(toabctl): Luckily, genisoimage, mkisofs and xorrisofs understand
# the same parameters which are currently used.
cmds = ['genisoimage', 'mkisofs', 'xorrisofs']
error: ty.Optional[Exception]
for c in cmds:
try:
p = subprocess.Popen(
@ -153,7 +155,7 @@ def pack(path):
raise RuntimeError(
'Error generating the configdrive.'
'Stdout: "%(stdout)s". Stderr: "%(stderr)s"'
% {'stdout': stdout, 'stderr': stderr}
% {'stdout': stdout.decode(), 'stderr': stderr.decode()}
)
tmpfile.seek(0)
@ -163,11 +165,8 @@ def pack(path):
shutil.copyfileobj(tmpfile, gz_file)
tmpzipfile.seek(0)
cd = base64.b64encode(tmpzipfile.read())
# NOTE(dtantsur): Ironic expects configdrive to be a string, but base64
# returns bytes on Python 3.
if not isinstance(cd, str):
cd = cd.decode('utf-8')
# NOTE(dtantsur): Ironic expects configdrive to be a string, but
# base64 returns bytes on Python 3.
cd = base64.b64encode(tmpzipfile.read()).decode()
return cd

View File

@ -89,7 +89,9 @@ CHANGE_BOOT_MODE_VERSION = '1.76'
"""API version in which boot_mode and secure_boot states can be changed"""
class ListMixin:
class Resource(resource.Resource):
base_path: str
@classmethod
def list(cls, session, details=False, **params):
"""This method is a generator which yields resource objects.
@ -113,7 +115,7 @@ class ListMixin:
base_path = cls.base_path
if details:
base_path += '/detail'
return super(ListMixin, cls).list(
return super().list(
session, paginated=True, base_path=base_path, **params
)

View File

@ -456,7 +456,7 @@ class Proxy(proxy.Proxy):
:return: The node boot device
"""
res = self._get_resource(_node.Node, node)
return res.get_boot_device()
return res.get_boot_device(self)
def set_node_boot_device(self, node, boot_device, persistent=False):
"""Set node boot device
@ -479,7 +479,7 @@ class Proxy(proxy.Proxy):
:return: The node boot device
"""
res = self._get_resource(_node.Node, node)
return res.get_supported_boot_devices()
return res.get_supported_boot_devices(self)
def set_node_boot_mode(self, node, target):
"""Make a request to change node's boot mode

View File

@ -16,7 +16,7 @@ from openstack import resource
from openstack import utils
class Allocation(_common.ListMixin, resource.Resource):
class Allocation(_common.Resource):
resources_key = 'allocations'
base_path = '/allocations'

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class Chassis(_common.ListMixin, resource.Resource):
class Chassis(_common.Resource):
resources_key = 'chassis'
base_path = '/chassis'

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class Conductor(_common.ListMixin, resource.Resource):
class Conductor(_common.Resource):
resources_key = 'conductors'
base_path = '/conductors'

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class DeployTemplate(_common.ListMixin, resource.Resource):
class DeployTemplate(_common.Resource):
resources_key = 'deploy_templates'
base_path = '/deploy_templates'

View File

@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import typing as ty
from openstack.baremetal.v1 import _common
from openstack import exceptions
from openstack import resource
@ -153,7 +155,7 @@ class Driver(resource.Resource):
return response.json()
def call_vendor_passthru(
self, session, verb: str, method: str, body: dict = None
self, session, verb: str, method: str, body: ty.Optional[dict] = None
):
"""Call a vendor specific passthru method

View File

@ -70,7 +70,7 @@ class WaitResult(
__slots__ = ()
class Node(_common.ListMixin, resource.Resource):
class Node(_common.Resource):
resources_key = 'nodes'
base_path = '/nodes'
@ -711,11 +711,9 @@ class Node(_common.ListMixin, resource.Resource):
request = self._prepare_request(requires_id=True)
request.url = utils.urljoin(request.url, 'management', 'inject_nmi')
body = {}
response = session.put(
request.url,
json=body,
json={},
headers=request.headers,
microversion=version,
retriable_status_codes=_common.RETRIABLE_STATUS_CODES,
@ -813,7 +811,7 @@ class Node(_common.ListMixin, resource.Resource):
body = {'id': vif_id}
retriable_status_codes = _common.RETRIABLE_STATUS_CODES
if not retry_on_conflict:
retriable_status_codes = set(retriable_status_codes) - {409}
retriable_status_codes = list(set(retriable_status_codes) - {409})
response = session.post(
request.url,
json=body,

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class Port(_common.ListMixin, resource.Resource):
class Port(_common.Resource):
resources_key = 'ports'
base_path = '/ports'

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class PortGroup(_common.ListMixin, resource.Resource):
class PortGroup(_common.Resource):
resources_key = 'portgroups'
base_path = '/portgroups'

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class VolumeConnector(_common.ListMixin, resource.Resource):
class VolumeConnector(_common.Resource):
resources_key = 'connectors'
base_path = '/volume/connectors'

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class VolumeTarget(_common.ListMixin, resource.Resource):
class VolumeTarget(_common.Resource):
resources_key = 'targets'
base_path = '/volume/targets'

View File

@ -14,7 +14,7 @@ from openstack.baremetal.v1 import _common
from openstack import resource
class IntrospectionRule(_common.ListMixin, resource.Resource):
class IntrospectionRule(_common.Resource):
resources_key = 'rules'
base_path = '/rules'

View File

@ -510,7 +510,7 @@ class Resource(dict):
#: Do responses for this resource have bodies
has_body = True
#: Does create returns a body (if False requires ID), defaults to has_body
create_returns_body = None
create_returns_body: ty.Optional[bool] = None
#: Maximum microversion to use for getting/creating/updating the Resource
_max_microversion: ty.Optional[str] = None

View File

@ -97,12 +97,12 @@ class TestPack(testtools.TestCase):
)
def test_genisoimage_fails(self, mock_popen):
mock_popen.return_value.communicate.return_value = "", "BOOM"
mock_popen.return_value.communicate.return_value = b"", b"BOOM"
mock_popen.return_value.returncode = 1
self.assertRaisesRegex(RuntimeError, "BOOM", configdrive.pack, "/fake")
def test_success(self, mock_popen):
mock_popen.return_value.communicate.return_value = "", ""
mock_popen.return_value.communicate.return_value = b"", b""
mock_popen.return_value.returncode = 0
result = configdrive.pack("/fake")
# Make sure the result is string on all python versions

View File

@ -562,7 +562,7 @@ class TestNodeVif(base.TestCase):
json={'id': self.vif_id},
headers=mock.ANY,
microversion='1.28',
retriable_status_codes={503},
retriable_status_codes=[503],
)
def test_detach_vif_existing(self):

View File

@ -48,8 +48,6 @@ exclude = (?x)(
| openstack/tests/unit
| openstack/tests/fixtures.py
| openstack/accelerator
| openstack/baremetal
| openstack/baremetal_introspection
| openstack/cloud
| openstack/clustering
| openstack/container_infrastructure_management