whenever-benlangfeld 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.travis.yml +18 -0
  4. data/CHANGELOG.md +333 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +260 -0
  8. data/Rakefile +10 -0
  9. data/bin/whenever +41 -0
  10. data/bin/wheneverize +68 -0
  11. data/gemfiles/activesupport4.1.gemfile +5 -0
  12. data/gemfiles/activesupport4.2.gemfile +5 -0
  13. data/lib/whenever.rb +34 -0
  14. data/lib/whenever/capistrano.rb +7 -0
  15. data/lib/whenever/capistrano/v2/hooks.rb +8 -0
  16. data/lib/whenever/capistrano/v2/recipes.rb +48 -0
  17. data/lib/whenever/capistrano/v2/support.rb +53 -0
  18. data/lib/whenever/capistrano/v3/tasks/whenever.rake +45 -0
  19. data/lib/whenever/command_line.rb +135 -0
  20. data/lib/whenever/cron.rb +153 -0
  21. data/lib/whenever/job.rb +54 -0
  22. data/lib/whenever/job_list.rb +155 -0
  23. data/lib/whenever/numeric.rb +13 -0
  24. data/lib/whenever/numeric_seconds.rb +48 -0
  25. data/lib/whenever/os.rb +7 -0
  26. data/lib/whenever/output_redirection.rb +57 -0
  27. data/lib/whenever/setup.rb +26 -0
  28. data/lib/whenever/tasks/whenever.rake +1 -0
  29. data/lib/whenever/version.rb +3 -0
  30. data/test/functional/command_line_test.rb +331 -0
  31. data/test/functional/output_at_test.rb +207 -0
  32. data/test/functional/output_default_defined_jobs_test.rb +296 -0
  33. data/test/functional/output_defined_job_test.rb +85 -0
  34. data/test/functional/output_env_test.rb +29 -0
  35. data/test/functional/output_jobs_for_roles_test.rb +65 -0
  36. data/test/functional/output_redirection_test.rb +248 -0
  37. data/test/test_case.rb +32 -0
  38. data/test/test_helper.rb +37 -0
  39. data/test/unit/capistrano_support_test.rb +147 -0
  40. data/test/unit/cron_test.rb +244 -0
  41. data/test/unit/job_test.rb +114 -0
  42. data/whenever.gemspec +27 -0
  43. metadata +167 -0
