/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-17 17:34:07 UTC
  • Revision ID: teddy@recompile.se-20190317173407-itlj1a7mzfwb3yz3
mandos-ctl: Refactor tests

* mandos-ctl: Change all calls to assertEqual (and related functions
              assertNotEqual and assertDictEqual) to always pass
              arguments in (expected, actual) order, for consistency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
891
891
    # tests, which is run by doctest.
892
892
 
893
893
    def test_rfc3339_zero_seconds(self):
894
 
        self.assertEqual(string_to_delta("PT0S"),
895
 
                         datetime.timedelta())
 
894
        self.assertEqual(datetime.timedelta(),
 
895
                         string_to_delta("PT0S"))
896
896
 
897
897
    def test_rfc3339_zero_days(self):
898
 
        self.assertEqual(string_to_delta("P0D"),
899
 
                         datetime.timedelta())
 
898
        self.assertEqual(datetime.timedelta(), string_to_delta("P0D"))
900
899
 
901
900
    def test_rfc3339_one_second(self):
902
 
        self.assertEqual(string_to_delta("PT1S"),
903
 
                         datetime.timedelta(0, 1))
 
901
        self.assertEqual(datetime.timedelta(0, 1),
 
902
                         string_to_delta("PT1S"))
904
903
 
905
904
    def test_rfc3339_two_hours(self):
906
 
        self.assertEqual(string_to_delta("PT2H"),
907
 
                         datetime.timedelta(0, 7200))
 
905
        self.assertEqual(datetime.timedelta(0, 7200),
 
906
                         string_to_delta("PT2H"))
908
907
 
909
908
    def test_falls_back_to_pre_1_6_1_with_warning(self):
910
909
        with self.assertLogs(log, logging.WARNING):
911
910
            value = string_to_delta("2h")
912
 
        self.assertEqual(value, datetime.timedelta(0, 7200))
 
911
        self.assertEqual(datetime.timedelta(0, 7200), value)
913
912
 
914
913
 
915
914
class Test_check_option_syntax(unittest.TestCase):
958
957
        # Exit code from argparse is guaranteed to be "2".  Reference:
959
958
        # https://docs.python.org/3/library
960
959
        # /argparse.html#exiting-methods
961
 
        self.assertEqual(e.exception.code, 2)
 
960
        self.assertEqual(2, e.exception.code)
962
961
 
963
962
    @staticmethod
964
963
    @contextlib.contextmanager
1089
1088
            def get_object(mockbus_self, busname, dbus_path):
1090
1089
                # Note that "self" is still the testcase instance,
1091
1090
                # this MockBus instance is in "mockbus_self".
1092
 
                self.assertEqual(busname, dbus_busname)
1093
 
                self.assertEqual(dbus_path, server_dbus_path)
 
1091
                self.assertEqual(dbus_busname, busname)
 
1092
                self.assertEqual(server_dbus_path, dbus_path)
1094
1093
                mockbus_self.called = True
1095
1094
                return mockbus_self
1096
1095
 
1108
1107
                bus = get_mandos_dbus_object(bus=MockBusFailing())
1109
1108
 
1110
1109
        if isinstance(e.exception.code, int):
1111
 
            self.assertNotEqual(e.exception.code, 0)
 
1110
            self.assertNotEqual(0, e.exception.code)
1112
1111
        else:
1113
1112
            self.assertIsNotNone(e.exception.code)
1114
1113
 
1147
1146
            dbus_logger.removeFilter(counting_handler)
1148
1147
 
1149
1148
        # Make sure the dbus logger was suppressed
1150
 
        self.assertEqual(counting_handler.count, 0)
 
1149
        self.assertEqual(0, counting_handler.count)
1151
1150
 
1152
1151
        # Test that the dbus_logger still works
1153
1152
        with self.assertLogs(dbus_logger, logging.ERROR):
1154
1153
            dbus_logger.error("Test")
1155
1154
 
1156
1155
        if isinstance(e.exception.code, int):
1157
 
            self.assertNotEqual(e.exception.code, 0)
 
1156
            self.assertNotEqual(0, e.exception.code)
1158
1157
        else:
1159
1158
            self.assertIsNotNone(e.exception.code)
1160
1159
 
1175
1174
        options = self.parser.parse_args(args)
1176
1175
        check_option_syntax(self.parser, options)
1177
1176
        commands = commands_from_options(options)
1178
 
        self.assertEqual(len(commands), 1)
 
1177
        self.assertEqual(1, len(commands))
1179
1178
        command = commands[0]
1180
1179
        self.assertIsInstance(command, command_cls)
1181
1180
        for key, value in cmd_attrs.items():
1182
 
            self.assertEqual(getattr(command, key), value)
 
1181
            self.assertEqual(value, getattr(command, key))
1183
1182
 
1184
1183
    def test_is_enabled_short(self):
1185
1184
        self.assert_command_from_args(["-V", "foo"],
1207
1206
                                          "foo"])
1208
1207
        check_option_syntax(self.parser, options)
1209
1208
        commands = commands_from_options(options)
1210
 
        self.assertEqual(len(commands), 2)
 
1209
        self.assertEqual(2, len(commands))
1211
1210
        self.assertIsInstance(commands[0], command.Deny)
1212
1211
        self.assertIsInstance(commands[1], command.Remove)
