/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 18:44:44 UTC
  • Revision ID: teddy@recompile.se-20190317184444-5o7vougp7c3p87ri
mandos-ctl: Refactor tests

* mandos-ctl (TestBaseCommands.test_DumpJSON_normal): Don't run the
  .output method on command; instead, use capture_stdout_to_buffer().
  (TestBaseCommands.test_DumpJSON_one_client): - '' -
  (TestBaseCommands.test_PrintTable_normal): - '' -
  (TestBaseCommands.test_PrintTable_verbose): - '' -
  (TestBaseCommands.test_PrintTable_one_client): - '' -
  (TestBaseCommands.capture_stdout_to_buffer): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1554
1554
    }
1555
1555
 
1556
1556
    def test_DumpJSON_normal(self):
1557
 
        output = command.DumpJSON().output(self.clients.values())
1558
 
        json_data = json.loads(output)
 
1557
        with self.capture_stdout_to_buffer() as buffer:
 
1558
            command.DumpJSON().run(self.clients)
 
1559
        json_data = json.loads(buffer.getvalue())
1559
1560
        self.assertDictEqual(self.expected_json, json_data)
1560
1561
 
 
1562
    @staticmethod
 
1563
    @contextlib.contextmanager
 
1564
    def capture_stdout_to_buffer():
 
1565
        capture_buffer = io.StringIO()
 
1566
        old_stdout = sys.stdout
 
1567
        sys.stdout = capture_buffer
 
1568
        try:
 
1569
            yield capture_buffer
 
1570
        finally:
 
1571
            sys.stdout = old_stdout
 
1572
 
1561
1573
    def test_DumpJSON_one_client(self):
1562
 
        output = command.DumpJSON().output(self.one_client.values())
1563
 
        json_data = json.loads(output)
 
1574
        with self.capture_stdout_to_buffer() as buffer:
 
1575
            command.DumpJSON().run(self.one_client)
 
1576
        json_data = json.loads(buffer.getvalue())
1564
1577
        expected_json = {"foo": self.expected_json["foo"]}
1565
1578
        self.assertDictEqual(expected_json, json_data)
1566
1579
 
1567
1580
    def test_PrintTable_normal(self):
1568
 
        output = command.PrintTable().output(self.clients.values())
 
1581
        with self.capture_stdout_to_buffer() as buffer:
 
1582
            command.PrintTable().run(self.clients)
1569
1583
        expected_output = "\n".join((
1570
1584
            "Name   Enabled Timeout  Last Successful Check",
1571
1585
            "foo    Yes     00:05:00 2019-02-03T00:00:00  ",
1572
1586
            "barbar Yes     00:05:00 2019-02-04T00:00:00  ",
1573
 
        ))
1574
 
        self.assertEqual(expected_output, output)
 
1587
        )) + "\n"
 
1588
        self.assertEqual(expected_output, buffer.getvalue())
1575
1589
 
1576
1590
    def test_PrintTable_verbose(self):
1577
 
        output = command.PrintTable(verbose=True).output(
1578
 
            self.clients.values())
 
1591
        with self.capture_stdout_to_buffer() as buffer:
 
1592
            command.PrintTable(verbose=True).run(self.clients)
1579
1593
        columns = (
1580
1594
            (
1581
1595
                "Name   ",
1663
1677
            )
1664
1678
        )
1665
1679
        num_lines = max(len(rows) for rows in columns)
1666
 
        expected_output = "\n".join("".join(rows[line]
1667
 
                                            for rows in columns)
1668
 
                                    for line in range(num_lines))
1669
 
        self.assertEqual(expected_output, output)
 
1680
        expected_output = ("\n".join("".join(rows[line]
 
1681
                                             for rows in columns)
 
1682
                                     for line in range(num_lines))
 
1683
                           + "\n")
 
1684
        self.assertEqual(expected_output, buffer.getvalue())
1670
1685
 
1671
1686
    def test_PrintTable_one_client(self):
1672
 
        output = command.PrintTable().output(self.one_client.values())
 
1687
        with self.capture_stdout_to_buffer() as buffer:
 
1688
            command.PrintTable().run(self.one_client)
1673
1689
        expected_output = "\n".join((
1674
1690
            "Name Enabled Timeout  Last Successful Check",
1675
1691
            "foo  Yes     00:05:00 2019-02-03T00:00:00  ",
1676
 
        ))
1677
 
        self.assertEqual(expected_output, output)
 
1692
        )) + "\n"
 
1693
        self.assertEqual(expected_output, buffer.getvalue())
1678
1694
 
1679
1695
 
1680
1696
class TestPropertyCmd(TestCommand):