unit-tests.dox 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 inherited from BIND10
  12. days but have been removed, so please do not write any new Kea unit
  13. tests in Python. (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.)
  15. You must have \c gtest installed or at least extracted in a directory
  16. before compiling Kea 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 and run the unit-tests with:
  27. @code
  28. make check
  29. @endcode
  30. @section unitTestsEnvironmentVariables Environment Variables
  31. The following environment variable can affect the unit tests:
  32. - KEA_LOCKFILE_DIR - Specifies a directory where the logging system should
  33. create its lock file. If not specified, it is <i>prefix</i>/var/run/kea,
  34. where <i>prefix</i> defaults to /usr/local. This variable must not end
  35. with a slash. There is one special value, "none", which instructs Kea to
  36. not create a lock file at all. This may cause issues if several processes
  37. log to the same file. (Also see the Kea User's Guide, section 15.3.)
  38. - KEA_LOGGER_DESTINATION - Specifies the logging destination. If not set, logged
  39. messages will not be recorded anywhere. There are three 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
  44. default is <i>prefix</i>/var/run/kea, where <i>prefix</i> defaults to
  45. /usr/local. This variable must not end with a slash.
  46. - KEA_SOCKET_TEST_DIR - if set, it specifies the directory where Unix
  47. sockets are created. There is an operating system limitation on how
  48. long a Unix socket path can be, typically slightly over 100
  49. characters. If you happen to build and run unit-tests in deeply nested
  50. directories, this may become a problem. KEA_SOCKET_TEST_DIR can be
  51. specified to instruct unit-test to use a different directory. It must
  52. not end with slash.
  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 the <i>keatest</i>
  59. database is created. This database should be empty. The unit tests
  60. also require that the <i>keatest</i> user is created and that this user
  61. is configured to access the database with a password of <i>keatest</i>.
  62. Unit tests use these credentials to create database schema, run test cases
  63. and drop the schema. Thus, the <i>keatest</i> user must have sufficiently
  64. high privileges to create and drop tables, as well as insert and modify the
  65. data within those tables.
  66. The database backends which support read only access to the host
  67. reservations databases (currently MySQL and PostgreSQL) include unit
  68. tests verifying that a database user with read-only privileges can be
  69. used to retrieve host reservations. Those tests require another user,
  70. <i>keatest_readonly</i>, with SQL SELECT privilege to the <i>keatest</i>
  71. database (i.e. without INSERT, UPDATE etc.), is also created.
  72. <i>keatest_readonly</i> should also have the password <i>keatest</i>.
  73. The following sections provide step-by-step guidelines how to setup the
  74. databases for running unit tests.
  75. @subsection mysqlUnitTestsPrerequisites MySQL Database
  76. The steps to create the database and users are:
  77. -# Log into MySQL as root:
  78. @verbatim
  79. % mysql -u root -p
  80. Enter password:
  81. :
  82. mysql>@endverbatim\n
  83. -# Create the test database. This must be called "keatest":
  84. @verbatim
  85. mysql> CREATE DATABASE keatest;
  86. mysql>@endverbatim\n
  87. -# Create the users under which the test client will connect to the database
  88. (the apostrophes around the words <i>keatest</i>, <i>keatest_readonly</i>, and
  89. <i>localhost</i> are required):
  90. @verbatim
  91. mysql> CREATE USER 'keatest'@'localhost' IDENTIFIED BY 'keatest';
  92. mysql> CREATE USER 'keatest_readonly'@'localhost' IDENTIFIED BY 'keatest';
  93. mysql>@endverbatim\n
  94. -# Grant the created users permissions to access the <i>keatest</i> database
  95. (again, the apostrophes around the user names and <i>localhost</i>
  96. are required):
  97. @verbatim
  98. mysql> GRANT ALL ON keatest.* TO 'keatest'@'localhost';
  99. mysql> GRANT SELECT ON keatest.* TO 'keatest_readonly'@'localhost';
  100. mysql>@endverbatim\n
  101. -# Exit MySQL:
  102. @verbatim
  103. mysql> quit
  104. Bye
  105. %@endverbatim
  106. The unit tests are run automatically when "make check" is executed (providing
  107. that Kea has been build with the \c --with-dhcp-mysql switch (see the installation
  108. section in the <a href="http://kea.isc.org/docs/kea-guide.html">Kea Administrator
  109. Reference Manual</a>).
  110. @subsection pgsqlUnitTestsPrerequisites PostgreSQL Database
  111. PostgreSQL set up differs from system to system. Please consult your
  112. operating system-specific PostgreSQL documentation. The remainder of
  113. that section uses Ubuntu 13.10 x64 (with PostgreSQL 9.0+) as an example.
  114. On Ubuntu, PostgreSQL is installed (with <tt>sudo apt-get install
  115. postgresql</tt>) under user <i>postgres</i>. To create new databases
  116. or add new users, initial commands must be issued under this username:
  117. @verbatim
  118. $ sudo -u postgres psql postgres
  119. [sudo] password for thomson:
  120. psql (9.1.12)
  121. Type "help" for help.
  122. postgres=# CREATE USER keatest WITH PASSWORD 'keatest';
  123. CREATE ROLE
  124. postgres=# CREATE DATABASE keatest;
  125. CREATE DATABASE
  126. postgres=# GRANT ALL PRIVILEGES ON DATABASE keatest TO keatest;
  127. GRANT
  128. postgres=# \q
  129. @endverbatim
  130. PostgreSQL versions earlier than 9.0 don't provide an SQL statement for granting
  131. privileges on all tables in a database. In newer PostgreSQL versions, it is
  132. possible to grant specific privileges on all tables within a schema.
  133. However, this only affects tables which exist when the privileges are granted.
  134. To ensure that the user has specific privileges to tables dynamically created
  135. by the unit tests, the default schema privileges must be altered.
  136. The following example demonstrates how to create the user <i>keatest_readonly</i>,
  137. which has SELECT privilege to the tables within the <i>keatest</i> database,
  138. in Postgres 9.0+. For earlier versions of Postgres, it is recommended to
  139. simply grant full privileges to <i>keatest_readonly</i>, using the
  140. same steps as for the <i>keatest</i> user.
  141. @verbatim
  142. $ psql -U postgres
  143. Password for user postgres:
  144. psql (9.1.12)
  145. Type "help" for help.
  146. postgres=# CREATE USER keatest_readonly WITH PASSWORD 'keatest';
  147. CREATE ROLE
  148. postgres=# \q
  149. $ psql -U keatest
  150. Password for user keatest:
  151. psql (9.1.12)
  152. Type "help" for help.
  153. keatest=> ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES to keatest_readonly;
  154. ALTER DEFAULT PRIVILEGES
  155. keatest=> \q
  156. @endverbatim
  157. Note that the <i>keatest</i> user (rather than <i>postgres</i>) is used to grant
  158. privileges to the <i>keatest_readonly</i> user. This ensures that the SELECT
  159. privilege is granted only on the tables that the <i>keatest</i> user can access
  160. within the public schema.
  161. Now we should be able to log into the newly created database using both user
  162. names:
  163. @verbatim
  164. $ psql -d keatest -U keatest
  165. Password for user keatest:
  166. psql (9.1.12)
  167. Type "help" for help.
  168. keatest=> \q
  169. $ psql -d keatest -U keatest_readonly
  170. Password for user keatest_readonly:
  171. psql (9.1.12)
  172. Type "help" for help.
  173. keatest=>
  174. @endverbatim
  175. If instead of seeing keatest=> prompt, your login is refused with an error
  176. code about failed peer or indent authentication, it means that PostgreSQL is
  177. configured to check unix username and reject login attempts if PostgreSQL names
  178. are different. To alter that, the PostgreSQL configuration must be changed -
  179. the <tt>/etc/postgresql/9.1/main/pg_hba.conf</tt> config file
  180. has to be altered. (It may be in a different location in your system.) The following
  181. lines:
  182. @verbatim
  183. local all all peer
  184. host all all 127.0.0.1/32 md5
  185. host all all ::1/128 md5
  186. @endverbatim
  187. need to be replaced with:
  188. @verbatim
  189. local all all password
  190. host all all 127.0.0.1/32 password
  191. host all all ::1/128 password
  192. @endverbatim
  193. Another possible problem is that you get no password prompt. This is
  194. most probably because you have no <tt>pg_hba.conf</tt> config file
  195. and everybody is by default trusted. As it has a very bad effect
  196. on the security you should have been warned this is a highly unsafe
  197. configuration. The solution is the same, i.e., require password or
  198. md5 authentication method.
  199. If you lose the postgres user access you can first add:
  200. @verbatim
  201. local all postgres trust
  202. @endverbatim
  203. to trust only the local postgres user. Note the postgres user can
  204. be pgsql on some systems.
  205. Please consult your PostgreSQL user manual before applying those changes as
  206. those changes may expose your other databases that you run on the same system.
  207. In general case, it is a poor idea to run anything of value on a system
  208. that runs tests. Use caution!
  209. The unit tests are run automatically when "make check" is executed (providing
  210. that Kea has been build with the \c --with-dhcp-pgsql switch (see the installation
  211. section in the <a href="http://kea.isc.org/docs/kea-guide.html">Kea Administrator
  212. Reference Manual</a>).
  213. */