/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 16:20:02 UTC
  • Revision ID: teddy@recompile.se-20190303162002-y0ezr98f71d0jfro
mandos-ctl: Refactor; test PrintTableCmd instead of TableOfClients

* mandos-ctl (Test_TableOfClients): Removed.
  (TestCmd): New abstract class for tests for command classes.
  (TestPrintTableCmd): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
765
765
            self.assertTrue(getattr(warning_filter, "found", False))
766
766
        self.assertEqual(value, datetime.timedelta(0, 7200))
767
767
 
768
 
class Test_TableOfClients(unittest.TestCase):
 
768
 
 
769
class TestCmd(unittest.TestCase):
 
770
    """Abstract class for tests of command classes"""
769
771
    def setUp(self):
770
 
        self.tableheaders = {
771
 
            "Attr1": "X",
772
 
            "AttrTwo": "Yy",
773
 
            "AttrThree": "Zzz",
774
 
            "Bool": "A D-BUS Boolean",
775
 
            "NonDbusBoolean": "A Non-D-BUS Boolean",
776
 
            "Integer": "An Integer",
777
 
            "Timeout": "Timedelta 1",
778
 
            "Interval": "Timedelta 2",
779
 
            "ApprovalDelay": "Timedelta 3",
780
 
            "ApprovalDuration": "Timedelta 4",
781
 
            "ExtendedTimeout": "Timedelta 5",
782
 
            "String": "A String",
783
 
        }
784
 
        self.keywords = ["Attr1", "AttrTwo"]
785
 
        self.clients = [
786
 
            {
787
 
                "Attr1": "x1",
788
 
                "AttrTwo": "y1",
789
 
                "AttrThree": "z1",
790
 
                "Bool": dbus.Boolean(False),
791
 
                "NonDbusBoolean": False,
792
 
                "Integer": 0,
793
 
                "Timeout": 0,
794
 
                "Interval": 1000,
795
 
                "ApprovalDelay": 2000,
796
 
                "ApprovalDuration": 3000,
797
 
                "ExtendedTimeout": 4000,
798
 
                "String": "",
799
 
            },
800
 
            {
801
 
                "Attr1": "x2",
802
 
                "AttrTwo": "y2",
803
 
                "AttrThree": "z2",
804
 
                "Bool": dbus.Boolean(True),
805
 
                "NonDbusBoolean": True,
806
 
                "Integer": 1,
807
 
                "Timeout": 93785000,
808
 
                "Interval": 93786000,
809
 
                "ApprovalDelay": 93787000,
810
 
                "ApprovalDuration": 93788000,
811
 
                "ExtendedTimeout": 93789000,
812
 
                "String": "A huge string which will not fit," * 10,
813
 
            },
814
 
        ]
815
 
    def test_short_header(self):
816
 
        text = str(TableOfClients(self.clients, self.keywords,
817
 
                                  self.tableheaders))
818
 
        expected_text = """
819
 
X  Yy
820
 
x1 y1
821
 
x2 y2
822
 
"""[1:-1]
823
 
        self.assertEqual(text, expected_text)
824
 
    def test_booleans(self):
825
 
        keywords = ["Bool", "NonDbusBoolean"]
826
 
        text = str(TableOfClients(self.clients, keywords,
827
 
                                  self.tableheaders))
828
 
        expected_text = """
829
 
A D-BUS Boolean A Non-D-BUS Boolean
830
 
No              False              
831
 
Yes             True               
832
 
"""[1:-1]
833
 
        self.assertEqual(text, expected_text)
834
 
    def test_milliseconds_detection(self):
835
 
        keywords = ["Integer", "Timeout", "Interval", "ApprovalDelay",
836
 
                    "ApprovalDuration", "ExtendedTimeout"]
837
 
        text = str(TableOfClients(self.clients, keywords,
838
 
                                  self.tableheaders))
839
 
        expected_text = """
840
 
An Integer Timedelta 1 Timedelta 2 Timedelta 3 Timedelta 4 Timedelta 5
841
 
0          00:00:00    00:00:01    00:00:02    00:00:03    00:00:04   
842
 
1          1T02:03:05  1T02:03:06  1T02:03:07  1T02:03:08  1T02:03:09 
843
 
"""[1:-1]
844
 
        self.assertEqual(text, expected_text)
845
 
    def test_empty_and_long_string_values(self):
846
 
        keywords = ["String"]
847
 
        text = str(TableOfClients(self.clients, keywords,
848
 
                                  self.tableheaders))
849
 
        expected_text = """
850
 
A String                                                                                                                                                                                                                                                                                                                                  
851
 
                                                                                                                                                                                                                                                                                                                                          
852
 
A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,A huge string which will not fit,
853
 
"""[1:-1]
854
 
        self.assertEqual(text, expected_text)
 
772
        testcase = self
 
773
        class MockClient(object):
 
774
            def __init__(self, name, **attributes):
 
775
                self.__dbus_object_path__ = "objpath_{}".format(name)
 
776
                self.attributes = attributes
 
777
                self.attributes["Name"] = name
 
778
            def Set(interface, property, value,
 
779
                    properties_interface):
 
780
                testcase.assertEqual(interface, client_interface)
 
781
                testcase.assertEqual(properties_interface,
 
782
                                     dbus.PROPERTIES_IFACE)
 
783
                self.attributes[property] = value
 
784
            def Get(interface, property, properties_interface):
 
785
                testcase.assertEqual(interface, client_interface)
 
