unit-tests.dox 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. /**
  7. @page unitTests Building Kea with Unit Tests
  8. @section unitTestsIntroduction Introduction
  9. Kea uses the Google C++ Testing Framework (also called googletest or gtest) as a
  10. base for our C++ unit-tests. See http://code.google.com/p/googletest/ for
  11. details. We used to have Python unit-tests that were inherited from BIND10
  12. days. Those tests are removed now, so please do not develop any new Python
  13. tests in Kea. If you want to write DHCP tests in Python, we encourage you to
  14. take a look at ISC Forge: http://kea.isc.org/wiki/IscForge. You must have \c
  15. gtest installed or at least extracted in a directory before compiling Kea
  16. unit-tests. To enable unit-tests in Kea, use:
  17. @code
  18. ./configure --with-gtest=/path/to/your/gtest/dir
  19. @endcode
  20. or
  21. @code
  22. ./configure --with-gtest-source=/path/to/your/gtest/dir
  23. @endcode
  24. Depending on how you compiled or installed \c gtest (e.g. from sources
  25. or using some package management system) one of those two switches will
  26. find \c gtest. After that you make run unit-tests:
  27. @code
  28. make check
  29. @endcode
  30. @section unitTestsEnvironmentVariables Environment Variables
  31. The following environment variable can affect unit-tests:
  32. - KEA_LOCKFILE_DIR - Specifies a directory where the logging system should
  33. create its lock file. If not specified, it is prefix/var/run/kea, where prefix
  34. defaults to /usr/local. This variable must not end with a slash. There is one
  35. special value: "none", which instructs Kea to not create lock file at
  36. all. This may cause issues if several processes log to the same file.
  37. Also see Kea User's Guide, section 15.3.
  38. - KEA_LOGGER_DESTINATION - Specifies logging destination. If not set, logged
  39. messages will not be recorded anywhere. There are 3 special values:
  40. stdout, stderr and syslog. Any other value is interpreted as a filename.
  41. Also see Kea User's Guide, section 15.3.
  42. - KEA_PIDFILE_DIR - Specifies the directory which should be used for PID files
  43. as used by dhcp::Daemon or its derivatives. If not specified, the default is
  44. prefix/var/run/kea, where prefix defaults to /usr/local. This variable must
  45. not end with a slash.
  46. - KEA_SOCKET_TEST_DIR - if set, it specifies the directory where Unix
  47. sockets are created. There's OS limitation on how long a Unix socket
  48. path can be. It is typcially slightly over 100 characters. If you
  49. happen to build and run unit-tests in deeply nested directories, this
  50. may become a problem. KEA_SOCKET_TEST_DIR can be specified to instruct
  51. unit-test to use a different directory. Must not end with slash (e.g.
  52. /tmp).
  53. @section unitTestsDatabaseConfig Databases Configuration for Unit Tests
  54. With the use of databases requiring separate authorisation, there are
  55. certain database-specific pre-requisites for successfully running the unit
  56. tests. These are listed in the following sections.
  57. @subsection unitTestsDatabaseUsers Database Users Required for Unit Tests
  58. Unit tests validating database backends require that <i>keatest</i> database
  59. is created. This database should be empty (should not include any relations).
  60. Unit tests will create required tables for each test case, and drop these tables
  61. when the test case ends. The unit tests also require that <i>keatest</i> user
  62. is created and that this user is configured to access <i>keatest</i>
  63. database with a <i>keatest</i> password.
  64. Unit tests use these credentials to create database schema, run test cases
  65. and drop the schema. Thus, the <i>keatest</i> user must have sufficiently
  66. high privileges to create and drop tables, as well as insert and modify the
  67. data within those tables.
  68. The database backends, which support read only access to the host reservations
  69. databases (currently MySQL and PostgreSQL), include unit tests verifying that
  70. a database user, with read-only privileges, can be used to retrieve host
  71. reservations. Those tests require that a user <i>keatest_readonly</i>, with
  72. SQL SELECT privilege to the <i>keatest</i> database (without INSERT, UPDATE etc.),
  73. is also created.
  74. The following sections provide step-by-step guidelines how to setup the
  75. databases for running unit tests.
  76. @subsection mysqlUnitTestsPrerequisites MySQL Database
  77. A database called <i>keatest</i> must be created. A database user, also called
  78. <i>keatest</i> (and with a password <i>keatest</i>) must also be created and
  79. be given full privileges in that database. The unit tests create the schema
  80. in the database before each test and delete it afterwards.
  81. In detail, the steps to create the database and user are:
  82. -# Log into MySQL as root:
  83. @verbatim
  84. % mysql -u root -p
  85. Enter password:
  86. :
  87. mysql>@endverbatim\n
  88. -# Create the test database. This must be called "keatest":
  89. @verbatim
  90. mysql> CREATE DATABASE keatest;
  91. mysql>@endverbatim\n
  92. -# Create the users under which the test client will connect to the database
  93. (the apostrophes around the words <i>keatest</i> and <i>localhost</i> are
  94. required):
  95. @verbatim
  96. mysql> CREATE USER 'keatest'@'localhost' IDENTIFIED BY 'keatest';
  97. mysql> CREATE USER 'keatest_readonly'@'localhost' IDENTIFIED BY 'keatest';
  98. mysql>@endverbatim\n
  99. -# Grant the created users permissions to access the <i>keatest</i> database
  100. (again, the apostrophes around the user names and <i>localhost</i>
  101. are required):
  102. @verbatim
  103. mysql> GRANT ALL ON keatest.* TO 'keatest'@'localhost';
  104. mysql> GRANT SELECT ON keatest.* TO 'keatest_readonly'@'localhost';
  105. mysql>@endverbatim\n
  106. -# Exit MySQL:
  107. @verbatim
  108. mysql> quit
  109. Bye
  110. %@endverbatim
  111. The unit tests are run automatically when "make check" is executed (providing
  112. that Kea has been build with the \--with-dhcp-mysql switch (see the installation
  113. section in the <a href="http://kea.isc.org/docs/kea-guide.html">Kea Administrator
  114. Reference Manual</a>).
  115. @subsection pgsqlUnitTestsPrerequisites PostgreSQL Database
  116. Conceptually, the steps required to run PostgreSQL unit-tests are the same as
  117. in MySQL. First, a database called <i>keatest</i> must be created. A database
  118. user, also called <i>keatest</i> (that will be allowed to log in using password
  119. <i>keatest</i>) must be created and given full privileges in that database. A
  120. database user, called <i>keatest_readonly</i> (using password <i>keatest</i>)
  121. must be created with SELECT privilege on all tables.
  122. The unit tests create the schema in the database before each test and delete it
  123. afterwards.
  124. PostgreSQL set up differs from system to system. Please consult your OS-specific
  125. PostgreSQL documentation. The remainder of that section uses Ubuntu 13.10 x64
  126. (with PostgreSQL 9.0+) as an example. On Ubuntu, after installing PostgreSQL
  127. (with <tt>sudo apt-get install postgresql</tt>), it is installed as user
  128. <i>postgres</i>. To create new databases or add new users, initial commands
  129. must be issued as user postgres:
  130. @verbatim
  131. $ sudo -u postgres psql postgres
  132. [sudo] password for thomson:
  133. psql (9.1.12)
  134. Type "help" for help.
  135. postgres=# CREATE USER keatest WITH PASSWORD 'keatest';
  136. CREATE ROLE
  137. postgres=# CREATE DATABASE keatest;
  138. CREATE DATABASE
  139. postgres=# GRANT ALL PRIVILEGES ON DATABASE keatest TO keatest;
  140. GRANT
  141. postgres=# \q
  142. @endverbatim
  143. PostgreSQL versions earlier than 9.0 don't provide an SQL statement for granting
  144. privileges on all tables in a database. In newer PostgreSQL versions, it is
  145. possible to grant specific privileges on all tables within a schema.
  146. However, this only affects tables which exist when the privileges are granted.
  147. To ensure that the user has specific privileges to tables dynamically created
  148. by the unit tests, the default schema privileges must be altered.
  149. The following example demonstrates how to create user <i>keatest_readonly</i>,
  150. which has SELECT privilege to the tables within the <i>keatest</i> database,
  151. in Postgres 9.0+. For earlier versions of Postgres, it is recommended to
  152. simply grant full privileges to <i>keatest_readonly</i> user, using the
  153. same steps as for the <i>keatest</i> user.
  154. @verbatim
  155. $ psql -U postgres
  156. Password for user postgres:
  157. psql (9.1.12)
  158. Type "help" for help.
  159. postgres=# CREATE USER keatest_readonly WITH PASSWORD 'keatest';
  160. CREATE ROLE
  161. postgres=# \q
  162. $ psql -U keatest
  163. Password for user keatest:
  164. psql (9.1.12)
  165. Type "help" for help.
  166. keatest=> ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES to keatest_readonly;
  167. ALTER DEFAULT PRIVILEGES
  168. keatest=> \q
  169. @endverbatim
  170. Note that <i>keatest</i> user (rather than <i>postgres</i>) is used to grant
  171. privileges to the <i>keatest_readonly</i> user. This ensures that the SELECT
  172. privilege is granted only on the tables that the <i>keatest</i> user can access
  173. within the public schema.
  174. Now we should be able to log into the newly created database using both user
  175. names:
  176. @verbatim
  177. $ psql -d keatest -U keatest
  178. Password for user keatest:
  179. psql (9.1.12)
  180. Type "help" for help.
  181. keatest=> \q
  182. $ psql -d keatest -U keatest_readonly
  183. Password for user keatest_readonly:
  184. psql (9.1.12)
  185. Type "help" for help.
  186. keatest=>
  187. @endverbatim
  188. If instead of seeing keatest=> prompt, your login will be refused with error
  189. code about failed peer or indent authentication, it means that PostgreSQL is
  190. configured to check unix username and reject login attepts if PostgreSQL names
  191. are different. To alter that, PostgreSQL configuration must be changed.
  192. <tt>/etc/postgresql/9.1/main/pg_hba.conf</tt> config file
  193. has to be tweaked. It may be in a different location in your system. The following
  194. lines:
  195. @verbatim
  196. local all all peer
  197. host all all 127.0.0.1/32 md5
  198. host all all ::1/128 md5
  199. @endverbatim
  200. were replaced with:
  201. @verbatim
  202. local all all password
  203. host all all 127.0.0.1/32 password
  204. host all all ::1/128 password
  205. @endverbatim
  206. Another possible problem is to get no password prompt, in general because
  207. you have no <tt>pg_hba.conf</tt> config file and everybody is by default
  208. trusted. As it has a very bad effect on the security you should have
  209. been warned it is a highly unsafe config. The solution is the same,
  210. i.e., require password or md5 authentication method. If you lose
  211. the postgres user access you can add first:
  212. @verbatim
  213. local all postgres trust
  214. @endverbatim
  215. to trust only the local postgres user. Note the postgres user can
  216. be pgsql on some systems.
  217. Please consult your PostgreSQL user manual before applying those changes as
  218. those changes may expose your other databases that you run on the same system.
  219. In general case, it is a poor idea to run anything of value on a system
  220. that runs tests. Use caution!
  221. The unit tests are run automatically when "make check" is executed (providing
  222. that Kea has been build with the \--with-dhcp-pgsql switch (see the installation
  223. section in the <a href="http://kea.isc.org/docs/kea-guide.html">Kea Administrator
  224. Reference Manual</a>).
  225. */