1213
1212
 
1216
1215
                                          "--all"])
1217
1216
        check_option_syntax(self.parser, options)
1218
1217
        commands = commands_from_options(options)
1219
 
        self.assertEqual(len(commands), 2)
 
1218
        self.assertEqual(2, len(commands))
1220
1219
        self.assertIsInstance(commands[0], command.Deny)
1221
1220
        self.assertIsInstance(commands[1], command.Remove)
1222
1221
 
1382
1381
                self.attributes["Name"] = name
1383
1382
                self.calls = []
1384
1383
            def Set(self, interface, propname, value, dbus_interface):
1385
 
                testcase.assertEqual(interface, client_dbus_interface)
1386
 
                testcase.assertEqual(dbus_interface,
1387
 
                                     dbus.PROPERTIES_IFACE)
 
1384
                testcase.assertEqual(client_dbus_interface, interface)
 
1385
                testcase.assertEqual(dbus.PROPERTIES_IFACE,
 
1386
                                     dbus_interface)
1388
1387
                self.attributes[propname] = value
1389
1388
            def Approve(self, approve, dbus_interface):
1390
 
                testcase.assertEqual(dbus_interface,
1391
 
                                     client_dbus_interface)
 
1389
                testcase.assertEqual(client_dbus_interface,
 
1390
                                     dbus_interface)
1392
1391
                self.calls.append(("Approve", (approve,
1393
1392
                                               dbus_interface)))
1394
1393
        self.client = MockClient(
1451
1450
        class Bus(object):
1452
1451
            @staticmethod
1453
1452
            def get_object(client_bus_name, path):
1454
 
                self.assertEqual(client_bus_name, dbus_busname)
 
1453
                self.assertEqual(dbus_busname, client_bus_name)
1455
1454
                return {
1456
1455
                    # Note: "self" here is the TestCmd instance, not
1457
1456
                    # the Bus instance, since this is a static method!
1467
1466
        with self.assertRaises(SystemExit) as e:
1468
1467
            command.IsEnabled().run(self.one_client)
1469
1468
        if e.exception.code is not None:
1470
 
            self.assertEqual(e.exception.code, 0)
 
1469
            self.assertEqual(0, e.exception.code)
1471
1470
        else:
1472
1471
            self.assertIsNone(e.exception.code)
1473
1472
 
1476
1475
        with self.assertRaises(SystemExit) as e:
1477
1476
            command.IsEnabled().run(self.one_client)
1478
1477
        if isinstance(e.exception.code, int):
1479
 
            self.assertNotEqual(e.exception.code, 0)
 
1478
            self.assertNotEqual(0, e.exception.code)
1480
1479
        else:
1481
1480
            self.assertIsNotNone(e.exception.code)
1482
1481
 
1560
1559
    def test_DumpJSON_normal(self):
1561
1560
        output = command.DumpJSON().output(self.clients.values())
1562
1561
        json_data = json.loads(output)
1563
 
        self.assertDictEqual(json_data, self.expected_json)
 
1562
        self.assertDictEqual(self.expected_json, json_data)
1564
1563
 
1565
1564
    def test_DumpJSON_one_client(self):
1566
1565
        output = command.DumpJSON().output(self.one_client.values())
1567
1566
        json_data = json.loads(output)
1568
1567
        expected_json = {"foo": self.expected_json["foo"]}
1569
 
        self.assertDictEqual(json_data, expected_json)
 
1568
        self.assertDictEqual(expected_json, json_data)
1570
1569
 
1571
1570
    def test_PrintTable_normal(self):
1572
1571
        output = command.PrintTable().output(self.clients.values())
1575
1574
            "foo    Yes     00:05:00 2019-02-03T00:00:00  ",
1576
1575
            "barbar Yes     00:05:00 2019-02-04T00:00:00  ",
1577
1576
        ))
1578
 
        self.assertEqual(output, expected_output)
 
1577
        self.assertEqual(expected_output, output)
1579
1578
 
1580
1579
    def test_PrintTable_verbose(self):
1581
1580
        output = command.PrintTable(verbose=True).output(
1670
1669
        expected_output = "\n".join("".join(rows[line]
1671
1670
                                            for rows in columns)
1672
1671
                                    for line in range(num_lines))
1673
 
        self.assertEqual(output, expected_output)
 
1672
        self.assertEqual(expected_output, output)
1674
1673
 
1675
1674
    def test_PrintTable_one_client(self):
1676
1675
        output = command.PrintTable().output(self.one_client.values())
1678
1677
            "Name Enabled Timeout  Last Successful Check",
1679
1678
            "foo  Yes     00:05:00 2019-02-03T00:00:00  ",
1680
1679
        ))
1681
 
        self.assertEqual(output, expected_output)
 
1680
        self.assertEqual(expected_output, output)
1682
1681
 
1683
1682
 
1684
1683
class TestPropertyCmd(TestCommand):
1699
1698
                client = self.bus.get_object(dbus_busname, clientpath)
1700
1699
                value = client.attributes[self.propname]
1701
1700
                self.assertNotIsInstance(value, self.Unique)
1702
 
                self.assertEqual(value, value_to_get)
 
1701
                self.assertEqual(value_to_get, value)
1703
1702
 
1704
1703
    class Unique(object):
1705
1704
        """Class for objects which exist only to be unique objects,