|
| 1 | +Testing Neutron |
| 2 | +============================================================= |
| 3 | + |
| 4 | +Overview |
| 5 | +-------- |
| 6 | + |
| 7 | +The unit tests are meant to cover as much code as possible and should |
| 8 | +be executed without the service running. They are designed to test |
| 9 | +the various pieces of the neutron tree to make sure any new changes |
| 10 | +don't break existing functionality. |
| 11 | + |
| 12 | +The functional tests are intended to validate actual system |
| 13 | +interaction. Mocks should be used sparingly, if at all. Care |
| 14 | +should be taken to ensure that existing system resources are not |
| 15 | +modified and that resources created in tests are properly cleaned |
| 16 | +up. |
| 17 | + |
| 18 | +Development process |
| 19 | +------------------- |
| 20 | + |
| 21 | +It is expected that any new changes that are proposed for merge |
| 22 | +come with tests for that feature or code area. Ideally any bugs |
| 23 | +fixes that are submitted also have tests to prove that they stay |
| 24 | +fixed! In addition, before proposing for merge, all of the |
| 25 | +current tests should be passing. |
| 26 | + |
| 27 | +Virtual environments |
| 28 | +~~~~~~~~~~~~~~~~~~~~ |
| 29 | + |
| 30 | +Testing OpenStack projects, including Neutron, is made easier with `DevStack <https://github.com/openstack-dev/devstack>`_. |
| 31 | + |
| 32 | +Create a machine (such as a VM or Vagrant box) running a distribution supported |
| 33 | +by DevStack and install DevStack there. For example, there is a Vagrant script |
| 34 | +for DevStack at https://github.com/bcwaldon/vagrant_devstack. |
| 35 | + |
| 36 | + .. note:: |
| 37 | + |
| 38 | + If you prefer not to use DevStack, you can still check out source code on your local |
| 39 | + machine and develop from there. |
| 40 | + |
| 41 | + |
| 42 | +Running unit tests |
| 43 | +------------------ |
| 44 | + |
| 45 | +There are three mechanisms for running tests: run_tests.sh, tox, |
| 46 | +and nose. Before submitting a patch for review you should always |
| 47 | +ensure all test pass; a tox run is triggered by the jenkins gate |
| 48 | +executed on gerrit for each patch pushed for review. |
| 49 | + |
| 50 | +With these mechanisms you can either run the tests in the standard |
| 51 | +environment or create a virtual environment to run them in. |
| 52 | + |
| 53 | +By default after running all of the tests, any pep8 errors |
| 54 | +found in the tree will be reported. |
| 55 | + |
| 56 | + |
| 57 | +With `run_tests.sh` |
| 58 | +~~~~~~~~~~~~~~~~~~~ |
| 59 | + |
| 60 | +You can use the `run_tests.sh` script in the root source directory to execute |
| 61 | +tests in a virtualenv:: |
| 62 | + |
| 63 | + ./run_tests -V |
| 64 | + |
| 65 | + |
| 66 | +With `nose` |
| 67 | +~~~~~~~~~~~ |
| 68 | + |
| 69 | +You can use `nose`_ to run individual tests, as well as use for debugging |
| 70 | +portions of your code:: |
| 71 | + |
| 72 | + source .venv/bin/activate |
| 73 | + pip install nose |
| 74 | + nosetests |
| 75 | + |
| 76 | +There are disadvantages to running Nose - the tests are run sequentially, so |
| 77 | +race condition bugs will not be triggered, and the full test suite will |
| 78 | +take significantly longer than tox & testr. The upside is that testr has |
| 79 | +some rough edges when it comes to diagnosing errors and failures, and there is |
| 80 | +no easy way to set a breakpoint in the Neutron code, and enter an |
| 81 | +interactive debugging session while using testr. |
| 82 | + |
| 83 | +.. _nose: https://nose.readthedocs.org/en/latest/index.html |
| 84 | + |
| 85 | +With `tox` |
| 86 | +~~~~~~~~~~ |
| 87 | + |
| 88 | +Neutron, like other OpenStack projects, uses `tox`_ for managing the virtual |
| 89 | +environments for running test cases. It uses `Testr`_ for managing the running |
| 90 | +of the test cases. |
| 91 | + |
| 92 | +Tox handles the creation of a series of `virtualenvs`_ that target specific |
| 93 | +versions of Python (2.6, 2.7, 3.3, etc). |
| 94 | + |
| 95 | +Testr handles the parallel execution of series of test cases as well as |
| 96 | +the tracking of long-running tests and other things. |
| 97 | + |
| 98 | +Running unit tests is as easy as executing this in the root directory of the |
| 99 | +Neutron source code:: |
| 100 | + |
| 101 | + tox |
| 102 | + |
| 103 | +For more information on the standard Tox-based test infrastructure used by |
| 104 | +OpenStack and how to do some common test/debugging procedures with Testr, |
| 105 | +see this wiki page: |
| 106 | + |
| 107 | + https://wiki.openstack.org/wiki/Testr |
| 108 | + |
| 109 | +.. _Testr: https://wiki.openstack.org/wiki/Testr |
| 110 | +.. _tox: http://tox.readthedocs.org/en/latest/ |
| 111 | +.. _virtualenvs: https://pypi.python.org/pypi/virtualenv |
| 112 | + |
| 113 | + |
| 114 | +Running individual tests |
| 115 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 116 | + |
| 117 | +For running individual test modules or cases, you just need to pass |
| 118 | +the dot-separated path to the module you want as an argument to it. |
| 119 | + |
| 120 | +For executing a specific test case, specify the name of the test case |
| 121 | +class separating it from the module path with a colon. |
| 122 | + |
| 123 | +For example, the following would run only the JSONV2TestCase tests from |
| 124 | +neutron/tests/unit/test_api_v2.py:: |
| 125 | + |
| 126 | + $ ./run_tests.sh neutron.tests.unit.test_api_v2:JSONV2TestCase |
| 127 | + |
| 128 | +or:: |
| 129 | + |
| 130 | + $ ./tox neutron.tests.unit.test_api_v2:JSONV2TestCase |
| 131 | + |
| 132 | +Adding more tests |
| 133 | +~~~~~~~~~~~~~~~~~ |
| 134 | + |
| 135 | +Neutron has a fast growing code base and there is plenty of areas that |
| 136 | +need to be covered by unit and functional tests. |
| 137 | + |
| 138 | +To get a grasp of the areas where tests are needed, you can check |
| 139 | +current coverage by running:: |
| 140 | + |
| 141 | + $ ./run_tests.sh -c |
| 142 | + |
| 143 | +Debugging |
| 144 | +--------- |
| 145 | + |
| 146 | +By default, calls to pdb.set_trace() will be ignored when tests |
| 147 | +are run. For pdb statements to work, invoke run_tests as follows:: |
| 148 | + |
| 149 | + $ ./run_tests.sh -d [test module path] |
| 150 | + |
| 151 | +It's possible to debug tests in a tox environment:: |
| 152 | + |
| 153 | + $ tox -e venv -- python -m testtools.run [test module path] |
| 154 | + |
| 155 | +Tox-created virtual environments (venv's) can also be activated |
| 156 | +after a tox run and reused for debugging:: |
| 157 | + |
| 158 | + $ tox -e venv |
| 159 | + $ . .tox/venv/bin/activate |
| 160 | + $ python -m testtools.run [test module path] |
| 161 | + |
| 162 | +Tox packages and installs the neutron source tree in a given venv |
| 163 | +on every invocation, but if modifications need to be made between |
| 164 | +invocation (e.g. adding more pdb statements), it is recommended |
| 165 | +that the source tree be installed in the venv in editable mode:: |
| 166 | + |
| 167 | + # run this only after activating the venv |
| 168 | + $ pip install --editable . |
| 169 | + |
| 170 | +Editable mode ensures that changes made to the source tree are |
| 171 | +automatically reflected in the venv, and that such changes are not |
| 172 | +overwritten during the next tox run. |
| 173 | + |
| 174 | +Post-mortem debugging |
| 175 | +~~~~~~~~~~~~~~~~~~~~~ |
| 176 | + |
| 177 | +Setting OS_POST_MORTEM_DEBUG=1 in the shell environment will ensure |
| 178 | +that pdb.post_mortem() will be invoked on test failure:: |
| 179 | + |
| 180 | + $ OS_POST_MORTEM_DEBUG=1 ./run_tests.sh -d [test module path] |
0 commit comments