~smoser/ubuntu/vivid/cloud-init/snappy

« back to all changes in this revision

Viewing changes to tests/unittests/test_datasource/test_altcloud.py

  • Committer: Scott Moser
  • Date: 2015-02-27 20:55:58 UTC
  • mfrom: (355.2.8 vivid)
  • Revision ID: smoser@ubuntu.com-20150227205558-glrwdgxqkaz6zyxa
* Merge with vivid at 0.7.7~bzr1067-0ubuntu1
* New upstream snapshot.
  * fix broken consumption of gzipped user-data (LP: #1424900)
  * functional user-data on Azure again (LP: #1423972)
  * CloudStack: support fetching password from virtual router (LP: #1422388)
* New upstream snapshot.
  * Fix for ascii decode in DataSourceAzure (LP: #1422993).
* New upstream snapshot.
  * support for gpt partitioning, utilized in Azure [Daniel Watkins]
  * fix bug in exception handling in mount_cb.
* New upstream snapshot.
  * move to python3 (LP: #1247132)
  * systemd: run cloud-init before systemd-user-sessions.service
  * Use the GCE short hostname. (LP: #1383794)
  * Enable user-data encoding support for GCE. (LP: #1404311)
  * Update to use a newer and better OMNIBUS_URL
  * Be more tolerant of 'ssh_authorized_keys' types
  * Fix parse_ssh_config failing in ssh_util.py
  * Increase the robustness/configurability of the chef module.
  * retain trailing newline from template files when using
    jinja2 (LP: #1355343)
  * fix broken output handling (LP: #1387340)
  * digital ocean datasource
  * update url in config drive documentation
  * freebsd: enable correct behavior on Ec2.
  * freebsd: Use the proper virtio FreeBSD network interface name.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import tempfile
27
27
 
28
28
from cloudinit import helpers
 
29
from cloudinit import util
29
30
from unittest import TestCase
30
31
 
31
32
# Get the cloudinit.sources.DataSourceAltCloud import items needed.
45
46
    cifile = open(cloudinit.sources.DataSourceAltCloud.CLOUD_INFO_FILE, 'w')
46
47
    cifile.write(value)
47
48
    cifile.close()
48
 
    os.chmod(cloudinit.sources.DataSourceAltCloud.CLOUD_INFO_FILE, 0664)
 
49
    os.chmod(cloudinit.sources.DataSourceAltCloud.CLOUD_INFO_FILE, 0o664)
49
50
 
50
51
 
51
52
def _remove_cloud_info_file():
66
67
    udfile = open(deltacloud_user_data_file, 'w')
67
68
    udfile.write(value)
68
69
    udfile.close()
69
 
    os.chmod(deltacloud_user_data_file, 0664)
 
70
    os.chmod(deltacloud_user_data_file, 0o664)
70
71
 
71
72
    udfile = open(user_data_file, 'w')
72
73
    udfile.write(value)
73
74
    udfile.close()
74
 
    os.chmod(user_data_file, 0664)
 
75
    os.chmod(user_data_file, 0o664)
75
76
 
76
77
 
77
78
def _remove_user_data_files(mount_dir,
98
99
            pass
99
100
 
100
101
 
 
102
def _dmi_data(expected):
 
103
    '''
 
104
    Spoof the data received over DMI
 
105
    '''
 
106
    def _data(key):
 
107
        return expected
 
108
 
 
109
    return _data
 
110
 
 
111
 
101
112
class TestGetCloudType(TestCase):
102
113
    '''
103
114
    Test to exercise method: DataSourceAltCloud.get_cloud_type()
106
117
    def setUp(self):
107
118
        '''Set up.'''
108
119
        self.paths = helpers.Paths({'cloud_dir': '/tmp'})
 
120
        self.dmi_data = util.read_dmi_data
109
121
        # We have a different code path for arm to deal with LP1243287
110
122
        # We have to switch arch to x86_64 to avoid test failure
111
123
        force_arch('x86_64')
112
124
 
113
125
    def tearDown(self):
114
126
        # Reset
115
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
116
 
            ['dmidecode', '--string', 'system-product-name']
117
 
        # Return back to original arch
 
127
        util.read_dmi_data = self.dmi_data
118
128
        force_arch()
119
129
 
120
130
    def test_rhev(self):
121
131
        '''
122
132
        Test method get_cloud_type() for RHEVm systems.
123
 
        Forcing dmidecode return to match a RHEVm system: RHEV Hypervisor
 
133
        Forcing read_dmi_data return to match a RHEVm system: RHEV Hypervisor
124
134
        '''
125
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
126
 
            ['echo', 'RHEV Hypervisor']
 
135
        util.read_dmi_data = _dmi_data('RHEV')
127
136
        dsrc = DataSourceAltCloud({}, None, self.paths)
128
137
        self.assertEquals('RHEV', \
129
138
            dsrc.get_cloud_type())
131
140
    def test_vsphere(self):
132
141
        '''
133
142
        Test method get_cloud_type() for vSphere systems.
134
 
        Forcing dmidecode return to match a vSphere system: RHEV Hypervisor
 
143
        Forcing read_dmi_data return to match a vSphere system: RHEV Hypervisor
135
144
        '''
136
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
137
 
            ['echo', 'VMware Virtual Platform']
 
145
        util.read_dmi_data = _dmi_data('VMware Virtual Platform')
138
146
        dsrc = DataSourceAltCloud({}, None, self.paths)
139
147
        self.assertEquals('VSPHERE', \
140
148
            dsrc.get_cloud_type())
142
150
    def test_unknown(self):
143
151
        '''
144
152
        Test method get_cloud_type() for unknown systems.
145
 
        Forcing dmidecode return to match an unrecognized return.
146
 
        '''
147
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
148
 
            ['echo', 'Unrecognized Platform']
149
 
        dsrc = DataSourceAltCloud({}, None, self.paths)
150
 
        self.assertEquals('UNKNOWN', \
151
 
            dsrc.get_cloud_type())
152
 
 
153
 
    def test_exception1(self):
154
 
        '''
155
 
        Test method get_cloud_type() where command dmidecode fails.
156
 
        '''
157
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
158
 
            ['ls', 'bad command']
159
 
        dsrc = DataSourceAltCloud({}, None, self.paths)
160
 
        self.assertEquals('UNKNOWN', \
161
 
            dsrc.get_cloud_type())
162
 
 
163
 
    def test_exception2(self):
164
 
        '''
165
 
        Test method get_cloud_type() where command dmidecode is not available.
166
 
        '''
167
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
168
 
            ['bad command']
 
153
        Forcing read_dmi_data return to match an unrecognized return.
 
154
        '''
 
155
        util.read_dmi_data = _dmi_data('Unrecognized Platform')
169
156
        dsrc = DataSourceAltCloud({}, None, self.paths)
170
157
        self.assertEquals('UNKNOWN', \
171
158
            dsrc.get_cloud_type())
180
167
        '''Set up.'''
181
168
        self.paths = helpers.Paths({'cloud_dir': '/tmp'})
182
169
        self.cloud_info_file = tempfile.mkstemp()[1]
 
170
        self.dmi_data = util.read_dmi_data
183
171
        cloudinit.sources.DataSourceAltCloud.CLOUD_INFO_FILE = \
184
172
            self.cloud_info_file
185
173
 
192
180
        except OSError:
193
181
            pass
194
182
 
 
183
        util.read_dmi_data = self.dmi_data
195
184
        cloudinit.sources.DataSourceAltCloud.CLOUD_INFO_FILE = \
196
185
            '/etc/sysconfig/cloud-info'
197
186
 
243
232
    def setUp(self):
244
233
        '''Set up.'''
245
234
        self.paths = helpers.Paths({'cloud_dir': '/tmp'})
 
235
        self.dmi_data = util.read_dmi_data
246
236
        cloudinit.sources.DataSourceAltCloud.CLOUD_INFO_FILE = \
247
237
            'no such file'
248
238
        # We have a different code path for arm to deal with LP1243287
253
243
        # Reset
254
244
        cloudinit.sources.DataSourceAltCloud.CLOUD_INFO_FILE = \
255
245
            '/etc/sysconfig/cloud-info'
256
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
257
 
            ['dmidecode', '--string', 'system-product-name']
 
246
        util.read_dmi_data = self.dmi_data
258
247
        # Return back to original arch
259
248
        force_arch()
260
249
 
261
250
    def test_rhev_no_cloud_file(self):
262
251
        '''Test No cloud info file module get_data() forcing RHEV.'''
263
252
 
264
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
265
 
            ['echo', 'RHEV Hypervisor']
 
253
        util.read_dmi_data = _dmi_data('RHEV Hypervisor')
266
254
        dsrc = DataSourceAltCloud({}, None, self.paths)
267
255
        dsrc.user_data_rhevm = lambda: True
268
256
        self.assertEquals(True, dsrc.get_data())
270
258
    def test_vsphere_no_cloud_file(self):
271
259
        '''Test No cloud info file module get_data() forcing VSPHERE.'''
272
260
 
273
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
274
 
            ['echo', 'VMware Virtual Platform']
 
261
        util.read_dmi_data = _dmi_data('VMware Virtual Platform')
275
262
        dsrc = DataSourceAltCloud({}, None, self.paths)
276
263
        dsrc.user_data_vsphere = lambda: True
277
264
        self.assertEquals(True, dsrc.get_data())
279
266
    def test_failure_no_cloud_file(self):
280
267
        '''Test No cloud info file module get_data() forcing unrecognized.'''
281
268
 
282
 
        cloudinit.sources.DataSourceAltCloud.CMD_DMI_SYSTEM = \
283
 
            ['echo', 'Unrecognized Platform']
 
269
        util.read_dmi_data = _dmi_data('Unrecognized Platform')
284
270
        dsrc = DataSourceAltCloud({}, None, self.paths)
285
271
        self.assertEquals(False, dsrc.get_data())
286
272