/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-30 21:23:59 UTC
  • Revision ID: teddy@recompile.se-20190330212359-z7u9ik1lh5auybhm
mandos-ctl: Refactor: fix confusion about values_to_set/values_to_get

* mandos-ctl (TestPropertySetterCmd.runTest): Generalize to work on
                                              any command regardless
                                              of what combination of
                                              values_to_set or
                                              values_to_get attributes
                                              it has.
  (TestPropertySetterCmd.run_command): Remove.
  (TestEnableCmd, TestDisableCmd, TestBumpTimeoutCmd,
  TestStartCheckerCmd, TestStopCheckerCmd, TestApproveByDefaultCmd,
  TestDenyByDefaultCmd): Change "values_to_set" to "values_to_get",
                         since that is what is intended.
  (TestPropertySetterValueCmd): Remove; all inheritors changed to
                                inherit from TestPropertySetterCmd.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2070
2070
    def runTest(self):
2071
2071
        if not hasattr(self, "command"):
2072
2072
            return              # Abstract TestCase class
2073
 
        values_to_get = getattr(self, "values_to_get",
2074
 
                                self.values_to_set)
2075
 
        for value_to_set, value_to_get in zip(self.values_to_set,
2076
 
                                              values_to_get):
2077
 
            for clientpath in self.bus.clients:
2078
 
                self.bus.clients[clientpath][self.propname] = Unique()
2079
 
            self.run_command(value_to_set, self.bus.clients)
2080
 
            for clientpath in self.bus.clients:
2081
 
                value = self.bus.clients[clientpath][self.propname]
 
2073
 
 
2074
        if hasattr(self, "values_to_set"):
 
2075
            cmd_args = [(value,) for value in self.values_to_set]
 
2076
            values_to_get = getattr(self, "values_to_get",
 
2077
                                    self.values_to_set)
 
2078
        else:
 
2079
            cmd_args = [() for x in range(len(self.values_to_get))]
 
2080
            values_to_get = self.values_to_get
 
2081
        for value_to_get, cmd_arg in zip(values_to_get, cmd_args):
 
2082
            for clientpath in self.bus.clients:
 
2083
                self.bus.clients[clientpath][self.propname] = (
 
2084
                    Unique())
 
2085
            self.command(*cmd_arg).run(self.bus.clients, self.bus)
 
2086
            for clientpath in self.bus.clients:
 
2087
                value = (self.bus.clients[clientpath]
 
2088
                         [self.propname])
2082
2089
                self.assertNotIsInstance(value, Unique)
2083
2090
                self.assertEqual(value_to_get, value)
2084
2091
 
2085
 
    def run_command(self, value, clients):
2086
 
        self.command().run(clients, self.bus)
2087
 
 
2088
2092
 
2089
2093
class TestEnableCmd(TestPropertySetterCmd):
2090
2094
    command = command.Enable
2091
2095
    propname = "Enabled"
2092
 
    values_to_set = [True]
 
2096
    values_to_get = [True]
2093
2097
 
2094
2098
 
2095
2099
class TestDisableCmd(TestPropertySetterCmd):
2096
2100
    command = command.Disable
2097
2101
    propname = "Enabled"
2098
 
    values_to_set = [False]
 
2102
    values_to_get = [False]
2099
2103
 
2100
2104
 
2101
2105
class TestBumpTimeoutCmd(TestPropertySetterCmd):
2102
2106
    command = command.BumpTimeout
2103
2107
    propname = "LastCheckedOK"
2104
 
    values_to_set = [""]
 
2108
    values_to_get = [""]
2105
2109
 
2106
2110
 
2107
2111
class TestStartCheckerCmd(TestPropertySetterCmd):
2108
2112
    command = command.StartChecker
2109
2113
    propname = "CheckerRunning"
2110
 
    values_to_set = [True]
 
2114
    values_to_get = [True]
2111
2115
 
2112
2116
 
2113
2117
class TestStopCheckerCmd(TestPropertySetterCmd):
2114
2118
    command = command.StopChecker
2115
2119
    propname = "CheckerRunning"
2116
 
    values_to_set = [False]
 
