liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# pylint: disable-msg=C0111
#!/usr/bin/python
#
# Copyright 2008 Google Inc. All Rights Reserved.
 
"""Test for acl."""
 
import unittest, sys
 
import common
from autotest_lib.cli import acl, cli_mock
 
 
class acl_list_unittest(cli_mock.cli_unittest):
    def test_parse_list_acl(self):
        acl_list = acl.acl_list()
        afile = cli_mock.create_file('acl0\nacl3\nacl4\n')
        sys.argv = ['atest', 'acl0', 'acl1,acl2',
                    '--alist', afile.name, 'acl5', 'acl6,acl7']
        acl_list.parse()
        self.assertEqualNoOrder(['acl%s' % x for x in range(8)],
                                acl_list.acls)
        afile.clean()
 
 
    def test_parse_list_user(self):
        acl_list = acl.acl_list()
        sys.argv = ['atest', '--user', 'user0']
        acl_list.parse()
        self.assertEqual('user0', acl_list.users)
 
 
    def test_parse_list_host(self):
        acl_list = acl.acl_list()
        sys.argv = ['atest', '--mach', 'host0']
        acl_list.parse()
        self.assertEqual('host0', acl_list.hosts)
 
 
    def _test_parse_bad_options(self):
        acl_list = acl.acl_list()
        self.god.mock_io()
        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
        self.assertRaises(cli_mock.ExitException, acl_list.parse)
        (out, err) = self.god.unmock_io()
        self.god.check_playback()
        self.assert_(err.find('usage'))
 
 
    def test_parse_list_acl_user(self):
        sys.argv = ['atest', 'acl0', '-u', 'user']
        self._test_parse_bad_options()
 
 
    def test_parse_list_acl_2users(self):
        sys.argv = ['atest', '-u', 'user0,user1']
        self._test_parse_bad_options()
 
 
    def test_parse_list_acl_host(self):
        sys.argv = ['atest', 'acl0', '--mach', 'mach']
        self._test_parse_bad_options()
 
 
    def test_parse_list_acl_2hosts(self):
        sys.argv = ['atest', '--mach', 'mach0,mach1']
        self._test_parse_bad_options()
 
 
    def test_parse_list_user_host(self):
        sys.argv = ['atest', '-u', 'user', '--mach', 'mach']
        self._test_parse_bad_options()
 
 
    def test_parse_list_all(self):
        sys.argv = ['atest', '-u', 'user', '--mach', 'mach', 'acl0']
        self._test_parse_bad_options()
 
 
    def test_execute_list_all_acls(self):
        self.run_cmd(argv=['atest', 'acl', 'list', '-v'],
                     rpcs=[('get_acl_groups', {}, True,
                           [{'id': 1L,
                             'name': 'Everyone',
                             'description': '',
                             'users': ['debug_user'],
                             'hosts': []}])],
                     out_words_ok=['debug_user'])
 
 
    def test_execute_list_acls_for_acl(self):
        self.run_cmd(argv=['atest', 'acl', 'list', 'acl0'],
                     rpcs=[('get_acl_groups', {'name__in': ['acl0']}, True,
                           [{'id': 1L,
                             'name': 'Everyone',
                             'description': '',
                             'users': ['user0'],
                             'hosts': []}])],
                     out_words_ok=['Everyone'])
 
 
    def test_execute_list_acls_for_user(self):
        self.run_cmd(argv=['atest', 'acl', 'list', '-v', '--user', 'user0'],
                     rpcs=[('get_acl_groups', {'users__login': 'user0'}, True,
                           [{'id': 1L,
                             'name': 'Everyone',
                             'description': '',
                             'users': ['user0'],
                             'hosts': []}])],
                     out_words_ok=['user0'])
 
 
    def test_execute_list_acls_for_host(self):
        self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0'],
                     rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},
                            True,
                           [{'id': 1L,
                             'name': 'Everyone',
                             'description': '',
                             'users': ['user0'],
                             'hosts': ['host0']}])],
                     out_words_ok=['Everyone'],
                     out_words_no=['host0'])
 
 
    def test_execute_list_acls_for_host_verb(self):
        self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0', '-v'],
                     rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},
                            True,
                           [{'id': 1L,
                             'name': 'Everyone',
                             'description': '',
                             'users': ['user0'],
                             'hosts': ['host0']}])],
                     out_words_ok=['Everyone', 'host0'])
 
 
 
