2626import tempfile
2727
2828from cloudinit import helpers
29+ from cloudinit import util
2930from unittest import TestCase
3031
3132# Get the cloudinit.sources.DataSourceAltCloud import items needed.
@@ -98,6 +99,16 @@ def _remove_user_data_files(mount_dir,
9899 pass
99100
100101
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+
101112class TestGetCloudType (TestCase ):
102113 '''
103114 Test to exercise method: DataSourceAltCloud.get_cloud_type()
@@ -106,66 +117,42 @@ class TestGetCloudType(TestCase):
106117 def setUp (self ):
107118 '''Set up.'''
108119 self .paths = helpers .Paths ({'cloud_dir' : '/tmp' })
120+ self .dmi_data = util .read_dmi_data
109121 # We have a different code path for arm to deal with LP1243287
110122 # We have to switch arch to x86_64 to avoid test failure
111123 force_arch ('x86_64' )
112124
113125 def tearDown (self ):
114126 # 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
118128 force_arch ()
119129
120130 def test_rhev (self ):
121131 '''
122132 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
124134 '''
125- cloudinit .sources .DataSourceAltCloud .CMD_DMI_SYSTEM = \
126- ['echo' , 'RHEV Hypervisor' ]
135+ util .read_dmi_data = _dmi_data ('RHEV' )
127136 dsrc = DataSourceAltCloud ({}, None , self .paths )
128137 self .assertEquals ('RHEV' , \
129138 dsrc .get_cloud_type ())
130139
131140 def test_vsphere (self ):
132141 '''
133142 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
135144 '''
136- cloudinit .sources .DataSourceAltCloud .CMD_DMI_SYSTEM = \
137- ['echo' , 'VMware Virtual Platform' ]
145+ util .read_dmi_data = _dmi_data ('VMware Virtual Platform' )
138146 dsrc = DataSourceAltCloud ({}, None , self .paths )
139147 self .assertEquals ('VSPHERE' , \
140148 dsrc .get_cloud_type ())
141149
142150 def test_unknown (self ):
143151 '''
144152 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.
153+ Forcing read_dmi_data return to match an unrecognized return.
166154 '''
167- cloudinit .sources .DataSourceAltCloud .CMD_DMI_SYSTEM = \
168- ['bad command' ]
155+ util .read_dmi_data = _dmi_data ('Unrecognized Platform' )
169156 dsrc = DataSourceAltCloud ({}, None , self .paths )
170157 self .assertEquals ('UNKNOWN' , \
171158 dsrc .get_cloud_type ())
@@ -180,6 +167,7 @@ def setUp(self):
180167 '''Set up.'''
181168 self .paths = helpers .Paths ({'cloud_dir' : '/tmp' })
182169 self .cloud_info_file = tempfile .mkstemp ()[1 ]
170+ self .dmi_data = util .read_dmi_data
183171 cloudinit .sources .DataSourceAltCloud .CLOUD_INFO_FILE = \
184172 self .cloud_info_file
185173
@@ -192,6 +180,7 @@ def tearDown(self):
192180 except OSError :
193181 pass
194182
183+ util .read_dmi_data = self .dmi_data
195184 cloudinit .sources .DataSourceAltCloud .CLOUD_INFO_FILE = \
196185 '/etc/sysconfig/cloud-info'
197186
@@ -243,6 +232,7 @@ class TestGetDataNoCloudInfoFile(TestCase):
243232 def setUp (self ):
244233 '''Set up.'''
245234 self .paths = helpers .Paths ({'cloud_dir' : '/tmp' })
235+ self .dmi_data = util .read_dmi_data
246236 cloudinit .sources .DataSourceAltCloud .CLOUD_INFO_FILE = \
247237 'no such file'
248238 # We have a different code path for arm to deal with LP1243287
@@ -253,34 +243,30 @@ def tearDown(self):
253243 # Reset
254244 cloudinit .sources .DataSourceAltCloud .CLOUD_INFO_FILE = \
255245 '/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
258247 # Return back to original arch
259248 force_arch ()
260249
261250 def test_rhev_no_cloud_file (self ):
262251 '''Test No cloud info file module get_data() forcing RHEV.'''
263252
264- cloudinit .sources .DataSourceAltCloud .CMD_DMI_SYSTEM = \
265- ['echo' , 'RHEV Hypervisor' ]
253+ util .read_dmi_data = _dmi_data ('RHEV Hypervisor' )
266254 dsrc = DataSourceAltCloud ({}, None , self .paths )
267255 dsrc .user_data_rhevm = lambda : True
268256 self .assertEquals (True , dsrc .get_data ())
269257
270258 def test_vsphere_no_cloud_file (self ):
271259 '''Test No cloud info file module get_data() forcing VSPHERE.'''
272260
273- cloudinit .sources .DataSourceAltCloud .CMD_DMI_SYSTEM = \
274- ['echo' , 'VMware Virtual Platform' ]
261+ util .read_dmi_data = _dmi_data ('VMware Virtual Platform' )
275262 dsrc = DataSourceAltCloud ({}, None , self .paths )
276263 dsrc .user_data_vsphere = lambda : True
277264 self .assertEquals (True , dsrc .get_data ())
278265
279266 def test_failure_no_cloud_file (self ):
280267 '''Test No cloud info file module get_data() forcing unrecognized.'''
281268
282- cloudinit .sources .DataSourceAltCloud .CMD_DMI_SYSTEM = \
283- ['echo' , 'Unrecognized Platform' ]
269+ util .read_dmi_data = _dmi_data ('Unrecognized Platform' )
284270 dsrc = DataSourceAltCloud ({}, None , self .paths )
285271 self .assertEquals (False , dsrc .get_data ())
286272
0 commit comments