def test_is_due_interval_passed(self):
     """
     If the interval has passed between the current time and last run time, is_due should
     return True.
     """
     command = ScheduledCommandFactory(last_run=aware_datetime(2013, 2, 1, 5, 0, 0),
                                       interval_minutes=15)
     with patch('captain.projects.models.timezone') as timezone:
         timezone.now.return_value = aware_datetime(2013, 2, 1, 5, 16, 0)
         assert_true(command.is_due)
    def test_get_queryset(self):
        """
        The queryset being displayed should be all the sent commands for
        the project, sorted by the time they were sent in reverse.
        """
        project = ProjectFactory.create()
        command1 = SentCommandFactory.create(project=project, sent=aware_datetime(2013, 4, 1))
        command2 = SentCommandFactory.create(project=project, sent=aware_datetime(2013, 4, 2))
        command3 = SentCommandFactory.create(project=project, sent=aware_datetime(2013, 4, 3))

        view = views.ProjectHistory(kwargs={'project_id': project.id})
        assert_equal(list(view.get_queryset()), [command3, command2, command1])
    def test_inactive_thread_mark_inactive_shoves(self):
        """
        mark_inactive_shoves should find any ShoveInstances with a
        last_heartbeat older than the HEARTBEAT_INACTIVE_DELAY setting
        and mark them as inactive.
        """
        up_to_date_instance = ShoveInstanceFactory.create(
            last_heartbeat=aware_datetime(2014, 1, 1, 10, 0, 0), active=True)
        out_of_date_instance = ShoveInstanceFactory.create(
            last_heartbeat=aware_datetime(2014, 1, 1, 9, 30, 0), active=True)

        thread = monitor_shove_instances.MarkInactiveShoveInstancesThread()
        with self.settings(HEARTBEAT_INACTIVE_DELAY=10*60):
            with patch.object(monitor_shove_instances, 'timezone') as mock_timezone:
                mock_timezone.now.return_value = aware_datetime(2014, 1, 1, 10, 5, 0)
                thread.mark_inactive_shoves()

        assert_equal(ShoveInstance.objects.get(pk=up_to_date_instance.pk).active, True)
        assert_equal(ShoveInstance.objects.get(pk=out_of_date_instance.pk).active, False)
    def test_run(self):
        instance1 = ShoveInstanceFactory.create(hostname='foo', active=True)
        instance2 = ShoveInstanceFactory.create(hostname='bar', active=True)
        command = ScheduledCommandFactory(command='asdf', hostnames='foo,bar')
        command.project.send_command = Mock()

        with patch('captain.projects.models.timezone') as mock_timezone:
            now =  aware_datetime(2014, 1, 1, 1, 1, 1)
            mock_timezone.now.return_value = now
            command.run()

        assert_equal(command.last_run, now)
        command.project.send_command.assert_called_with(None, 'asdf',
                                                        CONTAINS(instance1, instance2))