3 # Copyright 2008 Google Inc. All Rights Reserved.
10 from autotest_lib
.cli
import topic_common
, action_common
, acl
, cli_mock
13 class 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',
18 '--alist', afile
.name
, 'acl5', 'acl6,acl7']
20 self
.assertEqualNoOrder(['acl%s' % x
for x
in range(8)],
25 def test_parse_list_user(self
):
26 acl_list
= acl
.acl_list()
27 sys
.argv
= ['atest', '--user', 'user0']
29 self
.assertEqual('user0', acl_list
.users
)
32 def test_parse_list_host(self
):
33 acl_list
= acl
.acl_list()
34 sys
.argv
= ['atest', '--mach', 'host0']
36 self
.assertEqual('host0', acl_list
.hosts
)
39 def _test_parse_bad_options(self
):
40 acl_list
= acl
.acl_list()
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'))
49 def test_parse_list_acl_user(self
):
50 sys
.argv
= ['atest', 'acl0', '-u', 'user']
51 self
._test
_parse
_bad
_options
()
54 def test_parse_list_acl_2users(self
):
55 sys
.argv
= ['atest', '-u', 'user0,user1']
56 self
._test
_parse
_bad
_options
()
59 def test_parse_list_acl_host(self
):
60 sys
.argv
= ['atest', 'acl0', '--mach', 'mach']
61 self
._test
_parse
_bad
_options
()
64 def test_parse_list_acl_2hosts(self
):
65 sys
.argv
= ['atest', '--mach', 'mach0,mach1']
66 self
._test
_parse
_bad
_options
()
69 def test_parse_list_user_host(self
):
70 sys
.argv
= ['atest', '-u', 'user', '--mach', 'mach']
71 self
._test
_parse
_bad
_options
()
74 def test_parse_list_all(self
):
75 sys
.argv
= ['atest', '-u', 'user', '--mach', 'mach', 'acl0']
76 self
._test
_parse
_bad
_options
()
79 def test_execute_list_all_acls(self
):
80 self
.run_cmd(argv
=['atest', 'acl', 'list', '-v'],
81 rpcs
=[('get_acl_groups', {}, True,
85 'users': ['debug_user'],
87 out_words_ok
=['debug_user'])
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,
98 out_words_ok
=['Everyone'])
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,
109 out_words_ok
=['user0'])
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'},
120 'hosts': ['host0']}])],
121 out_words_ok
=['Everyone'],
122 out_words_no
=['host0'])
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'},
133 'hosts': ['host0']}])],
134 out_words_ok
=['Everyone', 'host0'])
138 class 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']
144 self
.assertEqual('my_favorite_acl', acls
.data
['description'])
147 def test_acl_create_parse_no_desc(self
):
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()
157 def test_acl_create_parse_2_acls(self
):
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()
168 def test_acl_create_parse_no_option(self
):
170 acls
= acl
.acl_create()
172 sys
.exit
.expect_call(1).and_raises(cli_mock
.ExitException
)
173 self
.assertRaises(cli_mock
.ExitException
, acls
.parse
)
174 self
.god
.check_playback()
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',
186 out_words_ok
=['acl0'])
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',
197 '''{'name': 'This value must be '''
198 '''unique (acl0)'}''')],
199 err_words_ok
=['acl0', 'ValidationError',
203 class 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'])
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'},
214 'DoesNotExist: acl_group matching '
215 'query does not exist.')],
216 err_words_ok
=['acl0', 'DoesNotExist'])
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',
222 'acl0', 'acl1', '--alist', alist
.name
],
223 rpcs
=[('delete_acl_group',
235 out_words_ok
=['acl0', 'acl1', 'acl2', 'Deleted'])
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',
242 'acl0', 'acl1', '--alist', alist
.name
],
243 rpcs
=[('delete_acl_group',
250 'DoesNotExist: acl_group '
251 'matching query does not exist.'),
256 out_words_ok
=['acl0', 'acl2', 'Deleted'],
257 err_words_ok
=['acl1', 'delete_acl_group',
258 'DoesNotExist', 'acl_group',
263 class acl_add_unittest(cli_mock
.cli_unittest
):
264 def test_acl_add_parse_no_option(self
):
268 sys
.exit
.expect_call(1).and_raises(cli_mock
.ExitException
)
269 self
.assertRaises(cli_mock
.ExitException
, acls
.parse
)
271 self
.god
.check_playback()
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',
279 'users': ['user0', 'user1']},
282 ('acl_group_add_hosts',
287 out_words_ok
=['acl0', 'user0',
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',
296 'users': ['user0', 'user1']},
298 'DoesNotExist: The following Users do not exist: '
300 err_words_ok
=['user0', 'user1'])
303 def test_acl_add_bad_users_hosts(self
):
304 self
.run_cmd(argv
=['atest', 'acl', 'add', 'acl0',
305 '-u', 'user0,user1', '-m', 'host0,host1'],
306 rpcs
=[('acl_group_add_users',
308 'users': ['user0', 'user1']},
310 'DoesNotExist: The following Users do not exist: '
312 ('acl_group_add_users',
317 ('acl_group_add_hosts',
319 'hosts': ['host1', 'host0']},
321 'DoesNotExist: The following Hosts do not exist: '
323 ('acl_group_add_hosts',
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'])
334 class 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',
340 'users': ['user0', 'user1']},
343 out_words_ok
=['acl0', 'user0', 'user1'],
344 out_words_no
=['host'])
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',
352 'hosts': ['host1', 'host0']},
355 out_words_ok
=['acl0', 'host0', 'host1'],
356 out_words_no
=['user'])
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',
365 'users': ['user0', 'user1']},
368 ('acl_group_remove_hosts',
370 'hosts': ['host1', 'host0']},
373 out_words_ok
=['acl0', 'user0', 'user1',
377 if __name__
== '__main__':