class acl_create_unittest(cli_mock.cli_unittest):
    def test_acl_create_parse_ok(self):
        acls = acl.acl_create()
        sys.argv = ['atest', 'acl0',
                    '--desc', 'my_favorite_acl']
        acls.parse()
        self.assertEqual('my_favorite_acl', acls.data['description'])
 
 
    def test_acl_create_parse_no_desc(self):
        self.god.mock_io()
        acls = acl.acl_create()
        sys.argv = ['atest', 'acl0']
        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
        self.assertRaises(cli_mock.ExitException, acls.parse)
        self.god.check_playback()
        self.god.unmock_io()
 
 
    def test_acl_create_parse_2_acls(self):
        self.god.mock_io()
        acls = acl.acl_create()
        sys.argv = ['atest', 'acl0', 'acl1',
                    '-desc', 'my_favorite_acl']
        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
        self.assertRaises(cli_mock.ExitException, acls.parse)
        self.god.check_playback()
        self.god.unmock_io()
 
 
    def test_acl_create_parse_no_option(self):
        self.god.mock_io()
        acls = acl.acl_create()
        sys.argv = ['atest']
        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
        self.assertRaises(cli_mock.ExitException, acls.parse)
        self.god.check_playback()
        self.god.unmock_io()
 
 
    def test_acl_create_acl_ok(self):
        self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',
                           '--desc', 'my_favorite_acl'],
                     rpcs=[('add_acl_group',
                           {'description': 'my_favorite_acl',
                            'name': 'acl0'},
                           True,
                            3L)],
                     out_words_ok=['acl0'])
 
 
    def test_acl_create_duplicate_acl(self):
        self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',
                           '--desc', 'my_favorite_acl'],
                     rpcs=[('add_acl_group',
                           {'description': 'my_favorite_acl',
                            'name': 'acl0'},
                           False,
                           'ValidationError:'
                           '''{'name': 'This value must be '''
                           '''unique (acl0)'}''')],
                     err_words_ok=['acl0', 'ValidationError',
                                   'unique'])
 
 
class acl_delete_unittest(cli_mock.cli_unittest):
    def test_acl_delete_acl_ok(self):
        self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0',
                           '--no-confirmation'],
                     rpcs=[('delete_acl_group', {'id': 'acl0'}, True, None)],
                     out_words_ok=['acl0'])
 
 
    def test_acl_delete_acl_does_not_exist(self):
        self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0',
                           '--no-confirmation'],
                     rpcs=[('delete_acl_group', {'id': 'acl0'},
                            False,
                            'DoesNotExist: acl_group matching '
                            'query does not exist.')],
                     err_words_ok=['acl0', 'DoesNotExist'])
 
 
    def test_acl_delete_multiple_acl_ok(self):
        alist = cli_mock.create_file('acl2\nacl1')
        self.run_cmd(argv=['atest', 'acl', 'delete',
                           'acl0', 'acl1', '--no-confirmation',
                           '--alist', alist.name],
                     rpcs=[('delete_acl_group',
                           {'id': 'acl0'},
                           True,
                           None),
                          ('delete_acl_group',
                           {'id': 'acl1'},
                           True,
                           None),
                          ('delete_acl_group',
                           {'id': 'acl2'},
                           True,
                           None)],
                     out_words_ok=['acl0', 'acl1', 'acl2', 'Deleted'])
        alist.clean()
 
 
    def test_acl_delete_multiple_acl_bad(self):
        alist = cli_mock.create_file('acl2\nacl1')
        self.run_cmd(argv=['atest', 'acl', 'delete',
                           'acl0', 'acl1', '--no-confirmation',
                           '--alist', alist.name],
                     rpcs=[('delete_acl_group',
                           {'id': 'acl0'},
                           True,
                           None),
                          ('delete_acl_group',
                           {'id': 'acl1'},
                           False,
                           'DoesNotExist: acl_group '
                           'matching query does not exist.'),
                          ('delete_acl_group',
                           {'id': 'acl2'},
                           True,
                           None)],
                     out_words_ok=['acl0', 'acl2', 'Deleted'],
                     err_words_ok=['acl1', 'delete_acl_group',
                                   'DoesNotExist', 'acl_group',
                                   'matching'])
        alist.clean()
 
 
