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