/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-06 22:21:51 UTC
  • Revision ID: teddy@recompile.se-20190306222151-cp303jcuqhast6nu
mandos-ctl: Refactor tests and add more tests

* mandos-ctl (TestOptions): Rename to "Test_command_from_options".
  (Test_command_from_options.test_default_is_show_table): Rename to
                         "Test_command_from_options.test_print_table".
  (Test_command_from_options.test_show_table_verbose): Rename to
                 "Test_command_from_options.test_print_table_verbose".
  (Test_command_from_options.test_bump_timeout): New.
  (Test_command_from_options.test_start_checker): - '' -
  (Test_command_from_options.test_stop_checker): - '' -
  (Test_command_from_options.test_remove): - '' -
  (Test_command_from_options.test_checker): - '' -
  (Test_command_from_options.test_timeout): - '' -
  (Test_command_from_options.test_extended_timeout): - '' -
  (Test_command_from_options.test_interval): - '' -
  (Test_command_from_options.test_approve_by_default): - '' -
  (Test_command_from_options.test_deny_by_default): - '' -
  (Test_command_from_options.test_approval_delay): - '' -
  (Test_command_from_options.test_approval_duration): - '' -
  (Test_command_from_options.test_host): - '' -
  (Test_command_from_options.test_secret_devnull): - '' -
  (Test_command_from_options.test_secret_tempfile): - '' -
  (Test_command_from_options.test_approve): - '' -
  (Test_command_from_options.test_deny): - '' -
  (Test_command_from_options.test_dump_json): - '' -
  (Test_command_from_options.test_is_enabled): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
import unittest
44
44
import logging
45
45
import io
 
46
import tempfile
46
47
 
47
48
import dbus
48
49
 
1126
1127
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1127
1128
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
1128
1129
 
1129
 
class TestOptions(unittest.TestCase):
 
1130
class Test_command_from_options(unittest.TestCase):
1130
1131
    def setUp(self):
1131
1132
        self.parser = argparse.ArgumentParser()
1132
1133
        add_command_line_options(self.parser)
1140
1141
        self.assertIsInstance(command, command_cls)
1141
1142
        for key, value in cmd_attrs.items():
1142
1143
            self.assertEqual(getattr(command, key), value)
1143
 
    def test_default_is_show_table(self):
 
1144
    def test_print_table(self):
1144
1145
        self.assert_command_from_args([], PrintTableCmd,
1145
1146
                                      verbose=False)
1146
 
    def test_show_table_verbose(self):
 
1147
 
 
1148
    def test_print_table_verbose(self):
1147
1149
        self.assert_command_from_args(["--verbose"], PrintTableCmd,
1148
1150
                                      verbose=True)
 
1151
 
1149
1152
    def test_enable(self):
1150
1153
        self.assert_command_from_args(["--enable", "foo"], EnableCmd)
 
1154
 
1151
1155
    def test_disable(self):
1152
1156
        self.assert_command_from_args(["--disable", "foo"],
1153
1157
                                      DisableCmd)
1154
1158
 
 
1159
    def test_bump_timeout(self):
 
1160
        self.assert_command_from_args(["--bump-timeout", "foo"],
 
1161
                                      BumpTimeoutCmd)
 
1162
 
 
1163
    def test_start_checker(self):
 
1164
        self.assert_command_from_args(["--start-checker", "foo"],
 
1165
                                      StartCheckerCmd)
 
1166
 
 
1167
    def test_stop_checker(self):
 
1168
        self.assert_command_from_args(["--stop-checker", "foo"],
 
1169
                                      StopCheckerCmd)
 
1170
 
 
1171
    def test_remove(self):
 
1172
        self.assert_command_from_args(["--remove", "foo"],
 
1173
                                      RemoveCmd)
 
1174
 
 
1175
    def test_checker(self):
 
1176
        self.assert_command_from_args(["--checker", ":", "foo"],
 
1177
                                      SetCheckerCmd, value_to_set=":")
 
1178
 
 
1179
    def test_timeout(self):
 
1180
        self.assert_command_from_args(["--timeout", "PT5M", "foo"],
 
1181
                                      SetTimeoutCmd,
 
1182
                                      value_to_set=300000)
 
1183
 
 
1184
    def test_extended_timeout(self):
 
1185
        self.assert_command_from_args(["--extended-timeout", "PT15M",
 
1186
                                       "foo"],
 
1187
                                      SetExtendedTimeoutCmd,
 
1188
                                      value_to_set=900000)
 
1189
 
 
1190
    def test_interval(self):
 
1191
        self.assert_command_from_args(["--interval", "PT2M", "foo"],
 
1192
                                      SetIntervalCmd,
 
1193
                                      value_to_set=120000)
 
1194
 
 
1195
    def test_approve_by_default(self):
 
1196
        self.assert_command_from_args(["--approve-by-default", "foo"],
 
1197
                                      ApproveByDefaultCmd)
 
1198
 
 
1199
    def test_deny_by_default(self):
 
1200
        self.assert_command_from_args(["--deny-by-default", "foo"],
 
1201
                                      DenyByDefaultCmd)
 
1202
 
 
1203
    def test_approval_delay(self):
 
1204
        self.assert_command_from_args(["--approval-delay", "PT30S",
 
1205
                                       "foo"], SetApprovalDelayCmd,
 
1206
                                      value_to_set=30000)
 
1207
 
 
1208
    def test_approval_duration(self):
 
1209
        self.assert_command_from_args(["--approval-duration", "PT1S",
 
1210
                                       "foo"], SetApprovalDurationCmd,
 
1211
                                      value_to_set=1000)
 
1212
 
 
1213
    def test_host(self):
 
1214
        self.assert_command_from_args(["--host", "foo.example.org",
 
1215
                                       "foo"], SetHostCmd,
 
1216
                                      value_to_set="foo.example.org")
 
1217
 
 
1218
    def test_secret_devnull(self):
 
1219
        self.assert_command_from_args(["--secret", os.path.devnull,
 
1220
                                       "foo"], SetSecretCmd,
 
1221
                                      value_to_set=b"")
 
1222
 
 
1223
    def test_secret_tempfile(self):
 
1224
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
 
1225
            value = b"secret\0xyzzy\nbar"
 
1226
            f.write(value)
 
1227
            f.seek(0)
 
1228
            self.assert_command_from_args(["--secret", f.name,
 
1229
                                           "foo"], SetSecretCmd,
 
1230
                                          value_to_set=value)
 
1231
 
 
1232
    def test_approve(self):
 
1233
        self.assert_command_from_args(["--approve", "foo"],
 
1234
                                      ApproveCmd)
 
1235
 
 
1236
    def test_deny(self):
 
1237
        self.assert_command_from_args(["--deny", "foo"], DenyCmd)
 
1238
 
 
1239
    def test_dump_json(self):
 
1240
        self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
 
1241
 
 
1242
    def test_is_enabled(self):
 
1243
        self.assert_command_from_args(["--is-enabled", "foo"],
 
1244
                                      IsEnabledCmd)
 
1245
 
1155
1246
 
1156
1247
 
1157
1248
def should_only_run_tests():