/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-07 20:28:17 UTC
  • Revision ID: teddy@recompile.se-20190307202817-avhha20s3pl14j6y
mandos-ctl: Refactor; move parsing of intervals into argument parsing

* mandos-ctl (MillisecondsValueArgumentMixIn.value_to_set): Assume
  that an incoming value is datetime.timedelta(), not a string.
  (add_command_line_options): Add "type=string_to_delta" to --timeout,
  --extended-timeout, --interval, --approval-delay, and
  --approval-duration.
  (TestSetTimeoutCmd, TestSetExtendedTimeoutCmd, TestSetIntervalCmd,
  TestSetApprovalDelayCmd, TestSetApprovalDurationCmd): Change
  values_to_set to be datetime.timedelta() values, and change to more
  appropriate values to test.  Also adjust values_to_get accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
316
316
    @value_to_set.setter
317
317
    def value_to_set(self, value):
318
318
        """When setting, convert value to a datetime.timedelta"""
319
 
        self._vts = string_to_delta(value).total_seconds() * 1000
 
319
        self._vts = int(round(value.total_seconds() * 1000))
320
320
 
321
321
# Actual (non-abstract) command classes
322
322
 
536
536
                        help="Remove client")
537
537
    parser.add_argument("-c", "--checker",
538
538
                        help="Set checker command for client")
539
 
    parser.add_argument("-t", "--timeout",
 
539
    parser.add_argument("-t", "--timeout", type=string_to_delta,
540
540
                        help="Set timeout for client")
541
 
    parser.add_argument("--extended-timeout",
 
541
    parser.add_argument("--extended-timeout", type=string_to_delta,
542
542
                        help="Set extended timeout for client")
543
 
    parser.add_argument("-i", "--interval",
 
543
    parser.add_argument("-i", "--interval", type=string_to_delta,
544
544
                        help="Set checker interval for client")
545
545
    approve_deny_default = parser.add_mutually_exclusive_group()
546
546
    approve_deny_default.add_argument(
551
551
        "--deny-by-default", action="store_false",
552
552
        dest="approved_by_default",
553
553
        help="Set client to be denied by default")
554
 
    parser.add_argument("--approval-delay",
 
554
    parser.add_argument("--approval-delay", type=string_to_delta,
555
555
                        help="Set delay before client approve/deny")
556
 
    parser.add_argument("--approval-duration",
 
556
    parser.add_argument("--approval-duration", type=string_to_delta,
557
557
                        help="Set duration of one client approval")
558
558
    parser.add_argument("-H", "--host", help="Set host for client")
559
559
    parser.add_argument("-s", "--secret",
1100
1100
class TestSetTimeoutCmd(TestValueArgumentPropertyCmd):
1101
1101
    command = SetTimeoutCmd
1102
1102
    property = "Timeout"
1103
 
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1104
 
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
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]
1105
1109
 
1106
1110
class TestSetExtendedTimeoutCmd(TestValueArgumentPropertyCmd):
1107
1111
    command = SetExtendedTimeoutCmd
1108
1112
    property = "ExtendedTimeout"
1109
 
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1110
 
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
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]
1111
1119
 
1112
1120
class TestSetIntervalCmd(TestValueArgumentPropertyCmd):
1113
1121
    command = SetIntervalCmd
1114
1122
    property = "Interval"
1115
 
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1116
 
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
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]
1117
1129
 
1118
1130
class TestSetApprovalDelayCmd(TestValueArgumentPropertyCmd):
1119
1131
    command = SetApprovalDelayCmd
1120
1132
    property = "ApprovalDelay"
1121
 
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1122
 
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
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]
1123
1139
 
1124
1140
class TestSetApprovalDurationCmd(TestValueArgumentPropertyCmd):
1125
1141
    command = SetApprovalDurationCmd
1126
1142
    property = "ApprovalDuration"
1127
 
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1128
 
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
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]
1129
1149
 
1130
1150
class Test_command_from_options(unittest.TestCase):
1131
1151
    def setUp(self):