/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-03 18:11:39 UTC
  • Revision ID: teddy@recompile.se-20190303181139-f8r50k8bgirs3d5f
mandos-ctl: Add test for IsEnabledCmd class

* mandos-ctl (TestCmd.setUp.MockClient.Set,
              TestCmd.setUp.MockClient.Get): Fix bugs.
  (TestCmd.setUp.MockClient.__setitem__): New.
  (TestIsEnabledCmd): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
777
777
                self.__dbus_object_path__ = "objpath_{}".format(name)
778
778
                self.attributes = attributes
779
779
                self.attributes["Name"] = name
780
 
            def Set(interface, property, value,
781
 
                    properties_interface):
 
780
                self.calls = []
 
781
            def Set(self, interface, property, value, dbus_interface):
782
782
                testcase.assertEqual(interface, client_interface)
783
 
                testcase.assertEqual(properties_interface,
 
783
                testcase.assertEqual(dbus_interface,
784
784
                                     dbus.PROPERTIES_IFACE)
785
785
                self.attributes[property] = value
786
 
            def Get(interface, property, properties_interface):
 
786
                self.calls.append(("Set", (interface, property, value,
 
787
                                           dbus_interface)))
 
788
            def Get(self, interface, property, dbus_interface):
787
789
                testcase.assertEqual(interface, client_interface)
788
 
                testcase.assertEqual(properties_interface,
 
790
                testcase.assertEqual(dbus_interface,
789
791
                                     dbus.PROPERTIES_IFACE)
 
792
                self.calls.append(("Get", (interface, property,
 
793
                                           dbus_interface)))
790
794
                return self.attributes[property]
791
795
            def __getitem__(self, key):
792
796
                return self.attributes[key]
 
797
            def __setitem__(self, key, value):
 
798
                self.attributes[key] = value
793
799
        self.clients = collections.OrderedDict([
794
800
            ("foo",
795
801
             MockClient(
930
936
        expected_json = {"foo": self.expected_json["foo"]}
931
937
        self.assertDictEqual(json_data, expected_json)
932
938
 
 
939
class TestIsEnabledCmd(TestCmd):
 
940
    def test_is_enabled(self):
 
941
        self.assertTrue(all(IsEnabledCmd().is_enabled(client)
 
942
                            for client in self.clients.values()))
 
943
    def test_is_enabled_does_get_attribute(self):
 
944
        client = self.clients["foo"]
 
945
        self.assertTrue(IsEnabledCmd().is_enabled(client))
 
946
        self.assertListEqual(client.calls,
 
947
                             [("Get",
 
948
                               ("se.recompile.Mandos.Client",
 
949
                                "Enabled",
 
950
                                "org.freedesktop.DBus.Properties"))])
 
951
    def test_is_enabled_run_exits_successfully(self):
 
952
        client = self.clients["foo"]
 
953
        with self.assertRaises(SystemExit) as e:
 
954
            IsEnabledCmd().run_on_one_client(client)
 
955
        if e.exception.code is not None:
 
956
            self.assertEqual(e.exception.code, 0)
 
957
        else:
 
958
            self.assertIsNone(e.exception.code)
 
959
    def test_is_enabled_run_exits_with_failure(self):
 
960
        client = self.clients["foo"]
 
961
        client["Enabled"] = dbus.Boolean(False)
 
962
        with self.assertRaises(SystemExit) as e:
 
963
            IsEnabledCmd().run_on_one_client(client)
 
964
        if isinstance(e.exception.code, int):
 
965
            self.assertNotEqual(e.exception.code, 0)
 
966
        else:
 
967
            self.assertIsNotNone(e.exception.code)
 
968
 
 
969
 
933
970
 
934
971
def should_only_run_tests():
935
972
    parser = argparse.ArgumentParser(add_help=False)