Use standard locale in list_partitions

parted function can output values which cannot be parsed by
_PARTED_PRINT_RE regexp in disk_partitioner module when using for
example ukrainian locale, as comma is used to separate integer and
fractional parts of float numbers. Standard 'C' locale should be used
to run parted.

Change-Id: Ic5b043796d548d91fb57a7ee34fe2e5a3a09088c
Closes-bug: #1371079
This commit is contained in:
Vladyslav Drok 2014-09-18 16:39:01 +03:00
parent 989b0d7edf
commit 87d1680ccf
2 changed files with 11 additions and 4 deletions

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import re
from oslo.config import cfg
@ -186,8 +187,10 @@ def list_partitions(device):
:returns: list of dictionaries (one per partition) with keys:
start, end, size (in MiB), filesystem, flags
"""
output = utils.execute(
'parted', '-s', '-m', device, 'unit', 'MiB', 'print')[0]
env = os.environ.copy()
env['LC_ALL'] = 'C'
output = utils.execute('parted', '-s', '-m', device, 'unit', 'MiB',
'print', env_variables=env)[0]
lines = [line for line in output.split('\n') if line.strip()][2:]
# Example of line: 1:1.00MiB:501MiB:500MiB:ext4::boot
fields = ('start', 'end', 'size', 'filesystem', 'flags')

View File

@ -15,6 +15,7 @@
import fixtures
import mock
import os
from testtools.matchers import HasLength
from ironic.common import disk_partitioner
@ -166,7 +167,8 @@ class DiskPartitionerTestCase(base.TestCase):
@mock.patch.object(utils, 'execute')
class ListPartitionsTestCase(base.TestCase):
def test_correct(self, execute_mock):
@mock.patch.object(os.environ, 'copy', return_value={})
def test_correct(self, env_mock, execute_mock):
output = """
BYT;
/dev/sda:500107862016B:scsi:512:4096:msdos:ATA HGST HTS725050A7:;
@ -180,10 +182,12 @@ BYT;
'filesystem': '', 'flags': ''},
]
execute_mock.return_value = (output, '')
env = {'LC_ALL': 'C'}
result = disk_partitioner.list_partitions('/dev/fake')
self.assertEqual(expected, result)
execute_mock.assert_called_once_with(
'parted', '-s', '-m', '/dev/fake', 'unit', 'MiB', 'print')
'parted', '-s', '-m', '/dev/fake', 'unit', 'MiB', 'print',
env_variables=env)
@mock.patch.object(disk_partitioner.LOG, 'warn')
def test_incorrect(self, log_mock, execute_mock):