@@ -0,0 +1,248 @@
1
+ require 'test_helper'
2
+
3
+ class OutputRedirectionTest < Whenever::TestCase
4
+ test "command when the output is set to nil" do
5
+ output = Whenever.cron \
6
+ <<-file
7
+ set :job_template, nil
8
+ set :output, nil
9
+ every 2.hours do
10
+ command "blahblah"
11
+ end
12
+ file
13
+
14
+ assert_match(/^.+ .+ .+ .+ blahblah >> \/dev\/null 2>&1$/, output)
15
+ end
16
+
17
+
18
+ test "command when the output is set" do
19
+ output = Whenever.cron \
20
+ <<-file
21
+ set :job_template, nil
22
+ set :output, 'logfile.log'
23
+ every 2.hours do
24
+ command "blahblah"
25
+ end
26
+ file
27
+
28
+ assert_match(/^.+ .+ .+ .+ blahblah >> logfile.log 2>&1$/, output)
29
+ end
30
+
31
+ test "command when the error and standard output is set by the command" do
32
+ output = Whenever.cron \
33
+ <<-file
34
+ set :job_template, nil
35
+ every 2.hours do
36
+ command "blahblah", :output => {:standard => 'dev_null', :error => 'dev_err'}
37
+ end
38
+ file
39
+
40
+ assert_match(/^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, output)
41
+ end
42
+
43
+ test "command when the output is set and the comand overrides it" do
44
+ output = Whenever.cron \
45
+ <<-file
46
+ set :job_template, nil
47
+ set :output, 'logfile.log'
48
+ every 2.hours do
49
+ command "blahblah", :output => 'otherlog.log'
50
+ end
51
+ file
52
+
53
+ assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
54
+ assert_match(/^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1$/, output)
55
+ end
56
+
57
+ test "command when the output is set and the comand overrides with standard and error" do
58
+ output = Whenever.cron \
59
+ <<-file
60
+ set :job_template, nil
61
+ set :output, 'logfile.log'
62
+ every 2.hours do
63
+ command "blahblah", :output => {:error => 'dev_err', :standard => 'dev_null' }
64
+ end
65
+ file
66
+
67
+ assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
68
+ assert_match(/^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, output)
69
+ end
70
+
71
+ test "command when the output is set and the comand rejects it" do
72
+ output = Whenever.cron \
73
+ <<-file
74
+ set :job_template, nil
75
+ set :output, 'logfile.log'
76
+ every 2.hours do
77
+ command "blahblah", :output => false
78
+ end
79
+ file
80
+
81
+ assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
82
+ assert_match(/^.+ .+ .+ .+ blahblah$/, output)
83
+ end
84
+
85
+ test "command when the output is set and is overridden by the :set option" do
86
+ output = Whenever.cron :set => 'output=otherlog.log', :string => \
87
+ <<-file
88
+ set :job_template, nil
89
+ set :output, 'logfile.log'
90
+ every 2.hours do
91
+ command "blahblah"
92
+ end
93
+ file
94
+
95
+ assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
96
+ assert_match(/^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1/, output)
97
+ end
98
+
99
+ test "command when the error and standard output is set" do
100
+ output = Whenever.cron \
101
+ <<-file
102
+ set :job_template, nil
103
+ set :output, {:error => 'dev_err', :standard => 'dev_null' }
104
+ every 2.hours do
105
+ command "blahblah"
106
+ end
107
+ file
108
+
109
+ assert_match(/^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, output)
110
+ end
111
+
112
+ test "command when error output is set" do
113
+ output = Whenever.cron \
114
+ <<-file
115
+ set :job_template, nil
116
+ set :output, {:error => 'dev_null'}
117
+ every 2.hours do
118
+ command "blahblah"
119
+ end
120
+ file
121
+
122
+ assert_match(/^.+ .+ .+ .+ blahblah 2>> dev_null$/, output)
123
+ end
124
+
125
+ test "command when the standard output is set" do
126
+ output = Whenever.cron \
127
+ <<-file
128
+ set :job_template, nil
129
+ set :output, {:standard => 'dev_out'}
130
+ every 2.hours do
131
+ command "blahblah"
132
+ end
133
+ file
134
+
135
+ assert_match(/^.+ .+ .+ .+ blahblah >> dev_out$/, output)
136
+ end
137
+
138
+ test "command when error output is set by the command" do
139
+ output = Whenever.cron \
140
+ <<-file
141
+ set :job_template, nil
142
+ every 2.hours do
143
+ command "blahblah", :output => {:error => 'dev_err'}
144
+ end
145
+ file
146
+
147
+ assert_match(/^.+ .+ .+ .+ blahblah 2>> dev_err$/, output)
148
+ end
149
+
150
+ test "command when standard output is set by the command" do
151
+ output = Whenever.cron \
152
+ <<-file
153
+ set :job_template, nil
154
+ every 2.hours do
155
+ command "blahblah", :output => {:standard => 'dev_out'}
156
+ end
157
+ file
158
+
159
+ assert_match(/^.+ .+ .+ .+ blahblah >> dev_out$/, output)
160
+ end
161
+
162
+ test "command when standard output is set to nil" do
163
+ output = Whenever.cron \
164
+ <<-file
165
+ set :job_template, nil
166
+ every 2.hours do
167
+ command "blahblah", :output => {:standard => nil}
168
+ end
169
+ file
170
+
171
+ assert_match(/^.+ .+ .+ .+ blahblah > \/dev\/null$/, output)
172
+ end
173
+
174
+ test "command when standard error is set to nil" do
175
+ output = Whenever.cron \
176
+ <<-file
177
+ set :job_template, nil
178
+ every 2.hours do
179
+ command "blahblah", :output => {:error => nil}
180
+ end
181
+ file
182
+
183
+ assert_match(/^.+ .+ .+ .+ blahblah 2> \/dev\/null$/, output)
184
+ end
185
+
186
+ test "command when standard output and standard error is set to nil" do
187
+ output = Whenever.cron \
188
+ <<-file
189
+ set :job_template, nil
190
+ every 2.hours do
191
+ command "blahblah", :output => {:error => nil, :standard => nil}
192
+ end
193
+ file
194
+
195
+ assert_match(/^.+ .+ .+ .+ blahblah > \/dev\/null 2>&1$/, output)
196
+ end
197
+
198
+ test "command when standard output is set and standard error is set to nil" do
199
+ output = Whenever.cron \
200
+ <<-file
201
+ set :job_template, nil
202
+ every 2.hours do
203
+ command "blahblah", :output => {:error => nil, :standard => 'my.log'}
204
+ end
205
+ file
206
+
207
+ assert_match(/^.+ .+ .+ .+ blahblah >> my.log 2> \/dev\/null$/, output)
208
+ end
209
+
210
+ test "command when standard output is nil and standard error is set" do
211
+ output = Whenever.cron \
212
+ <<-file
213
+ set :job_template, nil
214
+ every 2.hours do
215
+ command "blahblah", :output => {:error => 'my_error.log', :standard => nil}
216
+ end
217
+ file
218
+
219
+ assert_match(/^.+ .+ .+ .+ blahblah >> \/dev\/null 2>> my_error.log$/, output)
220
+ end
221
+
222
+ test "command when the deprecated :cron_log is set" do
223
+ output = Whenever.cron \
224
+ <<-file
225
+ set :job_template, nil
226
+ set :cron_log, "cron.log"
227
+ every 2.hours do
228
+ command "blahblah"
229
+ end
230
+ file
231
+
232
+ assert_match(/^.+ .+ .+ .+ blahblah >> cron.log 2>&1$/, output)
233
+ end
234
+
235
+
236
+ test "a command when the standard output is set to a lambda" do
237
+ output = Whenever.cron \
238
+ <<-file
239
+ set :job_template, nil
240
+ set :output, lambda { "2>&1 | logger -t whenever_cron" }
241
+ every 2.hours do
242
+ command "blahblah"
243
+ end
244
+ file
245
+
246
+ assert_match(/^.+ .+ .+ .+ blahblah 2>&1 | logger -t whenever_cron$/, output)
247
+ end
248
+ end
@@ -0,0 +1,32 @@
1
+ module Whenever
2
+ require 'minitest/autorun'
3
+ begin
4
+ # 2.0.0
5
+ class TestCase < MiniTest::Test; end
6
+ rescue NameError
7
+ # 1.9.3
8
+ class TestCase < MiniTest::Unit::TestCase; end
9
+ end
10
+
11
+
12
+ class TestCase
13
+ class << self
14
+ def setup(&block)
15
+ define_method(:setup) do
16
+ super()
17
+ instance_eval(&block)
18
+ end
19
+ end
20
+
21
+ def test(name, &block)
22
+ define_method("test_#{name}".to_sym, &block)
23
+ end
24
+ alias should test
25
+ end
26
+
27
+ def assert_no_match(regexp, string)
28
+ message = "<#{regexp}> expected to not match\n<#{string}>"
29
+ assert regexp !~ string, message
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ require 'whenever'
2
+ require 'test_case'
3
+ require 'mocha/setup'
4
+
5
+ module Whenever::TestHelpers
6
+ protected
7
+ def new_job(options={})
8
+ Whenever::Job.new(options)
9
+ end
10
+
11
+ def parse_time(time = nil, task = nil, at = nil)
12
+ Whenever::Output::Cron.new(time, task, at).time_in_cron_syntax
13
+ end
14
+
15
+ def two_hours
16
+ "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
17
+ end
18
+
19
+ def assert_days_and_hours_and_minutes_equals(expected, time)
20
+ cron = parse_time(Whenever.seconds(2, :months), 'some task', time)
21
+ minutes, hours, days, _ = cron.split(' ')
22
+ assert_equal expected, [days, hours, minutes]
23
+ end
24
+
25
+ def assert_hours_and_minutes_equals(expected, time)
26
+ cron = parse_time(Whenever.seconds(2, :days), 'some task', time)
27
+ minutes, hours, _ = cron.split(' ')
28
+ assert_equal expected, [hours, minutes]
29
+ end
30
+
31
+ def assert_minutes_equals(expected, time)
32
+ cron = parse_time(Whenever.seconds(2, :hours), 'some task', time)
33
+ assert_equal expected, cron.split(' ')[0]
34
+ end
35
+ end
36
+
37
+ Whenever::TestCase.send(:include, Whenever::TestHelpers)
@@ -0,0 +1,147 @@
1
+ require 'test_helper'
2
+ require 'whenever/capistrano/v2/support'
3
+
4
+ class CapistranoSupportTestSubject
5
+ include Whenever::CapistranoSupport
6
+ end
7
+
8
+ class CapistranoTestCase < Whenever::TestCase
9
+ setup do
10
+ @capistrano = CapistranoSupportTestSubject.new
11
+ configuration = mock()
12
+ configuration.stubs(:load).yields(@capistrano)
13
+ Whenever::CapistranoSupport.load_into(configuration)
14
+ end
15
+ end
16
+
17
+ class CapistranoSupportTest < CapistranoTestCase
18
+ should "return fetch(:whenever_options) from #whenever_options" do
19
+ @capistrano.expects(:fetch).with(:whenever_options)
20
+ @capistrano.whenever_options
21
+ end
22
+
23
+ should "return whenever_options[:roles] as an array from #whenever_roles with one role" do
24
+ @capistrano.stubs(:whenever_options).returns({:roles => :role1})
25
+ assert_equal [:role1], @capistrano.whenever_roles
26
+ end
27
+
28
+ should "return an empty array from #whenever_roles with no defined roles" do
29
+ @capistrano.stubs(:whenever_options).returns({})
30
+ assert_equal [], @capistrano.whenever_roles
31
+ end
32
+
33
+ should "return the list of servers returned by find_servers from #whenever_servers" do
34
+ @capistrano.stubs(:whenever_options).returns({})
35
+ @capistrano.stubs(:find_servers).returns([:server1, :server2])
36
+
37
+ assert_equal [:server1, :server2], @capistrano.whenever_servers
38
+ end
39
+
40
+ should "#whenever_prepare_for_rollback: set path to previous_release if there is a previous release" do
41
+ args = {}
42
+ @capistrano.stubs(:fetch).with(:previous_release).returns("/some/path/20121221010000")
43
+ assert_equal({:path => "/some/path/20121221010000"}, @capistrano.whenever_prepare_for_rollback(args))
44
+ end
45
+
46
+ should "#whenever_prepare_for_rollback: set path to release_path and flags to whenever_clear_flags if there is no previous release" do
47
+ args = {}
48
+ @capistrano.stubs(:fetch).with(:previous_release).returns(nil)
49
+ @capistrano.stubs(:fetch).with(:release_path).returns("/some/path/20121221010000")
50
+ @capistrano.stubs(:fetch).with(:whenever_clear_flags).returns("--clear-crontab whenever_identifier")
51
+ assert_equal({:path => "/some/path/20121221010000", :flags => "--clear-crontab whenever_identifier"}, @capistrano.whenever_prepare_for_rollback(args))
52
+ end
53
+
54
+ should "#whenever_run_commands: require :command arg" do
55
+ assert_raises ArgumentError do
56
+ @capistrano.whenever_run_commands(:options => {}, :path => {}, :flags => {})
57
+ end
58
+ end
59
+
60
+ should "#whenever_run_commands: require :path arg" do
61
+ assert_raises ArgumentError do
62
+ @capistrano.whenever_run_commands(:options => {}, :command => {}, :flags => {})
63
+ end
64
+ end
65
+
66
+ should "#whenever_run_commands: require :flags arg" do
67
+ assert_raises ArgumentError do
68
+ @capistrano.whenever_run_commands(:options => {}, :path => {}, :command => {})
69
+ end
70
+ end
71
+ end
72
+
73
+ class ServerRolesTest < CapistranoTestCase
74
+ setup do
75
+ @mock_servers = ["foo", "bar"]
76
+ @capistrano.stubs(:whenever_servers).returns(@mock_servers)
77
+
78
+ @mock_server1, @mock_server2, @mock_server3 = mock("Server1"), mock("Server2"), mock("Server3")
79
+ @mock_server1.stubs(:host).returns("server1.foo.com")
80
+ @mock_server2.stubs(:host).returns("server2.foo.com")
81
+ @mock_server3.stubs(:host => "server3.foo.com", :port => 1022, :user => 'test')
82
+ @mock_servers = [@mock_server1, @mock_server2]
83
+ end
84
+
85
+ should "return a map of servers to their role(s)" do
86
+ @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
87
+ @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1])
88
+ @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
89
+ assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
90
+ end
91
+
92
+ should "exclude non-requested roles" do
93
+ @capistrano.stubs(:whenever_roles).returns([:role1, :role2])
94
+ @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
95
+ @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
96
+ assert_equal({"foo" => [:role1], "bar" => [:role2]}, @capistrano.whenever_server_roles)
97
+ end
98
+
99
+ should "include all roles for servers w/ >1 when they're requested" do
100
+ @capistrano.stubs(:whenever_roles).returns([:role1, :role2, :role3])
101
+ @capistrano.stubs(:role_names_for_host).with("foo").returns([:role1, :role3])
102
+ @capistrano.stubs(:role_names_for_host).with("bar").returns([:role2])
103
+ assert_equal({"foo" => [:role1, :role3], "bar" => [:role2]}, @capistrano.whenever_server_roles)
104
+ end
105
+
106
+ should "call run for each host w/ appropriate role args" do
107
+ @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1])
108
+ @capistrano.stubs(:role_names_for_host).with(@mock_server2).returns([:role2])
109
+ @capistrano.stubs(:whenever_servers).returns(@mock_servers)
110
+ roles = [:role1, :role2]
111
+ @capistrano.stubs(:whenever_options).returns({:roles => roles})
112
+
113
+ @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1', {:roles => roles, :hosts => @mock_server1})
114
+ @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role2', {:roles => roles, :hosts => @mock_server2})
115
+
116
+ @capistrano.whenever_run_commands(:command => "whenever",
117
+ :path => "/foo/bar",
118
+ :flags => "--flag1 --flag2")
119
+ end
120
+
121
+ should "call run w/ all role args for servers w/ >1 role" do
122
+ @capistrano.stubs(:role_names_for_host).with(@mock_server1).returns([:role1, :role3])
123
+ @capistrano.stubs(:whenever_servers).returns([@mock_server1])
124
+ roles = [:role1, :role2, :role3]
125
+ @capistrano.stubs(:whenever_options).returns({:roles => roles})
126
+
127
+ @capistrano.expects(:run).once.with('cd /foo/bar && whenever --flag1 --flag2 --roles role1,role3', {:roles => roles, :hosts => @mock_server1})
128
+
129
+ @capistrano.whenever_run_commands(:command => "whenever",
130
+ :path => "/foo/bar",
131
+ :flags => "--flag1 --flag2")
132
+ end
133
+
134
+ should "call run w/ proper server options (port, user)" do
135
+ @capistrano.stubs(:role_names_for_host).with(@mock_server3).returns([:role3])
136
+ @capistrano.stubs(:whenever_servers).returns([@mock_server3])
137
+ @capistrano.stubs(:whenever_options).returns({:roles => [:role3]})
138
+
139
+ @capistrano.expects(:run).once.with do |command, options|
140
+ options[:hosts].user == "test" && options[:hosts].port == 1022
141
+ end
142
+
143
+ @capistrano.whenever_run_commands(:command => "whenever",
144
+ :path => "/foo/bar",
145
+ :flags => "--flag1 --flag2")
146
+ end
147
+ end