5
from cloudinit.config import cc_mounts
7
from .. import helpers as test_helpers
10
from unittest import mock
15
class TestSanitizeDevname(test_helpers.FilesystemMockingTestCase):
18
super(TestSanitizeDevname, self).setUp()
19
self.new_root = tempfile.mkdtemp()
20
self.addCleanup(shutil.rmtree, self.new_root)
21
self.patchOS(self.new_root)
23
def _touch(self, path):
24
path = os.path.join(self.new_root, path.lstrip('/'))
25
basedir = os.path.dirname(path)
26
if not os.path.exists(basedir):
28
open(path, 'a').close()
30
def _makedirs(self, directory):
31
directory = os.path.join(self.new_root, directory.lstrip('/'))
32
if not os.path.exists(directory):
33
os.makedirs(directory)
35
def mock_existence_of_disk(self, disk_path):
36
self._touch(disk_path)
37
self._makedirs(os.path.join('/sys/block', disk_path.split('/')[-1]))
39
def mock_existence_of_partition(self, disk_path, partition_number):
40
self.mock_existence_of_disk(disk_path)
41
self._touch(disk_path + str(partition_number))
42
disk_name = disk_path.split('/')[-1]
43
self._makedirs(os.path.join('/sys/block',
45
disk_name + str(partition_number)))
47
def test_existent_full_disk_path_is_returned(self):
48
disk_path = '/dev/sda'
49
self.mock_existence_of_disk(disk_path)
50
self.assertEqual(disk_path,
51
cc_mounts.sanitize_devname(disk_path,
55
def test_existent_disk_name_returns_full_path(self):
57
disk_path = '/dev/' + disk_name
58
self.mock_existence_of_disk(disk_path)
59
self.assertEqual(disk_path,
60
cc_mounts.sanitize_devname(disk_name,
64
def test_existent_meta_disk_is_returned(self):
65
actual_disk_path = '/dev/sda'
66
self.mock_existence_of_disk(actual_disk_path)
69
cc_mounts.sanitize_devname('ephemeral0',
70
lambda x: actual_disk_path,
73
def test_existent_meta_partition_is_returned(self):
74
disk_name, partition_part = '/dev/sda', '1'
75
actual_partition_path = disk_name + partition_part
76
self.mock_existence_of_partition(disk_name, partition_part)
78
actual_partition_path,
79
cc_mounts.sanitize_devname('ephemeral0.1',
83
def test_existent_meta_partition_with_p_is_returned(self):
84
disk_name, partition_part = '/dev/sda', 'p1'
85
actual_partition_path = disk_name + partition_part
86
self.mock_existence_of_partition(disk_name, partition_part)
88
actual_partition_path,
89
cc_mounts.sanitize_devname('ephemeral0.1',
93
def test_first_partition_returned_if_existent_disk_is_partitioned(self):
94
disk_name, partition_part = '/dev/sda', '1'
95
actual_partition_path = disk_name + partition_part
96
self.mock_existence_of_partition(disk_name, partition_part)
98
actual_partition_path,
99
cc_mounts.sanitize_devname('ephemeral0',
103
def test_nth_partition_returned_if_requested(self):
104
disk_name, partition_part = '/dev/sda', '3'
105
actual_partition_path = disk_name + partition_part
106
self.mock_existence_of_partition(disk_name, partition_part)
108
actual_partition_path,
109
cc_mounts.sanitize_devname('ephemeral0.3',
113
def test_transformer_returning_none_returns_none(self):
115
cc_mounts.sanitize_devname(
116
'ephemeral0', lambda x: None, mock.Mock()))
118
def test_missing_device_returns_none(self):
120
cc_mounts.sanitize_devname('/dev/sda', None, mock.Mock()))
122
def test_missing_sys_returns_none(self):
123
disk_path = '/dev/sda'
124
self._makedirs(disk_path)
126
cc_mounts.sanitize_devname(disk_path, None, mock.Mock()))
128
def test_existent_disk_but_missing_partition_returns_none(self):
129
disk_path = '/dev/sda'
130
self.mock_existence_of_disk(disk_path)
132
cc_mounts.sanitize_devname(
133
'ephemeral0.1', lambda x: disk_path, mock.Mock()))