495
505
MillisecondsValueArgumentMixIn):
496
506
property = "ApprovalDuration"
498
def has_actions(options):
499
return any((options.enable,
501
options.bump_timeout,
502
options.start_checker,
503
options.stop_checker,
506
options.checker is not None,
507
options.timeout is not None,
508
options.extended_timeout is not None,
509
options.interval is not None,
510
options.approved_by_default is not None,
511
options.approval_delay is not None,
512
options.approval_duration is not None,
513
options.host is not None,
514
options.secret is not None,
518
508
def add_command_line_options(parser):
519
509
parser.add_argument("--version", action="version",
520
510
version="%(prog)s {}".format(version),
619
609
SetExtendedTimeoutCmd(options.extended_timeout))
621
611
if options.interval is not None:
622
command.append(SetIntervalCmd(options.interval))
612
commands.append(SetIntervalCmd(options.interval))
624
614
if options.approved_by_default is not None:
625
615
if options.approved_by_default:
626
command.append(ApproveByDefaultCmd())
616
commands.append(ApproveByDefaultCmd())
628
command.append(DenyByDefaultCmd())
618
commands.append(DenyByDefaultCmd())
630
620
if options.approval_delay is not None:
631
command.append(SetApprovalDelayCmd(options.approval_delay))
621
commands.append(SetApprovalDelayCmd(options.approval_delay))
633
623
if options.approval_duration is not None:
635
625
SetApprovalDurationCmd(options.approval_duration))
637
627
if options.host is not None:
638
command.append(SetHostCmd(options.host))
628
commands.append(SetHostCmd(options.host))
640
630
if options.secret is not None:
641
command.append(SetSecretCmd(options.secret))
631
commands.append(SetSecretCmd(options.secret))
643
633
if options.approve:
644
634
commands.append(ApproveCmd())
1115
1127
values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1116
1128
values_to_get = [0, 300000, 1000, 120000, 31449600000]
1118
class TestOptions(unittest.TestCase):
1130
class Test_command_from_options(unittest.TestCase):
1119
1131
def setUp(self):
1120
1132
self.parser = argparse.ArgumentParser()
1121
1133
add_command_line_options(self.parser)
1122
def commands_from_args(self, args):
1123
self.options = self.parser.parse_args(args)
1124
return commands_from_options(self.options)
1125
def test_default_is_show_table(self):
1126
commands = self.commands_from_args([])
1127
self.assertEqual(len(commands), 1)
1128
command = commands[0]
1129
self.assertIsInstance(command, PrintTableCmd)
1130
self.assertEqual(command.verbose, False)
1131
def test_show_table_verbose(self):
1132
commands = self.commands_from_args(["--verbose"])
1133
self.assertEqual(len(commands), 1)
1134
command = commands[0]
1135
self.assertIsInstance(command, PrintTableCmd)
1136
self.assertEqual(command.verbose, True)
1134
def assert_command_from_args(self, args, command_cls, **cmd_attrs):
1135
"""Assert that parsing ARGS should result in an instance of
1136
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
1137
options = self.parser.parse_args(args)
1138
commands = commands_from_options(options)
1139
self.assertEqual(len(commands), 1)
1140
command = commands[0]
1141
self.assertIsInstance(command, command_cls)
1142
for key, value in cmd_attrs.items():
1143
self.assertEqual(getattr(command, key), value)
1144
def test_print_table(self):
1145
self.assert_command_from_args([], PrintTableCmd,
1148
def test_print_table_verbose(self):
1149
self.assert_command_from_args(["--verbose"], PrintTableCmd,
1137
1152
def test_enable(self):
1138
commands = self.commands_from_args(["--enable", "foo"])
1139
self.assertEqual(len(commands), 1)
1140
command = commands[0]
1141
self.assertIsInstance(command, EnableCmd)
1142
self.assertEqual(self.options.client, ["foo"])
1153
self.assert_command_from_args(["--enable", "foo"], EnableCmd)
1143
1155
def test_disable(self):
1144
commands = self.commands_from_args(["--disable", "foo"])
1145
self.assertEqual(len(commands), 1)
1146
command = commands[0]
1147
self.assertIsInstance(command, DisableCmd)
1148
self.assertEqual(self.options.client, ["foo"])
1156
self.assert_command_from_args(["--disable", "foo"],
1159
def test_bump_timeout(self):
1160
self.assert_command_from_args(["--bump-timeout", "foo"],
1163
def test_start_checker(self):
1164
self.assert_command_from_args(["--start-checker", "foo"],
1167
def test_stop_checker(self):
1168
self.assert_command_from_args(["--stop-checker", "foo"],
1171
def test_remove(self):
1172
self.assert_command_from_args(["--remove", "foo"],
1175
def test_checker(self):
1176
self.assert_command_from_args(["--checker", ":", "foo"],
1177
SetCheckerCmd, value_to_set=":")
1179
def test_checker_empty(self):
1180
self.assert_command_from_args(["--checker", "", "foo"],
1181
SetCheckerCmd, value_to_set="")
1183
def test_timeout(self):
1184
self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1186
value_to_set=300000)
1188
def test_extended_timeout(self):
1189
self.assert_command_from_args(["--extended-timeout", "PT15M",
1191
SetExtendedTimeoutCmd,
1192
value_to_set=900000)
1194
def test_interval(self):
1195
self.assert_command_from_args(["--interval", "PT2M", "foo"],
1197
value_to_set=120000)
1199
def test_approve_by_default(self):
1200
self.assert_command_from_args(["--approve-by-default", "foo"],
1201
ApproveByDefaultCmd)
1203
def test_deny_by_default(self):
1204
self.assert_command_from_args(["--deny-by-default", "foo"],
1207
def test_approval_delay(self):
1208
self.assert_command_from_args(["--approval-delay", "PT30S",
1209
"foo"], SetApprovalDelayCmd,
1212
def test_approval_duration(self):
1213
self.assert_command_from_args(["--approval-duration", "PT1S",
1214
"foo"], SetApprovalDurationCmd,
1217
def test_host(self):
1218
self.assert_command_from_args(["--host", "foo.example.org",
1220
value_to_set="foo.example.org")
1222
def test_secret_devnull(self):
1223
self.assert_command_from_args(["--secret", os.path.devnull,
1224
"foo"], SetSecretCmd,
1227
def test_secret_tempfile(self):
1228
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1229
value = b"secret\0xyzzy\nbar"
1232
self.assert_command_from_args(["--secret", f.name,
1233
"foo"], SetSecretCmd,
1236
def test_approve(self):
1237
self.assert_command_from_args(["--approve", "foo"],
1240
def test_deny(self):
1241
self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1243
def test_dump_json(self):
1244
self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1246
def test_is_enabled(self):
1247
self.assert_command_from_args(["--is-enabled", "foo"],