Skip to content

Commit

Permalink
Merge pull request #51408 from aplanas/backport_49804
Browse files Browse the repository at this point in the history
parted: fix the ordering of list command
  • Loading branch information
dwoz authored Feb 1, 2019
2 parents 7e14499 + 467daf2 commit 9f87dda
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 26 deletions.
31 changes: 21 additions & 10 deletions salt/modules/parted.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def list_(device, unit=None):
for line in out:
if line in ('BYT;', 'CHS;', 'CYL;'):
continue
cols = line.replace(';', '').split(':')
cols = line.rstrip(';').split(':')
if mode == 'info':
if 7 <= len(cols) <= 8:
ret['info'] = {
Expand All @@ -178,15 +178,26 @@ def list_(device, unit=None):
raise CommandExecutionError(
'Problem encountered while parsing output from parted')
else:
if len(cols) == 7:
ret['partitions'][cols[0]] = {
'number': cols[0],
'start': cols[1],
'end': cols[2],
'size': cols[3],
'type': cols[4],
'file system': cols[5],
'flags': cols[6]}
# Parted (v3.1) have a variable field list in machine
# readable output:
#
# number:start:end:[size:]([file system:name:flags;]|[free;])
#
# * If units are in CHS 'size' is not printed.
# * If is a logical partition with PED_PARTITION_FREESPACE
# set, the last three fields are replaced with the
# 'free' text.
#
fields = ['number', 'start', 'end']
if unit != 'chs':
fields.append('size')
if cols[-1] == 'free':
# Drop the last element from the list
cols.pop()
else:
fields.extend(['file system', 'name', 'flags'])
if len(fields) == len(cols):
ret['partitions'][cols[0]] = dict(six.moves.zip(fields, cols))
else:
raise CommandExecutionError(
'Problem encountered while parsing output from parted')
Expand Down
73 changes: 57 additions & 16 deletions tests/unit/modules/test_parted.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def parted_print_output(k):
'''1:17.4kB:150MB:150MB:ext3::boot;\n'''
'''2:3921GB:4000GB:79.3GB:linux-swap(v1)::;\n'''
),
"valid chs": (
'''CHS;\n'''
'''/dev/sda:3133,0,2:scsi:512:512:gpt:AMCC 9650SE-24M DISK:;\n'''
'''1:0,0,34:2431,134,43:ext3::boot;\n'''
'''2:2431,134,44:2492,80,42:linux-swap(v1)::;\n'''
),
"valid_legacy": (
'''BYT;\n'''
'''/dev/sda:4000GB:scsi:512:512:gpt:AMCC 9650SE-24M DISK;\n'''
Expand Down Expand Up @@ -207,17 +213,17 @@ def test_list__valid_cmd_output(self):
'end': '150MB',
'number': '1',
'start': '17.4kB',
'file system': '',
'file system': 'ext3',
'flags': 'boot',
'type': 'ext3',
'name': '',
'size': '150MB'},
'2': {
'end': '4000GB',
'number': '2',
'start': '3921GB',
'file system': '',
'file system': 'linux-swap(v1)',
'flags': '',
'type': 'linux-swap(v1)',
'name': '',
'size': '79.3GB'
}
}
Expand Down Expand Up @@ -245,23 +251,58 @@ def test_list__valid_unit_valid_cmd_output(self):
'end': '150MB',
'number': '1',
'start': '17.4kB',
'file system': '',
'file system': 'ext3',
'flags': 'boot',
'type': 'ext3',
'name': '',
'size': '150MB'},
'2': {
'end': '4000GB',
'number': '2',
'start': '3921GB',
'file system': '',
'file system': 'linux-swap(v1)',
'flags': '',
'type': 'linux-swap(v1)',
'name': '',
'size': '79.3GB'
}
}
}
self.assertEqual(output, expected)

def test_list__valid_unit_chs_valid_cmd_output(self):
with patch('salt.modules.parted._validate_device', MagicMock()):
self.cmdrun_stdout.return_value = self.parted_print_output('valid chs')
output = parted.list_('/dev/sda', unit='chs')
self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda unit chs print')
expected = {
'info': {
'logical sector': '512',
'physical sector': '512',
'interface': 'scsi',
'model': 'AMCC 9650SE-24M DISK',
'disk': '/dev/sda',
'disk flags': '',
'partition table': 'gpt',
'size': '3133,0,2'
},
'partitions': {
'1': {
'end': '2431,134,43',
'number': '1',
'start': '0,0,34',
'file system': 'ext3',
'flags': 'boot',
'name': ''},
'2': {
'end': '2492,80,42',
'number': '2',
'start': '2431,134,44',
'file system': 'linux-swap(v1)',
'flags': '',
'name': ''}
}
}
self.assertEqual(output, expected)

def test_list__valid_legacy_cmd_output(self):
with patch('salt.modules.parted._validate_device', MagicMock()):
self.cmdrun_stdout.return_value = self.parted_print_output('valid_legacy')
Expand All @@ -282,17 +323,17 @@ def test_list__valid_legacy_cmd_output(self):
'end': '150MB',
'number': '1',
'start': '17.4kB',
'file system': '',
'file system': 'ext3',
'flags': 'boot',
'type': 'ext3',
'name': '',
'size': '150MB'},
'2': {
'end': '4000GB',
'number': '2',
'start': '3921GB',
'file system': '',
'file system': 'linux-swap(v1)',
'flags': '',
'type': 'linux-swap(v1)',
'name': '',
'size': '79.3GB'
}
}
Expand All @@ -319,17 +360,17 @@ def test_list__valid_unit_valid_legacy_cmd_output(self):
'end': '150MB',
'number': '1',
'start': '17.4kB',
'file system': '',
'file system': 'ext3',
'flags': 'boot',
'type': 'ext3',
'name': '',
'size': '150MB'},
'2': {
'end': '4000GB',
'number': '2',
'start': '3921GB',
'file system': '',
'file system': 'linux-swap(v1)',
'flags': '',
'type': 'linux-swap(v1)',
'name': '',
'size': '79.3GB'
}
}
Expand Down

0 comments on commit 9f87dda

Please sign in to comment.