Esempio n. 1
0
    def test_always_close(self):
        """Ensure that the connection is closed even if an exception is thrown."""
        self.channel.queue_declare.side_effect = IOError

        with assert_raises(IOError):
            shove.send_command('my_queue', 'my_project', 'asdf', 75)
        assert_true(self.connection.close.called)
Esempio n. 2
0
    def send_command(self, project, sent_command):
        """
        Send a command to be executed by this ShoveInstance.

        :param project:
            Project instance for the project to run the given command
            on.

        :param sent_command:
            SentCommand instance to log the results of this command to.
        """
        log = CommandLog.objects.create(shove_instance=self, sent_command=sent_command)
        shove.send_command(self.routing_key, project.project_name, sent_command.command, log.pk)
        return log
Esempio n. 3
0
    def test_basic_publish(self):
        shove.send_command('my_queue', 'my_project', 'asdf', 75)

        self.channel.queue_declare.assert_called_once_with(queue='my_queue', durable=True)
        self.channel.basic_publish.assert_called_once_with(exchange='', routing_key='my_queue',
                                                           body=ANY)
        assert_true(self.connection.close.called)

        body = self.channel.basic_publish.call_args[1]['body']
        assert_equal(json.loads(body), {
            'version': '1.0',
            'project': 'my_project',
            'command': 'asdf',
            'log_key': 75,
            'log_queue': 'test_log_queue',
        })
Esempio n. 4
0
    def test_basic_publish(self):
        shove.send_command("my_queue", "my_project", "asdf", 75)

        self.channel.queue_declare.assert_called_once_with(queue="my_queue", durable=True)
        self.channel.basic_publish.assert_called_once_with(exchange="", routing_key="my_queue", body=ANY)
        assert_true(self.connection.close.called)

        body = self.channel.basic_publish.call_args[1]["body"]
        assert_equal(
            json.loads(body),
            {
                "version": "1.0",
                "project": "my_project",
                "command": "asdf",
                "log_key": 75,
                "log_queue": "test_log_queue",
            },
        )
Esempio n. 5
0
    def send_command(self, user, command):
        """
        Send a command to be executed by shove for this project.

        :param user:
            User that is running the command. This can be None if the command is being run
            automatically as a scheduled command.

        :param command:
            Name of the command to execute.

        :raises:
            PermissionDenied: If a user is given and that user does not have permission to run a
            command on this project.
        """
        if user and not user.has_perm('projects.can_run_commands', self):
            raise PermissionDenied('User `{0}` does not have permission to run command `{1}` on '
                                   'project `{2}`.'.format(user.email, command, self.name))

        log = CommandLog.objects.create(project=self, user=user, command=command)
        shove.send_command(self.queue, self.project_name, command, log.pk)
        return log