842
842
class Test_check_option_syntax(unittest.TestCase):
844
self.parser = argparse.ArgumentParser()
845
add_command_line_options(self.parser)
847
def test_actions_requires_client_or_all(self):
848
for action, value in self.actions.items():
849
options = self.parser.parse_args()
850
setattr(options, action, value)
851
with self.assertParseError():
852
self.check_option_syntax(options)
843
854
# This mostly corresponds to the definition from has_actions() in
844
855
# check_option_syntax()
897
904
def check_option_syntax(self, options):
898
905
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
907
def test_actions_conflicts_with_verbose(self):
908
908
for action, value in self.actions.items():
909
909
options = self.parser.parse_args()
971
971
self.check_option_syntax(options)
974
class Test_command_from_options(unittest.TestCase):
974
class Test_commands_from_options(unittest.TestCase):
976
976
self.parser = argparse.ArgumentParser()
977
977
add_command_line_options(self.parser)
979
def test_is_enabled(self):
980
self.assert_command_from_args(["--is-enabled", "foo"],
978
983
def assert_command_from_args(self, args, command_cls,
980
985
"""Assert that parsing ARGS should result in an instance of
987
992
self.assertIsInstance(command, command_cls)
988
993
for key, value in cmd_attrs.items():
989
994
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,
996
def test_is_enabled_short(self):
997
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
999
def test_approve(self):
1000
self.assert_command_from_args(["--approve", "foo"],
1003
def test_approve_short(self):
1004
self.assert_command_from_args(["-A", "foo"], ApproveCmd)
1006
def test_deny(self):
1007
self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1009
def test_deny_short(self):
1010
self.assert_command_from_args(["-D", "foo"], DenyCmd)
1012
def test_remove(self):
1013
self.assert_command_from_args(["--remove", "foo"],
1016
def test_deny_before_remove(self):
1017
options = self.parser.parse_args(["--deny", "--remove",
1019
check_option_syntax(self.parser, options)
1020
commands = commands_from_options(options)
1021
self.assertEqual(len(commands), 2)
1022
self.assertIsInstance(commands[0], DenyCmd)
1023
self.assertIsInstance(commands[1], RemoveCmd)
1025
def test_deny_before_remove_reversed(self):
1026
options = self.parser.parse_args(["--remove", "--deny",
1028
check_option_syntax(self.parser, options)
1029
commands = commands_from_options(options)
1030
self.assertEqual(len(commands), 2)
1031
self.assertIsInstance(commands[0], DenyCmd)
1032
self.assertIsInstance(commands[1], RemoveCmd)
1034
def test_remove_short(self):
1035
self.assert_command_from_args(["-r", "foo"], RemoveCmd)
1037
def test_dump_json(self):
1038
self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1002
1040
def test_enable(self):
1003
1041
self.assert_command_from_args(["--enable", "foo"], EnableCmd)
1027
1065
self.assert_command_from_args(["--stop-checker", "foo"],
1028
1066
StopCheckerCmd)
1030
def test_remove(self):
1031
self.assert_command_from_args(["--remove", "foo"],
1068
def test_approve_by_default(self):
1069
self.assert_command_from_args(["--approve-by-default", "foo"],
1070
ApproveByDefaultCmd)
1034
def test_remove_short(self):
1035
self.assert_command_from_args(["-r", "foo"], RemoveCmd)
1072
def test_deny_by_default(self):
1073
self.assert_command_from_args(["--deny-by-default", "foo"],
1037
1076
def test_checker(self):
1038
1077
self.assert_command_from_args(["--checker", ":", "foo"],
1046
1085
self.assert_command_from_args(["-c", ":", "foo"],
1047
1086
SetCheckerCmd, value_to_set=":")
1088
def test_host(self):
1089
self.assert_command_from_args(["--host", "foo.example.org",
1091
value_to_set="foo.example.org")
1093
def test_host_short(self):
1094
self.assert_command_from_args(["-H", "foo.example.org",
1096
value_to_set="foo.example.org")
1098
def test_secret_devnull(self):
1099
self.assert_command_from_args(["--secret", os.path.devnull,
1100
"foo"], SetSecretCmd,
1103
def test_secret_tempfile(self):
1104
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1105
value = b"secret\0xyzzy\nbar"
1108
self.assert_command_from_args(["--secret", f.name,
1109
"foo"], SetSecretCmd,
1112
def test_secret_devnull_short(self):
1113
self.assert_command_from_args(["-s", os.path.devnull, "foo"],
1114
SetSecretCmd, value_to_set=b"")
1116
def test_secret_tempfile_short(self):
1117
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1118
value = b"secret\0xyzzy\nbar"
1121
self.assert_command_from_args(["-s", f.name, "foo"],
1049
1125
def test_timeout(self):
1050
1126
self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1090
1158
"foo"], SetApprovalDurationCmd,
1091
1159
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)
1161
def test_print_table(self):
1162
self.assert_command_from_args([], PrintTableCmd,
1165
def test_print_table_verbose(self):
1166
self.assert_command_from_args(["--verbose"], PrintTableCmd,
1169
def test_print_table_verbose_short(self):
1170
self.assert_command_from_args(["-v"], PrintTableCmd,
1172
1174
class TestCmd(unittest.TestCase):