1
from mocker import MockerTestCase
3
from cloudinit import distros
4
from cloudinit import helpers
5
from cloudinit import settings
8
class TestUGNormalize(MockerTestCase):
10
def _make_distro(self, dtype, def_user=None, def_groups=None):
11
cfg = dict(settings.CFG_BUILTIN)
12
cfg['system_info']['distro'] = dtype
13
paths = helpers.Paths(cfg['system_info']['paths'])
14
distro_cls = distros.fetch(dtype)
15
distro = distro_cls(dtype, cfg['system_info'], paths)
17
distro.default_user = def_user
19
distro.default_user_groups = def_groups
22
def _norm(self, cfg, distro):
23
return distros.normalize_users_groups(cfg, distro)
25
def test_basic_groups(self):
26
distro = self._make_distro('ubuntu')
30
(users, groups) = self._norm(ug_cfg, distro)
31
self.assertIn('bob', groups)
32
self.assertEquals({}, users)
34
def test_csv_groups(self):
35
distro = self._make_distro('ubuntu')
37
'groups': 'bob,joe,steve',
39
(users, groups) = self._norm(ug_cfg, distro)
40
self.assertIn('bob', groups)
41
self.assertIn('joe', groups)
42
self.assertIn('steve', groups)
43
self.assertEquals({}, users)
45
def test_more_groups(self):
46
distro = self._make_distro('ubuntu')
48
'groups': ['bob', 'joe', 'steve']
50
(users, groups) = self._norm(ug_cfg, distro)
51
self.assertIn('bob', groups)
52
self.assertIn('joe', groups)
53
self.assertIn('steve', groups)
54
self.assertEquals({}, users)
56
def test_member_groups(self):
57
distro = self._make_distro('ubuntu')
65
(users, groups) = self._norm(ug_cfg, distro)
66
self.assertIn('bob', groups)
67
self.assertEquals(['s'], groups['bob'])
68
self.assertEquals([], groups['joe'])
69
self.assertIn('joe', groups)
70
self.assertIn('steve', groups)
71
self.assertEquals({}, users)
73
def test_users_simple_dict(self):
74
distro = self._make_distro('ubuntu', 'bob')
80
(users, _groups) = self._norm(ug_cfg, distro)
81
self.assertIn('bob', users)
87
(users, _groups) = self._norm(ug_cfg, distro)
88
self.assertIn('bob', users)
94
(users, _groups) = self._norm(ug_cfg, distro)
95
self.assertIn('bob', users)
97
def test_users_simple_dict_no(self):
98
distro = self._make_distro('ubuntu', 'bob')
104
(users, _groups) = self._norm(ug_cfg, distro)
105
self.assertEquals({}, users)
111
(users, _groups) = self._norm(ug_cfg, distro)
112
self.assertEquals({}, users)
114
def test_users_simple_csv(self):
115
distro = self._make_distro('ubuntu')
119
(users, _groups) = self._norm(ug_cfg, distro)
120
self.assertIn('joe', users)
121
self.assertIn('bob', users)
122
self.assertEquals({'default': False}, users['joe'])
123
self.assertEquals({'default': False}, users['bob'])
125
def test_users_simple(self):
126
distro = self._make_distro('ubuntu')
133
(users, _groups) = self._norm(ug_cfg, distro)
134
self.assertIn('joe', users)
135
self.assertIn('bob', users)
136
self.assertEquals({'default': False}, users['joe'])
137
self.assertEquals({'default': False}, users['bob'])
139
def test_users_old_user(self):
140
distro = self._make_distro('ubuntu', 'bob')
145
(users, _groups) = self._norm(ug_cfg, distro)
146
self.assertIn('bob', users)
147
self.assertIn('zetta', users)
148
self.assertNotIn('default', users)
151
'users': 'default, joe'
153
(users, _groups) = self._norm(ug_cfg, distro)
154
self.assertIn('bob', users)
155
self.assertIn('joe', users)
156
self.assertIn('zetta', users)
157
self.assertNotIn('default', users)
160
'users': ['bob', 'joe']
162
(users, _groups) = self._norm(ug_cfg, distro)
163
self.assertNotIn('bob', users)
164
self.assertIn('joe', users)
165
self.assertIn('zetta', users)
173
(users, _groups) = self._norm(ug_cfg, distro)
174
self.assertIn('bob', users)
175
self.assertIn('joe', users)
176
self.assertIn('zetta', users)
180
(users, _groups) = self._norm(ug_cfg, distro)
181
self.assertIn('zetta', users)
183
(users, groups) = self._norm(ug_cfg, distro)
184
self.assertEquals({}, users)
185
self.assertEquals({}, groups)
187
def test_users_dict_default_additional(self):
188
distro = self._make_distro('ubuntu', 'bob')
191
{'name': 'default', 'blah': True}
194
(users, _groups) = self._norm(ug_cfg, distro)
195
self.assertIn('bob', users)
196
self.assertEquals(",".join(distro.get_default_user()['groups']),
197
users['bob']['groups'])
198
self.assertEquals(True,
199
users['bob']['blah'])
200
self.assertEquals(True,
201
users['bob']['default'])
203
def test_users_dict_extract(self):
204
distro = self._make_distro('ubuntu', 'bob')
210
(users, _groups) = self._norm(ug_cfg, distro)
211
self.assertIn('bob', users)
212
(name, config) = distros.extract_default(users)
213
self.assertEquals(name, 'bob')
217
def_config = distro.get_default_user()
218
except NotImplementedError:
222
expected_config.update(def_config)
224
# Ignore these for now
225
expected_config.pop('name', None)
226
expected_config.pop('groups', None)
227
config.pop('groups', None)
228
self.assertEquals(config, expected_config)
230
def test_users_dict_default(self):
231
distro = self._make_distro('ubuntu', 'bob')
237
(users, _groups) = self._norm(ug_cfg, distro)
238
self.assertIn('bob', users)
239
self.assertEquals(",".join(distro.get_default_user()['groups']),
240
users['bob']['groups'])
241
self.assertEquals(True,
242
users['bob']['default'])
244
def test_users_dict_trans(self):
245
distro = self._make_distro('ubuntu')
253
(users, _groups) = self._norm(ug_cfg, distro)
254
self.assertIn('joe', users)
255
self.assertIn('bob', users)
256
self.assertEquals({'tr_me': True, 'default': False}, users['joe'])
257
self.assertEquals({'default': False}, users['bob'])
259
def test_users_dict(self):
260
distro = self._make_distro('ubuntu')
267
(users, _groups) = self._norm(ug_cfg, distro)
268
self.assertIn('joe', users)
269
self.assertIn('bob', users)
270
self.assertEquals({'default': False}, users['joe'])
271
self.assertEquals({'default': False}, users['bob'])