1093
1083
class TestSetSecretCmd(TestValueArgumentPropertyCmd):
1094
1084
command = SetSecretCmd
1095
1085
property = "Secret"
1096
values_to_set = [open("/dev/null", "rb"),
1097
io.BytesIO(b"secret\0xyzzy\nbar")]
1098
values_to_get = [b"", b"secret\0xyzzy\nbar"]
1086
values_to_set = [b"", b"secret"]
1100
1088
class TestSetTimeoutCmd(TestValueArgumentPropertyCmd):
1101
1089
command = SetTimeoutCmd
1102
1090
property = "Timeout"
1103
values_to_set = [datetime.timedelta(),
1104
datetime.timedelta(minutes=5),
1105
datetime.timedelta(seconds=1),
1106
datetime.timedelta(weeks=1),
1107
datetime.timedelta(weeks=52)]
1108
values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1091
values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1092
values_to_get = [0, 300000, 1000, 120000, 31449600000]
1110
1094
class TestSetExtendedTimeoutCmd(TestValueArgumentPropertyCmd):
1111
1095
command = SetExtendedTimeoutCmd
1112
1096
property = "ExtendedTimeout"
1113
values_to_set = [datetime.timedelta(),
1114
datetime.timedelta(minutes=5),
1115
datetime.timedelta(seconds=1),
1116
datetime.timedelta(weeks=1),
1117
datetime.timedelta(weeks=52)]
1118
values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1097
values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1098
values_to_get = [0, 300000, 1000, 120000, 31449600000]
1120
1100
class TestSetIntervalCmd(TestValueArgumentPropertyCmd):
1121
1101
command = SetIntervalCmd
1122
1102
property = "Interval"
1123
values_to_set = [datetime.timedelta(),
1124
datetime.timedelta(minutes=5),
1125
datetime.timedelta(seconds=1),
1126
datetime.timedelta(weeks=1),
1127
datetime.timedelta(weeks=52)]
1128
values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1103
values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1104
values_to_get = [0, 300000, 1000, 120000, 31449600000]
1130
1106
class TestSetApprovalDelayCmd(TestValueArgumentPropertyCmd):
1131
1107
command = SetApprovalDelayCmd
1132
1108
property = "ApprovalDelay"
1133
values_to_set = [datetime.timedelta(),
1134
datetime.timedelta(minutes=5),
1135
datetime.timedelta(seconds=1),
1136
datetime.timedelta(weeks=1),
1137
datetime.timedelta(weeks=52)]
1138
values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1109
values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1110
values_to_get = [0, 300000, 1000, 120000, 31449600000]
1140
1112
class TestSetApprovalDurationCmd(TestValueArgumentPropertyCmd):
1141
1113
command = SetApprovalDurationCmd
1142
1114
property = "ApprovalDuration"
1143
values_to_set = [datetime.timedelta(),
1144
datetime.timedelta(minutes=5),
1145
datetime.timedelta(seconds=1),
1146
datetime.timedelta(weeks=1),
1147
datetime.timedelta(weeks=52)]
1148
values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1115
values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1116
values_to_get = [0, 300000, 1000, 120000, 31449600000]
1150
class Test_command_from_options(unittest.TestCase):
1118
class TestOptions(unittest.TestCase):
1151
1119
def setUp(self):
1152
1120
self.parser = argparse.ArgumentParser()
1153
1121
add_command_line_options(self.parser)
1154
def assert_command_from_args(self, args, command_cls, **cmd_attrs):
1155
"""Assert that parsing ARGS should result in an instance of
1156
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
1157
options = self.parser.parse_args(args)
1158
commands = commands_from_options(options)
1159
self.assertEqual(len(commands), 1)
1160
command = commands[0]
1161
self.assertIsInstance(command, command_cls)
1162
for key, value in cmd_attrs.items():
1163
self.assertEqual(getattr(command, key), value)
1164
def test_print_table(self):
1165
self.assert_command_from_args([], PrintTableCmd,
1168
def test_print_table_verbose(self):
1169
self.assert_command_from_args(["--verbose"], PrintTableCmd,
1172
def test_print_table_verbose_short(self):
1173
self.assert_command_from_args(["-v"], PrintTableCmd,
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)
1176
1137
def test_enable(self):
1177
self.assert_command_from_args(["--enable", "foo"], EnableCmd)
1179
def test_enable_short(self):
1180
self.assert_command_from_args(["-e", "foo"], EnableCmd)
1182
def test_disable(self):
1183
self.assert_command_from_args(["--disable", "foo"],
1186
def test_disable_short(self):
1187
self.assert_command_from_args(["-d", "foo"], DisableCmd)
1189
def test_bump_timeout(self):
1190
self.assert_command_from_args(["--bump-timeout", "foo"],
1193
def test_bump_timeout_short(self):
1194
self.assert_command_from_args(["-b", "foo"], BumpTimeoutCmd)
1196
def test_start_checker(self):
1197
self.assert_command_from_args(["--start-checker", "foo"],
1200
def test_stop_checker(self):
1201
self.assert_command_from_args(["--stop-checker", "foo"],
1204
def test_remove(self):
1205
self.assert_command_from_args(["--remove", "foo"],
1208
def test_remove_short(self):
1209
self.assert_command_from_args(["-r", "foo"], RemoveCmd)
1211
def test_checker(self):
1212
self.assert_command_from_args(["--checker", ":", "foo"],
1213
SetCheckerCmd, value_to_set=":")
1215
def test_checker_empty(self):
1216
self.assert_command_from_args(["--checker", "", "foo"],
1217
SetCheckerCmd, value_to_set="")
1219
def test_checker_short(self):
1220
self.assert_command_from_args(["-c", ":", "foo"],
1221
SetCheckerCmd, value_to_set=":")
1223
def test_timeout(self):
1224
self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1226
value_to_set=300000)
1228
def test_timeout_short(self):
1229
self.assert_command_from_args(["-t", "PT5M", "foo"],
1231
value_to_set=300000)
1233
def test_extended_timeout(self):
1234
self.assert_command_from_args(["--extended-timeout", "PT15M",
1236
SetExtendedTimeoutCmd,
1237
value_to_set=900000)
1239
def test_interval(self):
1240
self.assert_command_from_args(["--interval", "PT2M", "foo"],
1242
value_to_set=120000)
1244
def test_interval_short(self):
1245
self.assert_command_from_args(["-i", "PT2M", "foo"],
1247
value_to_set=120000)
1249
def test_approve_by_default(self):
1250
self.assert_command_from_args(["--approve-by-default", "foo"],
1251
ApproveByDefaultCmd)
1253
def test_deny_by_default(self):
1254
self.assert_command_from_args(["--deny-by-default", "foo"],
1257
def test_approval_delay(self):
1258
self.assert_command_from_args(["--approval-delay", "PT30S",
1259
"foo"], SetApprovalDelayCmd,
1262
def test_approval_duration(self):
1263
self.assert_command_from_args(["--approval-duration", "PT1S",
1264
"foo"], SetApprovalDurationCmd,
1267
def test_host(self):
1268
self.assert_command_from_args(["--host", "foo.example.org",
1270
value_to_set="foo.example.org")
1272
def test_host_short(self):
1273
self.assert_command_from_args(["-H", "foo.example.org",
1275
value_to_set="foo.example.org")
1277
def test_secret_devnull(self):
1278
self.assert_command_from_args(["--secret", os.path.devnull,
1279
"foo"], SetSecretCmd,
1282
def test_secret_tempfile(self):
1283
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1284
value = b"secret\0xyzzy\nbar"
1287
self.assert_command_from_args(["--secret", f.name,
1288
"foo"], SetSecretCmd,
1291
def test_secret_devnull_short(self):
1292
self.assert_command_from_args(["-s", os.path.devnull, "foo"],
1293
SetSecretCmd, value_to_set=b"")
1295
def test_secret_tempfile_short(self):
1296
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1297
value = b"secret\0xyzzy\nbar"
1300
self.assert_command_from_args(["-s", f.name, "foo"],
1304
def test_approve(self):
1305
self.assert_command_from_args(["--approve", "foo"],
1308
def test_approve_short(self):
1309
self.assert_command_from_args(["-A", "foo"], ApproveCmd)
1311
def test_deny(self):
1312
self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1314
def test_deny_short(self):
1315
self.assert_command_from_args(["-D", "foo"], DenyCmd)
1317
def test_dump_json(self):
1318
self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1320
def test_is_enabled(self):
1321
self.assert_command_from_args(["--is-enabled", "foo"],
1324
def test_is_enabled_short(self):
1325
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
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"])