/puphpet/puppet/modules/mysql/spec/classes/mysql_server_backup_spec.rb

https://gitlab.com/renatoruk/twentypress · Ruby · 405 lines · 377 code · 26 blank · 2 comment · 0 complexity · 1c01eab1df2898cb77ed87b080329b1d MD5 · raw file

  1. require 'spec_helper'
  2. describe 'mysql::server::backup' do
  3. on_supported_os.each do |os, facts|
  4. context "on #{os}" do
  5. let(:facts) {
  6. facts.merge({
  7. :root_home => '/root',
  8. })
  9. }
  10. let(:default_params) {
  11. { 'backupuser' => 'testuser',
  12. 'backuppassword' => 'testpass',
  13. 'backupdir' => '/tmp',
  14. 'backuprotate' => '25',
  15. 'delete_before_dump' => true,
  16. 'execpath' => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin',
  17. }
  18. }
  19. context 'standard conditions' do
  20. let(:params) { default_params }
  21. # Cannot use that_requires here, doesn't work on classes.
  22. it { is_expected.to contain_mysql_user('testuser@localhost').with(
  23. :require => 'Class[Mysql::Server::Root_password]') }
  24. it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with(
  25. :privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS']
  26. ).that_requires('Mysql_user[testuser@localhost]') }
  27. context 'with triggers included' do
  28. let(:params) do
  29. { :include_triggers => true }.merge(default_params)
  30. end
  31. it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with(
  32. :privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS', 'TRIGGER']
  33. ).that_requires('Mysql_user[testuser@localhost]') }
  34. end
  35. it { is_expected.to contain_cron('mysql-backup').with(
  36. :command => '/usr/local/sbin/mysqlbackup.sh',
  37. :ensure => 'present'
  38. )}
  39. it { is_expected.to contain_file('mysqlbackup.sh').with(
  40. :path => '/usr/local/sbin/mysqlbackup.sh',
  41. :ensure => 'present'
  42. ) }
  43. it { is_expected.to contain_file('mysqlbackupdir').with(
  44. :path => '/tmp',
  45. :ensure => 'directory'
  46. )}
  47. it 'should have compression by default' do
  48. is_expected.to contain_file('mysqlbackup.sh').with_content(
  49. /bzcat -zc/
  50. )
  51. end
  52. it 'should skip backing up events table by default' do
  53. is_expected.to contain_file('mysqlbackup.sh').with_content(
  54. /ADDITIONAL_OPTIONS="--ignore-table=mysql.event"/
  55. )
  56. end
  57. it 'should not mention triggers by default because file_per_database is false' do
  58. is_expected.to contain_file('mysqlbackup.sh').without_content(
  59. /.*triggers.*/
  60. )
  61. end
  62. it 'should not mention routines by default because file_per_database is false' do
  63. is_expected.to contain_file('mysqlbackup.sh').without_content(
  64. /.*routines.*/
  65. )
  66. end
  67. it 'should have 25 days of rotation' do
  68. # MySQL counts from 0
  69. is_expected.to contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/)
  70. end
  71. it 'should have a standard PATH' do
  72. is_expected.to contain_file('mysqlbackup.sh').with_content(%r{PATH=/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin})
  73. end
  74. end
  75. context 'custom ownership and mode for backupdir' do
  76. let(:params) do
  77. { :backupdirmode => '0750',
  78. :backupdirowner => 'testuser',
  79. :backupdirgroup => 'testgrp',
  80. }.merge(default_params)
  81. end
  82. it { is_expected.to contain_file('mysqlbackupdir').with(
  83. :path => '/tmp',
  84. :ensure => 'directory',
  85. :mode => '0750',
  86. :owner => 'testuser',
  87. :group => 'testgrp'
  88. ) }
  89. end
  90. context 'with compression disabled' do
  91. let(:params) do
  92. { :backupcompress => false }.merge(default_params)
  93. end
  94. it { is_expected.to contain_file('mysqlbackup.sh').with(
  95. :path => '/usr/local/sbin/mysqlbackup.sh',
  96. :ensure => 'present'
  97. ) }
  98. it 'should be able to disable compression' do
  99. is_expected.to contain_file('mysqlbackup.sh').without_content(
  100. /.*bzcat -zc.*/
  101. )
  102. end
  103. end
  104. context 'with mysql.events backedup' do
  105. let(:params) do
  106. { :ignore_events => false }.merge(default_params)
  107. end
  108. it { is_expected.to contain_file('mysqlbackup.sh').with(
  109. :path => '/usr/local/sbin/mysqlbackup.sh',
  110. :ensure => 'present'
  111. ) }
  112. it 'should be able to backup events table' do
  113. is_expected.to contain_file('mysqlbackup.sh').with_content(
  114. /ADDITIONAL_OPTIONS="--events"/
  115. )
  116. end
  117. end
  118. context 'with database list specified' do
  119. let(:params) do
  120. { :backupdatabases => ['mysql'] }.merge(default_params)
  121. end
  122. it { is_expected.to contain_file('mysqlbackup.sh').with(
  123. :path => '/usr/local/sbin/mysqlbackup.sh',
  124. :ensure => 'present'
  125. )
  126. }
  127. it 'should have a backup file for each database' do
  128. is_expected.to contain_file('mysqlbackup.sh').with_content(
  129. /mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date'/
  130. )
  131. end
  132. it 'should skip backup triggers by default' do
  133. is_expected.to contain_file('mysqlbackup.sh').with_content(
  134. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/
  135. )
  136. end
  137. it 'should skip backing up routines by default' do
  138. is_expected.to contain_file('mysqlbackup.sh').with_content(
  139. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
  140. )
  141. end
  142. context 'with include_triggers set to true' do
  143. let(:params) do
  144. default_params.merge({
  145. :backupdatabases => ['mysql'],
  146. :include_triggers => true
  147. })
  148. end
  149. it 'should backup triggers when asked' do
  150. is_expected.to contain_file('mysqlbackup.sh').with_content(
  151. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/
  152. )
  153. end
  154. end
  155. context 'with include_triggers set to false' do
  156. let(:params) do
  157. default_params.merge({
  158. :backupdatabases => ['mysql'],
  159. :include_triggers => false
  160. })
  161. end
  162. it 'should skip backing up triggers when asked to skip' do
  163. is_expected.to contain_file('mysqlbackup.sh').with_content(
  164. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/
  165. )
  166. end
  167. end
  168. context 'with include_routines set to true' do
  169. let(:params) do
  170. default_params.merge({
  171. :backupdatabases => ['mysql'],
  172. :include_routines => true
  173. })
  174. end
  175. it 'should backup routines when asked' do
  176. is_expected.to contain_file('mysqlbackup.sh').with_content(
  177. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/
  178. )
  179. end
  180. end
  181. context 'with include_routines set to false' do
  182. let(:params) do
  183. default_params.merge({
  184. :backupdatabases => ['mysql'],
  185. :include_triggers => true
  186. })
  187. end
  188. it 'should skip backing up routines when asked to skip' do
  189. is_expected.to contain_file('mysqlbackup.sh').with_content(
  190. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
  191. )
  192. end
  193. end
  194. end
  195. context 'with file per database' do
  196. let(:params) do
  197. default_params.merge({ :file_per_database => true })
  198. end
  199. it 'should loop through backup all databases' do
  200. is_expected.to contain_file('mysqlbackup.sh').with_content(/.*SHOW DATABASES.*/)
  201. end
  202. context 'with compression disabled' do
  203. let(:params) do
  204. default_params.merge({ :file_per_database => true, :backupcompress => false })
  205. end
  206. it 'should loop through backup all databases without compression' do
  207. is_expected.to contain_file('mysqlbackup.sh').with_content(
  208. /.*SHOW DATABASES.*/
  209. )
  210. is_expected.to contain_file('mysqlbackup.sh').without_content(
  211. /.*bzcat -zc.*/
  212. )
  213. end
  214. end
  215. it 'should skip backup triggers by default' do
  216. is_expected.to contain_file('mysqlbackup.sh').with_content(
  217. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/
  218. )
  219. end
  220. it 'should skip backing up routines by default' do
  221. is_expected.to contain_file('mysqlbackup.sh').with_content(
  222. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
  223. )
  224. end
  225. context 'with include_triggers set to true' do
  226. let(:params) do
  227. default_params.merge({
  228. :file_per_database => true,
  229. :include_triggers => true
  230. })
  231. end
  232. it 'should backup triggers when asked' do
  233. is_expected.to contain_file('mysqlbackup.sh').with_content(
  234. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/
  235. )
  236. end
  237. end
  238. context 'with include_triggers set to false' do
  239. let(:params) do
  240. default_params.merge({
  241. :file_per_database => true,
  242. :include_triggers => false
  243. })
  244. end
  245. it 'should skip backing up triggers when asked to skip' do
  246. is_expected.to contain_file('mysqlbackup.sh').with_content(
  247. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/
  248. )
  249. end
  250. end
  251. context 'with include_routines set to true' do
  252. let(:params) do
  253. default_params.merge({
  254. :file_per_database => true,
  255. :include_routines => true
  256. })
  257. end
  258. it 'should backup routines when asked' do
  259. is_expected.to contain_file('mysqlbackup.sh').with_content(
  260. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/
  261. )
  262. end
  263. end
  264. context 'with include_routines set to false' do
  265. let(:params) do
  266. default_params.merge({
  267. :file_per_database => true,
  268. :include_triggers => true
  269. })
  270. end
  271. it 'should skip backing up routines when asked to skip' do
  272. is_expected.to contain_file('mysqlbackup.sh').with_content(
  273. /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
  274. )
  275. end
  276. end
  277. end
  278. context 'with postscript' do
  279. let(:params) do
  280. default_params.merge({ :postscript => 'rsync -a /tmp backup01.local-lan:' })
  281. end
  282. it 'should be add postscript' do
  283. is_expected.to contain_file('mysqlbackup.sh').with_content(
  284. /rsync -a \/tmp backup01.local-lan:/
  285. )
  286. end
  287. end
  288. context 'with postscripts' do
  289. let(:params) do
  290. default_params.merge({ :postscript => [
  291. 'rsync -a /tmp backup01.local-lan:',
  292. 'rsync -a /tmp backup02.local-lan:',
  293. ]})
  294. end
  295. it 'should be add postscript' do
  296. is_expected.to contain_file('mysqlbackup.sh').with_content(
  297. /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/
  298. )
  299. end
  300. end
  301. context 'with the xtrabackup provider' do
  302. let(:params) do
  303. default_params.merge({:provider => 'xtrabackup'})
  304. end
  305. it 'should contain the wrapper script' do
  306. is_expected.to contain_file('xtrabackup.sh').with_content(
  307. /^innobackupex\s+"\$@"/
  308. )
  309. end
  310. context 'with prescript defined' do
  311. let(:params) do
  312. default_params.merge({
  313. :provider => 'xtrabackup',
  314. :prescript => [
  315. 'rsync -a /tmp backup01.local-lan:',
  316. 'rsync -a /tmp backup02.local-lan:',
  317. ]
  318. })
  319. end
  320. it 'should contain the prescript' do
  321. is_expected.to contain_file('xtrabackup.sh').with_content(
  322. /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/
  323. )
  324. end
  325. end
  326. context 'with postscript defined' do
  327. let(:params) do
  328. default_params.merge({
  329. :provider => 'xtrabackup',
  330. :postscript => [
  331. 'rsync -a /tmp backup01.local-lan:',
  332. 'rsync -a /tmp backup02.local-lan:',
  333. ]
  334. })
  335. end
  336. it 'should contain the prostscript' do
  337. is_expected.to contain_file('xtrabackup.sh').with_content(
  338. /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/
  339. )
  340. end
  341. end
  342. end
  343. end
  344. end
  345. end