/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-04 18:52:48 UTC
  • Revision ID: teddy@recompile.se-20190304185248-1yrqw03l1s2skue4
mandos-ctl: Refactor tests

* mandos-ctl (TestCmd.client): New; contains one mock client.
  (TestPrintTableCmd.test_one_client): Use TestCmd.client.
  (TestDumpJSONCmd.test_one_client): - '' -
  (TestIsEnabledCmd.test_is_enabled_does_get_attribute): - '' -
  (TestIsEnabledCmd.test_is_enabled_run_exits_successfully): - '' -
  (TestIsEnabledCmd.test_is_enabled_run_exits_with_failure): - '' -
  (TestRemoveCmd.test_remove): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
851
851
                 Expires="2019-02-05T00:00:00",
852
852
                 LastCheckerStatus=-2)),
853
853
            ])
 
854
        self.client = self.clients["foo"]
854
855
 
855
856
class TestPrintTableCmd(TestCmd):
856
857
    def test_normal(self):
870
871
"""[1:-1]
871
872
        self.assertEqual(output, expected_output)
872
873
    def test_one_client(self):
873
 
        output = PrintTableCmd().output({"foo": self.clients["foo"]})
 
874
        output = PrintTableCmd().output({"foo": self.client})
874
875
        expected_output = """
875
876
Name Enabled Timeout  Last Successful Check
876
877
foo  Yes     00:05:00 2019-02-03T00:00:00  
934
935
        json_data = json.loads(DumpJSONCmd().output(self.clients))
935
936
        self.assertDictEqual(json_data, self.expected_json)
936
937
    def test_one_client(self):
937
 
        clients = {"foo": self.clients["foo"]}
 
938
        clients = {"foo": self.client}
938
939
        json_data = json.loads(DumpJSONCmd().output(clients))
939
940
        expected_json = {"foo": self.expected_json["foo"]}
940
941
        self.assertDictEqual(json_data, expected_json)
944
945
        self.assertTrue(all(IsEnabledCmd().is_enabled(client)
945
946
                            for client in self.clients.values()))
946
947
    def test_is_enabled_does_get_attribute(self):
947
 
        client = self.clients["foo"]
948
 
        self.assertTrue(IsEnabledCmd().is_enabled(client))
949
 
        self.assertListEqual(client.calls,
 
948
        self.assertTrue(IsEnabledCmd().is_enabled(self.client))
 
949
        self.assertListEqual(self.client.calls,
950
950
                             [("Get",
951
951
                               ("se.recompile.Mandos.Client",
952
952
                                "Enabled",
953
953
                                "org.freedesktop.DBus.Properties"))])
954
954
    def test_is_enabled_run_exits_successfully(self):
955
 
        client = self.clients["foo"]
956
955
        with self.assertRaises(SystemExit) as e:
957
 
            IsEnabledCmd().run(None, [client])
 
956
            IsEnabledCmd().run(None, [self.client])
958
957
        if e.exception.code is not None:
959
958
            self.assertEqual(e.exception.code, 0)
960
959
        else:
961
960
            self.assertIsNone(e.exception.code)
962
961
    def test_is_enabled_run_exits_with_failure(self):
963
 
        client = self.clients["foo"]
964
 
        client["Enabled"] = dbus.Boolean(False)
 
962
        self.client["Enabled"] = dbus.Boolean(False)
965
963
        with self.assertRaises(SystemExit) as e:
966
 
            IsEnabledCmd().run(None, [client])
 
964
            IsEnabledCmd().run(None, [self.client])
967
965
        if isinstance(e.exception.code, int):
968
966
            self.assertNotEqual(e.exception.code, 0)
969
967
        else:
972
970
 
973
971
class TestRemoveCmd(TestCmd):
974
972
    def test_remove(self):
975
 
        client = self.clients["foo"]
976
973
        class MockMandos(object):
977
974
            def __init__(self):
978
975
                self.calls = []
979
976
            def RemoveClient(self, dbus_path):
980
977
                self.calls.append(("RemoveClient", (dbus_path,)))
981
978
        mandos = MockMandos()
982
 
        RemoveCmd().run(mandos, [client])
 
979
        RemoveCmd().run(mandos, [self.client])
983
980
        self.assertEqual(len(mandos.calls), 1)
984
981
        self.assertListEqual(mandos.calls,
985
982
                             [("RemoveClient",
986
 
                               (client.__dbus_object_path__,))])
 
983
                               (self.client.__dbus_object_path__,))])
987
984
 
988
985
 
989
986