Skip to content

Instantly share code, notes, and snippets.

@djoreilly
Last active April 12, 2017 13:16
Show Gist options
  • Select an option

  • Save djoreilly/07e5ee8e35a1b481929a to your computer and use it in GitHub Desktop.

Select an option

Save djoreilly/07e5ee8e35a1b481929a to your computer and use it in GitHub Desktop.
Tips for understanding the OpenStack codebase

Add LOG.debug() statements

Most OpenStack modules have a global variable LOG defined already, e.g. in Neutron:

from neutron.openstack.common import log
LOG = log.getLogger(__name__)

use prettyprint to format complex data structures

from pprint import pformat
LOG.debug("foo is: %s" % pformat(foo))

You can also use this decorator to see what was passed to a method.

Use grep

To find out what calls something or where something is defined.

grep -rn tunnel_sync --include \*.py /opt/stack/neutron

Print a traceback to see how a function was called

To find out how a function was called, add this line to it:

import traceback; LOG.debug(''.join(traceback.format_stack()))

then restart the service, do whatever to invoke the function, and check the logs for the traceback.

Use pyreverse to generate a class diagram

Prereq: graphviz

sudo apt-get -y install graphviz

Needs to be run from the parent directory of the Python package:

vagrant@devstack:/opt/stack/neutron$ pyreverse -A -o png -p ml2.png -m y --ignore exceptions.py,cisco,brocade,mech_ofagent.py neutron/plugins/ml2/

This created a classes_ml2.png.png and ignored the cisco and brocade packages, and the exceptions and mech_ofagent modules.

Read/run the unit tests

There may be unit tests for classes/methods you are interested in. If you are really lucky the mocked data might be explicit. The naming convention for test methods is to prefix the function name under test with test_ - so grep the source tree for that to find the test module. e.g. to find the tests for a method named tunnel_sync do:

 grep -r 'def test_tunnel_sync' --include \*.py /opt/stack/neutron/neutron/tests/unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment