1
from cloudinit import distros
2
from cloudinit import helpers
3
from cloudinit import settings
5
from ..helpers import TestCase
10
'plain_text_passwd': 'ubuntu',
11
'home': "/home/ubuntu",
19
class TestUGNormalize(TestCase):
21
def _make_distro(self, dtype, def_user=None):
22
cfg = dict(settings.CFG_BUILTIN)
23
cfg['system_info']['distro'] = dtype
24
paths = helpers.Paths(cfg['system_info']['paths'])
25
distro_cls = distros.fetch(dtype)
27
cfg['system_info']['default_user'] = def_user.copy()
28
distro = distro_cls(dtype, cfg['system_info'], paths)
31
def _norm(self, cfg, distro):
32
return distros.normalize_users_groups(cfg, distro)
34
def test_group_dict(self):
35
distro = self._make_distro('ubuntu')
37
[{'ubuntu': ['foo', 'bar'],
41
(_users, groups) = self._norm(g, distro)
42
self.assertIn('ubuntu', groups)
43
ub_members = groups['ubuntu']
44
self.assertEqual(sorted(['foo', 'bar']), sorted(ub_members))
45
self.assertIn('bob', groups)
46
b_members = groups['bob']
47
self.assertEqual(sorted(['users', 'users2']),
50
def test_basic_groups(self):
51
distro = self._make_distro('ubuntu')
55
(users, groups) = self._norm(ug_cfg, distro)
56
self.assertIn('bob', groups)
57
self.assertEqual({}, users)
59
def test_csv_groups(self):
60
distro = self._make_distro('ubuntu')
62
'groups': 'bob,joe,steve',
64
(users, groups) = self._norm(ug_cfg, distro)
65
self.assertIn('bob', groups)
66
self.assertIn('joe', groups)
67
self.assertIn('steve', groups)
68
self.assertEqual({}, users)
70
def test_more_groups(self):
71
distro = self._make_distro('ubuntu')
73
'groups': ['bob', 'joe', 'steve']
75
(users, groups) = self._norm(ug_cfg, distro)
76
self.assertIn('bob', groups)
77
self.assertIn('joe', groups)
78
self.assertIn('steve', groups)
79
self.assertEqual({}, users)
81
def test_member_groups(self):
82
distro = self._make_distro('ubuntu')
90
(users, groups) = self._norm(ug_cfg, distro)
91
self.assertIn('bob', groups)
92
self.assertEqual(['s'], groups['bob'])
93
self.assertEqual([], groups['joe'])
94
self.assertIn('joe', groups)
95
self.assertIn('steve', groups)
96
self.assertEqual({}, users)
98
def test_users_simple_dict(self):
99
distro = self._make_distro('ubuntu', bcfg)
105
(users, _groups) = self._norm(ug_cfg, distro)
106
self.assertIn('bob', users)
112
(users, _groups) = self._norm(ug_cfg, distro)
113
self.assertIn('bob', users)
119
(users, _groups) = self._norm(ug_cfg, distro)
120
self.assertIn('bob', users)
122
def test_users_simple_dict_no(self):
123
distro = self._make_distro('ubuntu', bcfg)
129
(users, _groups) = self._norm(ug_cfg, distro)
130
self.assertEqual({}, users)
136
(users, _groups) = self._norm(ug_cfg, distro)
137
self.assertEqual({}, users)
139
def test_users_simple_csv(self):
140
distro = self._make_distro('ubuntu')
144
(users, _groups) = self._norm(ug_cfg, distro)
145
self.assertIn('joe', users)
146
self.assertIn('bob', users)
147
self.assertEqual({'default': False}, users['joe'])
148
self.assertEqual({'default': False}, users['bob'])
150
def test_users_simple(self):
151
distro = self._make_distro('ubuntu')
158
(users, _groups) = self._norm(ug_cfg, distro)
159
self.assertIn('joe', users)
160
self.assertIn('bob', users)
161
self.assertEqual({'default': False}, users['joe'])
162
self.assertEqual({'default': False}, users['bob'])
164
def test_users_old_user(self):
165
distro = self._make_distro('ubuntu', bcfg)
170
(users, _groups) = self._norm(ug_cfg, distro)
171
self.assertNotIn('bob', users) # Bob is not the default now, zetta is
172
self.assertIn('zetta', users)
173
self.assertTrue(users['zetta']['default'])
174
self.assertNotIn('default', users)
177
'users': 'default, joe'
179
(users, _groups) = self._norm(ug_cfg, distro)
180
self.assertNotIn('bob', users) # Bob is not the default now, zetta is
181
self.assertIn('joe', users)
182
self.assertIn('zetta', users)
183
self.assertTrue(users['zetta']['default'])
184
self.assertNotIn('default', users)
187
'users': ['bob', 'joe']
189
(users, _groups) = self._norm(ug_cfg, distro)
190
self.assertIn('bob', users)
191
self.assertIn('joe', users)
192
self.assertIn('zetta', users)
193
self.assertTrue(users['zetta']['default'])
201
(users, _groups) = self._norm(ug_cfg, distro)
202
self.assertIn('bob', users)
203
self.assertIn('joe', users)
204
self.assertIn('zetta', users)
205
self.assertTrue(users['zetta']['default'])
209
(users, _groups) = self._norm(ug_cfg, distro)
210
self.assertIn('zetta', users)
212
(users, groups) = self._norm(ug_cfg, distro)
213
self.assertEqual({}, users)
214
self.assertEqual({}, groups)
216
def test_users_dict_default_additional(self):
217
distro = self._make_distro('ubuntu', bcfg)
220
{'name': 'default', 'blah': True}
223
(users, _groups) = self._norm(ug_cfg, distro)
224
self.assertIn('bob', users)
225
self.assertEqual(",".join(distro.get_default_user()['groups']),
226
users['bob']['groups'])
227
self.assertEqual(True, users['bob']['blah'])
228
self.assertEqual(True, users['bob']['default'])
230
def test_users_dict_extract(self):
231
distro = self._make_distro('ubuntu', bcfg)
237
(users, _groups) = self._norm(ug_cfg, distro)
238
self.assertIn('bob', users)
239
(name, config) = distros.extract_default(users)
240
self.assertEqual(name, 'bob')
244
def_config = distro.get_default_user()
245
except NotImplementedError:
249
expected_config.update(def_config)
251
# Ignore these for now
252
expected_config.pop('name', None)
253
expected_config.pop('groups', None)
254
config.pop('groups', None)
255
self.assertEqual(config, expected_config)
257
def test_users_dict_default(self):
258
distro = self._make_distro('ubuntu', bcfg)
264
(users, _groups) = self._norm(ug_cfg, distro)
265
self.assertIn('bob', users)
266
self.assertEqual(",".join(distro.get_default_user()['groups']),
267
users['bob']['groups'])
268
self.assertEqual(True, users['bob']['default'])
270
def test_users_dict_trans(self):
271
distro = self._make_distro('ubuntu')
279
(users, _groups) = self._norm(ug_cfg, distro)
280
self.assertIn('joe', users)
281
self.assertIn('bob', users)
282
self.assertEqual({'tr_me': True, 'default': False}, users['joe'])
283
self.assertEqual({'default': False}, users['bob'])
285
def test_users_dict(self):
286
distro = self._make_distro('ubuntu')
293
(users, _groups) = self._norm(ug_cfg, distro)
294
self.assertIn('joe', users)
295
self.assertIn('bob', users)
296
self.assertEqual({'default': False}, users['joe'])
297
self.assertEqual({'default': False}, users['bob'])