-
Notifications
You must be signed in to change notification settings - Fork 27
/
Rakefile
336 lines (272 loc) · 9.53 KB
/
Rakefile
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# encoding: utf-8
require '3scale/backend/configuration'
require '3scale/backend'
require '3scale/tasks/helpers'
include ThreeScale::Tasks::Helpers
load 'lib/3scale/tasks/connectivity.rake'
require '3scale/backend/logging/external'
ThreeScale::Backend::Logging::External.setup_rake
load 'lib/3scale/tasks/swagger.rake'
if Environment.testable?
ENV['RACK_ENV'] = "test"
require 'rake/testtask'
task :default => [:test, :spec]
test_task_dependencies = ['test:unit', 'test:integration']
desc 'Run unit and integration tests'
task :test => test_task_dependencies
desc 'Benchmark'
task :bench, [:file] do |_, args|
require 'benchmark'
require 'benchmark/ips'
require 'pathname'
require_relative 'test/test_helpers/configuration'
require_relative 'test/test_helpers/fixtures'
require_relative 'test/test_helpers/sequences.rb'
filelist = if args[:file]
"#{args[:file].sub(/\Abench\//, '')}"
else
'**/*_bench.rb'
end
FileList["#{File.dirname(__FILE__)}/bench/#{filelist}"].each do |f|
bench = Pathname.new(f).cleanpath
if bench.to_s.start_with?(File.dirname(__FILE__) + File::SEPARATOR)
puts "Running benchmark #{bench}"
Environment.using_async_redis? ? Sync { load(f) } : load(f)
else
STDERR.puts "Ignoring path #{f} as it points outside the project"
end
end
puts "Benchmarks finished"
end
namespace :test do
Rake::TestTask.new(:unit) do |task|
task.test_files = FileList['test/unit/**/*_test.rb']
task.verbose = true
task.warning = false
end
Rake::TestTask.new(:integration) do |task|
task.test_files = FileList['test/integration/**/*_test.rb']
task.verbose = true
task.warning = false
end
end
require 'rspec/core/rake_task'
spec_task_dependencies = [
'spec:unit', 'spec:integration', 'spec:acceptance', 'spec:api', 'spec:use_cases', 'spec:server'
]
desc 'Run RSpec tests'
task :spec => spec_task_dependencies
namespace :spec do
require 'rspec/core/rake_task'
desc 'Run all RSpec tests (unit, integration, acceptance, api, use_cases, server)'
task :all => spec_task_dependencies
desc 'Run RSpec unit tests'
RSpec::Core::RakeTask.new(:unit) do |task|
task.pattern = 'spec/unit/**/*_spec.rb'
#We require spec_helper because some tests
#do not include spec_helper by themselves
task.rspec_opts = '--require=spec_helper'
end
desc 'Run RSpec integration tests'
RSpec::Core::RakeTask.new(:integration) do |task|
task.pattern = 'spec/integration/**/*_spec.rb'
task.rspec_opts = '--require=spec_helper'
end
desc 'Run RSpec acceptance tests'
RSpec::Core::RakeTask.new(:acceptance) do |task|
task.pattern = 'spec/acceptance/**/*_spec.rb'
task.rspec_opts = '--require=spec_helper'
end
desc 'Run RSpec api tests'
RSpec::Core::RakeTask.new(:api) do |task|
task.pattern = 'spec/api/**/*_spec.rb'
task.rspec_opts = '--require=spec_helper'
end
desc 'Run RSpec use_cases tests'
RSpec::Core::RakeTask.new(:use_cases) do |task|
task.pattern = 'spec/use_cases/**/*_spec.rb'
task.rspec_opts = '--require=spec_helper'
end
desc 'Run Rspec server tests'
RSpec::Core::RakeTask.new(:server) do |task|
task.pattern = 'spec/server/**/*_spec.rb'
task.rspec_opts = '--require=spec_helper'
end
desc 'Run RSpec test/s specified by input file pattern'
RSpec::Core::RakeTask.new(:specific, :test_name) do |task, task_args|
task.pattern = "#{task_args[:test_name]}"
task.rspec_opts = '--require=spec_helper'
end
end
desc 'Generate API request documentation from API specs'
RSpec::Core::RakeTask.new('docs:generate') do |t|
t.pattern = 'spec/acceptance/**/*_spec.rb'
t.rspec_opts = ['--format RspecApiDocumentation::ApiFormatter']
end
desc 'Tag and push the current version'
task :release => ['release:tag', 'release:push']
namespace :release do
namespace :changelog do
task :shortlog do
version = `git describe --abbrev=0`.chomp
STDOUT.puts "Changes from #{version} to HEAD\n\n"
system "git shortlog --no-merges #{version}.."
end
# Link all issues and PRs in the changelog that have no link.
#
# By default links to PRs (althought GitHub redirects IIRC) but you can
# specify an issue link by preceding it with "issue"/"Issue" or specify
# a PR link (default) by preceding it with "PR".
task :link_prs, [:file] do |_, args|
file = args[:file] || File.join(File.dirname(__FILE__), 'CHANGELOG.md')
File.open file, File::RDWR do |f|
contents = f.read
contents.
# this regexp is not perfect but ok - ie. it would match issue(#5)
gsub!(/(\b|[^[:alnum]])([iI]ssue|PR)?([\([:space:]])#(\d+)/) do
# unfortunately gsub uses globals for groups :(
type_prefix, type, separator, number = $1, $2, $3, $4
link = case type.to_s.upcase
when 'ISSUE'
# even if quoted like this, remember that \ still escapes
%{https://github.com/3scale/apisonator/issues/%s}
else
# default to PR links
%{https://github.com/3scale/apisonator/pull/%s}
end
prefix = if type && separator == ' '
# remove "issue "
type_prefix
else
"#{type_prefix}#{separator}"
end
prefix << "[##{number}](#{link % number})"
end
# Overwrite the changelog
f.seek 0, IO::SEEK_SET
f.write contents
end
end
end
task :tag do
require File.dirname(__FILE__) + '/lib/3scale/backend/version'
version = "v#{ThreeScale::Backend::VERSION}"
STDOUT.puts "Creating tag #{version}"
system "git tag -sa #{version} -m \"#{version}\""
end
task :push do
system 'git push --tags'
end
end
desc 'Start the backend server in development'
task :start do
system "ruby -Ilib bin/3scale_backend start -p #{ENV['PORT'] || 3001}"
end
desc 'Start a backend_worker in development'
task :start_worker do
system 'ruby -Ilib bin/3scale_backend_worker --no-daemonize'
end
desc 'Stop a backend_worker in development'
task :stop_worker do
system 'ruby -Ilib bin/3scale_backend_worker stop'
end
desc 'Restart a backend_worker in development'
task :restart_worker do
system 'ruby -Ilib bin/3scale_backend_worker restart'
end
desc 'Check license compliance of dependencies'
task :license_finder do
STDOUT.puts "Checking license compliance\n"
unless system("license_finder --decisions-file=#{File.dirname(__FILE__)}" \
"/.dependency_decisions.yml")
STDERR.puts "\n*** License compliance test failed ***\n"
exit 1
end
end
namespace :license_finder do
namespace :report do
desc 'Generate an XML report for licenses'
task :xml do
require 'license_finder'
LicenseFinder::CLI::Main.start [
'report',
"--decisions-file=#{File.dirname(__FILE__)}/.dependency_decisions.yml",
'--format=xml',
'--quiet'
]
end
end
end
end
desc 'Reschedule failed jobs'
task :reschedule_failed_jobs do
reschedule_method = ThreeScale::Backend::FailedJobsScheduler.method(:reschedule_failed_jobs)
result = if Environment.using_async_redis?
Async { reschedule_method.call }.result
else
reschedule_method.call
end
puts "Rescheduled: #{result[:rescheduled]}. "\
"Failed and discarded: #{result[:failed_while_rescheduling]}. "\
"Pending failed jobs: #{result[:failed_current]}."
end
desc 'open debug console'
task :console do
require 'irb'
require '3scale/backend/job_fetcher'
require_relative 'app/api/api.rb'
# Good idea from IRB.start
STDOUT.sync = true
warn <<~EOF
Examples:
Sync { Resque.redis.queue_names }
Sync { Resque.redis.peek_in_queue "priority" }
EOF
module ThreeScale
module Backend
extend Resque::Helpers
if Environment.using_async_redis?
Sync { binding.irb }
else
binding.irb
end
end
end
end
namespace :stats do
desc 'Delete stats of services marked for deletion'
task :cleanup, [:redis_urls, :log_deleted_keys] do |_, args|
redis_conns = redis_conns(args[:redis_urls])
if redis_conns.empty?
puts 'No Redis URLs specified'
exit(false)
end
ThreeScale::Backend::Stats::Cleaner.delete!(
redis_conns, log_deleted_keys: logger_for_deleted_keys(args[:log_deleted_keys])
)
end
desc 'Delete stats keys set to 0'
task :delete_stats_keys_set_to_0, [:redis_urls, :log_deleted_keys] do |_, args|
redis_conns = redis_conns(args[:redis_urls])
if redis_conns.empty?
puts 'No Redis URLs specified'
exit(false)
end
ThreeScale::Backend::Stats::Cleaner.delete_stats_keys_set_to_0(
redis_conns, log_deleted_keys: logger_for_deleted_keys(args[:log_deleted_keys])
)
end
end
def redis_conns(urls)
redis_urls = urls && urls.split(' ')
return [] if redis_urls.nil? || redis_urls.empty?
redis_urls.map do |redis_url|
parsed_uri = URI.parse(ThreeScale::Backend::Storage::Helpers.send(
:to_redis_uri, redis_url)
)
Redis.new(host: parsed_uri.host, port: parsed_uri.port)
end
end
def logger_for_deleted_keys(arg_log_deleted_keys)
arg_log_deleted_keys == 'true' ? STDOUT : nil
end