forked from tent/tentd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
120 lines (98 loc) · 2.99 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
require 'bundler/setup'
require 'bundler/gem_tasks'
task :validator_spec do
$stdout, $stderr = STDOUT.dup, STDERR.dup
# get random port
require 'socket'
tmp_socket = Socket.new(:INET, :STREAM)
tmp_socket.bind(Addrinfo.tcp("127.0.0.1", 0))
host, port = tmp_socket.local_address.getnameinfo
tmp_socket.close
def puts_error(e)
$stderr.print "#{e.inspect}:\n\t"
$stderr.puts e.backtrace.slice(0, 20).join("\n\t")
end
tentd_pid = fork do
require 'puma/cli'
STDOUT.reopen '/dev/null'
STDERR.reopen '/dev/null'
# don't show database activity
ENV['DB_LOGFILE'] ||= '/dev/null'
ENV['TENT_ENTITY'] ||= "http://localhost:#{port}#{ENV['TENT_SUBDIR']}"
# use test database
unless ENV['DATABASE_URL'] = ENV['TEST_DATABASE_URL']
STDERR.puts "You must set TEST_DATABASE_URL!"
exit 1
end
$stdout.puts "Booting Tent server on port #{port}..."
ENV['RUN_SIDEKIQ'] = 'true' # Boot sidekiq server
ENV['SIDEKIQ_LOG'] = File.join(File.expand_path(File.dirname(__FILE__)), 'sidekiq.log')
rackup_path = File.expand_path(File.join(File.dirname(__FILE__), 'config.ru'))
cli = Puma::CLI.new ['--port', port.to_s, rackup_path]
begin
cli.run
rescue => e
puts_error(e)
exit 1
end
end
validator_pid = fork do
validator_pid = Process.pid
at_exit do
if Process.pid == validator_pid
$stdout.puts "Stopping Tent server (PID: #{tentd_pid})..."
begin
Process.kill("INT", tentd_pid)
rescue Errno::ESRCH
end
end
end
ENV['SIDEKIQ_LOG'] = File.join(File.expand_path(File.dirname(__FILE__)), 'validator-sidekiq.log')
# always use postgres for attachments
ENV['POSTGRES_ATTACHMENTS'] = 'true'
# wait until tentd server boots
tentd_started = false
until tentd_started
begin
Socket.tcp("127.0.0.1", port) do |connection|
tentd_started = true
connection.close
end
rescue Errno::ECONNREFUSED
rescue Interrupt
exit
end
end
# don't show database activity
ENV['DB_LOGFILE'] ||= '/dev/null'
begin
require 'tent-validator'
server_url = "http://localhost:#{port}#{ENV['TENT_SUBDIR']}"
TentValidator.setup!(
:remote_entity_uri => server_url,
:tent_database_url => ENV['TEST_VALIDATOR_TEND_DATABASE_URL']
)
TentValidator::Runner::CLI.run
rescue => e
puts_error(e)
exit 1
end
end
# wait for tentd process to exit
Process.waitpid(tentd_pid)
if $?.exitstatus == 0
Process.waitpid(validator_pid)
else
# kill validator if tentd exits first with non-0 status
$stdout.puts "Stopping Validator (PID: #{validator_pid})..."
begin
Process.kill("INT", validator_pid)
rescue Errno::ESRCH
end
end
exit $?.exitstatus
end
task :default => :validator_spec
lib = File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'tentd/tasks/db'