summaryrefslogtreecommitdiff
path: root/tests/unittests/test_distros/test_user_data_normalize.py
blob: caf479cdc69bd5d94305a76b3e8744069c63968e (plain)
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
from mocker import MockerTestCase

from cloudinit import distros
from cloudinit import helpers
from cloudinit import settings


class TestUGNormalize(MockerTestCase):

    def _make_distro(self, dtype, def_user=None, def_groups=None):
        cfg = dict(settings.CFG_BUILTIN)
        cfg['system_info']['distro'] = dtype
        paths = helpers.Paths(cfg['system_info']['paths'])
        distro_cls = distros.fetch(dtype)
        distro = distro_cls(dtype, cfg['system_info'], paths)
        if def_user:
            distro.default_user = def_user
        if def_groups:
            distro.default_user_groups = def_groups
        return distro

    def test_basic_groups(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'groups': ['bob'],
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('bob', groups)
        self.assertEquals({}, users)
        self.assertEquals({}, def_user)

    def test_csv_groups(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'groups': 'bob,joe,steve',
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('bob', groups)
        self.assertIn('joe', groups)
        self.assertIn('steve', groups)
        self.assertEquals({}, users)
        self.assertEquals({}, def_user)

    def test_more_groups(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'groups': ['bob', 'joe', 'steve',]
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('bob', groups)
        self.assertIn('joe', groups)
        self.assertIn('steve', groups)
        self.assertEquals({}, users)
        self.assertEquals({}, def_user)

    def test_member_groups(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'groups': {
                'bob': ['s'],
                'joe': [],
                'steve': [],
            }
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('bob', groups)
        self.assertEquals(['s'], groups['bob'])
        self.assertEquals([], groups['joe'])
        self.assertIn('joe', groups)
        self.assertIn('steve', groups)
        self.assertEquals({}, users)
        self.assertEquals({}, def_user)

    def test_users_simple_dict(self):
        distro = self._make_distro('ubuntu', 'bob')
        ug_cfg = {
            'users': {
                'default': True,
            }
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertEquals('bob', def_user['name'])
        ug_cfg = {
            'users': {
                'default': 'yes',
            }
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertEquals('bob', def_user['name'])
        ug_cfg = {
            'users': {
                'default': '1',
            }
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertEquals('bob', def_user['name'])

    def test_users_simple_dict_no(self):
        distro = self._make_distro('ubuntu', 'bob')
        ug_cfg = {
            'users': {
                'default': False,
            }
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertEquals({}, def_user)
        ug_cfg = {
            'users': {
                'default': 'no',
            }
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertEquals({}, def_user)

    def test_users_simple_csv(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'users': 'joe,bob',
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('joe', users)
        self.assertIn('bob', users)
        self.assertEquals({}, users['joe'])
        self.assertEquals({}, users['bob'])

    def test_users_simple(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'users': [
                'joe',
                'bob'
            ],
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('joe', users)
        self.assertIn('bob', users)
        self.assertEquals({}, users['joe'])
        self.assertEquals({}, users['bob'])

    def test_users_dict_default_additional(self):
        distro = self._make_distro('ubuntu', 'bob')
        ug_cfg = {
            'users': [
                {'name': 'default', 'blah': True}
            ],
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('bob', def_user['name'])
        self.assertEquals(",".join(distro.get_default_user_groups()),
                          def_user['config']['groups'])
        self.assertEquals(True,
                          def_user['config']['blah'])
        self.assertNotIn('bob', users)

    def test_users_dict_default(self):
        distro = self._make_distro('ubuntu', 'bob')
        ug_cfg = {
            'users': [
                'default',
            ],
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('bob', def_user['name'])
        self.assertEquals(",".join(distro.get_default_user_groups()),
                          def_user['config']['groups'])
        self.assertNotIn('bob', users)

    def test_users_dict_trans(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'users': [
                {'name': 'joe',
                 'tr-me': True},
                {'name': 'bob'},
            ],
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('joe', users)
        self.assertIn('bob', users)
        self.assertEquals({'tr_me': True}, users['joe'])
        self.assertEquals({}, users['bob'])

    def test_users_dict(self):
        distro = self._make_distro('ubuntu')
        ug_cfg = {
            'users': [
                {'name': 'joe'},
                {'name': 'bob'},
            ],
        }
        ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg)
        self.assertIn('joe', users)
        self.assertIn('bob', users)
        self.assertEquals({}, users['joe'])
        self.assertEquals({}, users['bob'])