blob: 231b0e0081a67e1059a17929ba8a4953d5a74b17 [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#!/usr/bin/python
2#
3# Copyright 2008 Google Inc. All Rights Reserved.
4
5"""Test for acl."""
6
7import unittest, sys
8
9import common
10from autotest_lib.cli import topic_common, action_common, acl, cli_mock
11
12
13class acl_list_unittest(cli_mock.cli_unittest):
14 def test_parse_list_acl(self):
15 acl_list = acl.acl_list()
16 afile = cli_mock.create_file('acl0\nacl3\nacl4\n')
17 sys.argv = ['atest', 'acl0', 'acl1,acl2',
mbligh41515392009-07-11 00:13:11 +000018 '--alist', afile.name, 'acl5', 'acl6,acl7']
mblighbe630eb2008-08-01 16:41:48 +000019 acl_list.parse()
20 self.assertEqualNoOrder(['acl%s' % x for x in range(8)],
21 acl_list.acls)
mbligh41515392009-07-11 00:13:11 +000022 afile.clean()
mblighbe630eb2008-08-01 16:41:48 +000023
24
25 def test_parse_list_user(self):
26 acl_list = acl.acl_list()
27 sys.argv = ['atest', '--user', 'user0']
28 acl_list.parse()
29 self.assertEqual('user0', acl_list.users)
30
31
32 def test_parse_list_host(self):
33 acl_list = acl.acl_list()
34 sys.argv = ['atest', '--mach', 'host0']
35 acl_list.parse()
36 self.assertEqual('host0', acl_list.hosts)
37
38
39 def _test_parse_bad_options(self):
40 acl_list = acl.acl_list()
41 self.god.mock_io()
42 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
43 self.assertRaises(cli_mock.ExitException, acl_list.parse)
44 (out, err) = self.god.unmock_io()
45 self.god.check_playback()
46 self.assert_(err.find('usage'))
47
48
49 def test_parse_list_acl_user(self):
50 sys.argv = ['atest', 'acl0', '-u', 'user']
51 self._test_parse_bad_options()
52
53
54 def test_parse_list_acl_2users(self):
55 sys.argv = ['atest', '-u', 'user0,user1']
56 self._test_parse_bad_options()
57
58
59 def test_parse_list_acl_host(self):
60 sys.argv = ['atest', 'acl0', '--mach', 'mach']
61 self._test_parse_bad_options()
62
63
64 def test_parse_list_acl_2hosts(self):
65 sys.argv = ['atest', '--mach', 'mach0,mach1']
66 self._test_parse_bad_options()
67
68
69 def test_parse_list_user_host(self):
70 sys.argv = ['atest', '-u', 'user', '--mach', 'mach']
71 self._test_parse_bad_options()
72
73
74 def test_parse_list_all(self):
75 sys.argv = ['atest', '-u', 'user', '--mach', 'mach', 'acl0']
76 self._test_parse_bad_options()
77
78
79 def test_execute_list_all_acls(self):
80 self.run_cmd(argv=['atest', 'acl', 'list', '-v'],
81 rpcs=[('get_acl_groups', {}, True,
82 [{'id': 1L,
83 'name': 'Everyone',
84 'description': '',
85 'users': ['debug_user'],
86 'hosts': []}])],
87 out_words_ok=['debug_user'])
88
89
90 def test_execute_list_acls_for_acl(self):
91 self.run_cmd(argv=['atest', 'acl', 'list', 'acl0'],
92 rpcs=[('get_acl_groups', {'name__in': ['acl0']}, True,
93 [{'id': 1L,
94 'name': 'Everyone',
95 'description': '',
96 'users': ['user0'],
97 'hosts': []}])],
98 out_words_ok=['Everyone'])
99
100
101 def test_execute_list_acls_for_user(self):
102 self.run_cmd(argv=['atest', 'acl', 'list', '-v', '--user', 'user0'],
103 rpcs=[('get_acl_groups', {'users__login': 'user0'}, True,
104 [{'id': 1L,
105 'name': 'Everyone',
106 'description': '',
107 'users': ['user0'],
108 'hosts': []}])],
109 out_words_ok=['user0'])
110
111
112 def test_execute_list_acls_for_host(self):
113 self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0'],
114 rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},
115 True,
116 [{'id': 1L,
117 'name': 'Everyone',
118 'description': '',
119 'users': ['user0'],
120 'hosts': ['host0']}])],
121 out_words_ok=['Everyone'],
122 out_words_no=['host0'])
123
124
125 def test_execute_list_acls_for_host_verb(self):
126 self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0', '-v'],
127 rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},
128 True,
129 [{'id': 1L,
130 'name': 'Everyone',
131 'description': '',
132 'users': ['user0'],
133 'hosts': ['host0']}])],
134 out_words_ok=['Everyone', 'host0'])
135
136
137
138class acl_create_unittest(cli_mock.cli_unittest):
139 def test_acl_create_parse_ok(self):
140 acls = acl.acl_create()
141 sys.argv = ['atest', 'acl0',
142 '--desc', 'my_favorite_acl']
143 acls.parse()
144 self.assertEqual('my_favorite_acl', acls.data['description'])
145
146
147 def test_acl_create_parse_no_desc(self):
148 self.god.mock_io()
149 acls = acl.acl_create()
150 sys.argv = ['atest', 'acl0']
151 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
152 self.assertRaises(cli_mock.ExitException, acls.parse)
153 self.god.check_playback()
154 self.god.unmock_io()
155
156
157 def test_acl_create_parse_2_acls(self):
158 self.god.mock_io()
159 acls = acl.acl_create()
160 sys.argv = ['atest', 'acl0', 'acl1',
161 '-desc', 'my_favorite_acl']
162 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
163 self.assertRaises(cli_mock.ExitException, acls.parse)
164 self.god.check_playback()
165 self.god.unmock_io()
166
167
168 def test_acl_create_parse_no_option(self):
169 self.god.mock_io()
170 acls = acl.acl_create()
171 sys.argv = ['atest']
172 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
173 self.assertRaises(cli_mock.ExitException, acls.parse)
174 self.god.check_playback()
175 self.god.unmock_io()
176
177
178 def test_acl_create_acl_ok(self):
179 self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',
180 '--desc', 'my_favorite_acl'],
181 rpcs=[('add_acl_group',
182 {'description': 'my_favorite_acl',
183 'name': 'acl0'},
184 True,
185 3L)],
186 out_words_ok=['acl0'])
187
188
189 def test_acl_create_duplicate_acl(self):
190 self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',
191 '--desc', 'my_favorite_acl'],
192 rpcs=[('add_acl_group',
193 {'description': 'my_favorite_acl',
194 'name': 'acl0'},
195 False,
196 'ValidationError:'
197 '''{'name': 'This value must be '''
198 '''unique (acl0)'}''')],
199 err_words_ok=['acl0', 'ValidationError',
200 'unique'])
201
202
203class acl_delete_unittest(cli_mock.cli_unittest):
204 def test_acl_delete_acl_ok(self):
205 self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0'],
206 rpcs=[('delete_acl_group', {'id': 'acl0'}, True, None)],
207 out_words_ok=['acl0'])
208
209
210 def test_acl_delete_acl_does_not_exist(self):
211 self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0'],
212 rpcs=[('delete_acl_group', {'id': 'acl0'},
213 False,
214 'DoesNotExist: acl_group matching '
215 'query does not exist.')],
216 err_words_ok=['acl0', 'DoesNotExist'])
217
218
219 def test_acl_delete_multiple_acl_ok(self):
220 alist = cli_mock.create_file('acl2\nacl1')
221 self.run_cmd(argv=['atest', 'acl', 'delete',
mbligh41515392009-07-11 00:13:11 +0000222 'acl0', 'acl1', '--alist', alist.name],
mblighbe630eb2008-08-01 16:41:48 +0000223 rpcs=[('delete_acl_group',
224 {'id': 'acl0'},
225 True,
226 None),
227 ('delete_acl_group',
228 {'id': 'acl1'},
229 True,
230 None),
231 ('delete_acl_group',
232 {'id': 'acl2'},
233 True,
234 None)],
235 out_words_ok=['acl0', 'acl1', 'acl2', 'Deleted'])
mbligh41515392009-07-11 00:13:11 +0000236 alist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000237
238
239 def test_acl_delete_multiple_acl_bad(self):
240 alist = cli_mock.create_file('acl2\nacl1')
241 self.run_cmd(argv=['atest', 'acl', 'delete',
mbligh41515392009-07-11 00:13:11 +0000242 'acl0', 'acl1', '--alist', alist.name],
mblighbe630eb2008-08-01 16:41:48 +0000243 rpcs=[('delete_acl_group',
244 {'id': 'acl0'},
245 True,
246 None),
247 ('delete_acl_group',
248 {'id': 'acl1'},
249 False,
250 'DoesNotExist: acl_group '
251 'matching query does not exist.'),
252 ('delete_acl_group',
253 {'id': 'acl2'},
254 True,
255 None)],
256 out_words_ok=['acl0', 'acl2', 'Deleted'],
257 err_words_ok=['acl1', 'delete_acl_group',
258 'DoesNotExist', 'acl_group',
259 'matching'])
mbligh41515392009-07-11 00:13:11 +0000260 alist.clean()
mblighbe630eb2008-08-01 16:41:48 +0000261
262
263class acl_add_unittest(cli_mock.cli_unittest):
264 def test_acl_add_parse_no_option(self):
265 self.god.mock_io()
266 acls = acl.acl_add()
267 sys.argv = ['atest']
268 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
269 self.assertRaises(cli_mock.ExitException, acls.parse)
270 self.god.unmock_io()
271 self.god.check_playback()
272
273
274 def test_acl_add_users_hosts(self):
275 self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
276 '-u', 'user0,user1', '-m', 'host0'],
277 rpcs=[('acl_group_add_users',
278 {'id': 'acl0',
279 'users': ['user0', 'user1']},
280 True,
281 None),
282 ('acl_group_add_hosts',
283 {'id': 'acl0',
284 'hosts': ['host0']},
285 True,
286 None)],
287 out_words_ok=['acl0', 'user0',
288 'user1', 'host0'])
289
290
291 def test_acl_add_bad_users(self):
292 self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
293 '-u', 'user0,user1'],
294 rpcs=[('acl_group_add_users',
295 {'id': 'acl0',
296 'users': ['user0', 'user1']},
297 False,
mbligh7a3ebe32008-12-01 17:10:33 +0000298 'DoesNotExist: The following Users do not exist: '
299 'user0, user1')],
300 err_words_ok=['user0', 'user1'])
301
302
303 def test_acl_add_bad_users_hosts(self):
304 self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
mblighad575162010-03-24 22:14:18 +0000305 '-u', 'user0,user1', '-m', 'host0,host1'],
mbligh7a3ebe32008-12-01 17:10:33 +0000306 rpcs=[('acl_group_add_users',
307 {'id': 'acl0',
308 'users': ['user0', 'user1']},
309 False,
310 'DoesNotExist: The following Users do not exist: '
311 'user0'),
312 ('acl_group_add_users',
313 {'id': 'acl0',
314 'users': ['user1']},
315 True,
316 None),
317 ('acl_group_add_hosts',
318 {'id': 'acl0',
mblighad575162010-03-24 22:14:18 +0000319 'hosts': ['host1', 'host0']},
mbligh7a3ebe32008-12-01 17:10:33 +0000320 False,
321 'DoesNotExist: The following Hosts do not exist: '
322 'host1'),
323 ('acl_group_add_hosts',
324 {'id': 'acl0',
325 'hosts': ['host0']},
326 True,
327 None)],
328 out_words_ok=['acl0', 'user1', 'host0'],
329 out_words_no=['user0', 'host1'],
330 err_words_ok=['user0', 'host1'],
331 err_words_no=['user1', 'host0'])
mblighbe630eb2008-08-01 16:41:48 +0000332
333
334class acl_remove_unittest(cli_mock.cli_unittest):
335 def test_acl_remove_remove_users(self):
336 self.run_cmd(argv=['atest', 'acl', 'remove',
337 'acl0', '-u', 'user0,user1'],
338 rpcs=[('acl_group_remove_users',
339 {'id': 'acl0',
340 'users': ['user0', 'user1']},
341 True,
342 None)],
343 out_words_ok=['acl0', 'user0', 'user1'],
344 out_words_no=['host'])
345
346
347 def test_acl_remove_remove_hosts(self):
348 self.run_cmd(argv=['atest', 'acl', 'remove',
349 'acl0', '--mach', 'host0,host1'],
350 rpcs=[('acl_group_remove_hosts',
351 {'id': 'acl0',
352 'hosts': ['host1', 'host0']},
353 True,
354 None)],
355 out_words_ok=['acl0', 'host0', 'host1'],
356 out_words_no=['user'])
357
358
359 def test_acl_remove_remove_both(self):
360 self.run_cmd(argv=['atest', 'acl', 'remove',
361 'acl0', '--user', 'user0,user1',
362 '-m', 'host0,host1'],
363 rpcs=[('acl_group_remove_users',
364 {'id': 'acl0',
365 'users': ['user0', 'user1']},
366 True,
367 None),
368 ('acl_group_remove_hosts',
369 {'id': 'acl0',
370 'hosts': ['host1', 'host0']},
371 True,
372 None)],
373 out_words_ok=['acl0', 'user0', 'user1',
374 'host0', 'host1'])
375
376
377if __name__ == '__main__':
378 unittest.main()