786
                testcase.assertEqual(properties_interface,
 
787
                                     dbus.PROPERTIES_IFACE)
 
788
                return self.attributes[property]
 
789
            def __getitem__(self, key):
 
790
                return self.attributes[key]
 
791
        self.clients = collections.OrderedDict([
 
792
            ("foo",
 
793
             MockClient(
 
794
                 "foo",
 
795
                 KeyID=("92ed150794387c03ce684574b1139a65"
 
796
                        "94a34f895daaaf09fd8ea90a27cddb12"),
 
797
                 Secret=b"secret",
 
798
                 Host="foo.example.org",
 
799
                 Enabled=dbus.Boolean(True),
 
800
                 Timeout=300000,
 
801
                 LastCheckedOK="2019-02-03T00:00:00",
 
802
                 Created="2019-01-02T00:00:00",
 
803
                 Interval=120000,
 
804
                 Fingerprint=("778827225BA7DE539C5A"
 
805
                              "7CFA59CFF7CDBD9A5920"),
 
806
                 CheckerRunning=dbus.Boolean(False),
 
807
                 LastEnabled="2019-01-03T00:00:00",
 
808
                 ApprovalPending=dbus.Boolean(False),
 
809
                 ApprovedByDefault=dbus.Boolean(True),
 
810
                 LastApprovalRequest="",
 
811
                 ApprovalDelay=0,
 
812
                 ApprovalDuration=1000,
 
813
                 Checker="fping -q -- %(host)s",
 
814
                 ExtendedTimeout=900000,
 
815
                 Expires="2019-02-04T00:00:00",
 
816
                 LastCheckerStatus=0)),
 
817
            ("barbar",
 
818
             MockClient(
 
819
                 "barbar",
 
820
                 KeyID=("0558568eedd67d622f5c83b35a115f79"
 
821
                        "6ab612cff5ad227247e46c2b020f441c"),
 
822
                 Secret=b"secretbar",
 
823
                 Host="192.0.2.3",
 
824
                 Enabled=dbus.Boolean(True),
 
825
                 Timeout=300000,
 
826
                 LastCheckedOK="2019-02-04T00:00:00",
 
827
                 Created="2019-01-03T00:00:00",
 
828
                 Interval=120000,
 
829
                 Fingerprint=("3E393AEAEFB84C7E89E2"
 
830
                              "F547B3A107558FCA3A27"),
 
831
                 CheckerRunning=dbus.Boolean(True),
 
832
                 LastEnabled="2019-01-04T00:00:00",
 
833
                 ApprovalPending=dbus.Boolean(False),
 
834
                 ApprovedByDefault=dbus.Boolean(False),
 
835
                 LastApprovalRequest="2019-01-03T00:00:00",
 
836
                 ApprovalDelay=30000,
 
837
                 ApprovalDuration=1000,
 
838
                 Checker=":",
 
839
                 ExtendedTimeout=900000,
 
840
                 Expires="2019-02-05T00:00:00",
 
841
                 LastCheckerStatus=-2)),
 
842
            ])
 
843
 
 
844
class TestPrintTableCmd(TestCmd):
 
845
    def test_normal(self):
 
846
        output = PrintTableCmd().output(self.clients)
 
847
        expected_output = """
 
848
Name   Enabled Timeout  Last Successful Check
 
849
foo    Yes     00:05:00 2019-02-03T00:00:00  
 
850
barbar Yes     00:05:00 2019-02-04T00:00:00  
 
851
"""[1:-1]
 
852
        self.assertEqual(output, expected_output)
 
853
    def test_verbose(self):
 
854
        output = PrintTableCmd(verbose=True).output(self.clients)
 
855
        expected_output = """
 
856
Name   Enabled Timeout  Last Successful Check Created             Interval Host            Key ID                                                           Fingerprint                              Check Is Running Last Enabled        Approval Is Pending Approved By Default Last Approval Request Approval Delay Approval Duration Checker              Extended Timeout Expires             Last Checker Status
 
857
foo    Yes     00:05:00 2019-02-03T00:00:00   2019-01-02T00:00:00 00:02:00 foo.example.org 92ed150794387c03ce684574b1139a6594a34f895daaaf09fd8ea90a27cddb12 778827225BA7DE539C5A7CFA59CFF7CDBD9A5920 No               2019-01-03T00:00:00 No                  Yes                                       00:00:00       00:00:01          fping -q -- %(host)s 00:15:00         2019-02-04T00:00:00 0                  
 
858
barbar Yes     00:05:00 2019-02-04T00:00:00   2019-01-03T00:00:00 00:02:00 192.0.2.3       0558568eedd67d622f5c83b35a115f796ab612cff5ad227247e46c2b020f441c 3E393AEAEFB84C7E89E2F547B3A107558FCA3A27 Yes              2019-01-04T00:00:00 No                  No                  2019-01-03T00:00:00   00:00:30       00:00:01          :                    00:15:00         2019-02-05T00:00:00 -2                 
 
859
"""[1:-1]
 
860
        self.assertEqual(output, expected_output)
 
861
    def test_one_client(self):
 
862
        output = PrintTableCmd().output({"foo": self.clients["foo"]})
 
863
        expected_output = """
 
864
Name Enabled Timeout  Last Successful Check
 
865
foo  Yes     00:05:00 2019-02-03T00:00:00  
 
866
"""[1:-1]
 
867
        self.assertEqual(output, expected_output)
855
868
 
856
869
 
857
870