Merge changes from topic "97346"
[transportpce.git] / docs / tox-guide.rst
1 .. _transportpce-tox-guide:
2
3 TransportPCE tox Guide
4 ======================
5
6 What is tox?
7 ------------
8
9 `Tox` is a tool written in Python to ease tests automation and dependencies management.
10 It provides a command line tool that run tests inside a `Python virtual environment <https://docs.python.org/3/glossary.html#term-virtual-environment>`_.
11
12 This means that it will not modify your local system settings or package and executable files.
13 Instead, it uses a hidden folder (`.tox`) to install the required Python dependencies via pip
14 (`the package installer for Python <https://pip.pypa.io/>`_) before running tests.
15
16 You can find more details about tox at https://github.com/tox-dev/tox .
17 `Tox` is often used as a front-end to Continuous Integration servers.
18
19 For instance, `Linux Foundation globaljjb <https://globaljjbdocstest.readthedocs.io/en/latest/jjb/lf-python-jobs.html#tox-verify>`_
20 provides a gerrit-tox-verify profile.
21 This profile is `used by TransportPCE <https://git.opendaylight.org/gerrit/c/releng/builder/+/78656>`_
22 in OpenDaylight Jenkins CI.
23
24 `Tox` configuration and behavior is very portable across GNU+Linux distributions
25 and others UNIX-like systems or environments.
26
27 Once tox installed in a local environment with
28 for example the following command on Debian based systems::
29
30     $ sudo apt-get install tox
31
32 or on Red Hat based systems::
33
34     $ sudo yum install python-tox
35
36 the same test suite than the CI can be run locally by simply calling the tox command
37 from the project git clone local folder::
38
39     $ tox
40
41 Tox configuration
42 -----------------
43
44 `Tox` configuration is written in the `tox.ini` file at the root folder of the Git project.
45 Please read `tox official documentation <https://tox.readthedocs.io/>`_ for more details.
46 For tox users, the most important parameter in the `[tox]` section is `envlist`.
47 It specifies which profiles to run by default (i.e. when tox is called without the option `-e`).
48 The option `-e` overrides this parameter and allows to choose which profiles to run.
49 For example::
50
51     $ tox -e gitlint
52
53 will only run the `gitlint` profile.
54 And::
55
56     $ tox -e gitlint,checkbashisms
57
58 will run the `gitlint` and `checkbashisms` profiles.
59
60 Profiles configuration are described in the `[testenv]` section.
61 Each profile specificities are usually configured in a subsection, for example the
62 subsection `[testenv:gitlint]` for the configuration of `Gitlint <https://jorisroovers.com/gitlint/>`_.
63
64 Docs profiles
65 -------------
66
67 The `docs` profile itself is used to generate from the sources the HTML documentation of the project.
68 This docuementation can be found in `docs/_build/html/`
69 once the following command has been run locally::
70
71     $ tox -e docs
72
73 The sources are written in the reStructuredText format a.k.a. RST and are located in the `docs/` folder.
74
75 `Sphinx` can also be used to check the validity of the URLs present in the sources
76 with the `docs-linkcheck` profile::
77
78     $ tox -e docs-linkcheck
79
80 False positives can be declared in the sphinx configuration file (usually `docs/conf.py`).
81 Both `docs` and `docs-linkcheck` profiles are run in the CI.
82
83 A third profile called `spelling` and based on `Sphinx` and `PyEnchant <https://pyenchant.github.io/pyenchant/>`_
84 can also be used as a spellchecker.
85
86 .. note::
87
88    All docs profiles call the sphinx Python package.
89    Documentation and more details can be found at `sphinx home page <https://www.sphinx-doc.org/>`_ .
90    Web page templates are inherited from the Python dependency `lfdocs-conf` delared in `docs/requirements.txt`.
91
92 Linter profiles
93 ---------------
94
95 A few linter are also provided from tox profiles.
96 Some are even performed in the CI.
97
98 * `gitlint`. Check that the last commit message is well formatted.
99 * `pylint`. Lint Python tests scripts
100 * `autopep8`. Autoformat Python tests scripts according to PEP8 standard rules.
101 * `pyang`. Lint YANG files.
102 * `pyangformat`. Autoformat YANG files.
103 * `checkbashisms`. Detect bashisms in shell scripts.
104
105
106 Pre-commit profiles
107 -------------------
108
109 `Pre-commit <https://pre-commit.com/>`_ is another wrapper for linters that relies on `Git Hooks <https://githooks.com/>`_.
110 It is particularly useful to address common programming issues such as
111 triming trailing whitespaces or removing tabs.
112
113 `Pre-commit` configuration can be found in the `.pre-commit-config.yaml` file
114 at the Git project root.
115 `Pre-commit` hooks, like any other Git hooks, are run automatically when the
116 command `'git commit'` is called. This avoids forgetting running linters.
117 Although, `pre-commit` can also be called directly from its shell command.
118 This is what is currently performed in TransportPCE CI.
119
120 The `pre-commit` profile allows to call `pre-commit` inside tox virtualenv
121 without installing the `pre-commit` package in the local system,
122 what is pretty convenient::
123
124     $ tox -e pre-commit
125
126 This is also true to install/uninstall the corresponding Git hooks in your
127 Git folder thanks to the profiles `pre-commit-install`
128 and `pre-commit-uninstall`::
129
130     $ tox -e pre-commit-install
131
132 and::
133
134     $ tox -e pre-commit-uninstall
135
136 Functional tests profiles
137 -------------------------
138
139 TransportPCE functional tests are Python scripts that allow to perform blackbox testing on a
140 controller instance.
141 They do not need tox to be performed locally and can be called directly from the shell or with
142 a launcher such as `nosetests <https://nose.readthedocs.io/>`_
143 (often available in Linux distributions packages under the name `python-nose`).
144 Currently, they require the presence of `Honeynode simulators <https://gitlab.com/Orange-OpenSource/lfn/odl/honeynode-simulator>`_
145 and the modification of the controller default OLM timers to speed-up the tests.
146 They are also supposed to be called within the tests folder::
147
148     $ cd tests/
149
150 These tests have been spread over several directories in `tranportpce_tests/`.
151 These directories are mostly named upon OpenROADM device versions.
152 Tests scripts files names are also numbered so that they are performed in a certain order.
153 To ease their integration in tox, a script `launch_tests.sh` can be used to call them.
154 For example, the following command::
155
156     $ ./launch_tests.sh pce
157
158 will call by default all the tests in the folder `tests/transportpce_tests/pce` with `nose`.
159 And the command::
160
161     $ ./launch_tests.sh 1.2.1 portmapping
162
163 is equivalent to::
164
165     $ nosetests --with-xunit transportpce_tests/1.2.1/test01_portmapping.py
166
167 Several tests can be listed in the arguments. For example::
168
169     $ ./launch_tests.sh 1.2.1 portmapping topology
170
171 is equivalent to::
172
173     $ nosetests --with-xunit transportpce_tests/1.2.1/test01_portmapping.py
174     $ nosetests --with-xunit transportpce_tests/1.2.1/test03_topology.py
175
176 Also, some shell environment variables can be used to modify their default behavior.
177 For example the commands::
178
179     $ export LAUNCHER="python3"
180     $ ./launch_tests.sh 2.2.1
181
182 are equivalent to::
183
184     $ python3 transportpce_tests/2.2.1/test01_portmapping.py
185
186 And::
187
188     $ export LAUNCHER="nosetests"
189     $ export USE_LIGHTY="True"
190     $ ./launch_tests.sh 7.1
191
192 are equivalent to::
193
194     $ nosetests transportpce_tests/2.2.1/test01_portmapping.py
195
196 but will ask tests script to use the controller `lighty.io <https://lighty.io/>`_
197 build instead of Karaf.
198
199 These variables are also understood inside tox virtualenv thanks to the `passenv` parameter
200 configured in `tox.ini`.
201
202 Tox TransportPCE functional tests support is split into several tox profiles.
203 Strictly spoken, only the following profiles perform functional tests as described above:
204
205 * `testsPCE`. To evaluate the Path Computation behavior.
206 * `tests121`. To evaluate the support of OpenROADM devices version 1.2.1 .
207 * `tests221`. To evaluate the support of OpenROADM devices version 2.2.1 .
208 * `tests71`. To evaluate the support of OpenROADM devices version 7.1 .
209 * `tests_hybrid`. To evaluate the controller behavior in a mixed environment with several versions of OpenROADM devices.
210 * `gnpy`. To evaluate the controller behavior when used in conjunction with `GNPy <https://github.com/Telecominfraproject/oopt-gnpy>`_. Requires `docker <https://www.docker.com/>`_.
211 * `nbinotifications`. To evaluate the controller north-bound interface notifications support. Requires `docker <https://www.docker.com/>`_.
212
213 Each of these profiles depend on the `buildcontroller` profile, which is simply
214 there to build the controller from sources and adapt OLM default timers.
215 They can also depend on `sims121` or `sims221` or `sims71` profiles to download
216 simulators of OpenROADM devices when needed.
217
218 The `depend` parameter in `tox.ini` allows tox to establish the most efficient
219 tests order strategy  when calling tox without the `-e` option.
220 This is particularly important when the parallelized mode is enabled.
221 If tox is called locally with the option `-e`, profiles not specified to this
222 option but listed in the `depends` parameters are simply ignored.
223 This means you have to specify manually the `buildcontroller` or `simsXXX`
224 profiles if the controller was not build yet or the sims were not downloaded.
225 For example::
226
227     $ tox -e buildcontroller,sims121,tests121
228
229 will build the controller and download simulators before running every functional
230 tests for OpenROADM devices 1.2.1.
231 Once that done, you only need to list the others sims versions profiles before
232 lauching hybrid tests::
233
234     $ tox -e sims221,sims71,tests_hybrid
235
236 Also the same way arguments can be passed to the `launch_tests.sh` script,
237 tests names can be passed as argument when calling the corresponding tox profiles.
238
239 For example:
240
241     $  tox -e tests121 portmapping
242
243 will launch by default the following command inside tox virtual environment::
244
245     $ nosetests --with-xunit transportpce_tests/1.2.1/test01_portmapping.py
246
247 And::
248
249     $  tox -e tests121 "portmapping topology"
250
251 will perform::
252
253     $ nosetests --with-xunit transportpce_tests/1.2.1/test01_portmapping.py
254     $ nosetests --with-xunit transportpce_tests/1.2.1/test03_topology.py
255
256 Note the necessity to use quotes here when listing several test names.
257 If you need to test the portmapping behavior for every OpenROADM devices versions::
258
259     $  tox -e tests121,tests221,tests71 portmapping
260
261 will perform::
262
263     $ nosetests --with-xunit transportpce_tests/1.2.1/test01_portmapping.py
264     $ nosetests --with-xunit transportpce_tests/2.2.1/test01_portmapping.py
265     $ nosetests --with-xunit transportpce_tests/7.1/test01_portmapping.py
266
267 Idem for OLM with only OpenROADM devices versions 1.2.1 and 2.2.1 ::
268
269     $  tox -e tests121,tests221 olm
270
271 will perform::
272
273     $ nosetests --with-xunit transportpce_tests/1.2.1/test05_olm.py
274     $ nosetests --with-xunit transportpce_tests/2.2.1/test08_olm.py
275
276 Profiles parrallelization
277 -------------------------
278
279 Tox profiles execution can be parallelized.
280 CI behavior can be configured from the `releng/builder` repository.
281 This is `the current configuration <https://git.opendaylight.org/gerrit/c/releng/builder/+/96557>`_
282 in TransportPCE CI.
283
284 Locally, tox jobs are not parallelized by default.
285 You have to use the `-p` option to specify the level of concurrency::
286
287     $  tox -p
288
289 or::
290
291     $  tox -p auto
292
293 or::
294
295     $  tox -p 2
296
297 The default parameter "auto" is based on the number of CPU cores,
298 which is a bad idea for TransportPCE functional tests.
299 Their most critical ressource is RAM, mostly because of the need
300 to launch several simulators very greedy in memory.
301 Unfortunately, "auto" is historically the only option available
302 in OpenDaylight CI configuration.
303 To palliate this problem, `tox.ini` current configuration uses the `depends` parameter
304 to artifically chain tests profiles and limit to only 2 the number of controller instances
305 run in parallel.
306
307 Also, the default display will change from the classical sequence mode.
308 You need to use the option `-o` to get it back (with the notable difference
309 that displayed messages are now mixed between tests profiles).
310 This is the default configuration in the CI::
311
312     $  tox -o -p 2
313
314 Running different tests in parallel also creates concurrency access problems
315 to others ressources than RAM, mostly the ports to listen to, and the log files.
316 To this sake, lighty.io and karaf build configuration have been customized to change
317 thier listening ports and log files from shell environment variables.
318 This environment variables are also understood by Python tests scripts and tox.
319
320 You can take a look at the following Gerrit changes for more details
321
322 https://git.opendaylight.org/gerrit/q/topic:%2522parallel%2522+project:transportpce
323
324 And particularly at
325
326 https://git.opendaylight.org/gerrit/c/transportpce/+/96696
327
328 and
329
330 https://git.opendaylight.org/gerrit/c/transportpce/+/96662
331
332 and
333
334 https://git.opendaylight.org/gerrit/c/transportpce/+/96663
335
336 In a nutshell, TransportPCE CI uses tox and parrallelization to perform functional tests.
337 And if your computer environment has enough RAM and CPU cores,
338 it is perfectly possible to run the same way several functional tests concurrently on it.
339 For example, the following command will test the portmapping behavior
340 for every OpenROADM devices supported versions::
341
342     $  tox -p 3 -e buildcontroller,sims121,sims221,sims71,tests121,tests221,tests71 portmapping