PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/chef/recipes/gitlab-rails_spec.rb

https://gitlab.com/denkweite/omnibus-gitlab
Ruby | 301 lines | 245 code | 55 blank | 1 comment | 0 complexity | 4db1324ccb0cd6cb6180fd9e168b7250 MD5 | raw file
  1. require 'chef_helper'
  2. describe 'gitlab::gitlab-rails' do
  3. let(:chef_run) { ChefSpec::SoloRunner.new(step_into: %w(templatesymlink)).converge('gitlab::default') }
  4. before do
  5. allow(Gitlab).to receive(:[]).and_call_original
  6. # Prevent chef converge from reloading the helper library, which would override our helper stub
  7. allow(Kernel).to receive(:load).and_call_original
  8. allow(Kernel).to receive(:load).with(%r{gitlab/libraries/storage_directory_helper}).and_return(true)
  9. end
  10. context 'when manage-storage-directories is disabled' do
  11. before do
  12. stub_gitlab_rb(gitlab_rails: { shared_path: '/tmp/shared' }, manage_storage_directories: { enable: false })
  13. end
  14. it 'does not create the shared directory' do
  15. expect(chef_run).to_not run_ruby_block('directory resource: /tmp/shared')
  16. end
  17. it 'does not create the artifacts directory' do
  18. expect(chef_run).to_not run_ruby_block('directory resource: /tmp/shared/artifacts')
  19. end
  20. it 'does not create the lfs storage directory' do
  21. expect(chef_run).to_not run_ruby_block('directory resource: /tmp/shared/lfs-objects')
  22. end
  23. it 'does not create the uploads storage directory' do
  24. stub_gitlab_rb(gitlab_rails: { uploads_directory: '/tmp/uploads' })
  25. expect(chef_run).to_not run_ruby_block('directory resource: /tmp/uploads')
  26. end
  27. it 'does not create the ci builds directory' do
  28. stub_gitlab_rb(gitlab_ci: { builds_directory: '/tmp/builds' })
  29. expect(chef_run).to_not run_ruby_block('directory resource: /tmp/builds')
  30. end
  31. it 'does not create the GitLab pages directory' do
  32. expect(chef_run).to_not run_ruby_block('directory resource: /tmp/shared/pages')
  33. end
  34. end
  35. context 'when manage-storage-directories is enabled' do
  36. before do
  37. stub_gitlab_rb(gitlab_rails: { shared_path: '/tmp/shared' } )
  38. end
  39. it 'creates the shared directory' do
  40. expect(chef_run).to run_ruby_block('directory resource: /tmp/shared')
  41. end
  42. it 'creates the artifacts directory' do
  43. expect(chef_run).to run_ruby_block('directory resource: /tmp/shared/artifacts')
  44. end
  45. it 'creates the lfs storage directory' do
  46. expect(chef_run).to run_ruby_block('directory resource: /tmp/shared/lfs-objects')
  47. end
  48. it 'creates the uploads directory' do
  49. stub_gitlab_rb(gitlab_rails: { uploads_directory: '/tmp/uploads' })
  50. expect(chef_run).to run_ruby_block('directory resource: /tmp/uploads')
  51. end
  52. it 'creates the ci builds directory' do
  53. stub_gitlab_rb(gitlab_ci: { builds_directory: '/tmp/builds' })
  54. expect(chef_run).to run_ruby_block('directory resource: /tmp/builds')
  55. end
  56. it 'creates the GitLab pages directory' do
  57. expect(chef_run).to run_ruby_block('directory resource: /tmp/shared/pages')
  58. end
  59. end
  60. context 'with redis settings' do
  61. let(:config_file) { '/var/opt/gitlab/gitlab-rails/etc/resque.yml' }
  62. context 'and default configuration' do
  63. it 'creates the config file with the required redis settings' do
  64. expect(chef_run).to render_file(config_file)
  65. .with_content(%r{url: unix:/var/opt/gitlab/redis/redis.socket})
  66. end
  67. end
  68. context 'and custom configuration' do
  69. before do
  70. stub_gitlab_rb(
  71. gitlab_rails: {
  72. redis_host: 'redis.example.com',
  73. redis_port: 8888,
  74. redis_database: 2,
  75. redis_password: 'mypass'
  76. }
  77. )
  78. end
  79. it 'creates the config file with custom host, port, password and database' do
  80. expect(chef_run).to render_file(config_file)
  81. .with_content(%r{url: redis://:mypass@redis.example.com:8888/2})
  82. end
  83. end
  84. end
  85. context 'with environment variables' do
  86. context 'by default' do
  87. it_behaves_like "enabled gitlab-rails env", "HOME", '\/var\/opt\/gitlab'
  88. it_behaves_like "enabled gitlab-rails env", "RAILS_ENV", 'production'
  89. it_behaves_like "enabled gitlab-rails env", "SIDEKIQ_MEMORY_KILLER_MAX_RSS", '1000000'
  90. it_behaves_like "enabled gitlab-rails env", "BUNDLE_GEMFILE", '\/opt\/gitlab\/embedded\/service\/gitlab-rails\/Gemfile'
  91. it_behaves_like "enabled gitlab-rails env", "PATH", '\/opt\/gitlab\/bin:\/opt\/gitlab\/embedded\/bin:\/bin:\/usr\/bin'
  92. it_behaves_like "enabled gitlab-rails env", "ICU_DATA", '\/opt\/gitlab\/embedded\/share\/icu\/current'
  93. it_behaves_like "enabled gitlab-rails env", "PYTHONPATH", '\/opt\/gitlab\/embedded\/lib\/python3.4\/site-packages'
  94. it_behaves_like "enabled gitlab-rails env", "LD_PRELOAD", '\/opt\/gitlab\/embedded\/lib\/libjemalloc.so'
  95. context 'when a custom env variable is specified' do
  96. before do
  97. stub_gitlab_rb(gitlab_rails: { env: { 'IAM' => 'CUSTOMVAR'}})
  98. end
  99. it_behaves_like "enabled gitlab-rails env", "IAM", 'CUSTOMVAR'
  100. it_behaves_like "enabled gitlab-rails env", "ICU_DATA", '\/opt\/gitlab\/embedded\/share\/icu\/current'
  101. it_behaves_like "enabled gitlab-rails env", "LD_PRELOAD", '\/opt\/gitlab\/embedded\/lib\/libjemalloc.so'
  102. end
  103. end
  104. context 'when jemalloc is disabled' do
  105. before do
  106. stub_gitlab_rb(gitlab_rails: { enable_jemalloc: false })
  107. end
  108. it_behaves_like "disabled gitlab-rails env", "LD_PRELOAD", '\/opt\/gitlab\/embedded\/lib\/libjemalloc.so'
  109. end
  110. end
  111. describe "with symlinked templates" do
  112. let(:chef_run) { ChefSpec::SoloRunner.new(step_into: %w(templatesymlink)).converge('gitlab::default') }
  113. before do
  114. %w(unicorn sidekiq gitlab-workhorse postgresql redis nginx logrotate).map { |svc| stub_should_notify?(svc, true)}
  115. end
  116. describe 'database.yml' do
  117. let(:templatesymlink_template) { chef_run.template('/var/opt/gitlab/gitlab-rails/etc/database.yml') }
  118. let(:templatesymlink_link) { chef_run.link("Link /opt/gitlab/embedded/service/gitlab-rails/config/database.yml to /var/opt/gitlab/gitlab-rails/etc/database.yml") }
  119. context 'by default' do
  120. it 'creates the template' do
  121. expect(chef_run).to create_template('/var/opt/gitlab/gitlab-rails/etc/database.yml')
  122. .with(
  123. owner: 'root',
  124. group: 'root',
  125. mode: '0644',
  126. )
  127. expect(chef_run).to render_file('/var/opt/gitlab/gitlab-rails/etc/database.yml').with_content(/host: \'\/var\/opt\/gitlab\/postgresql\'/)
  128. expect(chef_run).to render_file('/var/opt/gitlab/gitlab-rails/etc/database.yml').with_content(/database: gitlabhq_production/)
  129. end
  130. it 'template triggers notifications' do
  131. expect(templatesymlink_template).to notify('service[unicorn]').to(:restart).delayed
  132. expect(templatesymlink_template).to notify('service[sidekiq]').to(:restart).delayed
  133. expect(templatesymlink_template).to_not notify('service[gitlab-workhorse]').to(:restart).delayed
  134. expect(templatesymlink_template).to_not notify('service[nginx]').to(:restart).delayed
  135. end
  136. it 'creates the symlink' do
  137. expect(chef_run).to create_link("Link /opt/gitlab/embedded/service/gitlab-rails/config/database.yml to /var/opt/gitlab/gitlab-rails/etc/database.yml")
  138. end
  139. it 'linking triggers notifications' do
  140. expect(templatesymlink_link).to notify('service[unicorn]').to(:restart).delayed
  141. expect(templatesymlink_link).to notify('service[sidekiq]').to(:restart).delayed
  142. expect(templatesymlink_link).to_not notify('service[gitlab-workhorse]').to(:restart).delayed
  143. expect(templatesymlink_link).to_not notify('service[nginx]').to(:restart).delayed
  144. end
  145. end
  146. context 'with specific database settings' do
  147. context 'when multiple postgresql listen_address is used' do
  148. before do
  149. stub_gitlab_rb(postgresql: { listen_address: "127.0.0.1,1.1.1.1" })
  150. end
  151. it 'creates the postgres configuration file with multi listen_address and database.yml file with one host' do
  152. expect(chef_run).to render_file('/var/opt/gitlab/gitlab-rails/etc/database.yml').with_content(/host: '127.0.0.1'/)
  153. expect(chef_run).to render_file('/var/opt/gitlab/postgresql/data/postgresql.conf').with_content(/listen_addresses = '127.0.0.1,1.1.1.1'/)
  154. end
  155. end
  156. context 'when no postgresql listen_address is used' do
  157. it 'creates the postgres configuration file with empty listen_address and database.yml file with default one' do
  158. expect(chef_run).to render_file('/var/opt/gitlab/gitlab-rails/etc/database.yml').with_content(/host: '\/var\/opt\/gitlab\/postgresql'/)
  159. expect(chef_run).to render_file('/var/opt/gitlab/postgresql/data/postgresql.conf').with_content(/listen_addresses = ''/)
  160. end
  161. end
  162. context 'when one postgresql listen_address is used' do
  163. before do
  164. stub_gitlab_rb(postgresql: { listen_address: "127.0.0.1" })
  165. end
  166. it 'creates the postgres configuration file with one listen_address and database.yml file with one host' do
  167. expect(chef_run).to render_file('/var/opt/gitlab/gitlab-rails/etc/database.yml').with_content(/host: '127.0.0.1'/)
  168. expect(chef_run).to render_file('/var/opt/gitlab/postgresql/data/postgresql.conf').with_content(/listen_addresses = '127.0.0.1'/)
  169. end
  170. it 'template triggers notifications' do
  171. expect(templatesymlink_template).to notify('service[unicorn]').to(:restart).delayed
  172. expect(templatesymlink_template).to notify('service[sidekiq]').to(:restart).delayed
  173. expect(templatesymlink_template).to_not notify('service[gitlab-workhorse]').to(:restart).delayed
  174. expect(templatesymlink_template).to_not notify('service[nginx]').to(:restart).delayed
  175. end
  176. it 'creates the symlink' do
  177. expect(chef_run).to create_link("Link /opt/gitlab/embedded/service/gitlab-rails/config/database.yml to /var/opt/gitlab/gitlab-rails/etc/database.yml")
  178. end
  179. it 'linking triggers notifications' do
  180. expect(templatesymlink_link).to notify('service[unicorn]').to(:restart).delayed
  181. expect(templatesymlink_link).to notify('service[sidekiq]').to(:restart).delayed
  182. expect(templatesymlink_link).to_not notify('service[gitlab-workhorse]').to(:restart).delayed
  183. expect(templatesymlink_link).to_not notify('service[nginx]').to(:restart).delayed
  184. end
  185. end
  186. end
  187. end
  188. describe 'gitlab_workhorse_secret' do
  189. let(:templatesymlink_template) { chef_run.template('/var/opt/gitlab/gitlab-rails/etc/gitlab_workhorse_secret') }
  190. let(:templatesymlink_link) { chef_run.link("Link /opt/gitlab/embedded/service/gitlab-rails/.gitlab_workhorse_secret to /var/opt/gitlab/gitlab-rails/etc/gitlab_workhorse_secret") }
  191. context 'by default' do
  192. it 'creates the template' do
  193. expect(chef_run).to create_template('/var/opt/gitlab/gitlab-rails/etc/gitlab_workhorse_secret')
  194. .with(
  195. owner: 'root',
  196. group: 'root',
  197. mode: '0644',
  198. )
  199. end
  200. it 'template triggers notifications' do
  201. expect(templatesymlink_template).to notify('service[gitlab-workhorse]').to(:restart).delayed
  202. expect(templatesymlink_template).to notify('service[unicorn]').to(:restart).delayed
  203. expect(templatesymlink_template).to notify('service[sidekiq]').to(:restart).delayed
  204. end
  205. it 'creates the symlink' do
  206. expect(chef_run).to create_link("Link /opt/gitlab/embedded/service/gitlab-rails/.gitlab_workhorse_secret to /var/opt/gitlab/gitlab-rails/etc/gitlab_workhorse_secret")
  207. end
  208. it 'linking triggers notifications' do
  209. expect(templatesymlink_link).to notify('service[gitlab-workhorse]').to(:restart).delayed
  210. expect(templatesymlink_link).to notify('service[unicorn]').to(:restart).delayed
  211. expect(templatesymlink_link).to notify('service[sidekiq]').to(:restart).delayed
  212. end
  213. end
  214. context 'with specific gitlab_workhorse_secret' do
  215. before do
  216. stub_gitlab_rb(gitlab_workhorse: { secret_token: 'abc123-gitlab-workhorse' })
  217. end
  218. it 'renders the correct node attribute' do
  219. expect(chef_run).to render_file('/var/opt/gitlab/gitlab-rails/etc/gitlab_workhorse_secret')
  220. .with_content('abc123-gitlab-workhorse')
  221. end
  222. it 'uses the correct owner and permissions' do
  223. expect(chef_run).to create_template('/var/opt/gitlab/gitlab-rails/etc/gitlab_workhorse_secret')
  224. .with(
  225. owner: 'root',
  226. group: 'root',
  227. mode: '0644',
  228. )
  229. end
  230. it 'template triggers notifications' do
  231. expect(templatesymlink_template).to notify('service[gitlab-workhorse]').to(:restart).delayed
  232. expect(templatesymlink_template).to notify('service[unicorn]').to(:restart).delayed
  233. expect(templatesymlink_template).to notify('service[sidekiq]').to(:restart).delayed
  234. end
  235. it 'creates the symlink' do
  236. expect(chef_run).to create_link("Link /opt/gitlab/embedded/service/gitlab-rails/.gitlab_workhorse_secret to /var/opt/gitlab/gitlab-rails/etc/gitlab_workhorse_secret")
  237. end
  238. it 'linking triggers notifications' do
  239. expect(templatesymlink_link).to notify('service[gitlab-workhorse]').to(:restart).delayed
  240. expect(templatesymlink_link).to notify('service[unicorn]').to(:restart).delayed
  241. expect(templatesymlink_link).to notify('service[sidekiq]').to(:restart).delayed
  242. end
  243. end
  244. end
  245. end
  246. end