897
912
def check_option_syntax(self, options):
898
913
check_option_syntax(self.parser, options)
900
def test_actions_requires_client_or_all(self):
901
for action, value in self.actions.items():
902
options = self.parser.parse_args()
903
setattr(options, action, value)
904
with self.assertParseError():
905
self.check_option_syntax(options)
907
915
def test_actions_conflicts_with_verbose(self):
908
916
for action, value in self.actions.items():
909
917
options = self.parser.parse_args()
987
1000
self.assertIsInstance(command, command_cls)
988
1001
for key, value in cmd_attrs.items():
989
1002
self.assertEqual(getattr(command, key), value)
990
def test_print_table(self):
991
self.assert_command_from_args([], PrintTableCmd,
994
def test_print_table_verbose(self):
995
self.assert_command_from_args(["--verbose"], PrintTableCmd,
998
def test_print_table_verbose_short(self):
999
self.assert_command_from_args(["-v"], PrintTableCmd,
1004
def test_is_enabled_short(self):
1005
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1007
def test_approve(self):
1008
self.assert_command_from_args(["--approve", "foo"],
1011
def test_approve_short(self):
1012
self.assert_command_from_args(["-A", "foo"], ApproveCmd)
1014
def test_deny(self):
1015
self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1017
def test_deny_short(self):
1018
self.assert_command_from_args(["-D", "foo"], DenyCmd)
1020
def test_remove(self):
1021
self.assert_command_from_args(["--remove", "foo"],
1024
def test_deny_before_remove(self):
1025
options = self.parser.parse_args(["--deny", "--remove",
1027
check_option_syntax(self.parser, options)
1028
commands = commands_from_options(options)
1029
self.assertEqual(len(commands), 2)
1030
self.assertIsInstance(commands[0], DenyCmd)
1031
self.assertIsInstance(commands[1], RemoveCmd)
1033
def test_deny_before_remove_reversed(self):
1034
options = self.parser.parse_args(["--remove", "--deny",
1036
check_option_syntax(self.parser, options)
1037
commands = commands_from_options(options)
1038
self.assertEqual(len(commands), 2)
1039
self.assertIsInstance(commands[0], DenyCmd)
1040
self.assertIsInstance(commands[1], RemoveCmd)
1042
def test_remove_short(self):
1043
self.assert_command_from_args(["-r", "foo"], RemoveCmd)
1045
def test_dump_json(self):
1046
self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1002
1048
def test_enable(self):
1003
1049
self.assert_command_from_args(["--enable", "foo"], EnableCmd)
1027
1073
self.assert_command_from_args(["--stop-checker", "foo"],
1028
1074
StopCheckerCmd)
1030
def test_remove(self):
1031
self.assert_command_from_args(["--remove", "foo"],
1076
def test_approve_by_default(self):
1077
self.assert_command_from_args(["--approve-by-default", "foo"],
1078
ApproveByDefaultCmd)
1034
def test_remove_short(self):
1035
self.assert_command_from_args(["-r", "foo"], RemoveCmd)
1080
def test_deny_by_default(self):
1081
self.assert_command_from_args(["--deny-by-default", "foo"],
1037
1084
def test_checker(self):
1038
1085
self.assert_command_from_args(["--checker", ":", "foo"],
1046
1093
self.assert_command_from_args(["-c", ":", "foo"],
1047
1094
SetCheckerCmd, value_to_set=":")
1096
def test_host(self):
1097
self.assert_command_from_args(["--host", "foo.example.org",
1099
value_to_set="foo.example.org")
1101
def test_host_short(self):
1102
self.assert_command_from_args(["-H", "foo.example.org",
1104
value_to_set="foo.example.org")
1106
def test_secret_devnull(self):
1107
self.assert_command_from_args(["--secret", os.path.devnull,
1108
"foo"], SetSecretCmd,
1111
def test_secret_tempfile(self):
1112
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1113
value = b"secret\0xyzzy\nbar"
1116
self.assert_command_from_args(["--secret", f.name,
1117
"foo"], SetSecretCmd,
1120
def test_secret_devnull_short(self):
1121
self.assert_command_from_args(["-s", os.path.devnull, "foo"],
1122
SetSecretCmd, value_to_set=b"")
1124
def test_secret_tempfile_short(self):
1125
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1126
value = b"secret\0xyzzy\nbar"
1129
self.assert_command_from_args(["-s", f.name, "foo"],
1049
1133
def test_timeout(self):
1050
1134
self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1090
1166
"foo"], SetApprovalDurationCmd,
1091
1167
value_to_set=1000)
1093
def test_host(self):
1094
self.assert_command_from_args(["--host", "foo.example.org",
1096
value_to_set="foo.example.org")
1098
def test_host_short(self):
1099
self.assert_command_from_args(["-H", "foo.example.org",
1101
value_to_set="foo.example.org")
1103
def test_secret_devnull(self):
1104
self.assert_command_from_args(["--secret", os.path.devnull,
1105
"foo"], SetSecretCmd,
1108
def test_secret_tempfile(self):
1109
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1110
value = b"secret\0xyzzy\nbar"
1113
self.assert_command_from_args(["--secret", f.name,
1114
"foo"], SetSecretCmd,
1117
def test_secret_devnull_short(self):
1118
self.assert_command_from_args(["-s", os.path.devnull, "foo"],
1119
SetSecretCmd, value_to_set=b"")
1121
def test_secret_tempfile_short(self):
1122
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1123
value = b"secret\0xyzzy\nbar"
1126
self.assert_command_from_args(["-s", f.name, "foo"],
1130
def test_approve(self):
1131
self.assert_command_from_args(["--approve", "foo"],
1134
def test_approve_short(self):
1135
self.assert_command_from_args(["-A", "foo"], ApproveCmd)
1137
def test_deny(self):
1138
self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1140
def test_deny_short(self):
1141
self.assert_command_from_args(["-D", "foo"], DenyCmd)
1143
def test_dump_json(self):
1144
self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1146
def test_is_enabled(self):
1147
self.assert_command_from_args(["--is-enabled", "foo"],
1150
def test_is_enabled_short(self):
1151
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1153
def test_deny_before_remove(self):
1154
options = self.parser.parse_args(["--deny", "--remove",
1156
check_option_syntax(self.parser, options)
1157
commands = commands_from_options(options)
1158
self.assertEqual(len(commands), 2)
1159
self.assertIsInstance(commands[0], DenyCmd)
1160
self.assertIsInstance(commands[1], RemoveCmd)
1162
def test_deny_before_remove_reversed(self):
1163
options = self.parser.parse_args(["--remove", "--deny",
1165
check_option_syntax(self.parser, options)
1166
commands = commands_from_options(options)
1167
self.assertEqual(len(commands), 2)
1168
self.assertIsInstance(commands[0], DenyCmd)
1169
self.assertIsInstance(commands[1], RemoveCmd)
1169
def test_print_table(self):
1170
self.assert_command_from_args([], PrintTableCmd,
1173
def test_print_table_verbose(self):
1174
self.assert_command_from_args(["--verbose"], PrintTableCmd,
1177
def test_print_table_verbose_short(self):
1178
self.assert_command_from_args(["-v"], PrintTableCmd,
1172
1182
class TestCmd(unittest.TestCase):
1173
1183
"""Abstract class for tests of command classes"""
1174
1185
def setUp(self):
1175
1186
testcase = self
1176
1187
class MockClient(object):
1372
1389
return super(TestDumpJSONCmd, self).setUp()
1373
1391
def test_normal(self):
1374
json_data = json.loads(DumpJSONCmd().output(self.clients))
1392
output = DumpJSONCmd().output(self.clients.values())
1393
json_data = json.loads(output)
1375
1394
self.assertDictEqual(json_data, self.expected_json)
1376
1396
def test_one_client(self):
1377
clients = self.one_client
1378
json_data = json.loads(DumpJSONCmd().output(clients))
1397
output = DumpJSONCmd().output(self.one_client.values())
1398
json_data = json.loads(output)
1379
1399
expected_json = {"foo": self.expected_json["foo"]}
1380
1400
self.assertDictEqual(json_data, expected_json)
1509
1526
for clientpath in self.clients:
1510
1527
client = self.bus.get_object(dbus_busname, clientpath)
1511
1528
old_value = client.attributes[self.propname]
1512
self.assertNotIsInstance(old_value, Unique)
1513
client.attributes[self.propname] = Unique()
1529
self.assertNotIsInstance(old_value, self.Unique)
1530
client.attributes[self.propname] = self.Unique()
1514
1531
self.run_command(value_to_set, self.clients)
1515
1532
for clientpath in self.clients:
1516
1533
client = self.bus.get_object(dbus_busname, clientpath)
1517
1534
value = client.attributes[self.propname]
1518
self.assertNotIsInstance(value, Unique)
1535
self.assertNotIsInstance(value, self.Unique)
1519
1536
self.assertEqual(value, value_to_get)
1538
class Unique(object):
1539
"""Class for objects which exist only to be unique objects,
1540
since unittest.mock.sentinel only exists in Python 3.3"""
1520
1542
def run_command(self, value, clients):
1521
1543
self.command().run(clients, self.bus)