Beispiel #1
0
 def setUp(self):
     self.c = Command()
Beispiel #2
0
class HandleMethodTest(TestCase):
    """
    Test the handle method of the management command directly.
    """

    def setUp(self):
        self.c = Command()

    def tearDown(self):
        pass  # no tear down required

    def test_target_not_found(self):
        """
        Raises a CommandError if target is not found.
        """
        options = {
            'target_id': 0,
        }
        with self.assertRaisesRegexp(
            CommandError,
            "^Target 0 not found in database$"
        ):
            self.c.handle(None, **options)

    @mock.patch('pingu2.ping.ping')
    @mock.patch('pingu2.management.commands.ping_manager.time.sleep')
    def test_successful_ping_once(self, mock_sleep, mock_ping):
        """
        Create a single successful ping record for localhost.
        Mock the ping method to have it return a value of 500.
        """
        # create target
        t = models.Target(name='localhost', fqdn='127.0.0.1')
        t.save()
        # options for management command handle method
        options = {
            'target_id': t.id, 'sleep_seconds': 3, 'count': 1,
        }
        # have mock ping method return 500
        mock_ping.return_value = 500
        # invoke the handle method on the management command
        self.c.handle(None, **options)
        # check that ping was called as expected
        self.assertEqual(
            mock_ping.call_args_list, [mock.call(t.fqdn)]
        )
        # check that sleep was called as expected
        self.assertEqual(
            mock_sleep.call_args_list, [mock.call(3)]
        )
        # check that database records were created as expected
        self.assertEqual(models.Ping.objects.count(), 1)
        ping_from_db = models.Ping.objects.first()
        self.assertEqual(ping_from_db.target, t)
        self.assertEqual(ping_from_db.succeeded, True)
        self.assertEqual(ping_from_db.response_time, 500)

    @mock.patch('pingu2.ping.ping')
    @mock.patch('pingu2.management.commands.ping_manager.time.sleep')
    def test_successful_ping_several(self, mock_sleep, mock_ping):
        """
        Create several successful ping records for localhost.
        Mock the ping method to have it return 1, 2, then 3.
        """
        # create target
        t = models.Target(name='localhost', fqdn='127.0.0.1')
        t.save()
        # options for management command handle method
        options = {
            'target_id': t.id, 'sleep_seconds': 5, 'count': 3,
        }
        # have mock ping method return 1, then 2, then 3
        mock_ping.side_effect = range(1, 4)
        # invoke the handle method on the management command
        self.c.handle(None, **options)
        # check that ping was called thrice as expected
        self.assertEqual(
            mock_ping.call_args_list, [mock.call(t.fqdn) for i in range(3)]
        )
        # check that sleep was called as expected
        self.assertEqual(
            mock_sleep.call_args_list, [mock.call(5) for i in range(3)]
        )
        # check that 3 database records were created as expected
        self.assertEqual(models.Ping.objects.count(), 3)
        pings_from_db = models.Ping.objects.all()
        for idx, ping_from_db in enumerate(pings_from_db):
            self.assertEqual(ping_from_db.target, t)
            self.assertEqual(ping_from_db.succeeded, True)
            self.assertEqual(ping_from_db.response_time, idx+1)

    @mock.patch('pingu2.ping.ping')
    @mock.patch('pingu2.management.commands.ping_manager.time.sleep')
    def test_failed_ping_once(self, mock_sleep, mock_ping):
        """
        Create a single failed ping record for localhost.
        Mock the ping method to have it raise PingFailedException
        """
        # create target
        t = models.Target(name='localhost', fqdn='127.0.0.1')
        t.save()
        # options for management command handle method
        options = {
            'target_id': t.id, 'sleep_seconds': 3, 'count': 1,
        }
        # have mock ping method raise PingFailedException
        mock_ping.side_effect = ping.PingFailedException('Boom')
        # invoke the handle method on the management command
        self.c.handle(None, **options)
        # check that ping was called as expected
        self.assertEqual(
            mock_ping.call_args_list, [mock.call(t.fqdn)]
        )
        # check that sleep was called as expected
        self.assertEqual(
            mock_sleep.call_args_list, [mock.call(3)]
        )
        # check that database records were created as expected
        self.assertEqual(models.Ping.objects.count(), 1)
        ping_from_db = models.Ping.objects.first()
        self.assertEqual(ping_from_db.target, t)
        self.assertEqual(ping_from_db.succeeded, False)

    @mock.patch('pingu2.ping.ping')
    @mock.patch('pingu2.management.commands.ping_manager.time.sleep')
    def test_mixed_ping_several(self, mock_sleep, mock_ping):
        """
        Create several ping records for localhost - some successful,
        some failed.
        Mock the ping method to have it return a value of 1, then
        raise a PingFailedException, then a value of 3.
        """
        # create target
        t = models.Target(name='localhost', fqdn='127.0.0.1')
        t.save()
        # options for management command handle method
        options = {
            'target_id': t.id, 'sleep_seconds': 5, 'count': 3,
        }
        # have mock ping method return 1, then 2, then 3
        mock_ping.side_effect = [
            1, ping.PingFailedException('Boom'), 3
        ]
        # invoke the handle method on the management command
        self.c.handle(None, **options)
        # check that ping was called thrice as expected
        self.assertEqual(
            mock_ping.call_args_list, [mock.call(t.fqdn) for i in range(3)]
        )
        # check that sleep was called as expected
        self.assertEqual(
            mock_sleep.call_args_list, [mock.call(5) for i in range(3)]
        )
        # check that 3 database records were created as expected
        self.assertEqual(models.Ping.objects.count(), 3)
        pings_from_db = models.Ping.objects.all()
        for idx, ping_from_db in enumerate(pings_from_db):
            self.assertEqual(ping_from_db.target, t)
            if idx == 1:  # the second ping failed
                self.assertEqual(ping_from_db.succeeded, False)
            else:
                self.assertEqual(ping_from_db.succeeded, True)
                self.assertEqual(ping_from_db.response_time, idx+1)