def test_handle_heartbeat_remove_projects(self):
        """Remove any old projects not in the project_names list."""
        instance = ShoveInstanceFactory.create(routing_key='asdf')
        project1 = ProjectFactory.create(project_name='foo')
        project2 = ProjectFactory.create(project_name='bar')
        instance.projects.add(project1)

        monitor_shove_instances.handle_heartbeat_event({
            'routing_key': 'asdf',
            'hostname': 'paranoia.local',
            'project_names': 'bar,baz',
        })

        instance = ShoveInstance.objects.get(routing_key='asdf')
        assert_equal(list(instance.projects.all()), [project2])
    def test_handle_heartbeat_add_new_projects(self):
        """
        Add any projects specified by the project_names value to the
        list of projects supported by the ShoveInstance.
        """
        project1 = ProjectFactory.create(project_name='foo')
        project2 = ProjectFactory.create(project_name='bar')

        with patch.object(monitor_shove_instances, 'log') as log:
            monitor_shove_instances.handle_heartbeat_event({
                'routing_key': 'asdf',
                'hostname': 'paranoia.local',
                'project_names': 'foo,bar,baz',
            })
            # Warn about missing project but do not abort.
            assert_true(log.warning.called)

        instance = ShoveInstance.objects.get(routing_key='asdf')
        assert_equal(set(instance.projects.all()), set([project1, project2]))
Exemple #3
0
    def test_send_command_no_permission(self):
        """
        If the user doesn't have permission to run a command for this project, raise
        PermissionDenied.
        """
        user = UserFactory.create()
        project = ProjectFactory.create()

        with assert_raises(PermissionDenied):
            project.send_command(user, 'asdf')
    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)
    def test_non_whitespace_command(self):
        """If a command has non-whitespace characters, the form should be valid."""
        project = ProjectFactory.create()
        shove_instance = ShoveInstanceFactory.create()
        project.shove_instances.add(shove_instance)

        form_data = {
            'command': '   whitespace but not blank   ',
            'shove_instances': [shove_instance.pk]
        }
        form = RunCommandForm(form_data, project=project)
        assert_true(form.is_valid())
    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])
Exemple #8
0
    def test_send_command_no_user(self):
        """
        If None is passed in as the user, do not perform any permission checks and run the command.
        """
        project = ProjectFactory.create(queue='qwer', project_name='blah')

        with patch('captain.projects.models.shove') as shove:
            log = project.send_command(None, 'asdf')
        shove.send_command.assert_called_once_with('qwer', 'blah', 'asdf', log.pk)

        assert_equal(log.project, project)
        assert_equal(log.user, None)
        assert_equal(log.command, 'asdf')
    def test_queryset(self):
        """
        get_queryset should return all projects that the current user has permission to run
        commands on.
        """
        project1, project2, project3 = ProjectFactory.create_batch(3)
        user = UserFactory.create()
        assign_perm('can_run_commands', user, project1)
        assign_perm('can_run_commands', user, project2)

        self.client_login_user(user)
        response = self.client.get(reverse('projects.mine'))
        assert_equal(set(response.context['object_list']), set([project1, project2]))
    def test_send_command_no_user(self):
        """
        If None is passed in as the user, do not perform any permission checks and run the command.
        """
        project = ProjectFactory.create(project_name='blah')

        instance = ShoveInstanceFactory.create(active=True)
        project.shove_instances.add(instance)

        with patch.object(ShoveInstance, 'send_command', autospec=True) as send_command:
            sent_command = project.send_command(None, 'asdf', [instance])
            send_command.assert_called_with(instance, project, sent_command)

        assert_equal(sent_command.project, project)
        assert_equal(sent_command.user, None)
        assert_equal(sent_command.command, 'asdf')
    def test_handle_heartbeat_existing_shove_instance(self):
        """
        If a shove instance for the given routing key already exists,
        update it with the latest hostname and projects.
        """
        instance = ShoveInstanceFactory.create(routing_key='asdf', hostname='old.local')
        project1 = ProjectFactory.create(project_name='foo')

        monitor_shove_instances.handle_heartbeat_event({
            'routing_key': 'asdf',
            'hostname': 'paranoia.local',
            'project_names': 'foo',
        })
        instance = ShoveInstance.objects.get(pk=instance.pk)
        assert_equal(instance.hostname, 'paranoia.local')
        assert_equal(list(instance.projects.all()), [project1])
