-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
test_all.py
331 lines (294 loc) · 14.2 KB
/
test_all.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#
# Copyright (C) 2015 SUSE LINUX GmbH
# Copyright (C) 2015 <contact@redhat.com>
#
# Author: Owen Synge <osynge@suse.com>
# Author: Loic Dachary <loic@dachary.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see `<http://www.gnu.org/licenses/>`.
#
import logging
import mock
import testtools
import ceph_detect_init
from ceph_detect_init import alpine
from ceph_detect_init import arch
from ceph_detect_init import centos
from ceph_detect_init import debian
from ceph_detect_init import exc
from ceph_detect_init import fedora
from ceph_detect_init import main
from ceph_detect_init import rhel
from ceph_detect_init import suse
from ceph_detect_init import gentoo
from ceph_detect_init import freebsd
from ceph_detect_init import docker
from ceph_detect_init import oraclevms
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG)
class TestCephDetectInit(testtools.TestCase):
def test_alpine(self):
self.assertEqual('openrc', alpine.choose_init())
def test_arch(self):
self.assertEqual('systemd', arch.choose_init())
def test_freebsd(self):
self.assertEqual('bsdrc', freebsd.choose_init())
def test_docker(self):
self.assertEqual('none', docker.choose_init())
def test_oraclevms(self):
self.assertEqual('sysvinit', oraclevms.choose_init())
def test_centos(self):
with mock.patch('ceph_detect_init.centos.release',
'7.0'):
self.assertEqual('systemd', centos.choose_init())
self.assertEqual('sysvinit', centos.choose_init())
def test_debian(self):
with mock.patch.multiple('os.path',
isdir=lambda x: x == '/run/systemd/system'):
self.assertEqual('systemd', debian.choose_init())
def mock_call_with_upstart(*args, **kwargs):
if args[0] == '. /lib/lsb/init-functions ; init_is_upstart' and \
kwargs['shell']:
return 0
else:
return 1
with mock.patch.multiple('os.path',
isdir=lambda x: False,
isfile=lambda x: False):
with mock.patch.multiple('subprocess',
call=mock_call_with_upstart):
self.assertEqual('upstart', debian.choose_init())
with mock.patch.multiple('os.path',
isdir=lambda x: False,
isfile=lambda x: x == '/sbin/init',
islink=lambda x: x != '/sbin/init'):
with mock.patch.multiple('subprocess',
call=lambda *args, **kwargs: 1):
self.assertEqual('sysvinit', debian.choose_init())
with mock.patch.multiple('os.path',
isdir=lambda x: False,
isfile=lambda x: False):
with mock.patch.multiple('subprocess',
call=lambda *args, **kwargs: 1):
self.assertIs(None, debian.choose_init())
def test_fedora(self):
with mock.patch('ceph_detect_init.fedora.release',
'22'):
self.assertEqual('systemd', fedora.choose_init())
self.assertEqual('sysvinit', fedora.choose_init())
def test_rhel(self):
with mock.patch('ceph_detect_init.rhel.release',
'7.0'):
self.assertEqual('systemd', rhel.choose_init())
self.assertEqual('sysvinit', rhel.choose_init())
def test_suse(self):
with mock.patch('ceph_detect_init.suse.release',
'11'):
self.assertEqual('sysvinit', suse.choose_init())
with mock.patch('ceph_detect_init.suse.release',
'12'):
self.assertEqual('systemd', suse.choose_init())
with mock.patch('ceph_detect_init.suse.release',
'13.1'):
self.assertEqual('systemd', suse.choose_init())
with mock.patch('ceph_detect_init.suse.release',
'13.2'):
self.assertEqual('systemd', suse.choose_init())
def test_gentoo_is_openrc(self):
with mock.patch('os.path.isdir', return_value=True):
self.assertEqual(gentoo.is_openrc(), True)
with mock.patch('os.path.isdir', return_value=False):
self.assertEqual(gentoo.is_openrc(), False)
def test_gentoo_is_systemd(self):
import sys
if sys.version_info >= (3, 0):
mocked_fn = 'builtins.open'
else:
mocked_fn = '__builtin__.open'
f = mock.mock_open(read_data='systemd')
with mock.patch(mocked_fn, f, create=True) as m:
self.assertEqual(gentoo.is_systemd(), True)
m.assert_called_once_with('/proc/1/comm')
f = mock.mock_open(read_data='init')
with mock.patch(mocked_fn, f, create=True) as m:
self.assertEqual(gentoo.is_systemd(), False)
m.assert_called_once_with('/proc/1/comm')
f = mock.mock_open(read_data='upstart')
with mock.patch(mocked_fn, f, create=True) as m:
self.assertEqual(gentoo.is_systemd(), False)
m.assert_called_once_with('/proc/1/comm')
def test_gentoo(self):
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: True),
is_openrc=(lambda: True)):
self.assertEqual('openrc', gentoo.choose_init())
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: True),
is_openrc=(lambda: False)):
self.assertEqual('systemd', gentoo.choose_init())
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: False),
is_openrc=(lambda: True)):
self.assertEqual('openrc', gentoo.choose_init())
with mock.patch.multiple('ceph_detect_init.gentoo',
is_systemd=(lambda: False),
is_openrc=(lambda: False)):
self.assertEqual('unknown', gentoo.choose_init())
def test_get(self):
with mock.patch.multiple(
'platform',
system=lambda: 'Linux',
linux_distribution=lambda **kwargs: (('unknown', '', ''))):
g = ceph_detect_init.get
self.assertRaises(exc.UnsupportedPlatform, g)
try:
g()
except exc.UnsupportedPlatform as e:
self.assertIn('Platform is not supported', str(e))
with mock.patch.multiple(
'platform',
system=lambda: 'Linux',
linux_distribution=lambda **kwargs: (('debian', '6.0', ''))):
distro = ceph_detect_init.get()
self.assertEqual(debian, distro)
self.assertEqual('debian', distro.name)
self.assertEqual('debian', distro.normalized_name)
self.assertEqual('debian', distro.distro)
self.assertEqual(False, distro.is_el)
self.assertEqual('6.0', distro.release)
with mock.patch.multiple('platform',
system=lambda: 'FreeBSD',
release=lambda: '12.0-CURRENT',
version=lambda: 'FreeBSD 12.0 #1 r306554M:'):
distro = ceph_detect_init.get()
self.assertEqual(freebsd, distro)
self.assertEqual('freebsd', distro.name)
self.assertEqual('freebsd', distro.normalized_name)
self.assertEqual('freebsd', distro.distro)
self.assertFalse(distro.is_el)
self.assertEqual('12.0-CURRENT', distro.release)
self.assertEqual('r306554M', distro.codename)
self.assertEqual('bsdrc', distro.init)
with mock.patch('platform.system',
lambda: 'cephix'):
self.assertRaises(exc.UnsupportedPlatform, ceph_detect_init.get)
def test_get_distro(self):
g = ceph_detect_init._get_distro
self.assertEqual(None, g(None))
self.assertEqual(debian, g('debian'))
self.assertEqual(debian, g('ubuntu'))
self.assertEqual(centos, g('centos'))
self.assertEqual(centos, g('scientific'))
self.assertEqual(centos, g('Oracle Linux Server'))
self.assertEqual(oraclevms, g('Oracle VM server'))
self.assertEqual(fedora, g('fedora'))
self.assertEqual(suse, g('suse'))
self.assertEqual(rhel, g('redhat', use_rhceph=True))
self.assertEqual(gentoo, g('gentoo'))
self.assertEqual(centos, g('virtuozzo'))
def test_normalized_distro_name(self):
n = ceph_detect_init._normalized_distro_name
self.assertEqual('redhat', n('RedHat'))
self.assertEqual('redhat', n('redhat'))
self.assertEqual('redhat', n('Red Hat'))
self.assertEqual('redhat', n('red hat'))
self.assertEqual('scientific', n('scientific'))
self.assertEqual('scientific', n('Scientific'))
self.assertEqual('scientific', n('Scientific Linux'))
self.assertEqual('scientific', n('scientific linux'))
self.assertEqual('oraclel', n('Oracle Linux Server'))
self.assertEqual('oraclevms', n('Oracle VM server'))
self.assertEqual('suse', n('SUSE'))
self.assertEqual('suse', n('suse'))
self.assertEqual('suse', n('openSUSE'))
self.assertEqual('suse', n('opensuse'))
self.assertEqual('centos', n('CentOS'))
self.assertEqual('centos', n('centos'))
self.assertEqual('debian', n('Debian'))
self.assertEqual('debian', n('debian'))
self.assertEqual('ubuntu', n('Ubuntu'))
self.assertEqual('ubuntu', n('ubuntu'))
self.assertEqual('gentoo', n('Gentoo'))
self.assertEqual('gentoo', n('gentoo'))
self.assertEqual('gentoo', n('Funtoo'))
self.assertEqual('gentoo', n('funtoo'))
self.assertEqual('gentoo', n('Exherbo'))
self.assertEqual('gentoo', n('exherbo'))
self.assertEqual('virtuozzo', n('Virtuozzo Linux'))
@mock.patch('platform.system', lambda: 'Linux')
def test_platform_information_linux(self):
with mock.patch('platform.linux_distribution',
lambda **kwargs: (('debian', '8.0', ''))):
self.assertEqual(('debian', '8.0'),
ceph_detect_init.platform_information()[:-1])
with mock.patch('platform.linux_distribution',
lambda **kwargs: (('Oracle Linux Server', '7.3', ''))):
self.assertEqual(('Oracle Linux Server', '7.3', 'OL7.3'),
ceph_detect_init.platform_information())
with mock.patch('platform.linux_distribution',
lambda **kwargs: (('Oracle VM server', '3.4.2', ''))):
self.assertEqual(('Oracle VM server', '3.4.2', 'OVS3.4.2'),
ceph_detect_init.platform_information())
with mock.patch('platform.linux_distribution',
lambda **kwargs: (('Virtuozzo Linux', '7.3', ''))):
self.assertEqual(('Virtuozzo Linux', '7.3', 'virtuozzo'),
ceph_detect_init.platform_information())
@mock.patch('platform.linux_distribution')
def test_platform_information_container(self, mock_linux_dist):
import sys
if sys.version_info >= (3, 0):
mocked_fn = 'builtins.open'
else:
mocked_fn = '__builtin__.open'
with mock.patch(mocked_fn,
mock.mock_open(read_data="""1:name=systemd:/system.slice \
/docker-39cc1fb.scope"""),
create=True) as m:
self.assertEqual(('docker',
'docker',
'docker'),
ceph_detect_init.platform_information(),)
m.assert_called_once_with('/proc/self/cgroup', 'r')
with mock.patch(mocked_fn, mock.mock_open(), create=True) as m:
m.side_effect = IOError()
mock_linux_dist.return_value = ('Red Hat Enterprise Linux Server',
'7.3', 'Maipo')
# Just run the code to validate the code won't raise IOError
ceph_detect_init.platform_information()
with mock.patch('os.path.isfile', mock.MagicMock()) as m:
m.return_value = True
self.assertEqual(('docker',
'docker',
'docker'),
ceph_detect_init.platform_information(),)
m.assert_called_once_with('/.dockerenv')
@mock.patch('platform.system', lambda: 'FreeBSD')
def test_platform_information_freebsd(self):
with mock.patch.multiple('platform',
release=lambda: '12.0-CURRENT',
version=lambda: 'FreeBSD 12.0 #1 r306554M:'):
self.assertEqual(('freebsd', '12.0-CURRENT', 'r306554M'),
ceph_detect_init.platform_information())
def test_run(self):
argv = ['--use-rhceph', '--verbose']
self.assertEqual(0, main.run(argv))
with mock.patch.multiple(
'platform',
system=lambda: 'Linux',
linux_distribution=lambda **kwargs: (('unknown', '', ''))):
self.assertRaises(exc.UnsupportedPlatform, main.run, argv)
self.assertEqual(0, main.run(argv + ['--default=sysvinit']))
# Local Variables:
# compile-command: "cd .. ; .tox/py27/bin/py.test tests/test_all.py"
# End: