The simplest way in python to mock open() during unit testing

I believe that the most of python developers are familiar with pretty mock framework. It’s one of the simplest way to avoid unit testing of not interesting parts of your code: such like DB, files, API of other services, some heavy libs like libvirt and so on..

Unfortunately, there is one unclear place (at least for me)  how to mock open() properly especially in case where when with open() in used in code.

I spend a bit of time to resolve this problem and here is the code snippet:

import testtools
import mock

def some(file_path):

    with open(file_path) as f:

class MyTestCase(testtools.TestCase):

    @mock.patch("open", create=True)
    def test_some(self, mock_open):
        mock_open.side_effect = [

        self.assertEqual("Data1", some("fileA"))

        self.assertEqual("Data2", some("fileB"))

Read More

The simplest way to use OpenStack python clients

OpenStack is great but as all young projects it has some UX issues. One of the most user facing is initialization of OpenStack python clients. Every OpenStack Service like Nova, Cinder, Glance have own.

Since the begging in Rally we have internal class that unifies initialization of all clients. Recently we merged patch that allows to init this class from environment variables, which makes it really simple to use OpenStack python clients. Take a look at code snippet below:

boris@ubuntu:~$ . devstack/openrc admin admin

boris@ubuntu:~$ python
>>> from rally import osclients
>>> clients = osclients.Clients.create_from_env()
>>> clients.nova().flavors.list()
[<Flavor: m1.tiny>, <Flavor: m1.small>, <Flavor: m1.medium>, <Flavor: m1.large>, <Flavor: m1.nano>, <Flavor: m1.heat>, <Flavor: m1.xlarge>, <Flavor: m1.micro>]


Read More

Rally Tricks: “Stop load before your OpenStack goes wrong”

Benchmarking pre-production and production OpenStack clouds is not a trivial task. From the one side it’s important to reach the OpenStack cloud’s limits, from the other side the cloud shouldn’t be damaged. Rally aims to make this task as simple as possible. Since the very beginning Rally was able to generate enough load for any OpenStack cloud. Generating to big load was the major issue for production clouds, because Rally didn’t know how to stop the load until it was to late. Finally I am happy to say that we solved this issue.

With the new feature “stop on SLA failure” things are much better. 


Read More