Exemple #12
0
    def test_send_command_has_permission(self):
        """
        If the user has permission to run a command for this project, send the command to shove and
        create a log entry for it.
        """
        user = UserFactory.create()
        project = ProjectFactory.create(queue='qwer', project_name='blah')
        assign_perm('can_run_commands', user, project)

        with patch('captain.projects.models.shove') as shove:
            log = project.send_command(user, 'asdf')
        shove.send_command.assert_called_once_with('qwer', 'blah', 'asdf', log.pk)

        assert_equal(log.project, project)
        assert_equal(log.user, user)
        assert_equal(log.command, 'asdf')
    def test_whitespace_command(self):
        """If a command consists only of whitespace, the form should be invalid."""
        project = ProjectFactory.create()
        shove_instance = ShoveInstanceFactory.create()
        project.shove_instances.add(shove_instance)

        form = RunCommandForm({'command': '     ', 'shove_instances': [shove_instance.pk]},
                              project=project)
        assert_false(form.is_valid())

        form = RunCommandForm({'command': '\t\t', 'shove_instances': [shove_instance.pk]},
                              project=project)
        assert_false(form.is_valid())

        form = RunCommandForm({'command': '\n\n\n\n', 'shove_instances': [shove_instance.pk]},
                              project=project)
        assert_false(form.is_valid())
    def test_clean_hostnames(self):
        """
        The cleaned hostname value should be a comma-separated list of
        hostnames of the selected ShoveInstances.
        """
        project = ProjectFactory.create()
        instance1 = ShoveInstanceFactory.create(hostname='foo.bar')
        instance2 = ShoveInstanceFactory.create(hostname='baz.biff')
        project.shove_instances.add(instance1)
        project.shove_instances.add(instance2)

        form = CreateScheduledCommandForm({
            'command': 'test',
            'hostnames': [instance1.pk, instance2.pk],
        }, project=project)
        form.is_valid()
        hostnames = form.cleaned_data['hostnames'].split(',')
        assert_equal(set(hostnames), set(['foo.bar', 'baz.biff']))
    def test_send_command_inactive_shoves(self):
        """
        If any of the shove instances given are not in the set of active
        shove instances for this project, raise a ValueError.
        """
        user = UserFactory.create()
        project = ProjectFactory.create()
        assign_perm('can_run_commands', user, project)

        instance1, instance2 = ShoveInstanceFactory.create_batch(2, active=True)
        project.shove_instances.add(instance1)
        with assert_raises(ValueError):
            project.send_command(user, 'asdf', [instance1, instance2])

        # Now try with an inactive instance that is part of the
        # project's instances.
        instance2.active = False
        instance2.save()
        project.shove_instances.add(instance2)
        with assert_raises(ValueError):
            project.send_command(user, 'asdf', [instance1, instance2])
    def test_send_command_has_permission(self):
        """
        If the user has permission to run a command for this project,
        send the command to shove and create a sent command for it.
        """
        user = UserFactory.create()
        project = ProjectFactory.create(project_name='blah')
        assign_perm('can_run_commands', user, project)

        instance1, instance2 = ShoveInstanceFactory.create_batch(2, active=True)
        project.shove_instances.add(instance1, instance2)

        with patch.object(ShoveInstance, 'send_command', autospec=True) as send_command:
            sent_command = project.send_command(user, 'asdf', [instance1, instance2])
            send_command.assert_has_calls([call(instance1, project, sent_command),
                                           call(instance2, project, sent_command)],
                                          any_order=True)

        assert_equal(sent_command.project, project)
        assert_equal(sent_command.user, user)
        assert_equal(sent_command.command, 'asdf')
 def setUp(self):
     self.project = ProjectFactory.create()
     self.url = reverse('projects.details.run_command', args=(self.project.pk,))
 def setUp(self):
     self.project = ProjectFactory.create()
     self.shove_instances = ShoveInstanceFactory.create_batch(2)
     self.project.shove_instances.add(*self.shove_instances)
     self.url = reverse('projects.details.schedule', args=(self.project.pk,))