def test_get_queryset(self):
        """
        Restrict matched SentCommands to ones belonging to the project
        matching the ID in the URL.
        """
        project = ProjectFactory.create()
        command1 = SentCommandFactory.create(project=project)
        SentCommandFactory.create()  # Should not be in results.

        view = views.SentCommandDetails()
        view.kwargs = {'project_id': project.id}
        assert_equal(list(view.get_queryset()), [command1])
    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_success_false(self):
     """
     If any shove instance has a nonzero return code return False.
     """
     command = SentCommandFactory.create()
     CommandLogFactory.create(sent_command=command, return_code=0)
     CommandLogFactory.create(sent_command=command, return_code=1)
     assert_false(command.success)
 def test_success_true(self):
     """
     If all shove instances executed the command successfully,
     return True.
     """
     command = SentCommandFactory.create()
     CommandLogFactory.create_batch(2, sent_command=command, return_code=0)
     assert_true(command.success)
 def test_success_none(self):
     """
     If any shove instance hasn't received a return code yet, return
     None.
     """
     command = SentCommandFactory.create()
     CommandLogFactory.create(sent_command=command, return_code=0)
     CommandLogFactory.create(sent_command=command, return_code=None)
     assert_true(command.success is None)
    def test_get_context_data(self):
        sent_command = SentCommandFactory.create()
        log1, log2 = CommandLogFactory.create_batch(2, sent_command=sent_command)

        view = views.SentCommandDetails()
        view.kwargs = {'project_id': sent_command.project.id}
        view.object = sent_command  # Required for get_context_data.

        ctx = view.get_context_data()
        assert_equal(ctx['project'], sent_command.project)
        assert_equal(list(ctx['logs']), [log1, log2])
    def test_send_command(self):
        project = ProjectFactory.create(project_name='myproject')
        instance = ShoveInstanceFactory.create(active=True, routing_key='route')
        project.shove_instances.add(instance)
        sent_command = SentCommandFactory.create(command='asdf')

        with patch('captain.projects.models.shove') as shove:
            log = instance.send_command(project, sent_command)
            shove.send_command.assert_called_with('route', 'myproject', 'asdf', log.pk)
            assert_equal(log.shove_instance, instance)
            assert_equal(log.sent_command, sent_command)