class acl_add_unittest(cli_mock.cli_unittest):
    def test_acl_add_parse_no_option(self):
        self.god.mock_io()
        acls = acl.acl_add()
        sys.argv = ['atest']
        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
        self.assertRaises(cli_mock.ExitException, acls.parse)
        self.god.unmock_io()
        self.god.check_playback()
 
 
    def test_acl_add_users_hosts(self):
        self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
                           '-u', 'user0,user1', '-m', 'host0'],
                     rpcs=[('acl_group_add_users',
                           {'id': 'acl0',
                            'users': ['user0', 'user1']},
                           True,
                           None),
                          ('acl_group_add_hosts',
                           {'id': 'acl0',
                            'hosts': ['host0']},
                           True,
                           None)],
                     out_words_ok=['acl0', 'user0',
                                   'user1', 'host0'])
 
 
    def test_acl_add_bad_users(self):
        self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
                           '-u', 'user0,user1'],
                     rpcs=[('acl_group_add_users',
                           {'id': 'acl0',
                            'users': ['user0', 'user1']},
                            False,
                            'DoesNotExist: The following Users do not exist: '
                            'user0, user1')],
                     err_words_ok=['user0', 'user1'])
 
 
    def test_acl_add_bad_users_hosts(self):
        self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
                           '-u', 'user0,user1', '-m', 'host0,host1'],
                     rpcs=[('acl_group_add_users',
                           {'id': 'acl0',
                            'users': ['user0', 'user1']},
                            False,
                            'DoesNotExist: The following Users do not exist: '
                            'user0'),
                           ('acl_group_add_users',
                           {'id': 'acl0',
                            'users': ['user1']},
                            True,
                            None),
                           ('acl_group_add_hosts',
                            {'id': 'acl0',
                             'hosts': ['host1', 'host0']},
                            False,
                            'DoesNotExist: The following Hosts do not exist: '
                            'host1'),
                           ('acl_group_add_hosts',
                            {'id': 'acl0',
                             'hosts': ['host0']},
                            True,
                            None)],
                     out_words_ok=['acl0', 'user1', 'host0'],
                     out_words_no=['user0', 'host1'],
                     err_words_ok=['user0', 'host1'],
                     err_words_no=['user1', 'host0'])
 
 
class acl_remove_unittest(cli_mock.cli_unittest):
    def test_acl_remove_remove_users(self):
        self.run_cmd(argv=['atest', 'acl', 'remove',
                           'acl0', '-u', 'user0,user1'],
                     rpcs=[('acl_group_remove_users',
                           {'id': 'acl0',
                            'users': ['user0', 'user1']},
                           True,
                           None)],
                     out_words_ok=['acl0', 'user0', 'user1'],
                     out_words_no=['host'])
 
 
    def test_acl_remove_remove_hosts(self):
        self.run_cmd(argv=['atest', 'acl', 'remove',
                           'acl0', '--mach', 'host0,host1'],
                     rpcs=[('acl_group_remove_hosts',
                           {'id': 'acl0',
                            'hosts': ['host1', 'host0']},
                           True,
                           None)],
                     out_words_ok=['acl0', 'host0', 'host1'],
                     out_words_no=['user'])
 
 
    def test_acl_remove_remove_both(self):
        self.run_cmd(argv=['atest', 'acl', 'remove',
                           'acl0', '--user', 'user0,user1',
                           '-m', 'host0,host1'],
                     rpcs=[('acl_group_remove_users',
                           {'id': 'acl0',
                            'users': ['user0', 'user1']},
                           True,
                           None),
                          ('acl_group_remove_hosts',
                           {'id': 'acl0',
                            'hosts': ['host1', 'host0']},
                           True,
                           None)],
                     out_words_ok=['acl0', 'user0', 'user1',
                                   'host0', 'host1'])
 
 
if __name__ == '__main__':
    unittest.main()