Skip to content

Commit

Permalink
Merge "Accept drives with vFAT label 'cidata' as a configdrive."
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed Oct 16, 2023
2 parents 102cd5a + 9b17eaf commit 983e148
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cloudbaseinit/metadata/services/osconfigdrive/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _get_config_drive_from_raw_hdd(self, drive_label, metadata_file):

def _get_config_drive_from_vfat(self, drive_label, metadata_file):
for drive_path in self._osutils.get_physical_disks():
if vfat.is_vfat_drive(self._osutils, drive_path):
if vfat.is_vfat_drive(self._osutils, drive_path, drive_label):
LOG.info('Config Drive found on disk %r', drive_path)
vfat.copy_from_vfat_drive(self._osutils, drive_path,
self.target_path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ def test_get_config_drive_from_vfat(self, mock_is_vfat_drive,
self.osutils.get_physical_disks.assert_called_once_with()

expected_is_vfat_calls = [
mock.call(self.osutils, mock.sentinel.drive1),
mock.call(self.osutils, mock.sentinel.drive2),
mock.call(self.osutils, mock.sentinel.drive1, self._fake_label),
mock.call(self.osutils, mock.sentinel.drive2, self._fake_label),
]
self.assertEqual(expected_is_vfat_calls, mock_is_vfat_drive.mock_calls)
mock_copy_from_vfat_drive.assert_called_once_with(
Expand Down
23 changes: 20 additions & 3 deletions cloudbaseinit/tests/utils/windows/test_vfat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class TestVfat(unittest.TestCase):

def _test_is_vfat_drive(self, execute_process_value,
expected_logging,
expected_response):
expected_response,
drive_label='config-2'):

mock_osutils = mock.Mock()
mock_osutils.execute_process.return_value = execute_process_value
Expand All @@ -41,7 +42,8 @@ def _test_is_vfat_drive(self, execute_process_value,
with testutils.ConfPatcher('mtools_path', 'mtools_path'):

response = vfat.is_vfat_drive(mock_osutils,
mock.sentinel.drive)
mock.sentinel.drive,
drive_label)

mdir = os.path.join(CONF.mtools_path, "mlabel.exe")
mock_osutils.execute_process.assert_called_once_with(
Expand Down Expand Up @@ -105,6 +107,20 @@ def test_is_vfat_drive_works_uppercase(self):
expected_logging=expected_logging,
expected_response=expected_response)

def test_is_vfat_drive_works_alternate_drive_label(self):
mock_out = b"Volume label is CIDATA \r\n"
expected_logging = [
"Obtained label information for drive %r: %r"
% (mock.sentinel.drive, mock_out)
]
execute_process_value = (mock_out, None, 0)
expected_response = True

self._test_is_vfat_drive(execute_process_value=execute_process_value,
expected_logging=expected_logging,
expected_response=expected_response,
drive_label='cidata')

def test_is_vfat_drive_with_wrong_label(self):
mock_out = b"Not volu label \r\n"
expected_logging = [
Expand Down Expand Up @@ -143,7 +159,8 @@ def test_copy(self, mock_os_chdir):
def test_is_vfat_drive_mtools_not_given(self):
with self.assertRaises(exception.CloudbaseInitException) as cm:
vfat.is_vfat_drive(mock.sentinel.osutils,
mock.sentinel.target_path)
mock.sentinel.target_path,
mock.sentinel.drive_label)
expected_message = ('"mtools_path" needs to be provided in order '
'to access VFAT drives')
self.assertEqual(expected_message, str(cm.exception.args[0]))
Expand Down
6 changes: 3 additions & 3 deletions cloudbaseinit/utils/windows/vfat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@


CONF = cloudbaseinit_conf.CONF
CONFIG_DRIVE_LABELS = ['config-2', 'CONFIG-2']
LOG = oslo_logging.getLogger(__name__)
VOLUME_LABEL_REGEX = re.compile("Volume label is (.*?)$")

Expand All @@ -34,7 +33,7 @@ def _check_mtools_path():
'to access VFAT drives')


def is_vfat_drive(osutils, drive_path):
def is_vfat_drive(osutils, drive_path, drive_label):
"""Check if the given drive contains a VFAT filesystem."""
_check_mtools_path()
mlabel = os.path.join(CONF.mtools_path, "mlabel.exe")
Expand All @@ -50,7 +49,8 @@ def is_vfat_drive(osutils, drive_path):
LOG.debug("Obtained label information for drive %r: %r", drive_path, out)
out = out.decode().strip()
match = VOLUME_LABEL_REGEX.search(out)
return match.group(1) in CONFIG_DRIVE_LABELS if match else False
drive_labels = [drive_label.lower(), drive_label.upper()]
return match.group(1) in drive_labels if match else False


def copy_from_vfat_drive(osutils, drive_path, target_path):
Expand Down

0 comments on commit 983e148

Please sign in to comment.