~rcj/ubuntu/trusty/cloud-init/joyent-lxbrand

« back to all changes in this revision

Viewing changes to tests/unittests/test_handler/test_handler_mounts.py

  • Committer: Scott Moser
  • Author(s): Daniel Watkins
  • Date: 2015-08-14 12:54:02 UTC
  • Revision ID: smoser@ubuntu.com-20150814125402-514m056vv2ziyivh
Tags: 0.7.5-0ubuntu1.8
* debian/patches/lp-1411582-azure-udev-ephemeral-disks.patch:
    - Use udev rules to discover ephemeral disk locations rather than
      hard-coded device names (LP: #1411582).
* debian/patches/lp-1470880-fix-gce-az-determination.patch:
    - Correctly parse GCE's availability zones (LP: #1470880).
* d/patches/lp-1470890-include-regions-in-dynamic-mirror-discovery.patch:
    - Make %(region)s a valid substitution in mirror discovery
      (LP: #1470890).
* Remove python-serial from Build-Depends; it was mistakenly added last
  upload.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os.path
 
2
import shutil
 
3
import tempfile
 
4
 
 
5
from cloudinit.config import cc_mounts
 
6
 
 
7
from .. import helpers as test_helpers
 
8
 
 
9
try:
 
10
    from unittest import mock
 
11
except ImportError:
 
12
    import mock
 
13
 
 
14
 
 
15
class TestSanitizeDevname(test_helpers.FilesystemMockingTestCase):
 
16
 
 
17
    def setUp(self):
 
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)
 
22
 
 
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):
 
27
            os.makedirs(basedir)
 
28
        open(path, 'a').close()
 
29
 
 
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)
 
34
 
 
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]))
 
38
 
 
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',
 
44
                                    disk_name,
 
45
                                    disk_name + str(partition_number)))
 
46
 
 
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,
 
52
                                                    lambda x: None,
 
53
                                                    mock.Mock()))
 
54
 
 
55
    def test_existent_disk_name_returns_full_path(self):
 
56
        disk_name = 'sda'
 
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,
 
61
                                                    lambda x: None,
 
62
                                                    mock.Mock()))
 
63
 
 
64
    def test_existent_meta_disk_is_returned(self):
 
65
        actual_disk_path = '/dev/sda'
 
66
        self.mock_existence_of_disk(actual_disk_path)
 
67
        self.assertEqual(
 
68
            actual_disk_path,
 
69
            cc_mounts.sanitize_devname('ephemeral0',
 
70
                                       lambda x: actual_disk_path,
 
71
                                       mock.Mock()))
 
72
 
 
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)
 
77
        self.assertEqual(
 
78
            actual_partition_path,
 
79
            cc_mounts.sanitize_devname('ephemeral0.1',
 
80
                                       lambda x: disk_name,
 
81
                                       mock.Mock()))
 
82
 
 
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)
 
87
        self.assertEqual(
 
88
            actual_partition_path,
 
89
            cc_mounts.sanitize_devname('ephemeral0.1',
 
90
                                       lambda x: disk_name,
 
91
                                       mock.Mock()))
 
92
 
 
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)
 
97
        self.assertEqual(
 
98
            actual_partition_path,
 
99
            cc_mounts.sanitize_devname('ephemeral0',
 
100
                                       lambda x: disk_name,
 
101
                                       mock.Mock()))
 
102
 
 
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)
 
107
        self.assertEqual(
 
108
            actual_partition_path,
 
109
            cc_mounts.sanitize_devname('ephemeral0.3',
 
110
                                       lambda x: disk_name,
 
111
                                       mock.Mock()))
 
112
 
 
113
    def test_transformer_returning_none_returns_none(self):
 
114
        self.assertIsNone(
 
115
            cc_mounts.sanitize_devname(
 
116
                'ephemeral0', lambda x: None, mock.Mock()))
 
117
 
 
118
    def test_missing_device_returns_none(self):
 
119
        self.assertIsNone(
 
120
            cc_mounts.sanitize_devname('/dev/sda', None, mock.Mock()))
 
121
 
 
122
    def test_missing_sys_returns_none(self):
 
123
        disk_path = '/dev/sda'
 
124
        self._makedirs(disk_path)
 
125
        self.assertIsNone(
 
126
            cc_mounts.sanitize_devname(disk_path, None, mock.Mock()))
 
127
 
 
128
    def test_existent_disk_but_missing_partition_returns_none(self):
 
129
        disk_path = '/dev/sda'
 
130
        self.mock_existence_of_disk(disk_path)
 
131
        self.assertIsNone(
 
132
            cc_mounts.sanitize_devname(
 
133
                'ephemeral0.1', lambda x: disk_path, mock.Mock()))