2120
    values_to_get = [False]
2117
2121
 
2118
2122
 
2119
2123
class TestApproveByDefaultCmd(TestPropertySetterCmd):
2120
2124
    command = command.ApproveByDefault
2121
2125
    propname = "ApprovedByDefault"
2122
 
    values_to_set = [True]
 
2126
    values_to_get = [True]
2123
2127
 
2124
2128
 
2125
2129
class TestDenyByDefaultCmd(TestPropertySetterCmd):
2126
2130
    command = command.DenyByDefault
2127
2131
    propname = "ApprovedByDefault"
2128
 
    values_to_set = [False]
2129
 
 
2130
 
 
2131
 
class TestPropertySetterValueCmd(TestPropertySetterCmd):
2132
 
    """Abstract class for tests of PropertySetterValueCmd classes"""
2133
 
 
2134
 
    def run_command(self, value, clients):
2135
 
        self.command(value).run(clients, self.bus)
2136
 
 
2137
 
 
2138
 
class TestSetCheckerCmd(TestPropertySetterValueCmd):
 
2132
    values_to_get = [False]
 
2133
 
 
2134
 
 
2135
class TestSetCheckerCmd(TestPropertySetterCmd):
2139
2136
    command = command.SetChecker
2140
2137
    propname = "Checker"
2141
2138
    values_to_set = ["", ":", "fping -q -- %s"]
2142
2139
 
2143
2140
 
2144
 
class TestSetHostCmd(TestPropertySetterValueCmd):
 
2141
class TestSetHostCmd(TestPropertySetterCmd):
2145
2142
    command = command.SetHost
2146
2143
    propname = "Host"
2147
2144
    values_to_set = ["192.0.2.3", "client.example.org"]
2148
2145
 
2149
2146
 
2150
 
class TestSetSecretCmd(TestPropertySetterValueCmd):
 
2147
class TestSetSecretCmd(TestPropertySetterCmd):
2151
2148
    command = command.SetSecret
2152
2149
    propname = "Secret"
2153
2150
    values_to_set = [io.BytesIO(b""),
2155
2152
    values_to_get = [f.getvalue() for f in values_to_set]
2156
2153
 
2157
2154
 
2158
 
class TestSetTimeoutCmd(TestPropertySetterValueCmd):
 
2155
class TestSetTimeoutCmd(TestPropertySetterCmd):
2159
2156
    command = command.SetTimeout
2160
2157
    propname = "Timeout"
2161
2158
    values_to_set = [datetime.timedelta(),
2166
2163
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
2167
2164
 
2168
2165
 
2169
 
class TestSetExtendedTimeoutCmd(TestPropertySetterValueCmd):
 
2166
class TestSetExtendedTimeoutCmd(TestPropertySetterCmd):
2170
2167
    command = command.SetExtendedTimeout
2171
2168
    propname = "ExtendedTimeout"
2172
2169
    values_to_set = [datetime.timedelta(),
2177
2174
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
2178
2175
 
2179
2176
 
2180
 
class TestSetIntervalCmd(TestPropertySetterValueCmd):
 
2177
class TestSetIntervalCmd(TestPropertySetterCmd):
2181
2178
    command = command.SetInterval
2182
2179
    propname = "Interval"
2183
2180
    values_to_set = [datetime.timedelta(),
2188
2185
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
2189
2186
 
2190
2187
 
2191
 
class TestSetApprovalDelayCmd(TestPropertySetterValueCmd):
 
2188
class TestSetApprovalDelayCmd(TestPropertySetterCmd):
2192
2189
    command = command.SetApprovalDelay
2193
2190
    propname = "ApprovalDelay"
2194
2191
    values_to_set = [datetime.timedelta(),
2199
2196
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
2200
2197
 
2201
2198
 
2202
 
class TestSetApprovalDurationCmd(TestPropertySetterValueCmd):
 
2199
class TestSetApprovalDurationCmd(TestPropertySetterCmd):
2203
2200
    command = command.SetApprovalDuration
2204
2201
    propname = "ApprovalDuration"
2205
2202
    values_to_set = [datetime.timedelta(),