/
cli_spec.rb
276 lines (219 loc) · 8.73 KB
/
cli_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
require 'spec_helper'
require 'tempfile'
RSpec.describe PushmiPullyu::CLI do
let(:cli) { PushmiPullyu::CLI.instance }
describe '#run' do
before do
allow(cli).to receive(:start_server)
allow(cli).to receive(:start_server_as_daemon)
end
it 'starts working loop' do
cli.run
expect(cli).to have_received(:start_server).once
expect(cli).not_to have_received(:start_server_as_daemon)
end
context 'should run as daemon' do
it 'starts working loop as daemon' do
PushmiPullyu.options[:daemonize] = true
cli.run
expect(cli).to have_received(:start_server_as_daemon).once
expect(cli).not_to have_received(:start_server)
end
end
context 'Rollbar' do
it 'sets up Rollbar' do
PushmiPullyu.options[:rollbar][:token] = 'xyzzy'
cli.run
expect(Rollbar.configuration.access_token).to eq 'xyzzy'
end
it 'sets up Proxy for Rollbar' do
PushmiPullyu.options[:rollbar][:proxy_host] = 'your_proxy_host_url'
PushmiPullyu.options[:rollbar][:proxy_port] = '80'
PushmiPullyu.options[:rollbar][:proxy_user] = 'dummy_admin'
PushmiPullyu.options[:rollbar][:proxy_password] = 'securepassword'
cli.run
expect(Rollbar.configuration.proxy[:host]).to eq 'your_proxy_host_url'
expect(Rollbar.configuration.proxy[:port]).to eq '80'
expect(Rollbar.configuration.proxy[:user]).to eq 'dummy_admin'
expect(Rollbar.configuration.proxy[:password]).to eq 'securepassword'
end
end
end
describe '#start_server' do
it 'starts run tick loop' do
allow(cli).to receive(:setup_signal_traps)
allow(cli).to receive(:setup_log)
allow(cli).to receive(:print_banner)
allow(cli).to receive(:run_tick_loop)
cli.start_server
expect(cli).to have_received(:setup_signal_traps).once
expect(cli).to have_received(:setup_log).once
expect(cli).to have_received(:print_banner).once
expect(cli).to have_received(:run_tick_loop).once
end
end
describe 'shutdown handling' do
after do
PushmiPullyu.server_running = true
end
it 'prints a message the first time and exits on second time' do
allow(cli).to receive(:exit!)
allow(cli).to receive(:warn)
expect { cli.send(:shutdown) }.to change { PushmiPullyu.server_running? }.from(true).to(false)
expect(cli).not_to have_received(:exit!)
expect(cli).to have_received(:warn).with('Exiting... Interrupt again to force quit.').once
cli.send(:shutdown)
expect(cli).to have_received(:warn).with('Exiting... Interrupt again to force quit.').once
expect(cli).to have_received(:exit!)
end
end
describe '#parse' do
let!(:opts) { PushmiPullyu.options.dup }
after do
PushmiPullyu.override_options(opts)
end
it 'initializes setup' do
allow(cli).to receive(:parse_options).and_return({})
allow(cli).to receive(:parse_config)
cli.parse
expect(cli).to have_received(:parse_options).once
expect(cli).not_to have_received(:parse_config)
end
it 'initialize setup with config yaml if given' do
allow(cli).to receive(:parse_options).and_return(config_file: 'config/pushmi_pullyu.yml')
allow(cli).to receive(:parse_config).and_return({})
cli.parse
expect(cli).to have_received(:parse_options).once
expect(cli).to have_received(:parse_config).once
end
describe 'with cli options' do
it 'sets to debug mode' do
cli.parse(['--debug'])
expect(PushmiPullyu.options[:debug]).to be_truthy
end
it 'sets up Rollbar integration' do
cli.parse(['-r', 'asdfjkl11234eieio'])
expect(PushmiPullyu.options[:rollbar][:token]).to eq 'asdfjkl11234eieio'
end
it 'sets logdir' do
cli.parse(['-L', 'tmp/log'])
expect(PushmiPullyu.options[:logdir]).to eq 'tmp/log'
expect(PushmiPullyu.application_log_file).to eq 'tmp/log/pushmi_pullyu.log'
end
it 'sets piddir' do
cli.parse(['-D', 'dir/pids'])
expect(PushmiPullyu.options[:piddir]).to eq 'dir/pids'
end
it 'sets process_name' do
cli.parse(['-N', 'pmpy'])
expect(PushmiPullyu.options[:process_name]).to eq 'pmpy'
end
it 'sets monitor' do
cli.parse(['-m'])
expect(PushmiPullyu.options[:monitor]).to be_truthy
end
it 'sets minimum-age' do
cli.parse(['-a', '900'])
expect(PushmiPullyu.options[:minimum_age]).to be 900.0
end
it 'sets queue' do
cli.parse(['-q', 'test:pmpy:queue'])
expect(PushmiPullyu.options[:queue_name]).to eq 'test:pmpy:queue'
end
it 'sets workdir' do
cli.parse(['-W', '/path/to/workdir'])
expect(PushmiPullyu.options[:workdir]).to eq '/path/to/workdir'
end
end
describe 'with commands' do
it 'sets daemonize on start' do
cli.parse(['start'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
end
it 'sets daemonize on stop' do
cli.parse(['stop'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
end
it 'sets daemonize on restart' do
cli.parse(['restart'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
end
it 'sets daemonize on reload' do
cli.parse(['reload'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
end
it 'sets daemonize on run' do
cli.parse(['run'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
end
it 'sets daemonize on zap' do
cli.parse(['zap'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
end
it 'sets daemonize on status' do
cli.parse(['status'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
end
end
describe 'config file' do
it 'parses as expected' do
cli.parse(['-C', 'spec/fixtures/config.yml'])
expect(PushmiPullyu.options[:config_file]).to eq 'spec/fixtures/config.yml'
expect(PushmiPullyu.options[:logdir]).to eq 'tmp/spec/log'
expect(PushmiPullyu.application_log_file).to eq 'tmp/spec/log/test_pushmi_pullyu.log'
expect(PushmiPullyu.options[:piddir]).to eq 'tmp/spec/pids'
expect(PushmiPullyu.options[:process_name]).to eq 'test_pushmi_pullyu'
expect(PushmiPullyu.options[:minimum_age]).to be 1
expect(PushmiPullyu.options[:queue_name]).to eq 'test:pmpy_queue'
expect(PushmiPullyu.options[:swift][:auth_url]).to eq 'http://example.com:8080/auth/v1.0'
expect(PushmiPullyu.options[:rollbar][:token]).to eq 'abc123xyz'
end
it 'still allows command line arguments to take precedence' do
cli.parse(['start',
'-C', 'spec/fixtures/config.yml',
'--logdir', 'path/to/random',
'--minimum-age', '5',
'--piddir', 'path/to/piddir'])
expect(PushmiPullyu.options[:daemonize]).to be_truthy
expect(PushmiPullyu.options[:config_file]).to eq 'spec/fixtures/config.yml'
expect(PushmiPullyu.options[:debug]).to be_truthy
expect(PushmiPullyu.options[:logdir]).to eq 'path/to/random'
expect(PushmiPullyu.application_log_file).to eq 'path/to/random/test_pushmi_pullyu.log'
expect(PushmiPullyu.options[:piddir]).to eq 'path/to/piddir'
expect(PushmiPullyu.options[:process_name]).to eq 'test_pushmi_pullyu'
expect(PushmiPullyu.options[:minimum_age]).to be 5.0
end
end
describe 'env based config file' do
it 'parses as expected' do
cli.parse(['-C', 'spec/fixtures/config_with_envs.yml'])
expect(PushmiPullyu.options[:config_file]).to eq 'spec/fixtures/config_with_envs.yml'
expect(PushmiPullyu.options[:debug]).to be_truthy
expect(PushmiPullyu.options[:queue_name]).to eq 'erb_test:pmpy_queue'
expect(PushmiPullyu.options[:redis][:url]).to eq 'redis://192.168.77.11:9999'
end
end
describe 'an empty config file' do
let!(:tmp_file) { Tempfile.new('pmpy-test') }
let!(:tmp_path) { tmp_file.path }
before do
# deletes the file
tmp_file.close!
end
after do
# double makes sure file is deleted
File.unlink tmp_path if File.exist? tmp_path
end
it 'results in an identical options hash, except for config_file' do
cli.parse([])
old_options = PushmiPullyu.options.clone
cli.parse(['-C', tmp_path])
expect(tmp_path).to eq PushmiPullyu.options[:config_file]
new_options = PushmiPullyu.options.clone
expect(old_options).not_to eq new_options
new_options.delete(:config_file)
expect(old_options).to eq new_options
end
end
end
end