Thursday, December 26, 2013

Running nova unit tests in pycharm

I use a macbook pro (13" with 8 Gb of Ram) for my openstack development and run Fusion (headless) with ubuntu 12.04 running on that.  In my VM i run devstack pointing to a VC environment which is sitting under my desk at work.  I run samba on the VM and export my filesystem so i can use Pycharm as my IDE.  I also wanted to be able to run unit tests under Pycharm. Working with +Shawn Hartsock I was able to get it all working - but it took a couple of hours.  So i'll document it here for others.

First I needed to install mysql, postgres, and nose on my macbook.  I didn't investigate why i needed 2 different databases.  Once they were installed i made sure that mysql_config and pg_config were in my path.

I then grabbed a copy of nova from git and set up the virtual environment

git clone https://github.com/openstack/nova.git
cd nova
./run_tests.sh 

Since my virtual environment was not created, the script asked to create it and about 1/2 hour later it was all done.

Then i created a new project in pycharm by adding the nova directory and made sure that my python interpreter was pointing to my virtual env for this project.


Then edit your debug/runtime configuration and so it looks like this - i've created a /tmp/lock directory for lock files.


Ready to go - I opened up the tests that i wanted to run -  nova/tests/virt/test_virt.py and right clicked on the run unit tests  


and the tests run.  You will see the ones that passed and failed and can drill down into the failures.  In this case you can see that it's having double with /proc - which doesn't exist on a mac.  +Shawn Hartsock  has a patch for that which will hopefully be upstreamed soon.



I'm hoping this will be useful in my next patches as I try to write tests and code at the same time.  Please update if you follow this and have other issues.  I'll try to keep it updated as I continue using unit tests in pycharm.













Friday, December 13, 2013

Debugging Nova using a remote debugger

You can use a remote debugger to attach to nova services by specifying the host and port as command line arguments. For example

/usr/local/bin/nova-compute --config-file /etc/nova/nova.conf --remote_debug-host 172.16.245.1 --remote_debug-port 1234
In order to do this you need to have a remote debug server - this wiki describes how to use remote debugging with pycharm. You need a commercial license to do remote debugging with pycharm.

Preparing the Server


Start pycharm and create a remote debug runtime configuration. Change the host and port to your IP address and the port you want to use. The path map shows how to translate the path in your nova environment to the path that your debug server understands. The example here shows /opt/stack/nova mapped to /Volume/OpenStack/nova. I have used samba to mount my /opt/stack directory on my debug host at mount point /Volume/Openstack. You need this or you will not be able to see your code and set breakpoints. 



Once you have a remote debug configuration, press the Bug to start debugging.


You should then see the debugger waiting for connection....



Preparing the client


You need to prepare your environment where you run nova (remote host) to connect to the debug server.  


First you need to copy the pydev package from your pycharm installation to your remote host.  The file is called pycharm-debug.egg and it is located in the root directory of your pycharm installation.  Copy that to your remote host and add it to your python path.  

Debugging

With your debug server running, start your nova service.  For example:

/usr/local/bin/nova-compute --config-file /etc/nova/nova.conf --remote_debug-host 172.16.245.1 --remote_debug-port 1234

You should see the debugger connect in the console window.  You can now set breakpoints, examine variables, etc.   Note that when you are doing remote debugging, you are changing the way that Nova uses the eventlet library to support async IO.  So you may see things that do not occur without remote debugging.  Still i find it'a very handy to use and have not had any unexpected results.

Happy Debugging!

More information

For pycharm - http://blog.jetbrains.com/pycharm/2010/12/python-remote-debug-with-pycharm/
For Eclipse - http://pydev.org/manual_adv_remote_debugger.html