This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
/
deploy_spec.rb
227 lines (199 loc) · 8.67 KB
/
deploy_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# frozen_string_literal: true
#
# Cookbook Name:: opsworks_ruby
# Spec:: deploy
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
require 'spec_helper'
describe 'opsworks_ruby::deploy' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['scm'].delete('ssh_wrapper')
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
before do
stub_search(:aws_opsworks_app, '*:*').and_return([aws_opsworks_app])
stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([aws_opsworks_rds_db_instance])
end
it 'includes recipes' do
expect(chef_run).to include_recipe('opsworks_ruby::configure')
end
context 'DEPRECATION' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['keep_releases'] = 10
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
let(:chef_run_rhel) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['keep_releases'] = 10
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
let(:logs) { [] }
before do
allow(Chef::Log).to receive(:warn) do |message|
logs.push message
end
end
after do
expect(logs).to include(
'DEPRECATION WARNING: node[\'deploy\'][\'dummy_project\'][\'keep_releases\'] is deprecated ' \
'and will be removed. Please use node[\'deploy\'][\'dummy_project\'][\'global\'][\'keep_releases\'] instead.'
)
end
it 'debian: Shows warning' do
chef_run
end
it 'rhel: Shows warning' do
chef_run_rhel
end
end
context 'Postgresql + Git + Unicorn + Nginx + Sidekiq' do
it 'creates git wrapper script' do
expect(chef_run).to create_template('/tmp/ssh-git-wrapper.sh')
end
it 'adds and destroys ssh deploy key' do
expect(chef_run).to create_template('/tmp/.ssh-deploy-key')
expect(chef_run).to delete_file('/tmp/.ssh-deploy-key')
end
it 'performs a deploy' do
deploy = chef_run.deploy(aws_opsworks_app['shortname'])
service = chef_run.service('nginx')
expect(chef_run).to deploy_deploy('dummy_project').with(
repository: 'git@git.example.com:repo/project.git',
revision: 'master',
scm_provider: Chef::Provider::Git,
enable_submodules: false,
rollback_on_error: true,
environment: aws_opsworks_app['environment'].merge('RAILS_ENV' => 'staging'),
ssh_wrapper: '/tmp/ssh-git-wrapper.sh',
symlinks: {
'system' => 'public/system',
'assets' => 'public/assets',
'cache' => 'tmp/cache',
'pids' => 'tmp/pids',
'log' => 'log',
'test' => 'public/test'
},
'create_dirs_before_symlink' => %w(tmp public config ../../shared/cache ../../shared/assets ../shared/test),
'purge_before_symlink' => %w(log tmp/cache tmp/pids public/system public/assets public/test)
)
expect(chef_run).to run_execute('stop unicorn')
expect(chef_run).to run_execute('start unicorn')
expect(deploy).to notify('service[nginx]').to(:restart).delayed
expect(service).to do_nothing
end
it 'restarts sidekiqs via monit' do
expect(chef_run).to run_execute("monit restart sidekiq_#{aws_opsworks_app['shortname']}-1")
expect(chef_run).to run_execute("monit restart sidekiq_#{aws_opsworks_app['shortname']}-2")
end
end
context 'Puma + Apache + resque' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['appserver']['adapter'] = 'puma'
deploy['dummy_project']['webserver']['adapter'] = 'apache2'
deploy['dummy_project']['worker']['adapter'] = 'resque'
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
let(:chef_run_rhel) do
ChefSpec::SoloRunner.new(platform: 'amazon', version: '2016.03') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['appserver']['adapter'] = 'puma'
deploy['dummy_project']['webserver']['adapter'] = 'apache2'
deploy['dummy_project']['worker']['adapter'] = 'resque'
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
it 'performs a deploy on debian' do
deploy_debian = chef_run.deploy(aws_opsworks_app['shortname'])
expect(deploy_debian).to notify('service[apache2]').to(:restart).delayed
expect(chef_run).to run_execute('stop puma')
expect(chef_run).to run_execute('start puma')
end
it 'performs a deploy on rhel' do
deploy_rhel = chef_run_rhel.deploy(aws_opsworks_app['shortname'])
expect(deploy_rhel).to notify('service[httpd]').to(:restart).delayed
expect(chef_run_rhel).to run_execute('stop puma')
expect(chef_run_rhel).to run_execute('start puma')
end
it 'restarts resques via monit' do
expect(chef_run).to run_execute("monit restart resque_#{aws_opsworks_app['shortname']}-1")
expect(chef_run).to run_execute("monit restart resque_#{aws_opsworks_app['shortname']}-2")
end
end
context 'Thin + delayed_job' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['appserver']['adapter'] = 'thin'
deploy['dummy_project']['worker']['adapter'] = 'delayed_job'
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
let(:chef_run_rhel) do
ChefSpec::SoloRunner.new(platform: 'amazon', version: '2016.03') do |solo_node|
deploy = node['deploy']
deploy['dummy_project']['appserver']['adapter'] = 'thin'
deploy['dummy_project']['worker']['adapter'] = 'delayed_job'
solo_node.set['deploy'] = deploy
end.converge(described_recipe)
end
it 'performs a deploy on debian' do
expect(chef_run).to run_execute('stop thin')
expect(chef_run).to run_execute('start thin')
end
it 'performs a deploy on rhel' do
expect(chef_run_rhel).to run_execute('stop thin')
expect(chef_run_rhel).to run_execute('start thin')
end
it 'restarts delayed_jobs via monit' do
expect(chef_run).to run_execute("monit restart delayed_job_#{aws_opsworks_app['shortname']}-1")
expect(chef_run).to run_execute("monit restart delayed_job_#{aws_opsworks_app['shortname']}-2")
end
end
it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)
expect do
chef_run
end.not_to raise_error
end
it 'node[\'applications\']' do
stub_search(:aws_opsworks_app, '*:*').and_return([
aws_opsworks_app.merge(shortname: 'a1', deploy: true),
aws_opsworks_app.merge(shortname: 'a2', deploy: false),
aws_opsworks_app.merge(shortname: 'a3', deploy: true)
])
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = { 'a1' => {}, 'a2' => {}, 'a3' => {} }
solo_node.set['applications'] = %w(a1 a2)
end.converge(described_recipe)
service = chef_run.service('puma_a1')
expect(chef_run).to create_directory('/srv/www/a1/shared')
expect(chef_run).to create_directory('/srv/www/a1/shared/config')
expect(chef_run).to create_directory('/srv/www/a1/shared/log')
expect(chef_run).to create_directory('/srv/www/a1/shared/pids')
expect(chef_run).to create_directory('/srv/www/a1/shared/scripts')
expect(chef_run).to create_directory('/srv/www/a1/shared/sockets')
expect(chef_run).to create_directory('/srv/www/a1/shared/vendor/bundle')
expect(chef_run).to create_template('/srv/www/a1/shared/config/database.yml')
expect(chef_run).to create_template('/srv/www/a1/shared/config/puma.rb')
expect(chef_run).to create_template('/srv/www/a1/shared/scripts/puma.service')
expect(chef_run).to create_template('/etc/nginx/sites-available/a1.conf')
expect(chef_run).to create_link('/etc/nginx/sites-enabled/a1.conf')
expect(service).to do_nothing
expect(chef_run).to deploy_deploy('a1')
expect(chef_run).not_to deploy_deploy('a2')
end
end