Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rewrite the doc around the tests
[simgrid.git] / doc / doxygen / inside_tests.doc
1 /*! 
2 @page inside_tests Testing SimGrid
3
4 This page will teach you how to run the tests, selecting the ones you
5 want, and how to add new tests to the archive.
6
7 \tableofcontents
8
9 SimGrid code coverage is usually between 70% and 80%, which is much
10 more than most projects out there. This is because we consider SimGrid
11 to be a rather complex project, and we want to modify it with less fear.
12
13 We have two sets of tests in SimGrid: Each of the 10,000+ unit tests
14 check one specific case for one specific function, while the 500+
15 integration tests run a given simulation specifically intended to
16 exercise a larger amount of functions together. Every example provided
17 in examples/ is used as an integration test, while some other torture
18 tests and corner cases integration tests are located in teshsuite/.
19 For each integration test, we ensure that the output exactly matches
20 the defined expectations. Since SimGrid displays the timestamp of
21 every loggued line, this ensures that every change of the models'
22 prediction will be noticed. All these tests should ensure that SimGrid
23 is safe to use and to depend on.
24
25 \section inside_tests_runintegration Running the tests
26
27 Running the tests is done using the ctest binary that comes with
28 cmake. These tests are run for every commit and the result is publicly
29 <a href="https://ci.inria.fr/simgrid/">available</a>.
30
31 \verbatim
32 ctest                     # Launch all tests
33 ctest -R msg              # Launch only the tests which name match the string "msg"
34 ctest -j4                 # Launch all tests in parallel, at most 4 at the same time
35 ctest --verbose           # Display all details on what's going on
36 ctest --output-on-failure # Only get verbose for the tests that fail
37
38 ctest -R msg- -j5 --output-on-failure # You changed MSG and want to check that you didn't break anything, huh?
39                                       # That's fine, I do so all the time myself.
40 \endverbatim
41
42 \section inside_tests_rununit Running the unit tests
43
44 All unit tests are packed into the testall binary, that lives in src/.
45 These tests are run when you launch ctest, don't worry.
46
47 \verbatim
48 make testall                        # Rebuild the test runner on need
49 ./src/testall                       # Launch all tests
50 ./src/testall --help                # revise how it goes if you forgot
51 ./src/testall --tests=-all          # run no test at all (yeah, that's useless)
52 ./src/testall --dump-only           # Display all existing test suite
53 ./src/testall --tests=-all,+dict    # Only launch the tests from the dict testsuite
54 ./src/testall --tests=-all,+foo:bar # run only the bar test from the foo suite.
55 \endverbatim
56
57
58 \section inside_tests_add_units Adding unit tests
59
60 If you want to test a specific function or set of functions, you need
61 a unit test. Edit
62 <project/directory>/tools/cmake/UnitTesting.cmake to add your
63 source file to the TEST_CFILES list, and add the corresponding unit
64 file to the TEST_UNITS list. For example, if your file is toto.c,
65 your unit file will be toto_unit.c. The full path to your file must be
66 provided, but the unit file will always be in src/ directly.
67
68 If you want to create unit tests in the file src/xbt/toto.c, your
69 changes should look similar to:
70
71 \verbatim
72 --- a/tools/cmake/UnitTesting.cmake
73 +++ b/tools/cmake/UnitTesting.cmake
74 @@ -11,6 +11,7 @@ set(TEST_CFILES
75    src/xbt/xbt_strbuff.c
76    src/xbt/xbt_sha.c
77    src/xbt/config.c
78 +  src/xbt/toto.c
79    )
80  set(TEST_UNITS
81    ${CMAKE_CURRENT_BINARY_DIR}/src/cunit_unit.c
82 @@ -22,6 +23,7 @@ set(TEST_UNITS
83    ${CMAKE_CURRENT_BINARY_DIR}/src/xbt_strbuff_unit.c
84    ${CMAKE_CURRENT_BINARY_DIR}/src/xbt_sha_unit.c
85    ${CMAKE_CURRENT_BINARY_DIR}/src/config_unit.c
86 +  ${CMAKE_CURRENT_BINARY_DIR}/src/toto_unit.c
87  
88    ${CMAKE_CURRENT_BINARY_DIR}/src/simgrid_units_main.c
89    )
90 \endverbatim
91
92 Then, you want to actually add your tests in the source file. All the
93 tests must be protected by "#ifdef SIMGRID_TEST" so that they don't
94 get included in the regular build. Then, you want to add a test suite
95 that will contain a bunch of tests (in Junit, that would be a test
96 unit) with the macro #XBT_TEST_SUITE, and populate it with a bunch of
97 actual tests with the macro #XBT_TEST_UNIT (sorry for the mischosen
98 names if you are used to junit). Just look at the dynar example (or
99 any other) to see how it works in practice. Do not hesitate to stress
100 test your code this way, but make sure that it runs reasonably fast,
101 or nobody will run "ctest" before commiting code.
102
103 For more details on how the tests are extracted from the module
104 source, check the tools/sg_unit_extractor.pl script directly.
105
106
107 \section inside_tests_add_integration Adding integration tests
108
109 TESH (the TEsting SHell) is the test runner that we wrote for our
110 integration tests. It is distributed with the SimGrid source file, and
111 even comes with a man page. TESH ensures that the output produced by a
112 command perfectly matches the expected output. This is very precious
113 to ensure that no change modifies the timings computed by the models
114 without notice.
115
116 To add a new integration test, you thus have 3 things to do:
117
118  - <b>Write the code exercising the feature you target</b>. You should
119    strive to make this code clear, well documented and informative for
120    the users. If you manage to do so, put this somewhere under
121    examples/ and modify the cmake files as explained on this page:
122    \ref inside_cmake_examples. If you feel like you should write a
123    torture test that is not interesting to the users (because nobody
124    would sainly write something similar in user code), then put it under 
125    teshsuite/ somewhere.
126  - <b>Write the tesh file</b>, containing the command to run, the
127    provided input (if any, but almost no SimGrid test provide such an
128    input) and the expected output. Check the tesh man page for more
129    details. \n
130    Tesh is sometimes annoying as you have to ensure that the expected
131    output will always be exactly the same. In particular, your should
132    not output machine dependent informations, nor memory adresses as
133    they would change on each run. Several steps can be used here, such
134    as the obfucation of the memory adresses unless the verbose logs
135    are displayed (using the #XBT_LOG_ISENABLED() macro), or the
136    modification of the log formats to hide the timings when they
137    depend on the host machine.
138  - <b>Add your test in the cmake infrastructure</b>. For that, modify
139    the file <project/directory>/tools/cmake/Tests.cmake. Make sure to
140    pick a wise name for your test. It is often useful to check a
141    category of tests together. The only way to do so in ctest is to
142    use the -R argument that specifies a regular expression that the
143    test names must match. For example, you can run all MSG test with
144    "ctest -R msg". That explains the importance of the test names.
145
146 Once the name is chosen, create a new test by adding a line similar to
147 the following (assuming that you use tesh as expected).
148
149 \verbatim
150 # Usage: ADD_TEST(test-name ${CMAKE_BINARY_DIR}/bin/tesh <options> <tesh-file>)
151 #  option --setenv bindir set the directory containing the binary
152 #         --setenv srcdir set the directory containing the source file
153 #         --cd set the working directory
154 ADD_TEST(my-test-name ${CMAKE_BINARY_DIR}/bin/tesh 
155          --setenv bindir=${CMAKE_BINARY_DIR}/examples/my-test/
156          --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/my-test/
157          --cd ${CMAKE_HOME_DIRECTORY}/examples/my-test/
158          ${CMAKE_HOME_DIRECTORY}/examples/msg/io/io.tesh
159 )
160 \endverbatim             
161
162 As usual, you must run "make distcheck" after modifying the cmake files,
163 to ensure that you did not forget any files in the distributed archive.
164
165 \section inside_tests_ci Continous Integration
166
167 We use several systems to automatically test SimGrid with a large set
168 of parameters, across as many platforms as possible. 
169 We use <a href="https://ci.inria.fr/simgrid/">Jenkins on Inria
170 servers</a> as a workhorse: it runs all of our tests for many
171 configurations. It takes a long time to answer, and it often reports
172 issues but when it's green, then you know that SimGrid is very fit!
173 We use <a href="https://travis-ci.org/mquinson/simgrid">Travis</a> to
174 quickly run some tests on Linux and Mac. It answers quickly but may
175 miss issues. And we use <a href="https://ci.appveyor.com/project/mquinson/simgrid">AppVeyor</a>
176 to build and somehow test SimGrid on windows. 
177
178 \subsection inside_tests_jenkins Jenkins on the Inria CI servers
179
180 You should not have to change the configuration of the Jenkins tool
181 yourself, although you could have to change the slaves' configuration
182 using the <a href="https://ci.inria.fr">CI interface of INRIA</a> --
183 refer to the <a href="https://wiki.inria.fr/ciportal/">CI documentation</a>.
184
185 The result can be seen here: https://ci.inria.fr/simgrid/
186
187 We have 3 projects on Jenkins:
188 \li <a href="https://ci.inria.fr/simgrid/job/SimGrid-Multi/">SimGrid-Multi</a>
189     is the main project, running the tests that we spoke about.\n It is
190     configured (on Jenkins) to run the script <tt>tools/jenkins/build.sh</tt>
191 \li <a href="https://ci.inria.fr/simgrid/job/SimGrid-DynamicAnalysis/">SimGrid-DynamicAnalysis</a>
192     runs the tests both under valgrind to find the memory errors and
193     under gcovr to report the achieved test coverage.\n It is configured
194     (on Jenkins) to run the script <tt>tools/jenkins/DynamicAnalysis.sh</tt>
195 \li <a href="https://ci.inria.fr/simgrid/job/SimGrid-Windows/">SimGrid-Windows</a>
196     is an ongoing attempt to get Windows tested on Jenkins too.
197
198 In each case, SimGrid gets built in
199 /builds/workspace/$PROJECT/build_mode/$CONFIG/label/$SERVER/build 
200 with $PROJECT being for instance "SimGrid-Multi", $CONFIG "DEBUG" or
201 "ModelChecker" and $SERVER for instance "simgrid-fedora20-64-clang".
202
203 If some configurations are known to fail on some systems (such as
204 model-checking on non-linux systems), go to your Project and click on
205 "Configuration". There, find the field "combination filter" (if your
206 interface language is English) and tick the checkbox; then add a
207 groovy-expression to disable a specific configuration. For example, in
208 order to disable the "ModelChecker" build on host
209 "small-freebsd-64-clang", use:
210
211 \verbatim
212 (label=="small-freebsd-64-clang").implies(build_mode!="ModelChecker")
213 \endverbatim
214
215 \subsection inside_tests_travis Travis
216
217 Travis is a free (as in free beer) Continuous Integration system that
218 open-sourced project can use freely. It is very well integrated in the
219 GitHub ecosystem. There is a plenty of documentation out there. Our
220 configuration is in the file .travis.yml as it should be, and the
221 result is here: https://travis-ci.org/mquinson/simgrid
222
223 \subsection inside_tests_appveyor AppVeyor
224
225 AppVeyor aims at becoming the Travis of Windows. It is maybe less
226 mature than Travis, or maybe it is just that I'm less trained in
227 Windows. Our configuration is in the file appveyor.yml as it should
228 be, and the result is here: https://ci.appveyor.com/project/mquinson/simgrid
229
230 It should be noted that I miserably failed to use the environment
231 provided by AppVeyor, since SimGrid does not build with Microsoft
232 Visual Studio. Instead, we download a whole development environment
233 from the internet at each build. That's an archive of already compiled
234 binaries that are unpacked on the appveyor systems each time we start.
235 We re-use the ones from the 
236 <a href="https://github.com/symengine/symengine">symengine</a>
237 project. Thanks to them for compiling sane tools and constituting that
238 archive, it saved my mind! 
239
240 */