1
from curtin.block import lvm
3
from .helpers import CiTestCase
7
class TestBlockLvm(CiTestCase):
8
vg_name = 'ubuntu-volgroup'
10
@mock.patch('curtin.block.lvm.util')
11
def test_filter_lvm_info(self, mock_util):
12
"""make sure lvm._filter_lvm_info filters properly"""
13
match_name = "vg_name"
14
query_results = ["lv_1", "lv_2"]
15
lvtool_name = 'lvscan'
16
query_name = 'lv_name'
17
# NOTE: i didn't use textwrap.dedent here on purpose, want to make sure
18
# that the function can handle leading spaces as some of the
19
# tools have spaces before the fist column in their output
20
mock_util.subp.return_value = (
22
matchfield_bad1{sep}qfield1
23
{matchfield_good}{sep}{query_good1}
24
matchfield_bad2{sep}qfield2
25
{matchfield_good}{sep}{query_good2}
26
""".format(matchfield_good=self.vg_name,
27
query_good1=query_results[0],
28
query_good2=query_results[1],
30
result_list = lvm._filter_lvm_info(lvtool_name, match_name,
31
query_name, self.vg_name)
32
self.assertEqual(len(result_list), 2)
33
mock_util.subp.assert_called_with(
34
[lvtool_name, '-C', '--separator', lvm._SEP, '--noheadings', '-o',
35
'{},{}'.format(match_name, query_name)], capture=True)
36
self.assertEqual(result_list, query_results)
37
# make sure _filter_lvm_info can fail gracefully if no match
38
result_list = lvm._filter_lvm_info(lvtool_name, match_name,
39
query_name, 'bad_match_val')
40
self.assertEqual(len(result_list), 0)
42
@mock.patch('curtin.block.lvm._filter_lvm_info')
43
def test_get_lvm_info(self, mock_filter_lvm_info):
45
make sure that the get lvm info functions make the right calls to
48
lvm.get_pvols_in_volgroup(self.vg_name)
49
mock_filter_lvm_info.assert_called_with(
50
'pvdisplay', 'vg_name', 'pv_name', self.vg_name)
51
lvm.get_lvols_in_volgroup(self.vg_name)
52
mock_filter_lvm_info.assert_called_with(
53
'lvdisplay', 'vg_name', 'lv_name', self.vg_name)
55
@mock.patch('curtin.block.lvm.util')
56
def test_split_lvm_name(self, mock_util):
58
make sure that split_lvm_name makes the right call to dmsetup splitname
61
full_name = '{}-{}'.format(self.vg_name, lv_name)
62
mock_util.subp.return_value = (
63
' {vg_name}{sep}{lv_name} '.format(
64
vg_name=self.vg_name, lv_name=lv_name, sep=lvm._SEP), '')
65
(res_vg_name, res_lv_name) = lvm.split_lvm_name(full_name)
66
self.assertEqual(res_vg_name, self.vg_name)
67
self.assertEqual(res_lv_name, lv_name)
68
mock_util.subp.assert_called_with(
69
['dmsetup', 'splitname', full_name, '-c', '--noheadings',
70
'--separator', lvm._SEP, '-o', 'vg_name,lv_name'], capture=True)
72
@mock.patch('curtin.block.lvm.lvmetad_running')
73
@mock.patch('curtin.block.lvm.util')
74
def test_lvm_scan(self, mock_util, mock_lvmetad):
75
"""check that lvm_scan formats commands correctly for each release"""
76
for (count, (codename, lvmetad_status, use_cache)) in enumerate(
77
[('precise', False, False), ('precise', True, False),
78
('trusty', False, False), ('trusty', True, True),
79
('vivid', False, False), ('vivid', True, True),
80
('wily', False, False), ('wily', True, True),
81
('xenial', False, False), ('xenial', True, True),
82
('yakkety', True, True), ('UNAVAILABLE', True, True),
83
(None, True, True), (None, False, False)]):
84
mock_util.lsb_release.return_value = {'codename': codename}
85
mock_lvmetad.return_value = lvmetad_status
88
len(mock_util.subp.call_args_list), 2 * (count + 1))
89
for (expected, actual) in zip(
90
[['pvscan'], ['vgscan', '--mknodes']],
91
mock_util.subp.call_args_list[2 * count:2 * count + 2]):
93
expected.append('--cache')
94
self.assertEqual(mock.call(expected, capture=True), actual)